Files
Lamps/Core/Menu/clock.c

210 lines
5.3 KiB
C

#include "menu_items.h"
#include "clock.h"
#include "segment.h"
#include "clock_manager.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct {
uint32_t startTime;
uint32_t elapsedTime; // Íàêîïëåííîå âðåìÿ ïðè ïàóçå
bool running;
uint8_t state; // 0 = Ñáðîøåí, 1 = Çàïóùåí, 2 = Ïàóçà
} StopwatchData;
typedef struct {
uint32_t targetTime; // Öåëåâîå âðåìÿ â ìñ
uint32_t startTime;
uint32_t elapsedTime; // Íàêîïëåííîå âðåìÿ ïðè ïàóçå
uint8_t editStep; // 0-5: ðàçðÿäû, 6: ãîòîâî
uint8_t state; // 0 = Íàñòðîéêà, 1 = Çàïóùåí, 2 = Ïàóçà, 3 = Çàâåðøåí
bool running;
bool blinkState;
uint32_t lastBlink;
} TimerData;
static StopwatchData g_stopwatch;
static TimerData g_timer;
// Âñïîìîãàòåëüíàÿ ôóíêöèÿ äëÿ ôîðìàòèðîâàíèÿ ìì:ññ:öñ
static void FormatTime(uint32_t ms, char* buf) {
uint32_t total_sec = ms / 1000;
uint32_t minutes = total_sec / 60;
uint32_t seconds = total_sec % 60;
uint32_t centiseconds = (ms % 1000) / 10;
if (minutes > 0) {
sprintf(buf, "%2d%02d%02d", minutes, seconds, centiseconds);
} else {
sprintf(buf, " %2d%02d", seconds, centiseconds);
}
}
/////// CLOCK ////////
static void Display_Clock(void) {
time_t now = ClockManager_GetTime(1);
char buf[7];
sprintf(buf, "%02d%02d%02d", now.hour, now.min, now.sec);
Segment_SetString(buf);
}
static void OnEnter_Clock(void) {
g_currentActivity = &g_clockNode;
g_rootMenu.parent = &g_clockNode;
Menu_Refresh();
}
static void Clock_OnButton(Button_Type btn, bool longPress) {
if (longPress && btn == BUTTON_SELECT) {
Menu_OpenMenu(&g_rootMenu);
return;
}
if (btn == BUTTON_BACK) {
Menu_GoBack();
}
}
/////// ÑÅÊÓÍÄÎÌÅÐ ////////
static void Display_Stopwatch(void) {
char buf[7];
uint32_t current_ms = 0;
switch (g_stopwatch.state) {
case 0: // Ñáðîøåí
sprintf(buf, " 000");
break;
case 1: // Çàïóùåí
current_ms = (HAL_GetTick() - g_stopwatch.startTime) + g_stopwatch.elapsedTime;
FormatTime(current_ms, buf);
break;
case 2: // Ïàóçà
FormatTime(g_stopwatch.elapsedTime, buf);
break;
default:
sprintf(buf, "ERROR");
break;
}
Segment_SetString(buf);
}
static void OnUpdate_Stopwatch(void) {
// Äëÿ ñåêóíäîìåðà íå íóæåí îòäåëüíûé update,
// ò.ê. Display îáíîâëÿåòñÿ ïî òàéìåðó ìåíþ
static uint32_t lastUpdate = 0;
if (g_stopwatch.state == 1) {
uint32_t tick = HAL_GetTick();
if (tick - lastUpdate >= 10) { // Îáíîâëåíèå êàæäûå 10 ìñ
lastUpdate = tick;
Menu_Refresh();
}
}
}
static void OnEnter_Stopwatch(void) {
g_currentActivity = &g_stopwatchNode;
g_rootMenu.parent = &g_stopwatchNode;
g_stopwatch.state = 0;
g_stopwatch.running = false;
g_stopwatch.elapsedTime = 0;
Menu_Refresh();
}
static void Stopwatch_OnButton(Button_Type btn, bool longPress) {
if (longPress && btn == BUTTON_SELECT) {
Menu_OpenMenu(&g_rootMenu);
return;
}
uint32_t tick = HAL_GetTick();
if (g_stopwatch.state == 0 && btn == BUTTON_SELECT) {
// Ñòàðò
g_stopwatch.state = 1;
g_stopwatch.running = true;
g_stopwatch.startTime = tick;
g_stopwatch.elapsedTime = 0;
Menu_Refresh();
}
else if (g_stopwatch.state == 1 && btn == BUTTON_SELECT) {
// Ïàóçà
g_stopwatch.state = 2;
g_stopwatch.running = false;
g_stopwatch.elapsedTime += tick - g_stopwatch.startTime;
Menu_Refresh();
}
else if (g_stopwatch.state == 2 && btn == BUTTON_SELECT) {
// Ïðîäîëæèòü
g_stopwatch.state = 1;
g_stopwatch.running = true;
g_stopwatch.startTime = tick;
Menu_Refresh();
}
else if ((g_stopwatch.state == 1 || g_stopwatch.state == 2) && btn == BUTTON_BACK) {
// Ñáðîñ
g_stopwatch.state = 0;
g_stopwatch.running = false;
g_stopwatch.elapsedTime = 0;
Menu_Refresh();
}
}
/////// ÒÀÉÌÅÐ ////////
static void Display_Timer(void) {
Segment_SetString("FUTURE");
}
static void OnUpdate_Timer(void) {
}
static void OnEnter_Timer(void) {
}
static void Timer_OnButton(Button_Type btn, bool longPress) {
}
// NODES
MenuNode g_clockNode = {
.name = "CLOCH",
.parent = NULL,
.children = NULL,
.childCount = 0,
.selectedChild = 0,
.needsRedraw = 1,
.display = Display_Clock,
.onEnter = OnEnter_Clock,
.onUpdate = NULL,
.onButton = Clock_OnButton,
.data = NULL
};
MenuNode g_stopwatchNode = {
.name = "SECOND",
.parent = NULL,
.children = NULL,
.childCount = 0,
.selectedChild = 0,
.needsRedraw = 1,
.display = Display_Stopwatch,
.onEnter = OnEnter_Stopwatch,
.onUpdate = OnUpdate_Stopwatch,
.onButton = Stopwatch_OnButton,
.data = &g_stopwatch
};
MenuNode g_timerNode = {
.name = "TIHER",
.parent = NULL,
.children = NULL,
.childCount = 0,
.selectedChild = 0,
.needsRedraw = 1,
.display = Display_Timer,
.onEnter = OnEnter_Timer,
.onUpdate = OnUpdate_Timer,
.onButton = Timer_OnButton,
.data = &g_timer
};