|
1 | 1 | #include "components/motor/MotorController.h" |
| 2 | +#include <cstdint> |
2 | 3 | #include <hal/nrf_gpio.h> |
3 | | -#include "systemtask/SystemTask.h" |
| 4 | +#include "components/motion/MotionController.h" |
4 | 5 | #include "drivers/PinMap.h" |
5 | 6 |
|
6 | 7 | using namespace Pinetime::Controllers; |
7 | 8 |
|
| 9 | +MotorController::MotorController(Pinetime::Controllers::MotionController& motionController) : motionController {motionController} { |
| 10 | +} |
| 11 | + |
8 | 12 | void MotorController::Init() { |
9 | 13 | nrf_gpio_cfg_output(PinMap::Motor); |
10 | 14 | nrf_gpio_pin_set(PinMap::Motor); |
11 | 15 |
|
12 | | - shortVib = xTimerCreate("shortVib", 1, pdFALSE, nullptr, StopMotor); |
13 | | - longVib = xTimerCreate("longVib", pdMS_TO_TICKS(1000), pdTRUE, this, Ring); |
| 16 | + nextEvt = xTimerCreate("vib", 1, pdFALSE, this, NextEvent); |
| 17 | +} |
| 18 | + |
| 19 | +void MotorController::NextEvent(TimerHandle_t timer) { |
| 20 | + auto* motorController = static_cast<MotorController*>(pvTimerGetTimerID(timer)); |
| 21 | + |
| 22 | + TickType_t deviation = xTaskGetTickCount() - motorController->nextEvtTime; |
| 23 | + |
| 24 | + TickType_t duration; |
| 25 | + |
| 26 | + switch (motorController->state) { |
| 27 | + case State::Buzz: |
| 28 | + SetMotorRunning(false); |
| 29 | + motorController->state = State::Idle; |
| 30 | + return; |
| 31 | + case State::RingOn: |
| 32 | + SetMotorRunning(false); |
| 33 | + if (motorController->remainingBuzzes == 1) { |
| 34 | + motorController->state = State::Idle; |
| 35 | + return; |
| 36 | + } |
| 37 | + if (motorController->remainingBuzzes > 1) { |
| 38 | + motorController->remainingBuzzes--; |
| 39 | + } |
| 40 | + motorController->state = State::RingOff; |
| 41 | + duration = motorController->ringPeriod - motorController->ringOnTicks; |
| 42 | + break; |
| 43 | + case State::RingOff: |
| 44 | + SetMotorRunning(true); |
| 45 | + motorController->state = State::RingOn; |
| 46 | + duration = motorController->ringOnTicks; |
| 47 | + break; |
| 48 | + case State::Idle: |
| 49 | + // should never hit the idle state in the timer callback |
| 50 | + assert(false); |
| 51 | + __builtin_trap(); |
| 52 | + } |
| 53 | + motorController->nextEvtTime += duration; |
| 54 | + if (duration > deviation) { |
| 55 | + xTimerChangePeriod(timer, duration - deviation, 0); |
| 56 | + } else { |
| 57 | + xTimerChangePeriod(timer, 1, 0); |
| 58 | + } |
14 | 59 | } |
15 | 60 |
|
16 | | -void MotorController::Ring(TimerHandle_t xTimer) { |
17 | | - auto* motorController = static_cast<MotorController*>(pvTimerGetTimerID(xTimer)); |
18 | | - motorController->RunForDuration(50); |
| 61 | +uint16_t MotorController::CalculateLength(Intensity intensity) { |
| 62 | + // don't think shake speed really has units |
| 63 | + // 200 seems sensible, then clamp the multiplier to range 1-3 |
| 64 | + // a fancy curve would probably feel better, but this seems to work well |
| 65 | + float durationMultiplier = motionController.CurrentShakeSpeed() / 200.f; |
| 66 | + durationMultiplier = std::max(std::min(durationMultiplier, 3.f), 1.f); |
| 67 | + |
| 68 | + float length; |
| 69 | + |
| 70 | + switch (intensity) { |
| 71 | + case Intensity::Light: |
| 72 | + length = 30.f; |
| 73 | + break; |
| 74 | + case Intensity::Medium: |
| 75 | + length = 50.f; |
| 76 | + break; |
| 77 | + case Intensity::Strong: |
| 78 | + length = 90.f; |
| 79 | + break; |
| 80 | + } |
| 81 | + return length * durationMultiplier; |
19 | 82 | } |
20 | 83 |
|
21 | | -void MotorController::RunForDuration(uint8_t motorDuration) { |
22 | | - if (motorDuration > 0 && xTimerChangePeriod(shortVib, pdMS_TO_TICKS(motorDuration), 0) == pdPASS && xTimerStart(shortVib, 0) == pdPASS) { |
| 84 | +void MotorController::RunEvent(void* motorControllerPtr, uint32_t requestedStateInt) { |
| 85 | + auto* motorController = static_cast<MotorController*>(motorControllerPtr); |
| 86 | + auto requestedState = static_cast<RequestEvt>(requestedStateInt); |
| 87 | + |
| 88 | + // these aren't always set, but if they're not set they're never used |
| 89 | + // so it's safe to copy unconditionally |
| 90 | + motorController->ringOnTicks = motorController->nextOperation.ringOnTicks; |
| 91 | + motorController->ringPeriod = motorController->nextOperation.ringPeriod; |
| 92 | + motorController->remainingBuzzes = motorController->nextOperation.buzzCount; |
| 93 | + |
| 94 | + if (requestedState == RequestEvt::RequestRingOff) { |
| 95 | + SetMotorRunning(false); |
| 96 | + motorController->state = State::Idle; |
| 97 | + xTimerStop(motorController->nextEvt, 0); |
| 98 | + } else { |
| 99 | + SetMotorRunning(true); |
| 100 | + motorController->nextEvtTime = xTaskGetTickCount() + motorController->ringOnTicks; |
| 101 | + if (requestedState == RequestEvt::RequestBuzz) { |
| 102 | + motorController->state = State::Buzz; |
| 103 | + } else { |
| 104 | + motorController->state = State::RingOn; |
| 105 | + } |
| 106 | + // changing the period starts the timer |
| 107 | + xTimerChangePeriod(motorController->nextEvt, motorController->ringOnTicks, 0); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +void MotorController::PushEvent(RequestEvt state) { |
| 112 | + // since the timer callback modifies its own state, |
| 113 | + // we need to run timer state changes in the timer task to avoid races |
| 114 | + xTimerPendFunctionCall(RunEvent, this, static_cast<uint32_t>(state), portMAX_DELAY); |
| 115 | +} |
| 116 | + |
| 117 | +void MotorController::Buzz(Intensity intensity) { |
| 118 | + nextOperation.ringOnTicks = CalculateLength(intensity); |
| 119 | + |
| 120 | + PushEvent(RequestEvt::RequestBuzz); |
| 121 | +} |
| 122 | + |
| 123 | +void MotorController::SetMotorRunning(bool active) { |
| 124 | + if (active) { |
23 | 125 | nrf_gpio_pin_clear(PinMap::Motor); |
| 126 | + } else { |
| 127 | + nrf_gpio_pin_set(PinMap::Motor); |
24 | 128 | } |
25 | 129 | } |
26 | 130 |
|
27 | | -void MotorController::StartRinging() { |
28 | | - RunForDuration(50); |
29 | | - xTimerStart(longVib, 0); |
| 131 | +// numBuzzes = 0 rings forever |
| 132 | +void MotorController::Ring(Intensity intensity, TickType_t ringPeriod, uint16_t numBuzzes) { |
| 133 | + nextOperation.ringOnTicks = CalculateLength(intensity); |
| 134 | + nextOperation.ringPeriod = ringPeriod; |
| 135 | + nextOperation.buzzCount = numBuzzes; |
| 136 | + |
| 137 | + PushEvent(RequestEvt::RequestRingOn); |
30 | 138 | } |
31 | 139 |
|
32 | 140 | void MotorController::StopRinging() { |
33 | | - xTimerStop(longVib, 0); |
34 | | - nrf_gpio_pin_set(PinMap::Motor); |
| 141 | + PushEvent(RequestEvt::RequestRingOff); |
35 | 142 | } |
36 | 143 |
|
37 | 144 | bool MotorController::IsRinging() { |
38 | | - return (xTimerIsTimerActive(longVib) == pdTRUE); |
39 | | -} |
40 | | - |
41 | | -void MotorController::StopMotor(TimerHandle_t /*xTimer*/) { |
42 | | - nrf_gpio_pin_set(PinMap::Motor); |
| 145 | + return state == State::RingOn || state == State::RingOff; |
43 | 146 | } |
0 commit comments