diff options
Diffstat (limited to 'src/components')
-rw-r--r-- | src/components/alarm/AlarmController.cpp | 28 | ||||
-rw-r--r-- | src/components/alarm/AlarmController.h | 3 | ||||
-rw-r--r-- | src/components/motor/MotorController.cpp | 26 | ||||
-rw-r--r-- | src/components/motor/MotorController.h | 8 |
4 files changed, 31 insertions, 34 deletions
diff --git a/src/components/alarm/AlarmController.cpp b/src/components/alarm/AlarmController.cpp index d4122737..9f4e9105 100644 --- a/src/components/alarm/AlarmController.cpp +++ b/src/components/alarm/AlarmController.cpp @@ -17,7 +17,6 @@ */ #include "components/alarm/AlarmController.h" #include "systemtask/SystemTask.h" -#include "app_timer.h" #include "task.h" #include <chrono> @@ -27,20 +26,16 @@ using namespace std::chrono_literals; AlarmController::AlarmController(Controllers::DateTime& dateTimeController) : dateTimeController {dateTimeController} { } -APP_TIMER_DEF(alarmAppTimer); - namespace { - void SetOffAlarm(void* p_context) { - auto* controller = static_cast<Pinetime::Controllers::AlarmController*>(p_context); - if (controller != nullptr) { - controller->SetOffAlarmNow(); - } + void SetOffAlarm(TimerHandle_t xTimer) { + auto controller = static_cast<Pinetime::Controllers::AlarmController*>(pvTimerGetTimerID(xTimer)); + controller->SetOffAlarmNow(); } } void AlarmController::Init(System::SystemTask* systemTask) { - app_timer_create(&alarmAppTimer, APP_TIMER_MODE_SINGLE_SHOT, SetOffAlarm); this->systemTask = systemTask; + alarmTimer = xTimerCreate("Alarm", 1, pdFALSE, this, SetOffAlarm); } void AlarmController::SetAlarmTime(uint8_t alarmHr, uint8_t alarmMin) { @@ -49,8 +44,8 @@ void AlarmController::SetAlarmTime(uint8_t alarmHr, uint8_t alarmMin) { } void AlarmController::ScheduleAlarm() { - // Determine the next time the alarm needs to go off and set the app_timer - app_timer_stop(alarmAppTimer); + // Determine the next time the alarm needs to go off and set the timer + xTimerStop(alarmTimer, 0); auto now = dateTimeController.CurrentDateTime(); alarmTime = now; @@ -80,8 +75,9 @@ void AlarmController::ScheduleAlarm() { // now can convert back to a time_point alarmTime = std::chrono::system_clock::from_time_t(std::mktime(tmAlarmTime)); - auto mSecToAlarm = std::chrono::duration_cast<std::chrono::milliseconds>(alarmTime - now).count(); - app_timer_start(alarmAppTimer, APP_TIMER_TICKS(mSecToAlarm), this); + auto secondsToAlarm = std::chrono::duration_cast<std::chrono::seconds>(alarmTime - now).count(); + xTimerChangePeriod(alarmTimer, secondsToAlarm * configTICK_RATE_HZ, 0); + xTimerStart(alarmTimer, 0); state = AlarmState::Set; } @@ -91,7 +87,7 @@ uint32_t AlarmController::SecondsToAlarm() { } void AlarmController::DisableAlarm() { - app_timer_stop(alarmAppTimer); + xTimerStop(alarmTimer, 0); state = AlarmState::Not_Set; } @@ -101,14 +97,12 @@ void AlarmController::SetOffAlarmNow() { } void AlarmController::StopAlerting() { - systemTask->PushMessage(System::Messages::StopRinging); - // Alarm state is off unless this is a recurring alarm if (recurrence == RecurType::None) { state = AlarmState::Not_Set; } else { - state = AlarmState::Set; // set next instance ScheduleAlarm(); } + systemTask->PushMessage(System::Messages::StopRinging); } diff --git a/src/components/alarm/AlarmController.h b/src/components/alarm/AlarmController.h index f39fbded..d630a128 100644 --- a/src/components/alarm/AlarmController.h +++ b/src/components/alarm/AlarmController.h @@ -17,6 +17,8 @@ */ #pragma once +#include <FreeRTOS.h> +#include <timers.h> #include <cstdint> #include "components/datetime/DateTimeController.h" @@ -57,6 +59,7 @@ namespace Pinetime { private: Controllers::DateTime& dateTimeController; System::SystemTask* systemTask = nullptr; + TimerHandle_t alarmTimer; uint8_t hours = 7; uint8_t minutes = 0; std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> alarmTime; diff --git a/src/components/motor/MotorController.cpp b/src/components/motor/MotorController.cpp index c794a02c..90e41d20 100644 --- a/src/components/motor/MotorController.cpp +++ b/src/components/motor/MotorController.cpp @@ -1,43 +1,39 @@ #include "components/motor/MotorController.h" #include <hal/nrf_gpio.h> #include "systemtask/SystemTask.h" -#include "app_timer.h" #include "drivers/PinMap.h" -APP_TIMER_DEF(shortVibTimer); -APP_TIMER_DEF(longVibTimer); - using namespace Pinetime::Controllers; void MotorController::Init() { nrf_gpio_cfg_output(PinMap::Motor); nrf_gpio_pin_set(PinMap::Motor); - app_timer_init(); - app_timer_create(&shortVibTimer, APP_TIMER_MODE_SINGLE_SHOT, StopMotor); - app_timer_create(&longVibTimer, APP_TIMER_MODE_REPEATED, Ring); + shortVib = xTimerCreate("shortVib", 1, pdFALSE, nullptr, StopMotor); + longVib = xTimerCreate("longVib", pdMS_TO_TICKS(1000), pdTRUE, this, Ring); } -void MotorController::Ring(void* p_context) { - auto* motorController = static_cast<MotorController*>(p_context); +void MotorController::Ring(TimerHandle_t xTimer) { + auto* motorController = static_cast<MotorController*>(pvTimerGetTimerID(xTimer)); motorController->RunForDuration(50); } void MotorController::RunForDuration(uint8_t motorDuration) { - nrf_gpio_pin_clear(PinMap::Motor); - app_timer_start(shortVibTimer, APP_TIMER_TICKS(motorDuration), nullptr); + if (xTimerChangePeriod(shortVib, pdMS_TO_TICKS(motorDuration), 0) == pdPASS && xTimerStart(shortVib, 0) == pdPASS) { + nrf_gpio_pin_clear(PinMap::Motor); + } } void MotorController::StartRinging() { - Ring(this); - app_timer_start(longVibTimer, APP_TIMER_TICKS(1000), this); + RunForDuration(50); + xTimerStart(longVib, 0); } void MotorController::StopRinging() { - app_timer_stop(longVibTimer); + xTimerStop(longVib, 0); nrf_gpio_pin_set(PinMap::Motor); } -void MotorController::StopMotor(void* p_context) { +void MotorController::StopMotor(TimerHandle_t xTimer) { nrf_gpio_pin_set(PinMap::Motor); } diff --git a/src/components/motor/MotorController.h b/src/components/motor/MotorController.h index b5a592b1..6dea6d1f 100644 --- a/src/components/motor/MotorController.h +++ b/src/components/motor/MotorController.h @@ -1,5 +1,7 @@ #pragma once +#include <FreeRTOS.h> +#include <timers.h> #include <cstdint> namespace Pinetime { @@ -15,8 +17,10 @@ namespace Pinetime { void StopRinging(); private: - static void Ring(void* p_context); - static void StopMotor(void* p_context); + static void Ring(TimerHandle_t xTimer); + static void StopMotor(TimerHandle_t xTimer); + TimerHandle_t shortVib; + TimerHandle_t longVib; }; } } |