summaryrefslogtreecommitdiff
path: root/src/components
diff options
context:
space:
mode:
authorMichele Bini <michele.bini@gmail.com>2022-04-16 11:43:58 (GMT)
committerMichele Bini <michele.bini@gmail.com>2022-04-16 12:12:52 (GMT)
commit2e367ec0aea857a1aee7306a2de893f96a60f2df (patch)
tree86439281fb0ed810b83161cb36985f2a731b22bc /src/components
parentebf3859407adba9da7acbc77b1ebabfd39d80a37 (diff)
parentc9808e29f1bbfbd1396d7b83212f6d223d52f99d (diff)
Merge branch 'alarm-fix-work' into edge
# Conflicts: # src/components/timer/TimerController.cpp
Diffstat (limited to 'src/components')
-rw-r--r--src/components/alarm/AlarmController.cpp30
-rw-r--r--src/components/alarm/AlarmController.h4
-rw-r--r--src/components/motor/MotorController.cpp28
-rw-r--r--src/components/motor/MotorController.h8
-rw-r--r--src/components/timer/TimerController.cpp22
-rw-r--r--src/components/timer/TimerController.h3
6 files changed, 59 insertions, 36 deletions
diff --git a/src/components/alarm/AlarmController.cpp b/src/components/alarm/AlarmController.cpp
index 3330ed7..6e93f6d 100644
--- a/src/components/alarm/AlarmController.cpp
+++ b/src/components/alarm/AlarmController.cpp
@@ -27,19 +27,15 @@ 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);
+ alarmAppTimer = xTimerCreate("alarmAppTm", 1, pdFALSE, this, SetOffAlarm);
this->systemTask = systemTask;
}
@@ -52,20 +48,21 @@ void AlarmController::RescheduleAlarm() {
if (state != AlarmState::Set) { return; }
- app_timer_stop(alarmAppTimer);
+ xTimerStop(alarmAppTimer, 0);
auto now = dateTimeController.CurrentDateTime();
alarmTime = now;
auto mSecToAlarm = std::chrono::duration_cast<std::chrono::milliseconds>(alarmTime - now).count();
if (mSecToAlarm < 0) { mSecToAlarm = 0; }
- app_timer_start(alarmAppTimer, APP_TIMER_TICKS(mSecToAlarm), this);
+ xTimerChangePeriod(alarmAppTimer, APP_TIMER_TICKS(mSecToAlarm), 0);
+ xTimerStart(alarmAppTimer, 0);
}
void AlarmController::ScheduleAlarm() {
// Determine the next time the alarm needs to go off and set the app_timer
- app_timer_stop(alarmAppTimer);
+ xTimerStop(alarmAppTimer, 0);
auto now = dateTimeController.CurrentDateTime();
alarmTime = now;
@@ -96,7 +93,8 @@ 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);
+ xTimerChangePeriod(alarmAppTimer, APP_TIMER_TICKS(mSecToAlarm), 0);
+ xTimerStart(alarmAppTimer, 0);
state = AlarmState::Set;
}
@@ -106,7 +104,7 @@ uint32_t AlarmController::SecondsToAlarm() {
}
void AlarmController::DisableAlarm() {
- app_timer_stop(alarmAppTimer);
+ xTimerStop(alarmAppTimer, 0);
state = AlarmState::Not_Set;
}
@@ -117,6 +115,12 @@ void AlarmController::SetOffAlarmNow() {
void AlarmController::StopAlerting() {
systemTask->PushMessage(System::Messages::StopRinging);
+}
+
+void AlarmController::OnStopRinging() {
+ if (state != AlarmState::Alerting) {
+ return;
+ }
// Alarm state is off unless this is a recurring alarm
if (recurrence == RecurType::None) {
diff --git a/src/components/alarm/AlarmController.h b/src/components/alarm/AlarmController.h
index fca4287..df65c63 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"
@@ -37,6 +39,7 @@ namespace Pinetime {
void SetOffAlarmNow();
uint32_t SecondsToAlarm();
void StopAlerting();
+ void OnStopRinging();
enum class AlarmState { Not_Set, Set, Alerting };
enum class RecurType { None, Daily, Weekdays };
uint8_t Hours() const {
@@ -58,6 +61,7 @@ namespace Pinetime {
private:
Controllers::DateTime& dateTimeController;
System::SystemTask* systemTask = nullptr;
+ TimerHandle_t alarmAppTimer;
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 c794a02..5ff549e 100644
--- a/src/components/motor/MotorController.cpp
+++ b/src/components/motor/MotorController.cpp
@@ -12,32 +12,42 @@ 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", APP_TIMER_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, APP_TIMER_TICKS(motorDuration), 0) == pdPASS
+ && xTimerStart(shortVibTimer, 0) == pdPASS) {
+ return;
+ }
+ if (xTimerChangePeriodFromISR(shortVibTimer, APP_TIMER_TICKS(motorDuration), NULL) == pdPASS
+ && xTimerStartFromISR(shortVibTimer, NULL) == pdPASS) {
+ return;
+ }
+ nrf_gpio_pin_set(PinMap::Motor);
}
void MotorController::StartRinging() {
+ if (xTimerChangePeriod(longVibTimer, APP_TIMER_TICKS(1000), 0) == pdPASS
+ && xTimerStart(longVibTimer, 0) == pdPASS) {
+ return;
+ }
Ring(this);
- app_timer_start(longVibTimer, APP_TIMER_TICKS(1000), this);
}
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;
};
}
}
diff --git a/src/components/timer/TimerController.cpp b/src/components/timer/TimerController.cpp
index d6f8ffd..35e3512 100644
--- a/src/components/timer/TimerController.cpp
+++ b/src/components/timer/TimerController.cpp
@@ -4,30 +4,28 @@
#include "components/timer/TimerController.h"
#include "systemtask/SystemTask.h"
-#include "app_timer.h"
#include "task.h"
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)
- controller->OnTimerEnd();
+ void TimerEnd(TimerHandle_t xTimer) {
+ auto controller = static_cast<Pinetime::Controllers::TimerController*>(pvTimerGetTimerID(xTimer));
+ controller->OnTimerEnd();
}
}
void TimerController::Init() {
- app_timer_create(&timerAppTimer, APP_TIMER_MODE_SINGLE_SHOT, TimerEnd);
+ timerAppTimer = xTimerCreate("timerAppTm", 1, pdFALSE, this, TimerEnd);
}
void TimerController::StartTimer(uint32_t duration) {
- app_timer_stop(timerAppTimer);
+ xTimerStop(timerAppTimer,0);
auto currentTicks = xTaskGetTickCount();
- app_timer_start(timerAppTimer, APP_TIMER_TICKS(duration), this);
- endTicks = currentTicks + APP_TIMER_TICKS(duration);
+ TickType_t durationTicks = APP_TIMER_TICKS(duration);
+ xTimerChangePeriod(timerAppTimer, durationTicks, 0);
+ xTimerStart(timerAppTimer, 0);
+ endTicks = currentTicks + durationTicks;
timerRunning = true;
overtime = false;
}
@@ -44,7 +42,7 @@ int32_t TimerController::GetSecondsRemaining() {
}
void TimerController::StopTimer() {
- app_timer_stop(timerAppTimer);
+ xTimerStop(timerAppTimer, 0);
timerRunning = false;
overtime = false;
}
diff --git a/src/components/timer/TimerController.h b/src/components/timer/TimerController.h
index dbc9863..8542987 100644
--- a/src/components/timer/TimerController.h
+++ b/src/components/timer/TimerController.h
@@ -1,5 +1,7 @@
#pragma once
+#include <FreeRTOS.h>
+#include <timers.h>
#include <cstdint>
#include "app_timer.h"
#include "portmacro_cmsis.h"
@@ -32,6 +34,7 @@ namespace Pinetime {
private:
System::SystemTask* systemTask = nullptr;
+ TimerHandle_t timerAppTimer;
TickType_t endTicks;
bool timerRunning = false;
bool overtime = false;