2021-01-16 03:11:53 +00:00
|
|
|
#include "MotorController.h"
|
|
|
|
#include <hal/nrf_gpio.h>
|
|
|
|
#include "systemtask/SystemTask.h"
|
|
|
|
#include "app_timer.h"
|
|
|
|
|
2021-05-12 18:23:04 +00:00
|
|
|
APP_TIMER_DEF(shortVibTimer);
|
|
|
|
APP_TIMER_DEF(longVibTimer);
|
2021-01-16 03:11:53 +00:00
|
|
|
|
|
|
|
using namespace Pinetime::Controllers;
|
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
MotorController::MotorController(Controllers::Settings& settingsController) : settingsController {settingsController} {
|
|
|
|
}
|
2021-04-04 02:08:51 +00:00
|
|
|
|
2021-01-16 03:11:53 +00:00
|
|
|
void MotorController::Init() {
|
2021-04-18 17:28:14 +00:00
|
|
|
nrf_gpio_cfg_output(pinMotor);
|
|
|
|
nrf_gpio_pin_set(pinMotor);
|
|
|
|
app_timer_init();
|
2021-05-12 18:23:04 +00:00
|
|
|
|
|
|
|
app_timer_create(&shortVibTimer, APP_TIMER_MODE_SINGLE_SHOT, vibrate);
|
|
|
|
app_timer_create(&longVibTimer, APP_TIMER_MODE_REPEATED, vibrate);
|
|
|
|
isBusy = false;
|
2021-01-16 03:11:53 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 18:23:04 +00:00
|
|
|
void MotorController::RunForDuration(uint8_t motorDuration) {
|
2021-04-04 02:08:51 +00:00
|
|
|
|
2021-05-12 18:23:04 +00:00
|
|
|
if (settingsController.GetVibrationStatus() == Controllers::Settings::Vibration::OFF || isBusy)
|
2021-04-18 17:28:14 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
nrf_gpio_pin_clear(pinMotor);
|
|
|
|
/* Start timer for motorDuration miliseconds and timer triggers vibrate() when it finishes*/
|
2021-05-12 18:23:04 +00:00
|
|
|
app_timer_start(shortVibTimer, APP_TIMER_TICKS(motorDuration), NULL);
|
2021-01-25 17:44:58 +00:00
|
|
|
}
|
2021-02-05 16:06:56 +00:00
|
|
|
|
2021-05-12 18:23:04 +00:00
|
|
|
void MotorController::startRunning(uint8_t motorDuration) {
|
|
|
|
if (settingsController.GetVibrationStatus() == Controllers::Settings::Vibration::OFF || isBusy )
|
|
|
|
return;
|
|
|
|
//prevent other vibrations while running
|
|
|
|
isBusy = true;
|
|
|
|
nrf_gpio_pin_clear(pinMotor);
|
|
|
|
app_timer_start(longVibTimer, APP_TIMER_TICKS(motorDuration), NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MotorController::stopRunning() {
|
|
|
|
|
|
|
|
app_timer_stop(longVibTimer);
|
2021-04-18 17:28:14 +00:00
|
|
|
nrf_gpio_pin_set(pinMotor);
|
2021-05-12 18:23:04 +00:00
|
|
|
isBusy = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MotorController::vibrate(void* p_context) {
|
|
|
|
if (nrf_gpio_pin_out_read(pinMotor) == 0) {
|
|
|
|
nrf_gpio_pin_set(pinMotor);
|
|
|
|
} else {
|
|
|
|
nrf_gpio_pin_clear(pinMotor);
|
|
|
|
}
|
2021-02-05 16:06:56 +00:00
|
|
|
}
|