Skip to content

Commit

Permalink
Added Setting to decide motor strength
Browse files Browse the repository at this point in the history
removed some unsed code in motorcontroller

made snoozing take user to info page

fixed logic for skipping gradual wake while in snooze
  • Loading branch information
cyberneel committed Dec 3, 2024
1 parent 0608e0f commit 2fc156b
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/components/infinisleep/InfiniSleepController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void InfiniSleepController::ScheduleWakeAlarm() {
}

// Calculate the period for the gradualWakeTimer
if (isSnoozing != true && infiniSleepSettings.graddualWake && gradualWakeStep > 0) {
if (infiniSleepSettings.graddualWake && gradualWakeStep > 0) {
int64_t gradualWakePeriod = ((secondsToWakeAlarm - gradualWakeSteps[-1 + gradualWakeStep])) * (configTICK_RATE_HZ);
xTimerChangePeriod(gradualWakeTimer, gradualWakePeriod, 0);
xTimerStart(gradualWakeTimer, 0);
Expand Down
1 change: 1 addition & 0 deletions src/components/infinisleep/InfiniSleepController.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ namespace Pinetime {
bool smartAlarm = false;
uint8_t sleepCycleDuration = SLEEP_CYCLE_DURATION;
uint8_t desiredCycles = DESIRED_CYCLES;
uint8_t motorStrength = 100;
};

InfiniSleepSettings infiniSleepSettings;
Expand Down
46 changes: 4 additions & 42 deletions src/components/motor/MotorController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ void MotorController::Init() {
// Initialize timers for motor actions
shortVib = xTimerCreate("shortVib", 1, pdFALSE, nullptr, StopMotor);
longVib = xTimerCreate("longVib", pdMS_TO_TICKS(1000), pdTRUE, this, Ring);
alarmVib = xTimerCreate("alarmVib", pdMS_TO_TICKS(500), pdTRUE, this, AlarmRing);
wakeAlarmVib = xTimerCreate("wakeAlarmVib", pdMS_TO_TICKS(1000), pdTRUE, this, WakeAlarmRing);
}

Expand All @@ -57,12 +56,6 @@ void MotorController::Ring(TimerHandle_t xTimer) {
motorController->RunForDuration(50);
}

void MotorController::AlarmRing(TimerHandle_t xTimer) {
auto* motorController = static_cast<MotorController*>(pvTimerGetTimerID(xTimer));
motorController->SetMotorStrength(80);
motorController->RunForDuration(300);
}

void MotorController::RunForDuration(uint16_t motorDuration) {
if (motorDuration > 0 && xTimerChangePeriod(shortVib, pdMS_TO_TICKS(motorDuration), 0) == pdPASS && xTimerStart(shortVib, 0) == pdPASS) {
if (pwmValue == 0) {
Expand All @@ -86,21 +79,8 @@ void MotorController::StopRinging() {
nrf_gpio_pin_set(PinMap::Motor);
}

void MotorController::StartAlarm() {
SetMotorStrength(80);
RunForDuration(300);
xTimerStart(alarmVib, 0);
}

void MotorController::StopAlarm() {
xTimerStop(alarmVib, 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::StartWakeAlarm() {
wakeAlarmStrength = 80;
wakeAlarmStrength = (80 * infiniSleepMotorStrength) / 100;
wakeAlarmDuration = 100;
SetMotorStrength(wakeAlarmStrength);
RunForDuration(wakeAlarmDuration);
Expand All @@ -109,8 +89,8 @@ void MotorController::StartWakeAlarm() {

void MotorController::WakeAlarmRing(TimerHandle_t xTimer) {
auto* motorController = static_cast<MotorController*>(pvTimerGetTimerID(xTimer));
if (motorController->wakeAlarmStrength > 40) {
motorController->wakeAlarmStrength -= 1;
if (motorController->wakeAlarmStrength > (40 * motorController->infiniSleepMotorStrength) / 100) {
motorController->wakeAlarmStrength -= (1 * motorController->infiniSleepMotorStrength) / 100;
}
if (motorController->wakeAlarmDuration < 500) {
motorController->wakeAlarmDuration += 6;
Expand All @@ -133,24 +113,6 @@ void MotorController::StopMotor(TimerHandle_t /*xTimer*/) {
}

void MotorController::GradualWakeBuzz() {
SetMotorStrength(60);
SetMotorStrength((60 * infiniSleepMotorStrength) / 100);
RunForDuration(100);
// xTimerStart(gradualWakeBuzzDelay, 0);
// xTimerStart(gradualWakeBuzzEnd, 0);
}

void MotorController::GradualWakeBuzzRing(TimerHandle_t xTimer) {
auto* motorController = static_cast<MotorController*>(pvTimerGetTimerID(xTimer));
motorController->RunForDuration(12);
}

void MotorController::StopGradualWakeBuzzCallback(TimerHandle_t xTimer) {
auto* motorController = static_cast<MotorController*>(pvTimerGetTimerID(xTimer));
motorController->StopGradualWakeBuzz();
}

void MotorController::StopGradualWakeBuzz() {
// xTimerStop(gradualWakeBuzzDelay, 0);
xTimerStop(gradualWakeBuzzEnd, 0);
// StopMotor(nullptr);
}
9 changes: 1 addition & 8 deletions src/components/motor/MotorController.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ namespace Pinetime {
void RunForDuration(uint16_t motorDuration);
void StartRinging();
void StopRinging();
void StartAlarm();
void StopAlarm();
void StartWakeAlarm();
void StopWakeAlarm();
void GradualWakeBuzz();
Expand All @@ -25,20 +23,15 @@ namespace Pinetime {

uint8_t wakeAlarmStrength = 80;
uint16_t wakeAlarmDuration = 100;
uint8_t infiniSleepMotorStrength = 100;

private:
static void Ring(TimerHandle_t xTimer);
static void AlarmRing(TimerHandle_t xTimer);
static void WakeAlarmRing(TimerHandle_t xTimer);
static void StopMotor(TimerHandle_t xTimer);
static void GradualWakeBuzzRing(TimerHandle_t xTimer);
static void StopGradualWakeBuzzCallback(TimerHandle_t xTimer);

TimerHandle_t shortVib;
TimerHandle_t longVib;
TimerHandle_t alarmVib;
TimerHandle_t gradualWakeBuzzDelay;
TimerHandle_t gradualWakeBuzzEnd;

TimerHandle_t wakeAlarmVib;
};
Expand Down
8 changes: 5 additions & 3 deletions src/displayapp/DisplayApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,12 +407,14 @@ void DisplayApp::Refresh() {
// LoadNewScreen(Apps::Sleep, DisplayApp::FullRefreshDirections::None);
}
// motorController.RunForDuration(infiniSleepController.gradualWakeVibrationDurations[-1 + infiniSleepController.gradualWakeStep]);
motorController.GradualWakeBuzz();

if (infiniSleepController.isSnoozing == false) {
motorController.GradualWakeBuzz();
NRF_LOG_INFO("Gradual wake triggered");
}

infiniSleepController.UpdateGradualWake();

NRF_LOG_INFO("Gradual wake triggered");

break;
case Messages::SleepTrackerUpdate:
if (currentApp == Apps::Sleep) {
Expand Down
60 changes: 59 additions & 1 deletion src/displayapp/screens/Sleep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ static void SnoozeAlarmTaskCallback(lv_task_t* task) {
screen->StopAlerting(false);
screen->UpdateDisplay();
screen->SnoozeWakeAlarm();
screen->displayState = Sleep::SleepDisplayState::Info;
screen->UpdateDisplay();
}

static void PressesToStopAlarmTimeoutCallback(lv_task_t* task) {
Expand Down Expand Up @@ -377,7 +379,7 @@ void Sleep::DrawSettingsScreen() {
//{"Smart Alarm\n(alpha)", infiniSleepController.SmartAlarmEnabled()}
};

int y_offset = 50;
int y_offset = 30;
for (const auto& setting : settings) {

lv_obj_t* checkbox = lv_checkbox_create(lv_scr_act(), nullptr);
Expand Down Expand Up @@ -421,6 +423,51 @@ void Sleep::DrawSettingsScreen() {
infiniSleepController.infiniSleepSettings.sleepCycleDuration = 90;
infiniSleepController.SetSettingsChanged();

y_offset += 60; // Adjust the offset for the next UI element

lv_obj_t* lblMotorStrength = lv_label_create(lv_scr_act(), nullptr);
lv_label_set_text_static(lblMotorStrength, "Vibration\nStrength");
lv_obj_align(lblMotorStrength, lv_scr_act(), LV_ALIGN_IN_TOP_LEFT, 10, y_offset);

lv_obj_t* btnMotorStrength = lv_btn_create(lv_scr_act(), nullptr);
lv_obj_set_size(btnMotorStrength, 100, 50);
lv_obj_align(btnMotorStrength, lv_scr_act(), LV_ALIGN_IN_TOP_LEFT, 130, y_offset);
btnMotorStrength->user_data = this;
lv_obj_set_event_cb(btnMotorStrength, [](lv_obj_t* obj, lv_event_t e) {
if (e == LV_EVENT_CLICKED) {
auto* screen = static_cast<Sleep*>(obj->user_data);
uint8_t value = screen->infiniSleepController.infiniSleepSettings.motorStrength;
value += 25;
if (value > 200) value = 100;
screen->infiniSleepController.infiniSleepSettings.motorStrength = value;
screen->infiniSleepController.SetSettingsChanged();
lv_label_set_text_fmt(lv_obj_get_child(obj, nullptr), "%d%", value);
screen->motorController.infiniSleepMotorStrength = value;
}
});

lv_obj_t* lblMotorStrengthValue = lv_label_create(btnMotorStrength, nullptr);
lv_label_set_text_fmt(lblMotorStrengthValue, "%d%", infiniSleepController.infiniSleepSettings.motorStrength);
motorController.infiniSleepMotorStrength = infiniSleepController.infiniSleepSettings.motorStrength;
lv_obj_align(lblMotorStrengthValue, nullptr, LV_ALIGN_CENTER, 0, 0);

y_offset += 60; // Adjust the offset for the next UI element

lv_obj_t* btnTestMotorGradual = lv_btn_create(lv_scr_act(), nullptr);
lv_obj_set_size(btnTestMotorGradual, 220, 50);
lv_obj_align(btnTestMotorGradual, lv_scr_act(), LV_ALIGN_IN_TOP_LEFT, 10, y_offset);
btnTestMotorGradual->user_data = this;
lv_obj_set_event_cb(btnTestMotorGradual, [](lv_obj_t* obj, lv_event_t e) {
if (e == LV_EVENT_CLICKED) {
auto* screen = static_cast<Sleep*>(obj->user_data);
screen->motorController.GradualWakeBuzz();
}
});

lv_obj_t* txtTestMotorGradual = lv_label_create(btnTestMotorGradual, nullptr);
lv_label_set_text_static(txtTestMotorGradual, "Test Motor");
lv_obj_align(txtTestMotorGradual, nullptr, LV_ALIGN_CENTER, 0, 0);

y_offset += 70; // Adjust the offset for the next UI element
}

Expand All @@ -430,6 +477,8 @@ void Sleep::OnButtonEvent(lv_obj_t* obj, lv_event_t event) {
StopAlerting();
UpdateDisplay();
SnoozeWakeAlarm();
displayState = SleepDisplayState::Info;
UpdateDisplay();
return;
}
if (obj == btnStop) {
Expand All @@ -446,6 +495,10 @@ void Sleep::OnButtonEvent(lv_obj_t* obj, lv_event_t event) {
} else {
infiniSleepController.DisableWakeAlarm();
}
if (infiniSleepController.isSnoozing) {
infiniSleepController.RestorePreSnoozeTime();
}
infiniSleepController.isSnoozing = false;
return;
}
if (obj == trackerToggleBtn) {
Expand Down Expand Up @@ -485,6 +538,11 @@ bool Sleep::OnButtonPushed() {
return true;
}
}
if (displayState != SleepDisplayState::Info) {
displayState = SleepDisplayState::Info;
UpdateDisplay();
return true;
}
return false;
}

Expand Down

0 comments on commit 2fc156b

Please sign in to comment.