summaryrefslogtreecommitdiff
path: root/src/components/motor/MotorController.cpp
blob: 5ade19e44a892dcc7775fd358a733f2e564332b2 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "MotorController.h"
#include <hal/nrf_gpio.h>
#include "systemtask/SystemTask.h"
#include "app_timer.h"

APP_TIMER_DEF(shortVibTimer);
APP_TIMER_DEF(longVibTimer);

using namespace Pinetime::Controllers;

MotorController::MotorController(Controllers::Settings& settingsController) : settingsController {settingsController} {
}

void MotorController::Init() {
  nrf_gpio_cfg_output(pinMotor);
  nrf_gpio_pin_set(pinMotor);
  app_timer_init();

  app_timer_create(&shortVibTimer, APP_TIMER_MODE_SINGLE_SHOT, StopMotor);
  app_timer_create(&longVibTimer, APP_TIMER_MODE_REPEATED, Ring);
}

void MotorController::Ring(void* p_context) {
  auto* motorController = static_cast<MotorController*>(p_context);
  motorController->RunForDuration(50);
}

void MotorController::RunForDuration(uint8_t motorDuration) {
  if (settingsController.GetVibrationStatus() == Controllers::Settings::Vibration::OFF) {
    return;
  }

  nrf_gpio_pin_clear(pinMotor);
  app_timer_start(shortVibTimer, APP_TIMER_TICKS(motorDuration), nullptr);
}

void MotorController::StartRinging() {
  if (settingsController.GetVibrationStatus() == Controllers::Settings::Vibration::OFF) {
    return;
  }
  Ring(this);
  app_timer_start(longVibTimer, APP_TIMER_TICKS(1000), this);
}

// This function is the same as StartRinging(), but will ring even if notifications are turned off in Settings
void MotorController::StartRingingDisregardSettings() {
  Ring(this);
  app_timer_start(longVibTimer, APP_TIMER_TICKS(1000), this);
}

void MotorController::StopRinging() {
  app_timer_stop(longVibTimer);
  nrf_gpio_pin_set(pinMotor);
}

void MotorController::StopMotor(void* p_context) {
  nrf_gpio_pin_set(pinMotor);
}