Skip to content

Refactoring: modularize main.c into per-subsystem source files#169

Merged
AxxAxx merged 14 commits into
AxxAxx:devfrom
ENA526:refactoring
Jun 9, 2026
Merged

Refactoring: modularize main.c into per-subsystem source files#169
AxxAxx merged 14 commits into
AxxAxx:devfrom
ENA526:refactoring

Conversation

@ENA526

@ENA526 ENA526 commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

Summary

  • This PR extracts the monolithic main.c into subsystems with its own .c and .h pair while preserving byte-equivalent runtime behavior of the original firmware.
  • The PR is pure code organization. No behavior changes, no new features. RAM and FLASH footprint match upstream within rounding.

Why

A monolithic main.c has accumulated friction over time:

  • Hard to onboard
  • Hard to unit-test
  • Hard to extract or port code

By extracting subsystems into modules the code becomes self-contained and isolated. Therefore, beneficial for new developers to hop on and maintain the project.

Scope

Refactor only. No new features, no PID tuning changes, no UI changes, no protocol changes.

New modules

Module Responsibility
sensors.c/h Thermocouple, bus voltage, MCU temp, heater current ADC sampling and moving-average filters
heater.c/h TIM1 PWM duty management, ADC2 sync-triggered current sampling, duty cycle math
handle.c/h JBC handle detection (T210/T245/NT115), cartridge presence, per-handle power limits and PID gain selection
state_machine.c/h RUN / STANDBY / SLEEP / HALTED transitions, emergency shutdown checks, delta-temperature fault detection
encoder.c/h TIM2 quadrature decoding, setpoint write
buttons.c/h EXTI press handlers, TIM16 debounce, long-press / short-press detection
stand.c/h Stand-sense GPIO debounce and state transitions into STANDBY/SLEEP
settings.c/h Flash storage of Flash_values struct
telemetry.c/h Rate-limited UART debug packet transmission
power_source.c/h STUSB4500 USB-PD negotiation and power limit derivation
display_app.c/h Main screen render, popups, graph view, µGUI glue
controller.c/h PID instance, setpoint update, PID_TUNING build hooks
util.c/h clamp() helper
version.c/h Firmware and hardware version queries

Modified modules

Module Change
buzzer.c/h Extended with beep_at_set_temp() and ISR entry points for setpoint-reached audio feedback (logic was previously inline in main.c)
debug.c Moved one function signature to match the new header
graph.c / graph.h Tidied to remove cross-references that became unnecessary after extraction
menu_profiles.c / menu_profiles.h One small #include adjustment
menu_settings.c One small #include adjustment
tip_profile.h Forward-declaration adjustment for the new controller.h boundary

Dead code removed

Verified unused in upstream and dropped:
UART_buffer: buffer that was never read
timer_cleaned: flag that no code path checked
time_to_standby_ms, time_to_sleep_ms: overridden by the values in Flash_values
min() / min3(): only call site was inlined; standard < comparison used instead

What stayed in main.c

  1. CubeMX-generated peripheral init (MX_GPIO_Init, MX_ADC1_Init, etc.). Left verbatim to preserve regen-ability if anyone re-runs CubeMX from the .ioc.
    The USER CODE BEGIN 2 / BEGIN WHILE init sequences. Kept in byte-exact firing order with upstream. Reordering startup is its own PR.
    Thin one-line ISR dispatchers that route HAL callbacks (HAL_TIM_PeriodElapsedCallback, HAL_GPIO_EXTI_Callback, HAL_ADC_ConvCpltCallback, etc.) to the owning modules.

Result

main.c went from 2898 → 1203 lines

Verification status

Done (software-only):

Check Status
Clean build
Binary size matches upstream within rounding
Diff inspection: extracted blocks match origin in main.c
No circular includes, no orphan declarations

Not done: hardware functional verification still needed:
Bench testing on real hardware (T245/T210/NT115 cartridges, USB-PD source, 24 V brick) has not been run yet. The refactor is structurally equivalent to upstream per static checks, but behavior on hardware must be confirmed before merge.

pashamray and others added 10 commits April 29, 2026 18:48
Remove RGB_to_BRG() calls, fix spi data size bug
* feat: update dependencies + CI file

* ci: test

* ci: wrong branch name

* ci: remove old file
CI: cleanup & updating dependencies
- Root .gitignore for editor configs and source archives
- Extend AxxSolder_firmware/.gitignore with build/, .vscode/, .clangd
main.c was 2898 lines mixing peripheral init, ISR dispatchers, sensor
handling, state machine, PID control, display, buttons/encoder,
USB-PD negotiation, and persistent settings. This commit extracts
each subsystem into its own .c/.h pair while preserving the
byte-equivalent runtime behavior of the original.

New modules (Core/Src + Core/Inc):
  sensors        thermocouple, bus voltage, MCU temp, heater current
                 ADC sampling and moving-average filters
  heater         TIM1 PWM, ADC2 current measurement, duty cycle math
  handle         JBC handle detection (T210/T245/NT115), cartridge
                 presence, per-handle power limits and PID gains
  state_machine  RUN/STANDBY/SLEEP/HALTED transitions, emergency
                 shutdown checks, delta-temperature fault detection
  encoder        TIM2 quadrature decoding, setpoint write
  buttons        EXTI press handlers, TIM16 debounce, long/short press
  stand          stand-sense GPIO debounce and state transitions
  settings       flash storage of Flash_values struct
  telemetry      rate-limited UART debug packet transmission
  power_source   STUSB4500 USB-PD negotiation
  display_app    main screen render, popups, graph view, uGUI glue
  controller     PID instance, setpoint update, PID_TUNING hooks
  util           clamp()
  version        firmware and hardware version queries

Modified modules:
  buzzer         extended with beep_at_set_temp and ISR entry points

main.c now contains only the CubeMX-generated peripheral inits, the
USER CODE BEGIN 2 / BEGIN WHILE init sequences (byte-exact firing
order with the original), and thin one-line ISR dispatchers that
route HAL callbacks to the owning modules.

Dead code removed in this pass (verified unused in upstream):
  UART_buffer, timer_cleaned, time_to_standby_ms, time_to_sleep_ms,
  and the min()/min3() helpers (only call site was inlined).

No behavior changes, no new features. Code organization only.
RAM and FLASH footprint match upstream within rounding.
Added warnings about power supply usage and USB-C chargers to ensure safe operation of AxxSolder.
@ENA526 ENA526 marked this pull request as ready for review June 6, 2026 11:58
@AxxAxx AxxAxx self-requested a review June 6, 2026 12:33
@AxxAxx

AxxAxx commented Jun 8, 2026

Copy link
Copy Markdown
Owner

First thing, it looks like Fix LCD color handling: compile-time BRG565 definitions and STM32G4 S… broke the color rendering.

It is working as "intended" now. But we do not have the correct colors anymore.

I think we have to fix that.
It looks like i missed that when merging that to main...

As an exemple:
The power bar had the color "RGB_to_BRG(C_ORANGE)" which i choose because it had a red color.
That was obviously wrong, but it was choosen for it's color.

Now C_ORANGE is orange (as it should be) but that means it is not the RED is should be.

So how do we fix this? Should we back-calculate colors
LEGACY_C_ORANGE = 0x------

And use?

@AxxAxx

AxxAxx commented Jun 8, 2026

Copy link
Copy Markdown
Owner

ping @pashamray

@pashamray

pashamray commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

ping @pashamray

just replace C_ORANGE to C_RED in code

It's best to use aliases for colors, for example (in gui.h) #define C_ACCENT C_RED and replce C_ORANGE to C_ACCENT

#define C_ACCENT C_RED
#define C_ALERT C_RED
afbeelding

@AxxAxx #171

It's better to make many defines of the same color for different tasks, anyway it will turn into a value substitution during compilation and doesn't take up space

@AxxAxx AxxAxx merged commit 9beb116 into AxxAxx:dev Jun 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants