TimerController: Use chrono for durations
This commit is contained in:
parent
56b6291ab7
commit
05f404950a
|
@ -13,17 +13,17 @@ void TimerController::Init(Pinetime::System::SystemTask* systemTask) {
|
||||||
timer = xTimerCreate("Timer", 1, pdFALSE, this, TimerCallback);
|
timer = xTimerCreate("Timer", 1, pdFALSE, this, TimerCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TimerController::StartTimer(uint32_t duration) {
|
void TimerController::StartTimer(std::chrono::milliseconds duration) {
|
||||||
xTimerChangePeriod(timer, pdMS_TO_TICKS(duration), 0);
|
xTimerChangePeriod(timer, pdMS_TO_TICKS(duration.count()), 0);
|
||||||
xTimerStart(timer, 0);
|
xTimerStart(timer, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t TimerController::GetTimeRemaining() {
|
std::chrono::milliseconds TimerController::GetTimeRemaining() {
|
||||||
if (IsRunning()) {
|
if (IsRunning()) {
|
||||||
TickType_t remainingTime = xTimerGetExpiryTime(timer) - xTaskGetTickCount();
|
TickType_t remainingTime = xTimerGetExpiryTime(timer) - xTaskGetTickCount();
|
||||||
return (remainingTime * 1000 / configTICK_RATE_HZ);
|
return std::chrono::milliseconds(remainingTime * 1000 / configTICK_RATE_HZ);
|
||||||
}
|
}
|
||||||
return 0;
|
return std::chrono::milliseconds(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TimerController::StopTimer() {
|
void TimerController::StopTimer() {
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
#include <FreeRTOS.h>
|
#include <FreeRTOS.h>
|
||||||
#include <timers.h>
|
#include <timers.h>
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
namespace Pinetime {
|
namespace Pinetime {
|
||||||
namespace System {
|
namespace System {
|
||||||
class SystemTask;
|
class SystemTask;
|
||||||
|
@ -16,11 +18,11 @@ namespace Pinetime {
|
||||||
|
|
||||||
void Init(System::SystemTask* systemTask);
|
void Init(System::SystemTask* systemTask);
|
||||||
|
|
||||||
void StartTimer(uint32_t duration);
|
void StartTimer(std::chrono::milliseconds duration);
|
||||||
|
|
||||||
void StopTimer();
|
void StopTimer();
|
||||||
|
|
||||||
uint32_t GetTimeRemaining();
|
std::chrono::milliseconds GetTimeRemaining();
|
||||||
|
|
||||||
bool IsRunning();
|
bool IsRunning();
|
||||||
|
|
||||||
|
|
|
@ -104,9 +104,9 @@ void Timer::UpdateMask() {
|
||||||
|
|
||||||
void Timer::Refresh() {
|
void Timer::Refresh() {
|
||||||
if (timerController.IsRunning()) {
|
if (timerController.IsRunning()) {
|
||||||
uint32_t seconds = timerController.GetTimeRemaining() / 1000;
|
auto secondsRemaining = std::chrono::duration_cast<std::chrono::seconds>(timerController.GetTimeRemaining());
|
||||||
minuteCounter.SetValue(seconds / 60);
|
minuteCounter.SetValue(secondsRemaining.count() / 60);
|
||||||
secondCounter.SetValue(seconds % 60);
|
secondCounter.SetValue(secondsRemaining.count() % 60);
|
||||||
} else if (buttonPressing && xTaskGetTickCount() > pressTime + pdMS_TO_TICKS(150)) {
|
} else if (buttonPressing && xTaskGetTickCount() > pressTime + pdMS_TO_TICKS(150)) {
|
||||||
lv_label_set_text_static(txtPlayPause, "Reset");
|
lv_label_set_text_static(txtPlayPause, "Reset");
|
||||||
maskPosition += 15;
|
maskPosition += 15;
|
||||||
|
@ -133,13 +133,14 @@ void Timer::SetTimerStopped() {
|
||||||
|
|
||||||
void Timer::ToggleRunning() {
|
void Timer::ToggleRunning() {
|
||||||
if (timerController.IsRunning()) {
|
if (timerController.IsRunning()) {
|
||||||
uint32_t seconds = timerController.GetTimeRemaining() / 1000;
|
auto secondsRemaining = std::chrono::duration_cast<std::chrono::seconds>(timerController.GetTimeRemaining());
|
||||||
minuteCounter.SetValue(seconds / 60);
|
minuteCounter.SetValue(secondsRemaining.count() / 60);
|
||||||
secondCounter.SetValue(seconds % 60);
|
secondCounter.SetValue(secondsRemaining.count() % 60);
|
||||||
timerController.StopTimer();
|
timerController.StopTimer();
|
||||||
SetTimerStopped();
|
SetTimerStopped();
|
||||||
} else if (secondCounter.GetValue() + minuteCounter.GetValue() > 0) {
|
} else if (secondCounter.GetValue() + minuteCounter.GetValue() > 0) {
|
||||||
timerController.StartTimer((secondCounter.GetValue() + minuteCounter.GetValue() * 60) * 1000);
|
auto timerDuration = std::chrono::minutes(minuteCounter.GetValue()) + std::chrono::seconds(secondCounter.GetValue());
|
||||||
|
timerController.StartTimer(timerDuration);
|
||||||
Refresh();
|
Refresh();
|
||||||
SetTimerRunning();
|
SetTimerRunning();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue