From 13e3463276114dff838fc8fe281754eecfbe9538 Mon Sep 17 00:00:00 2001 From: Florian Date: Thu, 20 May 2021 20:43:54 +0200 Subject: Timer App (#355) * built timer app * Style improvements * making sure buttons stay hidden when the app is reopened and reappear after the timer runs out * more sensible calculations of time deltas. eliminated that mysterious scaling factor * changing the timer icon --- src/displayapp/screens/Timer.cpp | 173 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 src/displayapp/screens/Timer.cpp (limited to 'src/displayapp/screens/Timer.cpp') diff --git a/src/displayapp/screens/Timer.cpp b/src/displayapp/screens/Timer.cpp new file mode 100644 index 00000000..260a17ef --- /dev/null +++ b/src/displayapp/screens/Timer.cpp @@ -0,0 +1,173 @@ +#include "Timer.h" + +#include "Screen.h" +#include "Symbols.h" +#include "lvgl/lvgl.h" + + +using namespace Pinetime::Applications::Screens; + + +static void btnEventHandler(lv_obj_t* obj, lv_event_t event) { + Timer* screen = static_cast(obj->user_data); + screen->OnButtonEvent(obj, event); +} + +void Timer::createButtons() { + btnMinutesUp = lv_btn_create(lv_scr_act(), nullptr); + btnMinutesUp->user_data = this; + lv_obj_set_event_cb(btnMinutesUp, btnEventHandler); + lv_obj_align(btnMinutesUp, lv_scr_act(), LV_ALIGN_IN_LEFT_MID, 20, -80); + lv_obj_set_height(btnMinutesUp, 40); + lv_obj_set_width(btnMinutesUp, 60); + txtMUp = lv_label_create(btnMinutesUp, nullptr); + lv_label_set_text(txtMUp, "+"); + + btnMinutesDown = lv_btn_create(lv_scr_act(), nullptr); + btnMinutesDown->user_data = this; + lv_obj_set_event_cb(btnMinutesDown, btnEventHandler); + lv_obj_align(btnMinutesDown, lv_scr_act(), LV_ALIGN_IN_LEFT_MID, 20, +40); + lv_obj_set_height(btnMinutesDown, 40); + lv_obj_set_width(btnMinutesDown, 60); + txtMDown = lv_label_create(btnMinutesDown, nullptr); + lv_label_set_text(txtMDown, "-"); + + btnSecondsUp = lv_btn_create(lv_scr_act(), nullptr); + btnSecondsUp->user_data = this; + lv_obj_set_event_cb(btnSecondsUp, btnEventHandler); + lv_obj_align(btnSecondsUp, lv_scr_act(), LV_ALIGN_IN_RIGHT_MID, 10, -80); + lv_obj_set_height(btnSecondsUp, 40); + lv_obj_set_width(btnSecondsUp, 60); + txtSUp = lv_label_create(btnSecondsUp, nullptr); + lv_label_set_text(txtSUp, "+"); + + btnSecondsDown = lv_btn_create(lv_scr_act(), nullptr); + btnSecondsDown->user_data = this; + lv_obj_set_event_cb(btnSecondsDown, btnEventHandler); + lv_obj_align(btnSecondsDown, lv_scr_act(), LV_ALIGN_IN_RIGHT_MID, 10, +40); + lv_obj_set_height(btnSecondsDown, 40); + lv_obj_set_width(btnSecondsDown, 60); + txtSDown = lv_label_create(btnSecondsDown, nullptr); + lv_label_set_text(txtSDown, "-"); + +} + + +Timer::Timer(DisplayApp* app, Controllers::TimerController& timerController) + : Screen(app), + running{true}, + timerController{timerController} { + + 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); + + uint32_t seconds = timerController.GetTimeRemaining() / 1000; + lv_label_set_text_fmt(time, "%02d:%02d", seconds / 60, seconds % 60); + + lv_obj_align(time, lv_scr_act(), LV_ALIGN_IN_LEFT_MID, 0, -20); + + btnPlayPause = lv_btn_create(lv_scr_act(), nullptr); + btnPlayPause->user_data = this; + lv_obj_set_event_cb(btnPlayPause, btnEventHandler); + lv_obj_align(btnPlayPause, lv_scr_act(), LV_ALIGN_IN_BOTTOM_MID, 0, -10); + lv_obj_set_height(btnPlayPause, 40); + txtPlayPause = lv_label_create(btnPlayPause, nullptr); + if (timerController.IsRunning()) { + lv_label_set_text(txtPlayPause, Symbols::pause); + } else { + lv_label_set_text(txtPlayPause, Symbols::play); + createButtons(); + } + +} + +Timer::~Timer() { + lv_obj_clean(lv_scr_act()); + +} + +bool Timer::Refresh() { + if (timerController.IsRunning()) { + uint32_t seconds = timerController.GetTimeRemaining() / 1000; + lv_label_set_text_fmt(time, "%02d:%02d", seconds / 60, seconds % 60); + } + return running; +} + +void Timer::OnButtonEvent(lv_obj_t* obj, lv_event_t event) { + if (event == LV_EVENT_CLICKED) { + if (obj == btnPlayPause) { + if (timerController.IsRunning()) { + lv_label_set_text(txtPlayPause, Symbols::play); + uint32_t seconds = timerController.GetTimeRemaining() / 1000; + minutesToSet = seconds / 60; + secondsToSet = seconds % 60; + timerController.StopTimer(); + createButtons(); + + } else if (secondsToSet + minutesToSet > 0) { + lv_label_set_text(txtPlayPause, Symbols::pause); + timerController.StartTimer((secondsToSet + minutesToSet * 60) * 1000); + + 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); + + } else if (obj == btnMinutesDown) { + if (minutesToSet == 0) { + minutesToSet = 59; + } else { + minutesToSet--; + } + lv_label_set_text_fmt(time, "%02d:%02d", minutesToSet, secondsToSet); + + } else if (obj == btnSecondsUp) { + if (secondsToSet >= 59) { + secondsToSet = 0; + } else { + secondsToSet++; + } + lv_label_set_text_fmt(time, "%02d:%02d", minutesToSet, secondsToSet); + + } else if (obj == btnSecondsDown) { + if (secondsToSet == 0) { + secondsToSet = 59; + } else { + secondsToSet--; + } + lv_label_set_text_fmt(time, "%02d:%02d", minutesToSet, secondsToSet); + + } + } + + } + + } + +} + + +void Timer::setDone() { + lv_label_set_text(time, "00:00"); + lv_label_set_text(txtPlayPause, Symbols::play); + secondsToSet = 0; + minutesToSet = 0; + createButtons(); +} \ No newline at end of file -- cgit v1.2.3-70-g09d2 From 2f479e5fc7688ae47e99c0f4aead87c9cdc29123 Mon Sep 17 00:00:00 2001 From: Avamander Date: Fri, 25 Jun 2021 01:07:40 +0300 Subject: Fixed a bunch of format specifiers --- src/displayapp/screens/Steps.cpp | 6 +++--- src/displayapp/screens/SystemInfo.cpp | 4 ++-- src/displayapp/screens/Timer.cpp | 6 +++--- src/displayapp/screens/settings/SettingSteps.cpp | 6 +++--- 4 files changed, 11 insertions(+), 11 deletions(-) (limited to 'src/displayapp/screens/Timer.cpp') diff --git a/src/displayapp/screens/Steps.cpp b/src/displayapp/screens/Steps.cpp index b485c975..cc78813f 100644 --- a/src/displayapp/screens/Steps.cpp +++ b/src/displayapp/screens/Steps.cpp @@ -30,8 +30,8 @@ Steps::Steps( lSteps = lv_label_create(lv_scr_act(), nullptr); lv_obj_set_style_local_text_color(lSteps, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x00FF00)); - lv_obj_set_style_local_text_font(lSteps, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42); - lv_label_set_text_fmt(lSteps, "%li", stepsCount); + lv_obj_set_style_local_text_font(lSteps, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42); + lv_label_set_text_fmt(lSteps, "%li", stepsCount); lv_obj_align(lSteps, nullptr, LV_ALIGN_CENTER, 0, -20); lv_obj_t * lstepsL = lv_label_create(lv_scr_act(), nullptr); @@ -41,7 +41,7 @@ Steps::Steps( lv_obj_t * lstepsGoal = lv_label_create(lv_scr_act(), nullptr); lv_obj_set_style_local_text_color(lstepsGoal, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_CYAN); - lv_label_set_text_fmt(lstepsGoal,"Goal\n%i", settingsController.GetStepsGoal()); + lv_label_set_text_fmt(lstepsGoal, "Goal\n%lu", settingsController.GetStepsGoal()); lv_label_set_align(lstepsGoal, LV_LABEL_ALIGN_CENTER); lv_obj_align(lstepsGoal, lSteps, LV_ALIGN_OUT_BOTTOM_MID, 0, 60); diff --git a/src/displayapp/screens/SystemInfo.cpp b/src/displayapp/screens/SystemInfo.cpp index 60e53ad6..f70b67fd 100644 --- a/src/displayapp/screens/SystemInfo.cpp +++ b/src/displayapp/screens/SystemInfo.cpp @@ -192,11 +192,11 @@ std::unique_ptr SystemInfo::CreateScreen3() { "\n" "#444444 LVGL Memory#\n" " #444444 used# %d (%d%%)\n" - " #444444 max used# %d\n" + " #444444 max used# %lu\n" " #444444 frag# %d%%\n" " #444444 free# %d" "\n" - "#444444 Steps# %li", + "#444444 Steps# %i", bleAddr[5], bleAddr[4], bleAddr[3], diff --git a/src/displayapp/screens/Timer.cpp b/src/displayapp/screens/Timer.cpp index 260a17ef..99e979ba 100644 --- a/src/displayapp/screens/Timer.cpp +++ b/src/displayapp/screens/Timer.cpp @@ -63,8 +63,8 @@ Timer::Timer(DisplayApp* app, Controllers::TimerController& timerController) lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY); uint32_t seconds = timerController.GetTimeRemaining() / 1000; - lv_label_set_text_fmt(time, "%02d:%02d", seconds / 60, seconds % 60); - + lv_label_set_text_fmt(time, "%02lu:%02lu", seconds / 60, seconds % 60); + lv_obj_align(time, lv_scr_act(), LV_ALIGN_IN_LEFT_MID, 0, -20); btnPlayPause = lv_btn_create(lv_scr_act(), nullptr); @@ -90,7 +90,7 @@ Timer::~Timer() { bool Timer::Refresh() { if (timerController.IsRunning()) { uint32_t seconds = timerController.GetTimeRemaining() / 1000; - lv_label_set_text_fmt(time, "%02d:%02d", seconds / 60, seconds % 60); + lv_label_set_text_fmt(time, "%02lu:%02lu", seconds / 60, seconds % 60); } return running; } diff --git a/src/displayapp/screens/settings/SettingSteps.cpp b/src/displayapp/screens/settings/SettingSteps.cpp index b7c024f1..faa843e6 100644 --- a/src/displayapp/screens/settings/SettingSteps.cpp +++ b/src/displayapp/screens/settings/SettingSteps.cpp @@ -45,7 +45,7 @@ SettingSteps::SettingSteps( stepValue = lv_label_create(lv_scr_act(), NULL); lv_obj_set_style_local_text_font(stepValue, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42); - lv_label_set_text_fmt(stepValue,"%i", settingsController.GetStepsGoal()); + lv_label_set_text_fmt(stepValue, "%lu", settingsController.GetStepsGoal()); lv_label_set_align(stepValue, LV_LABEL_ALIGN_CENTER); lv_obj_align(stepValue, lv_scr_act(), LV_ALIGN_CENTER, 0, -10); @@ -81,7 +81,7 @@ void SettingSteps::UpdateSelected(lv_obj_t *object, lv_event_t event) { value += 1000; if ( value <= 500000 ) { settingsController.SetStepsGoal(value); - lv_label_set_text_fmt(stepValue,"%i", settingsController.GetStepsGoal()); + lv_label_set_text_fmt(stepValue, "%lu", settingsController.GetStepsGoal()); lv_obj_align(stepValue, lv_scr_act(), LV_ALIGN_CENTER, 0, -10); } } @@ -90,7 +90,7 @@ void SettingSteps::UpdateSelected(lv_obj_t *object, lv_event_t event) { value -= 1000; if ( value >= 1000 ) { settingsController.SetStepsGoal(value); - lv_label_set_text_fmt(stepValue,"%i", settingsController.GetStepsGoal()); + lv_label_set_text_fmt(stepValue, "%lu", settingsController.GetStepsGoal()); lv_obj_align(stepValue, lv_scr_act(), LV_ALIGN_CENTER, 0, -10); } } -- cgit v1.2.3-70-g09d2