From 791fb86c7cdc6b354916ea242d8140310ad48983 Mon Sep 17 00:00:00 2001 From: HarukiToreda <116696711+HarukiToreda@users.noreply.github.com> Date: Sun, 21 Dec 2025 00:17:33 -0500 Subject: [PATCH] added ADC calibration feature --- .../InkHUD/Applets/System/Menu/MenuAction.h | 1 + .../InkHUD/Applets/System/Menu/MenuApplet.cpp | 70 ++++++++++++++++--- .../InkHUD/Applets/System/Menu/MenuPage.h | 1 + 3 files changed, 64 insertions(+), 8 deletions(-) diff --git a/src/graphics/niche/InkHUD/Applets/System/Menu/MenuAction.h b/src/graphics/niche/InkHUD/Applets/System/Menu/MenuAction.h index a6d6dcbac..74ad5c85f 100644 --- a/src/graphics/niche/InkHUD/Applets/System/Menu/MenuAction.h +++ b/src/graphics/niche/InkHUD/Applets/System/Menu/MenuAction.h @@ -99,6 +99,7 @@ enum MenuAction { SET_TZ_PACIFIC_NZ, // Power TOGGLE_POWER_SAVE, + CALIBRATE_ADC, // Bluetooth TOGGLE_BLUETOOTH, TOGGLE_BLUETOOTH_PAIR_MODE, diff --git a/src/graphics/niche/InkHUD/Applets/System/Menu/MenuApplet.cpp b/src/graphics/niche/InkHUD/Applets/System/Menu/MenuApplet.cpp index 9db6cc623..0e8491226 100644 --- a/src/graphics/niche/InkHUD/Applets/System/Menu/MenuApplet.cpp +++ b/src/graphics/niche/InkHUD/Applets/System/Menu/MenuApplet.cpp @@ -479,6 +479,48 @@ void InkHUD::MenuApplet::execute(MenuItem item) rebootAtMsec = millis() + DEFAULT_REBOOT_SECONDS * 1000; break; #endif + // ADC Calibration + case CALIBRATE_ADC: { + // Read current measured voltage + float measuredV = powerStatus->getBatteryVoltageMv() / 1000.0f; + + // Sanity check + if (measuredV < 3.0f || measuredV > 4.5f) { + LOG_WARN("ADC calibration aborted, unreasonable voltage: %.2fV", measuredV); + break; + } + + // Determine the base multiplier currently in effect + float baseMult = 0.0f; + + if (config.power.adc_multiplier_override > 0.0f) { + baseMult = config.power.adc_multiplier_override; + } +#ifdef ADC_MULTIPLIER + else { + baseMult = ADC_MULTIPLIER; + } +#endif + + if (baseMult <= 0.0f) { + LOG_WARN("ADC calibration failed: no base multiplier"); + break; + } + + // Target voltage considered 100% by UI + constexpr float TARGET_VOLTAGE = 4.19f; + + // Calculate new multiplier + float newMult = baseMult * (TARGET_VOLTAGE / measuredV); + + config.power.adc_multiplier_override = newMult; + + nodeDB->saveToDisk(SEGMENT_CONFIG); + + LOG_INFO("ADC calibrated: measured=%.3fV base=%.4f new=%.4f", measuredV, baseMult, newMult); + + break; + } // Display case TOGGLE_DISPLAY_UNITS: @@ -905,11 +947,11 @@ void InkHUD::MenuApplet::showPage(MenuPage page) items.push_back(MenuItem("Position", MenuPage::NODE_CONFIG_POSITION)); #endif -#if defined(ARCH_ESP32) items.push_back(MenuItem("Power", MenuPage::NODE_CONFIG_POWER)); + +#if defined(ARCH_ESP32) items.push_back(MenuItem("Network", MenuPage::NODE_CONFIG_NETWORK)); #endif - items.push_back(MenuItem("Display", MenuPage::NODE_CONFIG_DISPLAY)); items.push_back(MenuItem("Bluetooth", MenuPage::NODE_CONFIG_BLUETOOTH)); @@ -952,11 +994,11 @@ void InkHUD::MenuApplet::showPage(MenuPage page) break; } -#if defined(ARCH_ESP32) case NODE_CONFIG_POWER: { items.push_back(MenuItem("Back", MenuAction::BACK, MenuPage::NODE_CONFIG)); +#if defined(ARCH_ESP32) items.push_back(MenuItem("Powersave", MenuAction::TOGGLE_POWER_SAVE, MenuPage::EXIT, &config.power.is_power_saving)); - +#endif // ADC Multiplier float effectiveMult = 0.0f; @@ -977,13 +1019,27 @@ void InkHUD::MenuApplet::showPage(MenuPage page) snprintf(buf, sizeof(buf), "ADC Mult: %.3f", effectiveMult); nodeConfigLabels.emplace_back(buf); - items.push_back(MenuItem(nodeConfigLabels.back().c_str(), MenuAction::NO_ACTION, MenuPage::NODE_CONFIG_POWER)); + items.push_back( + MenuItem(nodeConfigLabels.back().c_str(), MenuAction::NO_ACTION, MenuPage::NODE_CONFIG_POWER_ADC_CAL)); } items.push_back(MenuItem("Exit", MenuPage::EXIT)); break; } + case NODE_CONFIG_POWER_ADC_CAL: { + items.push_back(MenuItem("Back", MenuAction::BACK, MenuPage::NODE_CONFIG_POWER)); + + // Instruction text (header-style, non-selectable) + items.push_back(MenuItem::Header("Run on full charge Only")); + + // Action + items.push_back(MenuItem("Calibrate ADC", MenuAction::CALIBRATE_ADC, MenuPage::NODE_CONFIG_POWER)); + + items.push_back(MenuItem("Exit", MenuPage::EXIT)); + break; + } + case NODE_CONFIG_NETWORK: { items.push_back(MenuItem("Back", MenuAction::BACK, MenuPage::NODE_CONFIG)); @@ -1832,6 +1888,4 @@ void InkHUD::MenuApplet::freeCannedMessageResources() cm.selectedRecipientItem = nullptr; cm.messageItems.clear(); cm.recipientItems.clear(); -} - -#endif +} \ No newline at end of file diff --git a/src/graphics/niche/InkHUD/Applets/System/Menu/MenuPage.h b/src/graphics/niche/InkHUD/Applets/System/Menu/MenuPage.h index dd4d81048..138c389be 100644 --- a/src/graphics/niche/InkHUD/Applets/System/Menu/MenuPage.h +++ b/src/graphics/niche/InkHUD/Applets/System/Menu/MenuPage.h @@ -29,6 +29,7 @@ enum MenuPage : uint8_t { NODE_CONFIG_DEVICE, NODE_CONFIG_DEVICE_ROLE, NODE_CONFIG_POWER, + NODE_CONFIG_POWER_ADC_CAL, NODE_CONFIG_NETWORK, NODE_CONFIG_DISPLAY, NODE_CONFIG_BLUETOOTH,