Skip to content

Commit

Permalink
Timer: let timer go overtime
Browse files Browse the repository at this point in the history
  • Loading branch information
hatmajster committed Aug 4, 2021
1 parent 514481e commit 3f1b834
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 83 deletions.
32 changes: 12 additions & 20 deletions src/components/timer/TimerController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@

using namespace Pinetime::Controllers;


APP_TIMER_DEF(timerAppTimer);

namespace {
void TimerEnd(void* p_context) {
auto* controller = static_cast<Pinetime::Controllers::TimerController*> (p_context);
if(controller != nullptr)
auto* controller = static_cast<Pinetime::Controllers::TimerController*>(p_context);
if (controller != nullptr)
controller->OnTimerEnd();
}
}


void TimerController::Init() {
app_timer_create(&timerAppTimer, APP_TIMER_MODE_SINGLE_SHOT, TimerEnd);
}
Expand All @@ -31,37 +29,31 @@ void TimerController::StartTimer(uint32_t duration) {
app_timer_start(timerAppTimer, APP_TIMER_TICKS(duration), this);
endTicks = currentTicks + APP_TIMER_TICKS(duration);
timerRunning = true;
overtime = false;
}

uint32_t TimerController::GetTimeRemaining() {
int32_t TimerController::GetSecondsRemaining() {
if (!timerRunning) {
return 0;
}
auto currentTicks = xTaskGetTickCount();

TickType_t deltaTicks = 0;
if (currentTicks > endTicks) {
deltaTicks = 0xffffffff - currentTicks;
deltaTicks += (endTicks + 1);
} else {
deltaTicks = endTicks - currentTicks;
}

return (static_cast<TickType_t>(deltaTicks) / static_cast<TickType_t>(configTICK_RATE_HZ)) * 1000;

int32_t deltaTicks = static_cast<int32_t>(endTicks) - static_cast<int32_t>(currentTicks);

return (deltaTicks / static_cast<int32_t>(configTICK_RATE_HZ));
}

void TimerController::StopTimer() {
app_timer_stop(timerAppTimer);
timerRunning = false;
overtime = false;
}

bool TimerController::IsRunning() {
return timerRunning;
}
void TimerController::OnTimerEnd() {
timerRunning = false;
if(systemTask != nullptr)
overtime = true;
if (systemTask != nullptr) {
systemTask->PushMessage(System::Messages::OnTimerDone);
}
}

void TimerController::Register(Pinetime::System::SystemTask* systemTask) {
Expand Down
23 changes: 15 additions & 8 deletions src/components/timer/TimerController.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,26 @@ namespace Pinetime {
class SystemTask;
}
namespace Controllers {

class TimerController {
public:
TimerController() = default;

void Init();

void StartTimer(uint32_t duration);

void StopTimer();

uint32_t GetTimeRemaining();

bool IsRunning();

int32_t GetSecondsRemaining();

bool IsOvertime() {
return overtime;
}

bool IsRunning() {
return timerRunning;
}

void OnTimerEnd();

Expand All @@ -32,6 +38,7 @@ namespace Pinetime {
System::SystemTask* systemTask = nullptr;
TickType_t endTicks;
bool timerRunning = false;
bool overtime = false;
};
}
}
93 changes: 55 additions & 38 deletions src/displayapp/screens/Timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
#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<Timer*>(obj->user_data);
screen->OnButtonEvent(obj, event);
Expand All @@ -22,7 +20,7 @@ void Timer::createButtons() {
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);
Expand All @@ -31,7 +29,7 @@ void Timer::createButtons() {
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);
Expand All @@ -40,7 +38,7 @@ void Timer::createButtons() {
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);
Expand All @@ -49,47 +47,67 @@ void Timer::createButtons() {
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} {

: 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;

int32_t seconds = timerController.GetSecondsRemaining();
bool overtime = timerController.IsOvertime();

if (overtime) {
seconds = -seconds + 1; // "+ 1" is to not show -00:00 again after +00:00
lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
} else {
lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
}

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);
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);
lv_label_set_text(txtPlayPause, overtime ? Symbols::stop : Symbols::pause);
} else {
lv_label_set_text(txtPlayPause, Symbols::play);
createButtons();
}

}

Timer::~Timer() {
lv_obj_clean(lv_scr_act());

if (timerController.IsRunning() and timerController.IsOvertime()) {
timerController.StopTimer();
}
}

bool Timer::Refresh() {
if (timerController.IsRunning()) {
uint32_t seconds = timerController.GetTimeRemaining() / 1000;
int32_t seconds = timerController.GetSecondsRemaining();
if (timerController.IsOvertime()) {
seconds = -seconds + 1; // "+ 1" is to not show -00:00 again after +00:00

// safety measures, lets not overflow counter as it will display badly
if (seconds >= 100 * 60) {
minutesToSet = 0;
secondsToSet = 0;
lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
lv_label_set_text_static(time, "00:00");
lv_label_set_text(txtPlayPause, Symbols::play);
timerController.StopTimer();
createButtons();
return running;
}
}
lv_label_set_text_fmt(time, "%02lu:%02lu", seconds / 60, seconds % 60);
}
return running;
Expand All @@ -100,16 +118,24 @@ void Timer::OnButtonEvent(lv_obj_t* obj, lv_event_t event) {
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;
int32_t secondsRemaining = timerController.GetSecondsRemaining();
if (timerController.IsOvertime()) {
minutesToSet = 0;
secondsToSet = 0;
lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
lv_label_set_text_static(time, "00:00");
} else {
minutesToSet = secondsRemaining / 60;
secondsToSet = secondsRemaining % 60;
}
timerController.StopTimer();
createButtons();

} else if (secondsToSet + minutesToSet > 0) {
lv_label_set_text(txtPlayPause, Symbols::pause);
timerController.StartTimer((secondsToSet + minutesToSet * 60) * 1000);


// inlined destroyButtons()
lv_obj_del(btnSecondsDown);
btnSecondsDown = nullptr;
lv_obj_del(btnSecondsUp);
Expand All @@ -118,7 +144,6 @@ void Timer::OnButtonEvent(lv_obj_t* obj, lv_event_t event) {
btnMinutesDown = nullptr;
lv_obj_del(btnMinutesUp);
btnMinutesUp = nullptr;

}
} else {
if (!timerController.IsRunning()) {
Expand All @@ -129,45 +154,37 @@ void Timer::OnButtonEvent(lv_obj_t* obj, lv_event_t event) {
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();
lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
lv_label_set_text(txtPlayPause, Symbols::stop);
}
27 changes: 10 additions & 17 deletions src/displayapp/screens/Timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,28 @@
#include "components/timer/TimerController.h"

namespace Pinetime::Applications::Screens {



class Timer : public Screen {
public:

enum class Modes {
Normal, Done
};

Timer(DisplayApp* app, Controllers::TimerController& timerController);

~Timer() override;

bool Refresh() override;

void setDone();

void OnButtonEvent(lv_obj_t* obj, lv_event_t event);

private:

bool running;
uint8_t secondsToSet = 0;
uint8_t minutesToSet = 0;
Controllers::TimerController& timerController;

void createButtons();
lv_obj_t* time, * msecTime, * btnPlayPause, * txtPlayPause, * btnMinutesUp, * btnMinutesDown, * btnSecondsUp, * btnSecondsDown, * txtMUp,
* txtMDown, * txtSUp, * txtSDown;

lv_obj_t *time, *btnPlayPause, *txtPlayPause, *btnMinutesUp, *btnMinutesDown, *btnSecondsUp, *btnSecondsDown, *txtMUp, *txtMDown,
*txtSUp, *txtSDown;
};
}

0 comments on commit 3f1b834

Please sign in to comment.