Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 51 additions & 6 deletions src/components/motor/MotorController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ APP_TIMER_DEF(longVibTimer);

using namespace Pinetime::Controllers;

constexpr MotorController::Tune MotorController::tunes[];


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

Expand All @@ -16,24 +19,54 @@ void MotorController::Init() {
nrf_gpio_pin_set(pinMotor);
app_timer_init();

app_timer_create(&shortVibTimer, APP_TIMER_MODE_SINGLE_SHOT, StopMotor);
app_timer_create(&shortVibTimer, APP_TIMER_MODE_SINGLE_SHOT, Vibrate);
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);
motorController->VibrateTune(TuneType::RING);
}

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


void MotorController::StopTune() {
step = 255;
}

void MotorController::ScheduleTune(TuneType tune) {
step = 0;
runningTune = tune;
}

/**
* schedule next vibrate timer tick with or without vibration
*/

void MotorController::ScheduleVibrateTimer(uint8_t motorDuration, bool vibrate) {
if (vibrate) {
nrf_gpio_pin_clear(pinMotor);
} else {
nrf_gpio_pin_set(pinMotor);
}
app_timer_start(shortVibTimer, APP_TIMER_TICKS(motorDuration), this);
}

nrf_gpio_pin_clear(pinMotor);
app_timer_start(shortVibTimer, APP_TIMER_TICKS(motorDuration), nullptr);
void MotorController::VibrateTune(TuneType tune) {
if (settingsController.GetVibrationStatus() == Controllers::Settings::Vibration::OFF)
return;
ScheduleTune(tune);
Vibrate(this);
}


void MotorController::StartRinging() {
if (settingsController.GetVibrationStatus() == Controllers::Settings::Vibration::OFF) {
return;
Expand All @@ -47,6 +80,18 @@ void MotorController::StopRinging() {
nrf_gpio_pin_set(pinMotor);
}

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

void MotorController::Vibrate(void* p_context) {
auto* motorC = static_cast<MotorController*>(p_context);
auto* runningTune = &tunes[motorC->runningTune];

nrf_gpio_pin_set(pinMotor); //turn off vibration

//scedule next tune tick
if (motorC->step < 8 && motorC->step < runningTune->length) {
bool vibrate = ((1 << motorC->step) & runningTune->tune) > 0;
motorC->step++;

motorC->ScheduleVibrateTimer(runningTune->tempo, vibrate);
}
}
33 changes: 30 additions & 3 deletions src/components/motor/MotorController.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,46 @@
namespace Pinetime {
namespace Controllers {
static constexpr uint8_t pinMotor = 16;



class MotorController {


public:
enum TuneType : uint8_t {
NOTIFICATION,
SHORT,
RING
};

MotorController(Controllers::Settings& settingsController);
void Init();
void RunForDuration(uint8_t motorDuration);
void StartRinging();
static void StopRinging();
void VibrateTune(TuneType tune);

private:
static void Ring(void* p_context);
struct Tune {
uint8_t tune;
uint8_t length;
uint8_t tempo;
};
Controllers::Settings& settingsController;
static void StopMotor(void* p_context);
TuneType runningTune = TuneType::SHORT;
uint8_t step = 255;

static constexpr Tune tunes[] = {
[TuneType::NOTIFICATION] = {.tune = 0b00101001, .length = 6, .tempo = 50},
[TuneType::SHORT] = {.tune = 0b00000001, .length = 2, .tempo = 35},
[TuneType::RING] = {.tune = 0b00001111, .length = 8, .tempo = 50},
};

static void Vibrate(void* p_context);
static void Ring(void* p_context);
void ScheduleVibrateTimer(uint8_t motorDuration, bool vibrate);
void StopTune();
void ScheduleTune(TuneType tune);
};
}
}
2 changes: 1 addition & 1 deletion src/displayapp/screens/Notifications.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Notifications::Notifications(DisplayApp* app,
if (notification.category == Controllers::NotificationManager::Categories::IncomingCall) {
motorController.StartRinging();
} else {
motorController.RunForDuration(35);
motorController.VibrateTune(Controllers::MotorController::TuneType::NOTIFICATION);
timeoutLine = lv_line_create(lv_scr_act(), nullptr);

lv_obj_set_style_local_line_width(timeoutLine, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, 3);
Expand Down
2 changes: 1 addition & 1 deletion src/displayapp/screens/settings/QuickSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void QuickSettings::OnButtonEvent(lv_obj_t* object, lv_event_t event) {

if (lv_obj_get_state(btn3, LV_BTN_PART_MAIN) & LV_STATE_CHECKED) {
settingsController.SetVibrationStatus(Controllers::Settings::Vibration::ON);
motorController.RunForDuration(35);
motorController.VibrateTune(Controllers::MotorController::TuneType::NOTIFICATION);
lv_label_set_text_static(btn3_lvl, Symbols::notificationsOn);
} else {
settingsController.SetVibrationStatus(Controllers::Settings::Vibration::OFF);
Expand Down
4 changes: 2 additions & 2 deletions src/systemtask/SystemTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ void SystemTask::Work() {
if (isSleeping && !isWakingUp) {
GoToRunning();
}
motorController.RunForDuration(35);
motorController.VibrateTune(Controllers::MotorController::TuneType::SHORT);
displayApp.PushMessage(Pinetime::Applications::Display::Messages::TimerDone);
break;
case Messages::BleConnected:
Expand Down Expand Up @@ -325,7 +325,7 @@ void SystemTask::Work() {
stepCounterMustBeReset = true;
break;
case Messages::OnChargingEvent:
motorController.RunForDuration(15);
motorController.VibrateTune(motorController.TuneType::SHORT);
// Battery level is updated on every message - there's no need to do anything
break;

Expand Down