2021-10-13 22:08:35 +02:00
|
|
|
#include "displayapp/screens/Timer.h"
|
|
|
|
#include "displayapp/screens/Screen.h"
|
|
|
|
#include "displayapp/screens/Symbols.h"
|
|
|
|
#include <lvgl/lvgl.h>
|
2021-05-20 20:43:54 +02:00
|
|
|
|
|
|
|
using namespace Pinetime::Applications::Screens;
|
|
|
|
|
|
|
|
static void btnEventHandler(lv_obj_t* obj, lv_event_t event) {
|
2022-03-27 20:21:44 +02:00
|
|
|
auto* screen = static_cast<Timer*>(obj->user_data);
|
2021-05-20 20:43:54 +02:00
|
|
|
screen->OnButtonEvent(obj, event);
|
|
|
|
}
|
|
|
|
|
2022-05-13 20:35:05 +02:00
|
|
|
Timer::Timer(DisplayApp* app, Controllers::TimerController& timerController) : Screen(app), timerController {timerController} {
|
2021-07-19 15:26:12 +02:00
|
|
|
|
2022-05-19 11:08:47 +02:00
|
|
|
lv_obj_t* colonLabel = lv_label_create(lv_scr_act(), nullptr);
|
|
|
|
lv_obj_set_style_local_text_font(colonLabel, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_76);
|
|
|
|
lv_obj_set_style_local_text_color(colonLabel, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_WHITE);
|
|
|
|
lv_label_set_text_static(colonLabel, ":");
|
|
|
|
lv_obj_align(colonLabel, lv_scr_act(), LV_ALIGN_CENTER, 0, -29);
|
2021-07-19 15:26:12 +02:00
|
|
|
|
2022-05-13 20:35:05 +02:00
|
|
|
minuteCounter.Create();
|
|
|
|
secondCounter.Create();
|
|
|
|
lv_obj_align(minuteCounter.GetObject(), nullptr, LV_ALIGN_IN_TOP_LEFT, 0, 0);
|
|
|
|
lv_obj_align(secondCounter.GetObject(), nullptr, LV_ALIGN_IN_TOP_RIGHT, 0, 0);
|
2021-07-19 15:26:12 +02:00
|
|
|
|
2021-05-20 20:43:54 +02:00
|
|
|
btnPlayPause = lv_btn_create(lv_scr_act(), nullptr);
|
|
|
|
btnPlayPause->user_data = this;
|
2022-05-13 20:35:05 +02:00
|
|
|
lv_obj_set_style_local_radius(btnPlayPause, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_RADIUS_CIRCLE);
|
|
|
|
lv_obj_set_style_local_bg_color(btnPlayPause, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x38, 0x38, 0x38));
|
2021-05-20 20:43:54 +02:00
|
|
|
lv_obj_set_event_cb(btnPlayPause, btnEventHandler);
|
2022-05-13 20:35:05 +02:00
|
|
|
lv_obj_set_size(btnPlayPause, LV_HOR_RES, 50);
|
2022-04-09 11:55:01 +02:00
|
|
|
lv_obj_align(btnPlayPause, lv_scr_act(), LV_ALIGN_IN_BOTTOM_MID, 0, 0);
|
2021-05-20 20:43:54 +02:00
|
|
|
txtPlayPause = lv_label_create(btnPlayPause, nullptr);
|
2022-05-13 20:35:05 +02:00
|
|
|
|
2021-05-20 20:43:54 +02:00
|
|
|
if (timerController.IsRunning()) {
|
2022-05-13 20:35:05 +02:00
|
|
|
SetTimerRunning();
|
2021-05-20 20:43:54 +02:00
|
|
|
} else {
|
2022-05-13 20:35:05 +02:00
|
|
|
SetTimerStopped();
|
2021-05-20 20:43:54 +02:00
|
|
|
}
|
2021-07-19 15:26:12 +02:00
|
|
|
|
2021-07-19 16:06:20 +02:00
|
|
|
taskRefresh = lv_task_create(RefreshTaskCallback, LV_DISP_DEF_REFR_PERIOD, LV_TASK_PRIO_MID, this);
|
2021-05-20 20:43:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Timer::~Timer() {
|
2021-07-19 15:26:12 +02:00
|
|
|
lv_task_del(taskRefresh);
|
2021-05-20 20:43:54 +02:00
|
|
|
lv_obj_clean(lv_scr_act());
|
|
|
|
}
|
|
|
|
|
2021-07-19 15:26:12 +02:00
|
|
|
void Timer::Refresh() {
|
2021-05-20 20:43:54 +02:00
|
|
|
if (timerController.IsRunning()) {
|
|
|
|
uint32_t seconds = timerController.GetTimeRemaining() / 1000;
|
2022-05-19 11:08:47 +02:00
|
|
|
minuteCounter.SetValue(seconds / 60);
|
|
|
|
secondCounter.SetValue(seconds % 60);
|
2021-05-20 20:43:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-13 20:35:05 +02:00
|
|
|
void Timer::SetTimerRunning() {
|
2022-05-19 11:08:47 +02:00
|
|
|
minuteCounter.HideControls();
|
|
|
|
secondCounter.HideControls();
|
2022-05-13 20:35:05 +02:00
|
|
|
lv_label_set_text_static(txtPlayPause, "Pause");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Timer::SetTimerStopped() {
|
2022-05-19 11:08:47 +02:00
|
|
|
minuteCounter.ShowControls();
|
|
|
|
secondCounter.ShowControls();
|
2022-05-13 20:35:05 +02:00
|
|
|
lv_label_set_text_static(txtPlayPause, "Start");
|
|
|
|
}
|
|
|
|
|
2021-05-20 20:43:54 +02:00
|
|
|
void Timer::OnButtonEvent(lv_obj_t* obj, lv_event_t event) {
|
|
|
|
if (event == LV_EVENT_CLICKED) {
|
|
|
|
if (obj == btnPlayPause) {
|
|
|
|
if (timerController.IsRunning()) {
|
|
|
|
uint32_t seconds = timerController.GetTimeRemaining() / 1000;
|
2022-05-13 20:35:05 +02:00
|
|
|
minuteCounter.SetValue(seconds / 60);
|
|
|
|
secondCounter.SetValue(seconds % 60);
|
2021-05-20 20:43:54 +02:00
|
|
|
timerController.StopTimer();
|
2022-05-13 20:35:05 +02:00
|
|
|
SetTimerStopped();
|
|
|
|
} else if (secondCounter.GetValue() + minuteCounter.GetValue() > 0) {
|
|
|
|
timerController.StartTimer((secondCounter.GetValue() + minuteCounter.GetValue() * 60) * 1000);
|
|
|
|
Refresh();
|
|
|
|
SetTimerRunning();
|
2021-05-20 20:43:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-27 20:21:44 +02:00
|
|
|
void Timer::SetDone() {
|
2022-05-13 20:35:05 +02:00
|
|
|
minuteCounter.SetValue(0);
|
|
|
|
secondCounter.SetValue(0);
|
|
|
|
SetTimerStopped();
|
2021-07-19 15:26:12 +02:00
|
|
|
}
|