|
| 1 | +#include "SettingShakeThreshold.h" |
| 2 | +#include <lvgl/lvgl.h> |
| 3 | +#include "displayapp/DisplayApp.h" |
| 4 | +#include "displayapp/screens/Screen.h" |
| 5 | +#include "displayapp/screens/Symbols.h" |
| 6 | + |
| 7 | +using namespace Pinetime::Applications::Screens; |
| 8 | + |
| 9 | +namespace { |
| 10 | + void event_handler(lv_obj_t* obj, lv_event_t event) { |
| 11 | + SettingShakeThreshold* screen = static_cast<SettingShakeThreshold*>(obj->user_data); |
| 12 | + screen->UpdateSelected(obj, event); |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +SettingShakeThreshold::SettingShakeThreshold(DisplayApp* app, |
| 17 | + Controllers::Settings& settingsController, |
| 18 | + Controllers::MotionController& motionController, |
| 19 | + System::SystemTask& systemTask) |
| 20 | + : Screen(app), settingsController {settingsController}, motionController {motionController}, systemTask {systemTask} { |
| 21 | + |
| 22 | + lv_obj_t* title = lv_label_create(lv_scr_act(), nullptr); |
| 23 | + lv_label_set_text_static(title, "Wake Sensitivity"); |
| 24 | + lv_label_set_align(title, LV_LABEL_ALIGN_CENTER); |
| 25 | + lv_obj_align(title, lv_scr_act(), LV_ALIGN_IN_TOP_MID, 0, 0); |
| 26 | + |
| 27 | + positionArc = lv_arc_create(lv_scr_act(), nullptr); |
| 28 | + positionArc->user_data = this; |
| 29 | + |
| 30 | + lv_obj_set_event_cb(positionArc, event_handler); |
| 31 | + lv_arc_set_bg_angles(positionArc, 180, 360); |
| 32 | + lv_arc_set_range(positionArc, 0, 4095); |
| 33 | + lv_arc_set_adjustable(positionArc, true); |
| 34 | + lv_obj_set_width(positionArc, lv_obj_get_width(lv_scr_act()) - 10); |
| 35 | + lv_obj_set_height(positionArc, 240); |
| 36 | + lv_obj_align(positionArc, title, LV_ALIGN_OUT_BOTTOM_MID, 0, 0); |
| 37 | + |
| 38 | + animArc = lv_arc_create(positionArc, positionArc); |
| 39 | + lv_arc_set_adjustable(animArc, false); |
| 40 | + lv_obj_set_width(animArc, lv_obj_get_width(positionArc)); |
| 41 | + lv_obj_set_height(animArc, lv_obj_get_height(positionArc)); |
| 42 | + lv_obj_align_mid(animArc, positionArc, LV_ALIGN_CENTER, 0, 0); |
| 43 | + lv_obj_set_style_local_line_opa(animArc, LV_ARC_PART_BG, LV_STATE_DEFAULT, 0); |
| 44 | + lv_obj_set_style_local_line_opa(animArc, LV_ARC_PART_INDIC, LV_STATE_DEFAULT, LV_OPA_70); |
| 45 | + lv_obj_set_style_local_line_opa(animArc, LV_ARC_PART_KNOB, LV_STATE_DEFAULT, LV_OPA_0); |
| 46 | + lv_obj_set_style_local_line_color(animArc, LV_ARC_PART_INDIC, LV_STATE_DEFAULT, LV_COLOR_RED); |
| 47 | + lv_obj_set_style_local_bg_color(animArc, LV_ARC_PART_BG, LV_STATE_CHECKED, LV_COLOR_TRANSP); |
| 48 | + |
| 49 | + animArc->user_data = this; |
| 50 | + lv_obj_set_click(animArc, false); |
| 51 | + |
| 52 | + calButton = lv_btn_create(lv_scr_act(), nullptr); |
| 53 | + calButton->user_data = this; |
| 54 | + lv_obj_set_event_cb(calButton, event_handler); |
| 55 | + lv_obj_set_height(calButton, 80); |
| 56 | + lv_obj_set_width(calButton, 200); |
| 57 | + lv_obj_align(calButton, lv_scr_act(), LV_ALIGN_IN_BOTTOM_MID, 0, 0); |
| 58 | + lv_btn_set_checkable(calButton, true); |
| 59 | + calLabel = lv_label_create(calButton, NULL); |
| 60 | + lv_label_set_text(calLabel, "Calibrate"); |
| 61 | + |
| 62 | + lv_arc_set_value(positionArc, settingsController.GetShakeThreshold()); |
| 63 | + |
| 64 | + vDecay = xTaskGetTickCount(); |
| 65 | + calibrating = false; |
| 66 | + EnableForCal = false; |
| 67 | + if(!settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::Shake)){ |
| 68 | + EnableForCal = true; |
| 69 | + settingsController.setWakeUpMode(Pinetime::Controllers::Settings::WakeUpMode::Shake,true); |
| 70 | + } |
| 71 | + refreshTask = lv_task_create(RefreshTaskCallback, LV_DISP_DEF_REFR_PERIOD, LV_TASK_PRIO_MID, this); |
| 72 | +} |
| 73 | + |
| 74 | +SettingShakeThreshold::~SettingShakeThreshold() { |
| 75 | + settingsController.SetShakeThreshold(lv_arc_get_value(positionArc)); |
| 76 | + |
| 77 | + if(EnableForCal){ |
| 78 | + settingsController.setWakeUpMode(Pinetime::Controllers::Settings::WakeUpMode::Shake,false); |
| 79 | + EnableForCal = false; |
| 80 | + } |
| 81 | + lv_task_del(refreshTask); |
| 82 | + settingsController.SaveSettings(); |
| 83 | + lv_obj_clean(lv_scr_act()); |
| 84 | +} |
| 85 | + |
| 86 | +void SettingShakeThreshold::Refresh() { |
| 87 | + |
| 88 | + if (calibrating == 1) { |
| 89 | + if (xTaskGetTickCount() - vCalTime > pdMS_TO_TICKS(2000)) { |
| 90 | + vCalTime = xTaskGetTickCount(); |
| 91 | + calibrating = 2; |
| 92 | + lv_obj_set_style_local_bg_color(calButton, LV_BTN_PART_MAIN, LV_STATE_CHECKED, LV_COLOR_RED); |
| 93 | + lv_obj_set_style_local_bg_color(calButton, LV_BTN_PART_MAIN, LV_STATE_CHECKED, LV_COLOR_RED); |
| 94 | + lv_label_set_text(calLabel, "Shake!!"); |
| 95 | + } |
| 96 | + } |
| 97 | + if (calibrating == 2) { |
| 98 | + |
| 99 | + if ((motionController.currentShakeSpeed() - 300) > lv_arc_get_value(positionArc)) { |
| 100 | + lv_arc_set_value(positionArc, (int16_t) motionController.currentShakeSpeed() - 300); |
| 101 | + } |
| 102 | + if (xTaskGetTickCount() - vCalTime > pdMS_TO_TICKS(7500)) { |
| 103 | + lv_btn_set_state(calButton, LV_STATE_DEFAULT); |
| 104 | + lv_event_send(calButton, LV_EVENT_VALUE_CHANGED, NULL); |
| 105 | + } |
| 106 | + } |
| 107 | + if (motionController.currentShakeSpeed() - 300 > lv_arc_get_value(animArc)) { |
| 108 | + lv_arc_set_value(animArc, (uint16_t) motionController.currentShakeSpeed() - 300); |
| 109 | + vDecay = xTaskGetTickCount(); |
| 110 | + } else if ((xTaskGetTickCount() - vDecay) > pdMS_TO_TICKS(1500)) { |
| 111 | + lv_arc_set_value(animArc, lv_arc_get_value(animArc) - 25); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +void SettingShakeThreshold::UpdateSelected(lv_obj_t* object, lv_event_t event) { |
| 116 | + |
| 117 | + switch (event) { |
| 118 | + case LV_EVENT_VALUE_CHANGED: { |
| 119 | + if (object == calButton) { |
| 120 | + if (lv_btn_get_state(calButton) == LV_BTN_STATE_CHECKED_RELEASED && calibrating == 0) { |
| 121 | + lv_arc_set_value(positionArc, 0); |
| 122 | + calibrating = 1; |
| 123 | + vCalTime = xTaskGetTickCount(); |
| 124 | + lv_label_set_text(calLabel, "Ready!"); |
| 125 | + lv_obj_set_click(positionArc, false); |
| 126 | + lv_obj_set_style_local_bg_color(calButton, LV_BTN_PART_MAIN, LV_STATE_CHECKED, LV_COLOR_GREEN); |
| 127 | + lv_obj_set_style_local_bg_color(calButton, LV_BTN_PART_MAIN, LV_STATE_CHECKED, LV_COLOR_GREEN); |
| 128 | + } else if (lv_btn_get_state(calButton) == LV_BTN_STATE_RELEASED) { |
| 129 | + calibrating = 0; |
| 130 | + lv_obj_set_click(positionArc, true); |
| 131 | + lv_label_set_text(calLabel, "Calibrate"); |
| 132 | + } |
| 133 | + break; |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments