InfiniTime/src/components/motor/MotorController.cpp

44 lines
1.1 KiB
C++
Raw Normal View History

#include "components/motor/MotorController.h"
2021-01-15 22:11:53 -05:00
#include <hal/nrf_gpio.h>
#include "systemtask/SystemTask.h"
#include "app_timer.h"
2021-08-03 14:32:23 -04:00
#include "drivers/PinMap.h"
2021-01-15 22:11:53 -05:00
APP_TIMER_DEF(shortVibTimer);
APP_TIMER_DEF(longVibTimer);
2021-01-15 22:11:53 -05:00
using namespace Pinetime::Controllers;
void MotorController::Init() {
2021-08-03 14:32:23 -04:00
nrf_gpio_cfg_output(PinMap::Motor);
nrf_gpio_pin_set(PinMap::Motor);
app_timer_init();
2021-08-01 04:47:26 -04:00
2021-08-01 06:05:48 -04: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 06:05:48 -04:00
void MotorController::Ring(void* p_context) {
auto* motorController = static_cast<MotorController*>(p_context);
motorController->RunForDuration(50);
}
2021-08-01 06:05:48 -04:00
void MotorController::RunForDuration(uint8_t motorDuration) {
2021-08-03 14:32:23 -04:00
nrf_gpio_pin_clear(PinMap::Motor);
2021-08-01 06:05:48 -04:00
app_timer_start(shortVibTimer, APP_TIMER_TICKS(motorDuration), nullptr);
2021-01-25 12:44:58 -05:00
}
2021-02-05 11:06:56 -05:00
2021-08-01 06:05:48 -04:00
void MotorController::StartRinging() {
Ring(this);
app_timer_start(longVibTimer, APP_TIMER_TICKS(1000), this);
}
2021-08-01 06:05:48 -04:00
void MotorController::StopRinging() {
app_timer_stop(longVibTimer);
2021-08-03 14:32:23 -04:00
nrf_gpio_pin_set(PinMap::Motor);
}
2021-08-01 06:05:48 -04:00
void MotorController::StopMotor(void* p_context) {
2021-08-22 16:11:57 -04:00
nrf_gpio_pin_set(PinMap::Motor);
2021-08-01 04:47:26 -04:00
}