summaryrefslogtreecommitdiff
path: root/src/components/motor/MotorController.cpp
blob: 4c44fc4d7d43108d9ccaef13ce06fbd930b75bd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#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);

  shortVibTimer = xTimerCreate("shortVibTm", 1, pdFALSE, nullptr, StopMotor);
  longVibTimer = xTimerCreate("longVibTm", 1, pdTRUE, this, Ring);
}

void MotorController::Ring(TimerHandle_t xTimer) {
  auto motorController = static_cast<MotorController*>(pvTimerGetTimerID(xTimer));
  xTimerChangePeriod(motorController->longVibTimer, APP_TIMER_TICKS(1000), 0);
  motorController->RunForDuration(50);
}

void MotorController::RunForDuration(uint8_t motorDuration) {
  if (xTimerChangePeriod(shortVibTimer, APP_TIMER_TICKS(motorDuration), 0) == pdPASS && xTimerStart(shortVibTimer, 0) == pdPASS) {
    nrf_gpio_pin_clear(PinMap::Motor);
  }
}

void MotorController::StartRinging() {
  xTimerChangePeriod(longVibTimer, 1, 0);
  xTimerStart(longVibTimer, 0);
}

void MotorController::StopRinging() {
  xTimerStop(longVibTimer, 0);
  nrf_gpio_pin_set(PinMap::Motor);
}

void MotorController::StopMotor(TimerHandle_t xTimer) {
  nrf_gpio_pin_set(PinMap::Motor);
}