2021-01-15 22:11:53 -05:00
|
|
|
#include "MotorController.h"
|
|
|
|
#include <hal/nrf_gpio.h>
|
|
|
|
#include "systemtask/SystemTask.h"
|
|
|
|
#include "app_timer.h"
|
|
|
|
|
2021-05-12 20:23:04 +02:00
|
|
|
APP_TIMER_DEF(shortVibTimer);
|
|
|
|
APP_TIMER_DEF(longVibTimer);
|
2021-01-15 22:11:53 -05:00
|
|
|
|
|
|
|
using namespace Pinetime::Controllers;
|
|
|
|
|
2021-04-18 20:28:14 +03:00
|
|
|
MotorController::MotorController(Controllers::Settings& settingsController) : settingsController {settingsController} {
|
|
|
|
}
|
2021-04-04 03:08:51 +01:00
|
|
|
|
2021-01-15 22:11:53 -05:00
|
|
|
void MotorController::Init() {
|
2021-04-18 20:28:14 +03:00
|
|
|
nrf_gpio_cfg_output(pinMotor);
|
|
|
|
nrf_gpio_pin_set(pinMotor);
|
|
|
|
app_timer_init();
|
2021-08-01 11:47:26 +03:00
|
|
|
|
2021-08-01 13:05:48 +03:00
|
|
|
app_timer_create(&shortVibTimer, APP_TIMER_MODE_SINGLE_SHOT, StopMotor);
|
|
|
|
app_timer_create(&longVibTimer, APP_TIMER_MODE_REPEATED, Ring);
|
2021-01-15 22:11:53 -05:00
|
|
|
}
|
|
|
|
|
2021-08-01 13:05:48 +03:00
|
|
|
void MotorController::Ring(void* p_context) {
|
|
|
|
auto* motorController = static_cast<MotorController*>(p_context);
|
|
|
|
motorController->RunForDuration(50);
|
|
|
|
}
|
2021-04-04 03:08:51 +01:00
|
|
|
|
2021-08-01 13:05:48 +03:00
|
|
|
void MotorController::RunForDuration(uint8_t motorDuration) {
|
|
|
|
if (settingsController.GetVibrationStatus() == Controllers::Settings::Vibration::OFF) {
|
2021-04-18 20:28:14 +03:00
|
|
|
return;
|
2021-08-01 13:05:48 +03:00
|
|
|
}
|
2021-04-18 20:28:14 +03:00
|
|
|
|
|
|
|
nrf_gpio_pin_clear(pinMotor);
|
2021-08-01 13:05:48 +03:00
|
|
|
app_timer_start(shortVibTimer, APP_TIMER_TICKS(motorDuration), nullptr);
|
2021-01-25 12:44:58 -05:00
|
|
|
}
|
2021-02-05 17:06:56 +01:00
|
|
|
|
2021-08-01 13:05:48 +03:00
|
|
|
void MotorController::StartRinging() {
|
|
|
|
if (settingsController.GetVibrationStatus() == Controllers::Settings::Vibration::OFF) {
|
2021-05-12 20:23:04 +02:00
|
|
|
return;
|
2021-08-01 13:05:48 +03:00
|
|
|
}
|
|
|
|
Ring(this);
|
|
|
|
app_timer_start(longVibTimer, APP_TIMER_TICKS(1000), this);
|
2021-05-12 20:23:04 +02:00
|
|
|
}
|
|
|
|
|
2021-08-01 13:05:48 +03:00
|
|
|
void MotorController::StopRinging() {
|
2021-05-12 20:23:04 +02:00
|
|
|
app_timer_stop(longVibTimer);
|
2021-04-18 20:28:14 +03:00
|
|
|
nrf_gpio_pin_set(pinMotor);
|
2021-05-12 20:23:04 +02:00
|
|
|
}
|
|
|
|
|
2021-08-01 13:05:48 +03:00
|
|
|
void MotorController::StopMotor(void* p_context) {
|
|
|
|
nrf_gpio_pin_set(pinMotor);
|
2021-08-01 11:47:26 +03:00
|
|
|
}
|