Skip to content

Commit 1588696

Browse files
committed
Fix PR findings
- Switch from bool EnableForCal to always saving and restoring shake wake mode to avoid to many conditionals in the constructor and destructor - Refactor calibrating into currentCalibrationStepEnum with descriptive names - Initialize variables with default values in the class body - Switch to forward declarations for references in the header and remove unused include - Fix issue where text would stay on screen due to not calling lv_obj_clean() in the destructor (cherry picked from commit af30390)
1 parent 145a755 commit 1588696

File tree

2 files changed

+44
-35
lines changed

2 files changed

+44
-35
lines changed

src/displayapp/screens/settings/SettingShakeThreshold.cpp

+22-27
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
#include <lvgl/lvgl.h>
33
#include "displayapp/DisplayApp.h"
44
#include "displayapp/screens/Screen.h"
5-
#include "displayapp/screens/Symbols.h"
65
#include "displayapp/InfiniTimeTheme.h"
6+
#include <components/motion/MotionController.h>
7+
#include <components/settings/Settings.h>
8+
#include "systemtask/SystemTask.h"
79

810
using namespace Pinetime::Applications::Screens;
911

@@ -18,7 +20,6 @@ SettingShakeThreshold::SettingShakeThreshold(Controllers::Settings& settingsCont
1820
Controllers::MotionController& motionController,
1921
System::SystemTask& systemTask)
2022
: settingsController {settingsController}, motionController {motionController}, systemTask {systemTask} {
21-
2223
lv_obj_t* title = lv_label_create(lv_scr_act(), nullptr);
2324
lv_label_set_text_static(title, "Wake Sensitivity");
2425
lv_label_set_align(title, LV_LABEL_ALIGN_CENTER);
@@ -32,7 +33,7 @@ SettingShakeThreshold::SettingShakeThreshold(Controllers::Settings& settingsCont
3233
lv_label_set_text_static(
3334
explanation,
3435
"\nShake detector is disabled in sleep mode, and will neither wake up the watch nor calibrate.\nDisable sleep mode to calibrate.");
35-
calibrating = 255;
36+
currentCalibrationStep = CalibrationStep::Disabled;
3637
lv_obj_align(explanation, title, LV_ALIGN_OUT_BOTTOM_MID, 0, 0);
3738
return;
3839
}
@@ -75,42 +76,36 @@ SettingShakeThreshold::SettingShakeThreshold(Controllers::Settings& settingsCont
7576
lv_arc_set_value(positionArc, settingsController.GetShakeThreshold());
7677

7778
vDecay = xTaskGetTickCount();
78-
calibrating = false;
79-
EnableForCal = false;
80-
if (!settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::Shake)) {
81-
EnableForCal = true;
82-
settingsController.setWakeUpMode(Pinetime::Controllers::Settings::WakeUpMode::Shake, true);
83-
}
79+
oldWakeupModeShake = settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::Shake);
80+
// Enable shake to wake for the calibration
81+
settingsController.setWakeUpMode(Pinetime::Controllers::Settings::WakeUpMode::Shake, true);
8482
refreshTask = lv_task_create(RefreshTaskCallback, LV_DISP_DEF_REFR_PERIOD, LV_TASK_PRIO_MID, this);
8583
}
8684

8785
SettingShakeThreshold::~SettingShakeThreshold() {
88-
if (calibrating == 255)
89-
return;
90-
91-
settingsController.SetShakeThreshold(lv_arc_get_value(positionArc));
92-
93-
if (EnableForCal) {
94-
settingsController.setWakeUpMode(Pinetime::Controllers::Settings::WakeUpMode::Shake, false);
95-
EnableForCal = false;
86+
if (currentCalibrationStep != CalibrationStep::Disabled) {
87+
// Don't set new threshold if calibration is disabled due to sleep mode
88+
settingsController.SetShakeThreshold(lv_arc_get_value(positionArc));
89+
// Restore previous wakeup mode which is only changed if calibration is not disabled due to sleep mode
90+
settingsController.setWakeUpMode(Pinetime::Controllers::Settings::WakeUpMode::Shake, oldWakeupModeShake);
91+
settingsController.SaveSettings();
92+
lv_task_del(refreshTask);
9693
}
97-
lv_task_del(refreshTask);
98-
settingsController.SaveSettings();
94+
9995
lv_obj_clean(lv_scr_act());
10096
}
10197

10298
void SettingShakeThreshold::Refresh() {
103-
104-
if (calibrating == 1) {
99+
if (currentCalibrationStep == CalibrationStep::GettingReady) {
105100
if (xTaskGetTickCount() - vCalTime > pdMS_TO_TICKS(2000)) {
106101
vCalTime = xTaskGetTickCount();
107-
calibrating = 2;
102+
currentCalibrationStep = CalibrationStep::Calibrating;
108103
lv_obj_set_style_local_bg_color(calButton, LV_BTN_PART_MAIN, LV_STATE_CHECKED, LV_COLOR_RED);
109104
lv_obj_set_style_local_bg_color(calButton, LV_BTN_PART_MAIN, LV_STATE_CHECKED, LV_COLOR_RED);
110105
lv_label_set_text_static(calLabel, "Shake!");
111106
}
112107
}
113-
if (calibrating == 2) {
108+
if (currentCalibrationStep == CalibrationStep::Calibrating) {
114109

115110
if ((motionController.CurrentShakeSpeed() - 300) > lv_arc_get_value(positionArc)) {
116111
lv_arc_set_value(positionArc, (int16_t) motionController.CurrentShakeSpeed() - 300);
@@ -129,19 +124,19 @@ void SettingShakeThreshold::Refresh() {
129124
}
130125

131126
void SettingShakeThreshold::UpdateSelected(lv_obj_t* object, lv_event_t event) {
132-
133127
switch (event) {
134128
case LV_EVENT_VALUE_CHANGED: {
135129
if (object == calButton) {
136-
if (lv_btn_get_state(calButton) == LV_BTN_STATE_CHECKED_RELEASED && calibrating == 0) {
130+
if (lv_btn_get_state(calButton) == LV_BTN_STATE_CHECKED_RELEASED
131+
&& currentCalibrationStep == CalibrationStep::NotCalibrated) {
137132
lv_arc_set_value(positionArc, 0);
138-
calibrating = 1;
133+
currentCalibrationStep = CalibrationStep::GettingReady;
139134
vCalTime = xTaskGetTickCount();
140135
lv_label_set_text_static(calLabel, "Ready!");
141136
lv_obj_set_click(positionArc, false);
142137
lv_obj_set_style_local_bg_color(calButton, LV_BTN_PART_MAIN, LV_STATE_CHECKED, Colors::highlight);
143138
} else if (lv_btn_get_state(calButton) == LV_BTN_STATE_RELEASED) {
144-
calibrating = 0;
139+
currentCalibrationStep = CalibrationStep::NotCalibrated;
145140
lv_obj_set_click(positionArc, true);
146141
lv_label_set_text_static(calLabel, "Calibrate");
147142
}

src/displayapp/screens/settings/SettingShakeThreshold.h

+22-8
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
#include <cstdint>
44
#include <lvgl/lvgl.h>
5-
#include "components/settings/Settings.h"
65
#include "displayapp/screens/Screen.h"
7-
#include <components/motion/MotionController.h>
8-
#include "systemtask/SystemTask.h"
6+
7+
namespace Pinetime::Controllers {
8+
class Settings;
9+
class MotionController;
10+
}
11+
namespace Pinetime::System {
12+
class SystemTask;
13+
}
914

1015
namespace Pinetime {
1116

@@ -26,11 +31,20 @@ namespace Pinetime {
2631
Controllers::Settings& settingsController;
2732
Controllers::MotionController& motionController;
2833
System::SystemTask& systemTask;
29-
uint8_t calibrating;
30-
bool EnableForCal;
31-
uint32_t vDecay, vCalTime;
32-
lv_obj_t *positionArc, *animArc, *calButton, *calLabel;
33-
lv_task_t* refreshTask;
34+
35+
enum class CalibrationStep {
36+
NotCalibrated = 0,
37+
GettingReady = 1,
38+
Calibrating = 2,
39+
// Special case for disabled calibration due to sleep mode
40+
Disabled = 255,
41+
};
42+
43+
CalibrationStep currentCalibrationStep = CalibrationStep::NotCalibrated;
44+
bool oldWakeupModeShake = false;
45+
uint32_t vDecay = 0, vCalTime = 0;
46+
lv_obj_t *positionArc = nullptr, *animArc = nullptr, *calButton = nullptr, *calLabel = nullptr;
47+
lv_task_t* refreshTask = nullptr;
3448
};
3549
}
3650
}

0 commit comments

Comments
 (0)