refactor: simplify and reduce flash usage in osd.c and fc_msp.c via shared helpers#11573
Open
sensei-hacker wants to merge 8 commits into
Conversation
Contributor
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
|
Test firmware build ready — commit Download firmware for PR #11573 234 targets built. Find your board's
|
osd.c - collapse identical custom-element and GVAR switch cases: - OSD_CUSTOM_ELEMENT_1..3: three cases -> one using item - OSD_CUSTOM_ELEMENT_1 - OSD_CUSTOM_ELEMENT_4..8: five cases -> one using item - OSD_CUSTOM_ELEMENT_4 + 3 - OSD_GVAR_0..3: four cases -> one using item - OSD_GVAR_0 fc_msp.c - extract duplicated serialization loops into static helpers: - mspSerializeMotorMixer: shared by both loops in MSP2_COMMON_MOTOR_MIXER - mspSerializeServoMixer: shared by both loops in MSP2_INAV_SERVO_MIXER Flash impact on MATEKF405: mspFcProcessOutCommand -1108 bytes, net -472 bytes total.
MSP_SET_SERVO_CONFIGURATION and MSP2_INAV_SET_SERVO_CONFIG both read the same four servo param fields (min/max/middle/rate) and call servoComputeScalingFactors. Consolidate into a shared static helper. Flash impact on MATEKF405: mspFcProcessInCommand -324 bytes.
osdDisplayBattVoltDJI: consolidates DJI-compat digit adjustment + voltage display used by all four battery voltage cases (main, sag-comp, cell, sag-comp cell). osdFormatAngleDeg: shared buff layout (sym + 3-digit angle-or-dashes + SYM_DEGREES + null) for OSD_HEADING, OSD_GROUND_COURSE, OSD_AZIMUTH. osdUpdateEfficiencyFilter: shared GPS-fix guard + pt1 filter update used by OSD_EFFICIENCY_MAH_PER_KM and OSD_EFFICIENCY_WH_PER_KM. Flash impact on MATEKF405: osdDrawSingleElement -256 bytes, net -64 bytes (new helpers cost 372 bytes, save 436 from the function).
sbufWriteAxisU16 / sbufReadAxisU16: replace repeated 3-axis U16 triplet reads/writes for accZero, accGain, magZero, magGain in MSP_CALIBRATION_DATA and MSP_SET_CALIBRATION_DATA. mspReadRates: replace the identical FD_YAW-aware rate-constrain loop used in MSP_SET_RC_TUNING and twice in MSP2_INAV_SET_RATE_PROFILE. Flash impact on MATEKF405: mspFcProcessOutCommand -192 bytes mspFcProcessInCommand -360 bytes net total across all changes now -1032 bytes from baseline
- Remove spurious displayWrite inside drawStat_RXStats HD+CRSF block that caused the stat value to be written twice and row to be incremented twice on HD displays with CRSF (blocking regression) - mspReadRates: widen loop variable from uint8_t to int to match original code and avoid signed/unsigned comparison with FD_YAW - osdUpdateEfficiencyFilter: move GPS/speed guard fully into the helper and add comment clarifying the div/0 guard at call sites - sbufWriteAxisU16: add comment explaining int16_t input / U16 wire format convention
osdFormatVelocityStr: collapse 3-unit × 2-_max × 4-speedType nested switch (24 code paths, 12 tfp_sprintf call sites) into unit-symbol selection followed by a single speedType switch (7 code paths, 4 tfp_sprintf call sites). Saves 32 B in the function, 144 B total. mspSerializeServoParams: add output-side counterpart to the existing mspDeserializeServoParams helper. Used in MSP_SERVO_CONFIGURATIONS and MSP2_INAV_SERVO_CONFIG loops. LTO inlines it, so no binary size regression; code is now symmetric on both sides of the wire.
osd.c: add explicit case OSD_UNIT_METRIC before default in osdFormatVelocityStr unit symbol selection, making intent clear for future enum additions. fc_msp.c: use 0xFF instead of -1 in mspSerializeServoMixer compat byte to make the truncation-to-255 explicit.
c8a2f98 to
e667465
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Reduces binary flash usage by extracting repeated code patterns in the three largest functions into shared static helpers. All changes are pure refactoring — no behavior changes, same MSP wire format. Also simplifies the source code, making it more readable.
Flash savings on MATEKF405 (STM32F405, 1MB flash, -Os + LTO):
mspFcProcessOutCommandmspFcProcessInCommandosdDrawSingleElementChanges
osd.c
osdFormatIntUnit,osdWriteChar,osdWriteChar2,osdFormatTime_MMSS): replace 81tfp_sprintfcalls with lightweight helpers that skip format-string parsing — improves CPU efficiency at a small flash costOSD_CUSTOM_ELEMENT_1..3,OSD_CUSTOM_ELEMENT_4..8, andOSD_GVAR_0..3groups collapsed using index arithmetic (item - OSD_CUSTOM_ELEMENT_1etc.)osdDisplayBattVoltDJI: consolidates the DJI digit-count adjustment repeated verbatim across 4 battery voltage casesosdFormatAngleDeg: shared angle display layout (sym + 3-digit value-or-dashes + SYM_DEGREES + NUL) used byOSD_HEADING,OSD_GROUND_COURSE,OSD_AZIMUTHosdUpdateEfficiencyFilter: shared GPS-fix guard + pt1 filter update forOSD_EFFICIENCY_MAH_PER_KMandOSD_EFFICIENCY_WH_PER_KMfc_msp.c
mspSerializeMotorMixer: shared serializer for both motor mixer arrays inMSP2_COMMON_MOTOR_MIXERmspSerializeServoMixer: shared serializer for both servo mixer arrays inMSP2_INAV_SERVO_MIXERmspDeserializeServoParams: shared deserializer forMSP_SET_SERVO_CONFIGURATIONandMSP2_INAV_SET_SERVO_CONFIGmspReadRates: shared rate-read loop with FD_YAW constrain, used inMSP_SET_RC_TUNINGand twice inMSP2_INAV_SET_RATE_PROFILEsbufWriteAxisU16/sbufReadAxisU16: shared XYZ triplet serializers for calibration data (accZero, accGain, magZero, magGain)Testing
static_assertguards on OSD enum index arithmetic