diff options
| author | Michele Bini <michele.bini@gmail.com> | 2022-04-15 22:54:15 (GMT) |
|---|---|---|
| committer | Michele Bini <michele.bini@gmail.com> | 2022-04-16 01:18:36 (GMT) |
| commit | c92fdb2e8cec3c581124c5efcfb5297ac4cfdcb5 (patch) | |
| tree | ab550245fae41067ac2696203ee94ff19fbd9d34 /src/components | |
| parent | 59f59c5f9c929307ed42abafb30f7b7e2b4081f0 (diff) | |
Switch motorController to FreeRTOS timers.
Diffstat (limited to 'src/components')
| -rw-r--r-- | src/components/motor/MotorController.cpp | 25 | ||||
| -rw-r--r-- | src/components/motor/MotorController.h | 8 |
2 files changed, 22 insertions, 11 deletions
diff --git a/src/components/motor/MotorController.cpp b/src/components/motor/MotorController.cpp index c794a02..a02c037 100644 --- a/src/components/motor/MotorController.cpp +++ b/src/components/motor/MotorController.cpp @@ -12,32 +12,39 @@ 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); + shortVibTimer = xTimerCreate("shortVibTm", 1, pdFALSE, nullptr, StopMotor); + longVibTimer = xTimerCreate("longVibTm", 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(shortVibTimer, pdMS_TO_TICKS(motorDuration), 0) == pdPASS + && xTimerStart(shortVibTimer, 0) == pdPASS) { + return; + } + if (xTimerChangePeriodFromISR(shortVibTimer, pdMS_TO_TICKS(motorDuration), NULL) == pdPASS + && xTimerStartFromISR(shortVibTimer, NULL) == pdPASS) { + return; + } + nrf_gpio_pin_set(PinMap::Motor); } void MotorController::StartRinging() { Ring(this); - app_timer_start(longVibTimer, APP_TIMER_TICKS(1000), this); + xTimerStart(longVibTimer, 0); } void MotorController::StopRinging() { - app_timer_stop(longVibTimer); + xTimerStop(longVibTimer, 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 b5a592b..3c6cbd2 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 shortVibTimer; + TimerHandle_t longVibTimer; }; } } |
