Skip to content

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

  1. adc_sensor.h
  2. Added ADC_DEPEND_CHANNEL macro (default: 1, configurable via build flags)
  3. Added adc_read_with_channel_filter() function declaration
  4. Added adc_reset_pin_mode() function declaration
  5. Clear documentation of hardware jumper positions (ch1/ch2/ch3)

  6. adc_sensor.cpp

  7. 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
  8. Implemented adc_reset_pin_mode()

    • Safe GPIO pin reset: OUTPUT → LOW → ANALOG → delay(1)
  9. main.cpp

  10. Replaced inline ADC filtering (2 lines vs 8 original lines)
  11. Replaced inline pin reset with adc_reset_pin_mode() call
  12. Added refactoring comments explaining module integration

  13. platformio.ini

  14. Added ADC_DEPEND_CHANNEL=1 configuration
  15. Added documentation for command-line override usage
  16. Examples provided for ch1/ch2/ch3 switching

  17. REFACTORING_ROADMAP.md

  18. Checked all implementation items as complete
  19. Updated progress section (v1.3.0 status)
  20. Added history entry

  21. Git Commit

  22. Commit: 66e13d0 - "fix: make ADC channel dependency configurable"
  23. Files changed: 5
  24. Insertions: +101, Deletions: -19

Build Results

✓ Compilation: SUCCESS
✓ RAM usage:   6.9% (22592 / 327680 bytes)
✓ Flash usage: 22.6% (296409 / 1310720 bytes)

Learnings

  1. Module Responsibility Clarity: Moving ADC filtering from main.cpp to adc_sensor makes responsibilities clear. Main.cpp now focuses on orchestration, not implementation details.

  2. Build Flag Usage: Using -D preprocessor flags for hardware configuration is cleaner than hardcoded values. Allows same firmware to support different hardware without code edits.

  3. Backward Compatibility: Default value of ADC_DEPEND_CHANNEL=1 ensures existing behavior is preserved. No breaking changes to production systems.

  4. Code Length Reduction: ADC handling reduced from ~8 lines inline to 2 function calls. Improves readability without sacrificing clarity (functions are well-documented).

  5. Configuration Documentation: Clear platformio.ini comments with usage examples help developers know how to switch hardware configurations.

Next Steps

  1. Hardware Testing (when device available)
  2. Verify ADC values are correct with ch1 detection (default behavior)
  3. Optionally test ch2/ch3 configurations if jumpers changed

  4. Hardware Documentation (REFACTORING_ROADMAP section 2)

  5. Create docs/hardware/adc_design.md explaining why ADC depends on specific channel
  6. Create docs/hardware/gpio_pinout.md with GPIO pin allocation diagram

  7. Serial Verification Script (REFACTORING_ROADMAP section 3)

  8. Implement tools/verify_output.py for automated serial output validation
  9. Add task test:serial command to Taskfile.yml

  10. Configuration Centralization (v1.4.0 planning)

  11. Create include/config.h for all hardware pin definitions
  12. Consolidate scattered pin defines into single location
  13. Move from adc_sensor.h, cosmic_detector.h, etc.