Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions src/components/timer/TimerController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,32 @@ void TimerController::Init(Pinetime::System::SystemTask* systemTask) {
timer = xTimerCreate("Timer", 1, pdFALSE, this, TimerCallback);
}

void TimerController::StartTimer(std::chrono::milliseconds duration) {
xTimerChangePeriod(timer, pdMS_TO_TICKS(duration.count()), 0);
void TimerController::StartTimer(std::chrono::hours hours, std::chrono::minutes minutes) {
this->hours = hours;
this->minutes = minutes;
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(hours)
+ std::chrono::duration_cast<std::chrono::milliseconds>(minutes);
StartTimer(duration);
}

void TimerController::StartTimer(std::chrono::milliseconds duration){
state = TimerState::Running;
auto durationSeconds = std::chrono::duration_cast<std::chrono::seconds>(duration);
xTimerChangePeriod(timer, durationSeconds.count() * configTICK_RATE_HZ, 0);
xTimerStart(timer, 0);
}

std::chrono::milliseconds TimerController::GetTimeRemaining() {
if (IsRunning()) {
TickType_t remainingTime = xTimerGetExpiryTime(timer) - xTaskGetTickCount();
return std::chrono::milliseconds(remainingTime * 1000 / configTICK_RATE_HZ);
auto remainingSeconds = std::chrono::seconds(remainingTime / configTICK_RATE_HZ);
return std::chrono::duration_cast<std::chrono::milliseconds>(remainingSeconds);
}
return std::chrono::milliseconds(0);
}

void TimerController::StopTimer() {
state = TimerState::Dormant;
xTimerStop(timer, 0);
}

Expand All @@ -35,5 +47,25 @@ bool TimerController::IsRunning() {
}

void TimerController::OnTimerEnd() {
systemTask->PushMessage(System::Messages::OnTimerDone);

if(!useAlert){
state = TimerState::Dormant;
systemTask->PushMessage(System::Messages::OnTimerDone);
return;
}

state = TimerState::Alerting;
systemTask->PushMessage(System::Messages::StartTimerAlert);
}

void TimerController::SnoozeAlert(){
if(state != TimerState::Alerting) return;
state = TimerState::Snoozed;
auto snoozeSeconds = std::chrono::duration_cast<std::chrono::seconds>(snoozeTime);
xTimerChangePeriod(timer, snoozeSeconds.count() * configTICK_RATE_HZ, 0);
xTimerStart(timer, 0);
}

void TimerController::StopAlerting() {
StopTimer();
}
27 changes: 25 additions & 2 deletions src/components/timer/TimerController.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ namespace Pinetime {
namespace System {
class SystemTask;
}

namespace Controllers {

class TimerController {
public:
TimerController() = default;

void Init(System::SystemTask* systemTask);

void StartTimer(std::chrono::hours hours, std::chrono::minutes minutes);

void StartTimer(std::chrono::milliseconds duration);

void StopTimer();
Expand All @@ -28,9 +28,32 @@ namespace Pinetime {

void OnTimerEnd();

enum class TimerState { Dormant, Running, Alerting, Snoozed };

void SnoozeAlert();

void StopAlerting();

std::chrono::hours Hours() const{
return hours;
}

std::chrono::minutes Minutes() const{
return minutes;
}

TimerState State() const{
return state;
}

private:
System::SystemTask* systemTask = nullptr;
TimerHandle_t timer;
bool useAlert = true;
std::chrono::hours hours = std::chrono::hours(0);
std::chrono::minutes minutes = std::chrono::minutes(0);
std::chrono::milliseconds snoozeTime = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::minutes(10));
TimerState state = TimerState::Dormant;
};
}
}
10 changes: 9 additions & 1 deletion src/displayapp/DisplayApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@ void DisplayApp::Refresh() {
}
motorController.RunForDuration(35);
break;
case Messages::TimerAlerting:
if (currentApp == Apps::Timer) {
auto* Timer = static_cast<Screens::Timer*>(currentScreen.get());
Timer->SetAlerting();
} else {
LoadNewScreen(Apps::Timer, DisplayApp::FullRefreshDirections::None);
}
break;
case Messages::AlarmTriggered:
if (currentApp == Apps::Alarm) {
auto* alarm = static_cast<Screens::Alarm*>(currentScreen.get());
Expand Down Expand Up @@ -410,7 +418,7 @@ void DisplayApp::LoadScreen(Apps app, DisplayApp::FullRefreshDirections directio
Screens::Notifications::Modes::Preview);
break;
case Apps::Timer:
currentScreen = std::make_unique<Screens::Timer>(timerController);
currentScreen = std::make_unique<Screens::Timer>(timerController, *systemTask, motorController);
break;
case Apps::Alarm:
currentScreen = std::make_unique<Screens::Alarm>(alarmController, settingsController.GetClockType(), *systemTask, motorController);
Expand Down
1 change: 1 addition & 0 deletions src/displayapp/Messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Pinetime {
ButtonDoubleClicked,
NewNotification,
TimerDone,
TimerAlerting,
BleFirmwareUpdateStarted,
UpdateTimeOut,
DimScreen,
Expand Down
Loading