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-03-27 14:21:44 -04:00
|
|
|
void Timer::CreateButtons() {
|
2021-05-20 14:43:54 -04:00
|
|
|
btnMinutesUp = lv_btn_create(lv_scr_act(), nullptr);
|
|
|
|
btnMinutesUp->user_data = this;
|
|
|
|
lv_obj_set_event_cb(btnMinutesUp, btnEventHandler);
|
2022-04-09 05:55:01 -04:00
|
|
|
lv_obj_set_size(btnMinutesUp, 60, 40);
|
|
|
|
lv_obj_align(btnMinutesUp, lv_scr_act(), LV_ALIGN_IN_LEFT_MID, 20, -85);
|
2021-05-20 14:43:54 -04:00
|
|
|
txtMUp = lv_label_create(btnMinutesUp, nullptr);
|
2022-03-20 10:47:25 -04:00
|
|
|
lv_label_set_text_static(txtMUp, "+");
|
2021-07-19 09:26:12 -04:00
|
|
|
|
2021-05-20 14:43:54 -04:00
|
|
|
btnMinutesDown = lv_btn_create(lv_scr_act(), nullptr);
|
|
|
|
btnMinutesDown->user_data = this;
|
|
|
|
lv_obj_set_event_cb(btnMinutesDown, btnEventHandler);
|
2022-04-09 05:55:01 -04:00
|
|
|
lv_obj_set_size(btnMinutesDown, 60, 40);
|
|
|
|
lv_obj_align(btnMinutesDown, lv_scr_act(), LV_ALIGN_IN_LEFT_MID, 20, 35);
|
2021-05-20 14:43:54 -04:00
|
|
|
txtMDown = lv_label_create(btnMinutesDown, nullptr);
|
2022-03-20 10:47:25 -04:00
|
|
|
lv_label_set_text_static(txtMDown, "-");
|
2021-07-19 09:26:12 -04:00
|
|
|
|
2021-05-20 14:43:54 -04:00
|
|
|
btnSecondsUp = lv_btn_create(lv_scr_act(), nullptr);
|
|
|
|
btnSecondsUp->user_data = this;
|
|
|
|
lv_obj_set_event_cb(btnSecondsUp, btnEventHandler);
|
2022-04-09 05:55:01 -04:00
|
|
|
lv_obj_set_size(btnSecondsUp, 60, 40);
|
|
|
|
lv_obj_align(btnSecondsUp, lv_scr_act(), LV_ALIGN_IN_RIGHT_MID, -20, -85);
|
2021-05-20 14:43:54 -04:00
|
|
|
txtSUp = lv_label_create(btnSecondsUp, nullptr);
|
2022-03-20 10:47:25 -04:00
|
|
|
lv_label_set_text_static(txtSUp, "+");
|
2021-07-19 09:26:12 -04:00
|
|
|
|
2021-05-20 14:43:54 -04:00
|
|
|
btnSecondsDown = lv_btn_create(lv_scr_act(), nullptr);
|
|
|
|
btnSecondsDown->user_data = this;
|
|
|
|
lv_obj_set_event_cb(btnSecondsDown, btnEventHandler);
|
2022-04-09 05:55:01 -04:00
|
|
|
lv_obj_set_size(btnSecondsDown, 60, 40);
|
|
|
|
lv_obj_align(btnSecondsDown, lv_scr_act(), LV_ALIGN_IN_RIGHT_MID, -20, 35);
|
2021-05-20 14:43:54 -04:00
|
|
|
txtSDown = lv_label_create(btnSecondsDown, nullptr);
|
2022-03-20 10:47:25 -04:00
|
|
|
lv_label_set_text_static(txtSDown, "-");
|
2021-05-20 14:43:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Timer::Timer(DisplayApp* app, Controllers::TimerController& timerController)
|
2021-07-19 09:26:12 -04:00
|
|
|
: Screen(app), running {true}, timerController {timerController} {
|
2022-03-27 14:21:44 -04:00
|
|
|
backgroundLabel = lv_label_create(lv_scr_act(), nullptr);
|
|
|
|
lv_obj_set_click(backgroundLabel, true);
|
|
|
|
lv_label_set_long_mode(backgroundLabel, LV_LABEL_LONG_CROP);
|
|
|
|
lv_obj_set_size(backgroundLabel, 240, 240);
|
|
|
|
lv_obj_set_pos(backgroundLabel, 0, 0);
|
2022-03-20 10:47:25 -04:00
|
|
|
lv_label_set_text_static(backgroundLabel, "");
|
2021-07-19 09:26:12 -04:00
|
|
|
|
2021-05-20 14:43:54 -04:00
|
|
|
time = lv_label_create(lv_scr_act(), nullptr);
|
|
|
|
lv_obj_set_style_local_text_font(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_76);
|
|
|
|
lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
|
2021-07-19 09:26:12 -04:00
|
|
|
|
2021-05-20 14:43:54 -04:00
|
|
|
uint32_t seconds = timerController.GetTimeRemaining() / 1000;
|
2021-06-24 18:07:40 -04:00
|
|
|
lv_label_set_text_fmt(time, "%02lu:%02lu", seconds / 60, seconds % 60);
|
2022-04-09 05:55:01 -04:00
|
|
|
lv_obj_align(time, lv_scr_act(), LV_ALIGN_CENTER, 0, -25);
|
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;
|
|
|
|
lv_obj_set_event_cb(btnPlayPause, btnEventHandler);
|
2022-04-09 05:55:01 -04:00
|
|
|
lv_obj_set_size(btnPlayPause, 120, 50);
|
|
|
|
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);
|
|
|
|
if (timerController.IsRunning()) {
|
2022-03-20 10:47:25 -04:00
|
|
|
lv_label_set_text_static(txtPlayPause, Symbols::pause);
|
2021-05-20 14:43:54 -04:00
|
|
|
} else {
|
2022-03-20 10:47:25 -04:00
|
|
|
lv_label_set_text_static(txtPlayPause, Symbols::play);
|
2022-03-27 14:21:44 -04:00
|
|
|
CreateButtons();
|
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;
|
2021-06-24 18:07:40 -04:00
|
|
|
lv_label_set_text_fmt(time, "%02lu:%02lu", seconds / 60, seconds % 60);
|
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()) {
|
2022-03-20 10:47:25 -04:00
|
|
|
lv_label_set_text_static(txtPlayPause, Symbols::play);
|
2021-05-20 14:43:54 -04:00
|
|
|
uint32_t seconds = timerController.GetTimeRemaining() / 1000;
|
|
|
|
minutesToSet = seconds / 60;
|
|
|
|
secondsToSet = seconds % 60;
|
|
|
|
timerController.StopTimer();
|
2022-03-27 14:21:44 -04:00
|
|
|
CreateButtons();
|
2021-07-19 09:26:12 -04:00
|
|
|
|
2021-05-20 14:43:54 -04:00
|
|
|
} else if (secondsToSet + minutesToSet > 0) {
|
2022-03-20 10:47:25 -04:00
|
|
|
lv_label_set_text_static(txtPlayPause, Symbols::pause);
|
2021-05-20 14:43:54 -04:00
|
|
|
timerController.StartTimer((secondsToSet + minutesToSet * 60) * 1000);
|
2021-07-19 09:26:12 -04:00
|
|
|
|
2021-05-20 14:43:54 -04:00
|
|
|
lv_obj_del(btnSecondsDown);
|
|
|
|
btnSecondsDown = nullptr;
|
|
|
|
lv_obj_del(btnSecondsUp);
|
|
|
|
btnSecondsUp = nullptr;
|
|
|
|
lv_obj_del(btnMinutesDown);
|
|
|
|
btnMinutesDown = nullptr;
|
|
|
|
lv_obj_del(btnMinutesUp);
|
|
|
|
btnMinutesUp = nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!timerController.IsRunning()) {
|
|
|
|
if (obj == btnMinutesUp) {
|
|
|
|
if (minutesToSet >= 59) {
|
|
|
|
minutesToSet = 0;
|
|
|
|
} else {
|
|
|
|
minutesToSet++;
|
|
|
|
}
|
|
|
|
lv_label_set_text_fmt(time, "%02d:%02d", minutesToSet, secondsToSet);
|
2021-07-19 09:26:12 -04:00
|
|
|
|
2021-05-20 14:43:54 -04:00
|
|
|
} else if (obj == btnMinutesDown) {
|
|
|
|
if (minutesToSet == 0) {
|
|
|
|
minutesToSet = 59;
|
|
|
|
} else {
|
|
|
|
minutesToSet--;
|
|
|
|
}
|
|
|
|
lv_label_set_text_fmt(time, "%02d:%02d", minutesToSet, secondsToSet);
|
2021-07-19 09:26:12 -04:00
|
|
|
|
2021-05-20 14:43:54 -04:00
|
|
|
} else if (obj == btnSecondsUp) {
|
|
|
|
if (secondsToSet >= 59) {
|
|
|
|
secondsToSet = 0;
|
|
|
|
} else {
|
|
|
|
secondsToSet++;
|
|
|
|
}
|
|
|
|
lv_label_set_text_fmt(time, "%02d:%02d", minutesToSet, secondsToSet);
|
2021-07-19 09:26:12 -04:00
|
|
|
|
2021-05-20 14:43:54 -04:00
|
|
|
} else if (obj == btnSecondsDown) {
|
|
|
|
if (secondsToSet == 0) {
|
|
|
|
secondsToSet = 59;
|
|
|
|
} else {
|
|
|
|
secondsToSet--;
|
|
|
|
}
|
|
|
|
lv_label_set_text_fmt(time, "%02d:%02d", minutesToSet, secondsToSet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-27 14:21:44 -04:00
|
|
|
void Timer::SetDone() {
|
2022-03-20 10:47:25 -04:00
|
|
|
lv_label_set_text_static(time, "00:00");
|
|
|
|
lv_label_set_text_static(txtPlayPause, Symbols::play);
|
2021-05-20 14:43:54 -04:00
|
|
|
secondsToSet = 0;
|
|
|
|
minutesToSet = 0;
|
2022-03-27 14:21:44 -04:00
|
|
|
CreateButtons();
|
2021-07-19 09:26:12 -04:00
|
|
|
}
|