Skip to content

Conversation

@ewowi
Copy link
Collaborator

@ewowi ewowi commented Jan 1, 2026

Summary by CodeRabbit

  • New Features

    • Enhanced battery monitoring with real-time state-of-charge, voltage, and current tracking.
    • Improved battery measurement accuracy through dynamic ADC attenuation adjustments.
  • Refactor

    • Reorganized battery monitoring subsystem for better maintainability.
    • Refined Ethernet pin initialization handling.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Jan 1, 2026

Note

.coderabbit.yaml has unrecognized properties

CodeRabbit is using all valid settings from your configuration. Unrecognized properties (listed below) have been ignored and may indicate typos or deprecated fields that can be removed.

⚠️ Parsing warnings (1)
Validation error: Unrecognized key(s) in object: 'review', 'context'
⚙️ Configuration instructions
  • Please see the configuration documentation for more information.
  • You can also validate your configuration using the online YAML validator.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json
📝 Walkthrough

Walkthrough

The PR refactors battery monitoring and ADC readout functionality from the main application file into the ModuleIO module by: (1) adding battery pin discovery and management to ModuleIO, (2) introducing dynamic ADC attenuation adjustment, and (3) implementing a loop1s() callback for periodic battery state updates. The main.cpp is simplified to delegate battery handling to ModuleIO.loop1s().

Changes

Cohort / File(s) Summary
Battery subsystem integration in ModuleIO
src/MoonBase/Modules/ModuleIO.h
Introduced battery support with new public members (pinVoltage, pinCurrent, pinBattery), public method loop1s() for periodic battery updates, helper function adc_get_adjusted_gain() for dynamic ADC attenuation adjustment, and state tracking members for voltage/current ADC attenuation. Added discovery logic for battery-related pins and runtime pin control with logging.
Main application simplification
src/main.cpp
Removed battery/voltage/current ADC declarations, adc_get_adjusted_gain() helper, and all FT_BATTERY related setup/loop logic. Simplified runtime to delegate battery handling via calls to moduleIO.loop1s(), moduleDevices.loop1s(), and moduleTasks.loop1s().

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • voltage & current readout for lightcrafter16 #89: Modifies pin_High/pin_Low initialization and ADC/battery readout logic including adc_get_adjusted_gain and attenuation state tracking — aligns with this PR's refactoring of battery/ADC functionality into ModuleIO.

Poem

🐰 Battery now lives in its rightful place,

ModuleIO holds it with care and grace,

ADC attunement dances with might,

loop1s() calls keep measurements tight! ⚡

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: moving battery and ADC handling logic from main.cpp to the ModuleIO class via a new loop1s() method.
✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
src/MoonBase/Modules/ModuleIO.h (2)

687-689: Use UINT8_MAX instead of -1 for unsigned type initialization.

Assigning -1 to uint8_t relies on implicit wraparound to 255. For clarity and consistency with the comparison at line 719 (!= UINT8_MAX), use the explicit constant.

🔎 Proposed fix
-  uint8_t pinVoltage = -1;
-  uint8_t pinCurrent = -1;
-  uint8_t pinBattery = -1;
+  uint8_t pinVoltage = UINT8_MAX;
+  uint8_t pinCurrent = UINT8_MAX;
+  uint8_t pinBattery = UINT8_MAX;

726-737: Consider per-pin ADC attenuation for cleaner code patterns.

The current code uses analogSetAttenuation() to set global ADC attenuation before each read, then resets it afterward. While this works in the single-threaded context of this codebase, Arduino ESP32 provides analogSetPinAttenuation(pin, attenuation) for more direct per-pin control, which avoids the set-read-reset pattern.

Alternative approach
     if (pinVoltage != UINT8_MAX) {
-      analogSetAttenuation(voltage_readout_current_adc_attenuation);
+      analogSetPinAttenuation(pinVoltage, voltage_readout_current_adc_attenuation);
       uint32_t adc_mv_vinput = analogReadMilliVolts(pinVoltage);
-      analogSetAttenuation(ADC_11db);
       float volts = ((float)adc_mv_vinput) * 11.43 / (1.43 * 1000);
       batteryService->updateVoltage(volts);
       voltage_readout_current_adc_attenuation = adc_get_adjusted_gain(voltage_readout_current_adc_attenuation, adc_mv_vinput);
     }
     if (pinCurrent != UINT8_MAX) {
-      analogSetAttenuation(current_readout_current_adc_attenuation);
+      analogSetPinAttenuation(pinCurrent, current_readout_current_adc_attenuation);
       uint32_t adc_mv_cinput = analogReadMilliVolts(pinCurrent);
-      analogSetAttenuation(ADC_11db);
       current_readout_current_adc_attenuation = adc_get_adjusted_gain(current_readout_current_adc_attenuation, adc_mv_cinput);
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d80ee3d and aec2871.

📒 Files selected for processing (2)
  • src/MoonBase/Modules/ModuleIO.h
  • src/main.cpp
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-12-25T13:34:40.097Z
Learnt from: ewowi
Repo: MoonModules/MoonLight PR: 0
File: :0-0
Timestamp: 2025-12-25T13:34:40.097Z
Learning: For the MoonLight LED system: Effects task can run on Core 0 (PRO_CPU) since it only performs computation (calculating colors, writing to channels array) without timing-critical hardware interaction. WiFi preemption is acceptable for pure computation tasks. Only the driver task (using DMA, I2S, LCD, PARLIO) requires Core 1 (APP_CPU) for uninterrupted timing-critical hardware operations.

Applied to files:

  • src/main.cpp
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build
🔇 Additional comments (3)
src/MoonBase/Modules/ModuleIO.h (2)

316-329: LGTM!

The explicit gpio_set_direction/gpio_set_level calls for pin 3 are correctly retained here for early boot initialization before readPins() runs. The comments clearly document this requirement for the ethernet module. The pin assignments for SE16V2 board look correct.


653-684: LGTM!

The battery pin discovery logic correctly iterates through the pins array and extracts the voltage, current, and battery pins. The pin_High/pin_Low handling includes proper GPIO validation before setting direction and level.

src/main.cpp (1)

356-358: LGTM!

The delegation of per-second battery monitoring to moduleIO.loop1s() is a clean refactor that properly encapsulates IO-related functionality within the ModuleIO module. The call order with moduleDevices.loop1s() and moduleTasks.loop1s() is appropriate.

@ewowi ewowi merged commit 60df77d into main Jan 1, 2026
2 checks passed
@ewowi ewowi deleted the main-move-io branch January 1, 2026 20:07
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.

2 participants