Deluge Firmware 1.3.0
Build date: 2025.11.04
Loading...
Searching...
No Matches
level.h
1/*
2 * Copyright (c) 2014-2023 Synthstrom Audible Limited
3 *
4 * This file is part of The Synthstrom Audible Deluge Firmware.
5 *
6 * The Synthstrom Audible Deluge Firmware is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software Foundation,
8 * either version 3 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along with this program.
15 * If not, see <https://www.gnu.org/licenses/>.
16 */
17#pragma once
18#include "gui/menu_item/menu_item.h"
19#include "gui/ui_timer_manager.h"
20#include "hid/display/display.h"
21#include "hid/display/oled.h"
22#include "hid/display/seven_segment.h"
23#include "model/settings/runtime_feature_settings.h"
24#include <cstdio>
25// External battery voltage variable from deluge.cpp
26extern uint16_t batteryMV;
27
28namespace deluge::gui::menu_item::battery {
29class Level final : public MenuItem {
30public:
31 using MenuItem::MenuItem;
32
33 bool isRelevant(ModControllableAudio* modControllable, int32_t whichThing) override {
34 return runtimeFeatureSettings.isOn(RuntimeFeatureSettingType::ShowBatteryLevel);
35 }
36 void drawPixelsForOled() override {
37 deluge::hid::display::oled_canvas::Canvas& canvas = hid::display::OLED::main;
38 char buffer[50];
39 getBatteryString(buffer);
40 canvas.drawStringCentredShrinkIfNecessary(buffer, 22, 18, 20);
41 }
42
43 void beginSession(MenuItem* navigatedBackwardFrom) override {
44 // Store initial voltage to detect charging
45 lastBatteryMV = batteryMV;
46 voltageCheckCounter = 0;
47
48 drawValue();
49 // Start the timer for updates
50 uiTimerManager.setTimer(TimerName::UI_SPECIFIC, 500);
51 }
52
53 void drawValue() {
54 char buffer[50];
55 getBatteryString(buffer);
56 display->setScrollingText(buffer);
57 }
58
59 ActionResult timerCallback() override {
60 // Check if voltage is rising (charging detection)
61 voltageCheckCounter++;
62 if (voltageCheckCounter >= 4) { // Check every 2 seconds
63 int32_t voltageDiff = batteryMV - lastBatteryMV;
64 // Consider it charging if voltage rose by more than 5mV in 2 seconds
65 // This threshold may need tuning based on real hardware behavior
66 isCharging = (voltageDiff > 5);
67 lastBatteryMV = batteryMV;
68 voltageCheckCounter = 0;
69 }
70
71 drawValue();
72 uiTimerManager.setTimer(TimerName::UI_SPECIFIC, 500);
73 return ActionResult::DEALT_WITH;
74 }
75
76private:
77 uint16_t lastBatteryMV = 0;
78 uint8_t voltageCheckCounter = 0;
79 bool isCharging = false;
80
87 void getBatteryString(char* buffer) {
88 // Calculate battery percentage
89 // Assuming battery range is 2600mV (0%) to 4200mV (100%)
90 const uint16_t minVoltage = 2600;
91 const uint16_t maxVoltage = 4200;
92
93 int32_t percentage = 0;
94 if (batteryMV > minVoltage) {
95 percentage = ((int32_t)(batteryMV - minVoltage) * 100) / (maxVoltage - minVoltage);
96 if (percentage > 100)
97 percentage = 100;
98 if (percentage < 0)
99 percentage = 0;
100 }
101
102 // Determine status
103 const char* status = "";
104 if (percentage >= 99) {
105 status = " FULL";
106 }
107 else if (isCharging) {
108 status = " CHG"; // Charging indicator
109 }
110
111 // Format the string with percentage, voltage, and status
112 sprintf(buffer, "%d%% (%dmV)%s", percentage, batteryMV, status);
113 }
114};
115} // namespace deluge::gui::menu_item::battery
Definition mod_controllable_audio.h:47
ActionResult timerCallback() override
Handle a TimerName::UI_SPECIFIC event.
Definition level.h:59
void beginSession(MenuItem *navigatedBackwardFrom) override
Begin an editing session with this menu item.
Definition level.h:43
bool isRelevant(ModControllableAudio *modControllable, int32_t whichThing) override
Check if this MenuItem should show up in a containing deluge::gui::menu_item::Submenu.
Definition level.h:33
void getBatteryString(char *buffer)
Definition level.h:87
void drawPixelsForOled() override
Paints the pixels below the standard title block.
Definition level.h:36
void drawStringCentredShrinkIfNecessary(char const *string, int32_t pixelY, int32_t textWidth, int32_t textHeight)
Definition canvas.cpp:352