Skip to content

Commit

Permalink
Added natural wake alarm to motor controller.
Browse files Browse the repository at this point in the history
removed wakelock when using natural alrm mode as user may not wake up soon and unintended taps may happen.
  • Loading branch information
cyberneel committed Dec 6, 2024
1 parent 8d2e5f5 commit 073599e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 75 deletions.
20 changes: 20 additions & 0 deletions src/components/motor/MotorController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ void MotorController::Init() {
shortVib = xTimerCreate("shortVib", 1, pdFALSE, nullptr, StopMotor);
longVib = xTimerCreate("longVib", pdMS_TO_TICKS(1000), pdTRUE, this, Ring);
wakeAlarmVib = xTimerCreate("wakeAlarmVib", pdMS_TO_TICKS(1000), pdTRUE, this, WakeAlarmRing);
naturalWakeAlarmVib = xTimerCreate("natWakeVib", pdMS_TO_TICKS(30 * 1000), pdTRUE, this, NaturalWakeAlarmRing);
}

void MotorController::SetMotorStrength(uint8_t strength) {
Expand Down Expand Up @@ -106,6 +107,25 @@ void MotorController::StopWakeAlarm() {
nrf_gpio_pin_set(PinMap::Motor);
}

void MotorController::StartNaturalWakeAlarm() {
SetMotorStrength((60 * infiniSleepMotorStrength) / 100);
RunForDuration(280);
xTimerStart(naturalWakeAlarmVib, 0);
}

void MotorController::NaturalWakeAlarmRing(TimerHandle_t xTimer) {
auto* motorController = static_cast<MotorController*>(pvTimerGetTimerID(xTimer));
motorController->SetMotorStrength((60 * motorController->infiniSleepMotorStrength) / 100);
motorController->RunForDuration(280);
}

void MotorController::StopNaturalWakeAlarm() {
xTimerStop(naturalWakeAlarmVib, 0);
nrf_pwm_task_trigger(NRF_PWM2, NRF_PWM_TASK_STOP); // Stop the PWM sequence
pwmValue = 0; // Reset the PWM value
nrf_gpio_pin_set(PinMap::Motor);
}

void MotorController::StopMotor(TimerHandle_t /*xTimer*/) {
nrf_pwm_task_trigger(NRF_PWM2, NRF_PWM_TASK_STOP); // Stop the PWM sequence
pwmValue = 0; // Reset the PWM value
Expand Down
4 changes: 4 additions & 0 deletions src/components/motor/MotorController.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace Pinetime {
void StopRinging();
void StartWakeAlarm();
void StopWakeAlarm();
void StartNaturalWakeAlarm();
void StopNaturalWakeAlarm();
void GradualWakeBuzz();
void StopGradualWakeBuzz();
void SetMotorStrength(uint8_t strength);
Expand All @@ -28,12 +30,14 @@ namespace Pinetime {
private:
static void Ring(TimerHandle_t xTimer);
static void WakeAlarmRing(TimerHandle_t xTimer);
static void NaturalWakeAlarmRing(TimerHandle_t xTimer);
static void StopMotor(TimerHandle_t xTimer);

TimerHandle_t shortVib;
TimerHandle_t longVib;

TimerHandle_t wakeAlarmVib;
TimerHandle_t naturalWakeAlarmVib;
};
}
}
83 changes: 10 additions & 73 deletions src/displayapp/screens/Sleep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,6 @@ namespace {
}
}

extern InfiniSleepController infiniSleepController;

static void settingsToggleEventHandler(lv_obj_t* obj, lv_event_t e) {
if (e != LV_EVENT_VALUE_CHANGED) {
return;
}

const char* setting_name = static_cast<const char*>(obj->user_data);
bool enabled = lv_checkbox_is_checked(obj);

if (strcmp(setting_name, "Body Tracking") == 0) {
infiniSleepController.SetBodyTrackingEnabled(enabled);
infiniSleepController.SetSettingsChanged();
} else if (strcmp(setting_name, "Heart Rate\nTracking") == 0) {
infiniSleepController.SetHeartRateTrackingEnabled(enabled);
infiniSleepController.SetSettingsChanged();
} else if (strcmp(setting_name, "Gradual Wake") == 0) {
infiniSleepController.SetGradualWakeEnabled(enabled);
infiniSleepController.SetSettingsChanged();
} else if (strcmp(setting_name, "Smart Alarm\n(alpha)") == 0) {
infiniSleepController.SetSmartAlarmEnabled(enabled);
infiniSleepController.SetSettingsChanged();
}
}

static void btnEventHandler(lv_obj_t* obj, lv_event_t event) {
auto* screen = static_cast<Sleep*>(obj->user_data);
screen->OnButtonEvent(obj, event);
Expand Down Expand Up @@ -517,10 +492,6 @@ void Sleep::OnButtonEvent(lv_obj_t* obj, lv_event_t event) {
StopAlarmPush();
return;
}
if (obj == btnMessage) {
HideAlarmInfo();
return;
}
if (obj == enableSwitch) {
if (lv_switch_get_state(enableSwitch)) {
infiniSleepController.ScheduleWakeAlarm();
Expand Down Expand Up @@ -561,10 +532,6 @@ void Sleep::OnButtonEvent(lv_obj_t* obj, lv_event_t event) {
}

bool Sleep::OnButtonPushed() {
if (txtMessage != nullptr && btnMessage != nullptr) {
HideAlarmInfo();
return true;
}
if (infiniSleepController.IsAlerting()) {
if (StopAlarmPush()) {
return true;
Expand Down Expand Up @@ -692,8 +659,12 @@ void Sleep::SetAlerting() {
taskSnoozeWakeAlarm = lv_task_create(SnoozeAlarmTaskCallback, 120 * 1000, LV_TASK_PRIO_MID, this);
if (infiniSleepController.infiniSleepSettings.graddualWake) {
motorController.StartWakeAlarm();
} else if (infiniSleepController.infiniSleepSettings.naturalWake) {
motorController.StartNaturalWakeAlarm();
}
if (infiniSleepController.infiniSleepSettings.naturalWake != true) {
wakeLock.Lock();
}
wakeLock.Lock();
alreadyAlerting = true;
}

Expand All @@ -704,13 +675,17 @@ void Sleep::RedrawSetAlerting() {
lv_obj_set_hidden(btnSuggestedAlarm, true);
lv_obj_set_hidden(txtSuggestedAlarm, true);
lv_obj_set_hidden(iconSuggestedAlarm, true);
wakeLock.Lock();
if (infiniSleepController.infiniSleepSettings.naturalWake != true) {
wakeLock.Lock();
}
}

void Sleep::StopAlerting(bool setSwitch) {
infiniSleepController.StopAlerting();
if (infiniSleepController.infiniSleepSettings.graddualWake) {
motorController.StopWakeAlarm();
} else if (infiniSleepController.infiniSleepSettings.naturalWake) {
motorController.StopNaturalWakeAlarm();
}
if (setSwitch) {
SetSwitchState(LV_ANIM_OFF);
Expand All @@ -731,42 +706,4 @@ void Sleep::SetSwitchState(lv_anim_enable_t anim) {
} else {
lv_switch_off(enableSwitch, anim);
}
}

void Sleep::ShowAlarmInfo() {
if (btnMessage != nullptr) {
return;
}
btnMessage = lv_btn_create(lv_scr_act(), nullptr);
btnMessage->user_data = this;
lv_obj_set_event_cb(btnMessage, btnEventHandler);
lv_obj_set_height(btnMessage, 200);
lv_obj_set_width(btnMessage, 150);
lv_obj_align(btnMessage, lv_scr_act(), LV_ALIGN_CENTER, 0, 0);
txtMessage = lv_label_create(btnMessage, nullptr);
lv_obj_set_style_local_bg_color(btnMessage, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_NAVY);

if (infiniSleepController.GetWakeAlarm().isEnabled) {
auto timeToAlarm = infiniSleepController.SecondsToWakeAlarm();

auto daysToAlarm = timeToAlarm / 86400;
auto hrsToAlarm = (timeToAlarm % 86400) / 3600;
auto minToAlarm = (timeToAlarm % 3600) / 60;
auto secToAlarm = timeToAlarm % 60;

lv_label_set_text_fmt(txtMessage,
"Time to\nalarm:\n%2lu Days\n%2lu Hours\n%2lu Minutes\n%2lu Seconds",
daysToAlarm,
hrsToAlarm,
minToAlarm,
secToAlarm);
} else {
lv_label_set_text_static(txtMessage, "Alarm\nis not\nset.");
}
}

void Sleep::HideAlarmInfo() {
lv_obj_del(btnMessage);
txtMessage = nullptr;
btnMessage = nullptr;
}
2 changes: 0 additions & 2 deletions src/displayapp/screens/Sleep.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ namespace Pinetime {
void DisableWakeAlarm();
void SetSwitchState(lv_anim_enable_t anim);
void SetWakeAlarm();
void ShowAlarmInfo();
void HideAlarmInfo();
void UpdateWakeAlarmTime();
Widgets::Counter hourCounter = Widgets::Counter(0, 23, jetbrains_mono_76);
Widgets::Counter minuteCounter = Widgets::Counter(0, 59, jetbrains_mono_76);
Expand Down

0 comments on commit 073599e

Please sign in to comment.