Progress Log: ADC Channel Dependency Refactoring¶
Task Description¶
Refactored ADC (Analog-to-Digital Converter) logic from main.cpp to adc_sensor module, making hardware jumper configuration flexible without code changes. This aligns with v1.3.0 refactoring plan (REFACTORING_ROADMAP.md section 1.3).
Problem: ADC channel 1 dependency was hardcoded in main.cpp with scattered logic:
- Line 122-124: ch1 detection check inline
- Line 150-153: Pin mode switching (8 lines)
Solution: Centralize logic into adc_sensor module with configurable dependency via ADC_DEPEND_CHANNEL build flag.
Outcome¶
✅ Completed Successfully
Changes Made¶
- adc_sensor.h
- Added
ADC_DEPEND_CHANNELmacro (default: 1, configurable via build flags) - Added
adc_read_with_channel_filter()function declaration - Added
adc_reset_pin_mode()function declaration -
Clear documentation of hardware jumper positions (ch1/ch2/ch3)
-
adc_sensor.cpp
- Implemented
adc_read_with_channel_filter(int signal1, signal2, signal3)- Determines dependent channel via ADC_DEPEND_CHANNEL flag
- Filters ADC value to 0 if dependent channel has no detection
- Supports ch1, ch2, ch3 configurations
-
Implemented
adc_reset_pin_mode()- Safe GPIO pin reset: OUTPUT → LOW → ANALOG → delay(1)
-
main.cpp
- Replaced inline ADC filtering (2 lines vs 8 original lines)
- Replaced inline pin reset with
adc_reset_pin_mode()call -
Added refactoring comments explaining module integration
-
platformio.ini
- Added
ADC_DEPEND_CHANNEL=1configuration - Added documentation for command-line override usage
-
Examples provided for ch1/ch2/ch3 switching
-
REFACTORING_ROADMAP.md
- Checked all implementation items as complete
- Updated progress section (v1.3.0 status)
-
Added history entry
-
Git Commit
- Commit:
66e13d0- "fix: make ADC channel dependency configurable" - Files changed: 5
- Insertions: +101, Deletions: -19
Build Results¶
✓ Compilation: SUCCESS
✓ RAM usage: 6.9% (22592 / 327680 bytes)
✓ Flash usage: 22.6% (296409 / 1310720 bytes)
Learnings¶
-
Module Responsibility Clarity: Moving ADC filtering from main.cpp to adc_sensor makes responsibilities clear. Main.cpp now focuses on orchestration, not implementation details.
-
Build Flag Usage: Using
-Dpreprocessor flags for hardware configuration is cleaner than hardcoded values. Allows same firmware to support different hardware without code edits. -
Backward Compatibility: Default value of
ADC_DEPEND_CHANNEL=1ensures existing behavior is preserved. No breaking changes to production systems. -
Code Length Reduction: ADC handling reduced from ~8 lines inline to 2 function calls. Improves readability without sacrificing clarity (functions are well-documented).
-
Configuration Documentation: Clear platformio.ini comments with usage examples help developers know how to switch hardware configurations.
Next Steps¶
- Hardware Testing (when device available)
- Verify ADC values are correct with ch1 detection (default behavior)
-
Optionally test ch2/ch3 configurations if jumpers changed
-
Hardware Documentation (REFACTORING_ROADMAP section 2)
- Create
docs/hardware/adc_design.mdexplaining why ADC depends on specific channel -
Create
docs/hardware/gpio_pinout.mdwith GPIO pin allocation diagram -
Serial Verification Script (REFACTORING_ROADMAP section 3)
- Implement
tools/verify_output.pyfor automated serial output validation -
Add
task test:serialcommand to Taskfile.yml -
Configuration Centralization (v1.4.0 planning)
- Create
include/config.hfor all hardware pin definitions - Consolidate scattered pin defines into single location
- Move from adc_sensor.h, cosmic_detector.h, etc.