CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
SettingsHandlers.cpp
Go to the documentation of this file.
3#include "cdc_ui/ViewStack.h"
4#include "cdc_ui/I18n.h"
6#include "cdc_hal/IDisplay.h"
8#include "cdc_hal/IRtc.h"
9#include "nvs.h"
10#include <ctime>
11#include <sys/time.h>
12
13namespace cdc::ui::settings {
14
18static hal::IDisplay* s_display = nullptr;
20static LockScreenView* s_lockScreen = nullptr;
21
25static constexpr uint8_t BADGE_STEP_NONE = 0;
26static constexpr uint8_t BADGE_STEP_NAME = 1;
27static constexpr uint8_t BADGE_STEP_INFO = 2;
28static constexpr uint8_t BADGE_STEP_INFO2 = 3;
30
34static void showBadgeTextStep(uint8_t step);
35static void onBadgeNameSave(const char* text);
36static void onBadgeInfoSave(const char* text);
37static void onBadgeInfo2Save(const char* text);
38
48 s_sleep = sleep;
49 s_lockScreen = lockScreen;
50}
51
62
68void onBrightnessSave(uint16_t value) {
69 if (s_display) {
70 s_display->setBacklight(value * 10);
71 s_display->saveBacklight();
72 }
73}
74
80void onBrightnessChange(uint16_t value) {
81 if (s_display) {
82 s_display->setBacklight(value * 10);
83 }
84}
85
92uint16_t brightnessStepCallback(uint16_t current, bool increasing) {
93 if (increasing) {
94 if (current < 1) return 1;
95 if (current < 20) return 5;
96 return 10;
97 }
98 if (current <= 1) return 1;
99 if (current <= 20) return 5;
100 return 10;
101}
102
108void onSleepIntervalSave(uint16_t value) {
109 if (s_sleep) {
110 s_sleep->setLightSleepInterval(static_cast<uint32_t>(value) * 60);
111 }
112}
113
119void onTimezoneSave(uint16_t value) {
120 auto* rtc = hal::getRtcInstance();
121 if (rtc) {
122 // Value is 0-26 (slider range), convert to -12..+14 (actual timezone)
123 int8_t tzOffset = static_cast<int8_t>(static_cast<int16_t>(value) - 12);
124 rtc->setTimezoneOffset(tzOffset);
125
126 // Update lock screen clock
127 time_t now = time(nullptr);
128 struct tm* tm = localtime(&now);
129 if (tm && s_lockScreen) {
130 char buf[32];
131 snprintf(buf, sizeof(buf), "%02d:%02d", tm->tm_hour, tm->tm_min);
132 s_lockScreen->setClock(buf);
133 snprintf(buf, sizeof(buf), "%02d.%02d.%04d", tm->tm_mday, tm->tm_mon + 1, tm->tm_year + 1900);
134 s_lockScreen->setDate(buf);
135 }
136 }
137}
138
146void onDateConfirm(uint8_t day, uint8_t month, uint16_t year) {
147 time_t now = time(nullptr);
148 struct tm tm = {};
149 struct tm* current = localtime(&now);
150 if (current) tm = *current;
151 tm.tm_mday = day;
152 tm.tm_mon = month - 1;
153 tm.tm_year = year - 1900;
154
155 time_t newTime = mktime(&tm);
156 struct timeval tv = {.tv_sec = newTime, .tv_usec = 0};
157 settimeofday(&tv, nullptr);
158}
159
166void onTimeConfirm(uint8_t hour, uint8_t minute) {
167 time_t now = time(nullptr);
168 struct tm tm = {};
169 struct tm* current = localtime(&now);
170 if (current) tm = *current;
171 tm.tm_hour = hour;
172 tm.tm_min = minute;
173 tm.tm_sec = 0;
174
175 time_t newTime = mktime(&tm);
176 struct timeval tv = {.tv_sec = newTime, .tv_usec = 0};
177 settimeofday(&tv, nullptr);
178}
179
185void onPinChangeComplete(bool success) {
186 (void)success;
188}
189
197
203static void showBadgeTextStep(uint8_t step) {
204 if (!s_lockScreen) return;
205
206 const char* title = nullptr;
207 const char* initial = nullptr;
208 T9InputView::SaveCallback cb = nullptr;
209
210 switch (step) {
211 case BADGE_STEP_NAME:
212 title = ui::tr("core.name");
213 initial = s_lockScreen->getDisplayName();
214 cb = onBadgeNameSave;
215 break;
216 case BADGE_STEP_INFO:
217 title = ui::tr("core.info");
218 initial = s_lockScreen->getInfo();
219 cb = onBadgeInfoSave;
220 break;
221 case BADGE_STEP_INFO2:
222 title = ui::tr("core.info2");
223 initial = s_lockScreen->getInfo2();
224 cb = onBadgeInfo2Save;
225 break;
226 default:
227 return;
228 }
229
230 showT9Input(title, initial, cb, LockScreenView::MAX_TEXT_LEN);
231}
232
238static void onBadgeNameSave(const char* text) {
239 if (s_lockScreen) s_lockScreen->setDisplayName(text);
240 saveDisplayField("name", text);
242}
243
249static void onBadgeInfoSave(const char* text) {
250 if (s_lockScreen) s_lockScreen->setInfo(text);
251 saveDisplayField("info", text);
253}
254
260static void onBadgeInfo2Save(const char* text) {
261 if (s_lockScreen) s_lockScreen->setInfo2(text);
262 saveDisplayField("info2", text);
264}
265
272void saveDisplayField(const char* key, const char* value) {
273 if (!key) return;
274 nvs_handle_t nvs;
275 if (nvs_open("display", NVS_READWRITE, &nvs) != ESP_OK) return;
276 const char* safeValue = value ? value : "";
277 nvs_set_str(nvs, key, safeValue);
278 nvs_commit(nvs);
279 nvs_close(nvs);
280}
281
289bool loadDisplayField(const char* key, char* out, size_t outSize) {
290 if (!key || !out || outSize == 0) return false;
291 nvs_handle_t nvs;
292 if (nvs_open("display", NVS_READONLY, &nvs) != ESP_OK) return false;
293 size_t len = outSize;
294 esp_err_t err = nvs_get_str(nvs, key, out, &len);
295 nvs_close(nvs);
296 return err == ESP_OK && len > 1;
297}
298
299} // namespace cdc::ui::settings
Internationalization with English fallbacks in code and overlay translations loaded at runtime from a...
static constexpr uint8_t MAX_TEXT_LEN
void(*)(const char *text) SaveCallback
Definition T9InputView.h:29
static ViewStack & instance()
Returns singleton view-stack instance.
Definition ViewStack.cpp:34
IRtc * getRtcInstance()
Returns the singleton RTC service instance.
Definition Rtc.cpp:304
bool loadDisplayField(const char *key, char *out, size_t outSize)
Reads one display text field from NVS into the caller buffer.
static hal::ISleepController * s_sleep
static LockScreenView * s_lockScreen
static constexpr uint8_t BADGE_STEP_INFO
void onPinChangeComplete(bool success)
Handles completion of PIN-change flow.
void saveDisplayField(const char *key, const char *value)
Saves one display text field to NVS.
void onBrightnessChange(uint16_t value)
Applies backlight preview without persisting.
uint16_t brightnessStepCallback(uint16_t current, bool increasing)
Returns adaptive brightness step size.
static void onBadgeNameSave(const char *text)
Handles save callback for badge display name.
static constexpr uint8_t BADGE_STEP_NAME
void onBrightnessSave(uint16_t value)
Persists and applies selected backlight value.
static constexpr uint8_t BADGE_STEP_INFO2
void processPendingBadgeText()
Processes the next pending badge-text wizard step.
void onDateConfirm(uint8_t day, uint8_t month, uint16_t year)
Applies confirmed date to system time.
static void onBadgeInfoSave(const char *text)
Handles save callback for badge info line 1.
void startBadgeTextEdit()
Starts badge-text editing wizard.
void onTimeConfirm(uint8_t hour, uint8_t minute)
Applies confirmed time to system clock.
static hal::IDisplay * s_display
External dependencies injected by AppUi.
void onTimezoneSave(uint16_t value)
Saves timezone offset and refreshes lock-screen clock.
static void onBadgeInfo2Save(const char *text)
Handles save callback for badge info line 2.
static void showBadgeTextStep(uint8_t step)
Forward declarations for internal helper callbacks.
static uint8_t s_badgeTextPendingStep
static constexpr uint8_t BADGE_STEP_NONE
Badge text editing workflow state.
void init(hal::IDisplay *display, hal::ISleepController *sleep, LockScreenView *lockScreen)
Initializes shared dependencies used by the settings handlers.
void onSleepIntervalSave(uint16_t value)
Saves lock-screen sleep interval in minutes.
const char * tr(const char *key)
Look up a translation by string key.
Definition I18n.h:208
Gdey029T94 * display
T9InputView * showT9Input(const char *title, const char *initialText, T9InputView::SaveCallback onSave, uint16_t maxLen=128)
Shows a shared T9 input view instance.