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