|
| 1 | +#include "Metronome.h" |
| 2 | + |
| 3 | +#include "Screen.h" |
| 4 | +#include "Symbols.h" |
| 5 | +#include "lvgl/lvgl.h" |
| 6 | +#include "FreeRTOSConfig.h" |
| 7 | +#include "task.h" |
| 8 | + |
| 9 | +#include <string> |
| 10 | +#include <tuple> |
| 11 | + |
| 12 | +using namespace Pinetime::Applications::Screens; |
| 13 | + |
| 14 | +namespace { |
| 15 | + float calculateDelta(const TickType_t startTime, const TickType_t currentTime) { |
| 16 | + TickType_t delta = 0; |
| 17 | + // Take care of overflow |
| 18 | + if (startTime > currentTime) { |
| 19 | + delta = 0xffffffff - startTime; |
| 20 | + delta += (currentTime + 1); |
| 21 | + } else { |
| 22 | + delta = currentTime - startTime; |
| 23 | + } |
| 24 | + return static_cast<float>(delta) / static_cast<float>(configTICK_RATE_HZ); |
| 25 | + } |
| 26 | + |
| 27 | + static void eventHandler(lv_obj_t* obj, lv_event_t event) { |
| 28 | + Metronome* screen = static_cast<Metronome*>(obj->user_data); |
| 29 | + screen->OnEvent(obj, event); |
| 30 | + } |
| 31 | + |
| 32 | + lv_obj_t* createLabel(const char* name, lv_obj_t* reference, lv_align_t align, lv_font_t* font, uint8_t x = 0, uint8_t y = 0) { |
| 33 | + lv_obj_t* label = lv_label_create(lv_scr_act(), nullptr); |
| 34 | + lv_obj_set_style_local_text_font(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, font); |
| 35 | + lv_obj_set_style_local_text_color(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY); |
| 36 | + lv_label_set_text(label, name); |
| 37 | + lv_obj_align(label, reference, align, x, y); |
| 38 | + |
| 39 | + return label; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +Metronome::Metronome(DisplayApp* app, Controllers::MotorController& motorController, System::SystemTask& systemTask) |
| 44 | + : Screen(app), running {true}, currentState {States::Stopped}, startTime {}, motorController {motorController}, systemTask {systemTask} { |
| 45 | + |
| 46 | + bpmArc = lv_arc_create(lv_scr_act(), nullptr); |
| 47 | + bpmArc->user_data = this; |
| 48 | + lv_obj_set_event_cb(bpmArc, eventHandler); |
| 49 | + lv_arc_set_bg_angles(bpmArc, 0, 270); |
| 50 | + lv_arc_set_rotation(bpmArc, 135); |
| 51 | + lv_arc_set_range(bpmArc, 40, 220); |
| 52 | + lv_arc_set_value(bpmArc, bpm); |
| 53 | + lv_obj_set_size(bpmArc, 210, 210); |
| 54 | + lv_arc_set_adjustable(bpmArc, true); |
| 55 | + lv_obj_align(bpmArc, lv_scr_act(), LV_ALIGN_IN_TOP_MID, 0, 7); |
| 56 | + |
| 57 | + bpmValue = createLabel(std::to_string(lv_arc_get_value(bpmArc)).c_str(), bpmArc, LV_ALIGN_IN_TOP_MID, &jetbrains_mono_76, 0, 55); |
| 58 | + bpmLegend = createLabel("bpm", bpmValue, LV_ALIGN_OUT_BOTTOM_MID, &jetbrains_mono_bold_20, 0, 0); |
| 59 | + |
| 60 | + bpmTap = lv_btn_create(lv_scr_act(), nullptr); |
| 61 | + bpmTap->user_data = this; |
| 62 | + lv_obj_set_event_cb(bpmTap, eventHandler); |
| 63 | + lv_obj_set_style_local_bg_opa(bpmTap, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_TRANSP); |
| 64 | + lv_obj_set_height(bpmTap, 80); |
| 65 | + lv_obj_align(bpmTap, bpmValue, LV_ALIGN_IN_TOP_MID, 0, 0); |
| 66 | + |
| 67 | + bpbDropdown = lv_dropdown_create(lv_scr_act(), nullptr); |
| 68 | + bpbDropdown->user_data = this; |
| 69 | + lv_obj_set_event_cb(bpbDropdown, eventHandler); |
| 70 | + lv_obj_set_style_local_pad_left(bpbDropdown, LV_DROPDOWN_PART_MAIN, LV_STATE_DEFAULT, 20); |
| 71 | + lv_obj_set_style_local_pad_left(bpbDropdown, LV_DROPDOWN_PART_LIST, LV_STATE_DEFAULT, 20); |
| 72 | + lv_obj_align(bpbDropdown, lv_scr_act(), LV_ALIGN_IN_BOTTOM_LEFT, 15, -4); |
| 73 | + lv_dropdown_set_options(bpbDropdown, "1\n2\n3\n4\n5\n6\n7\n8\n9"); |
| 74 | + lv_dropdown_set_selected(bpbDropdown, bpb - 1); |
| 75 | + bpbLegend = lv_label_create(bpbDropdown, nullptr); |
| 76 | + lv_label_set_text(bpbLegend, "bpb"); |
| 77 | + lv_obj_align(bpbLegend, bpbDropdown, LV_ALIGN_IN_RIGHT_MID, -15, 0); |
| 78 | + |
| 79 | + playPause = lv_btn_create(lv_scr_act(), nullptr); |
| 80 | + playPause->user_data = this; |
| 81 | + lv_obj_set_event_cb(playPause, eventHandler); |
| 82 | + lv_obj_align(playPause, lv_scr_act(), LV_ALIGN_IN_BOTTOM_RIGHT, -15, -10); |
| 83 | + lv_obj_set_height(playPause, 39); |
| 84 | + playPauseLabel = lv_label_create(playPause, nullptr); |
| 85 | + lv_label_set_text(playPauseLabel, Symbols::play); |
| 86 | + |
| 87 | + app->SetTouchMode(DisplayApp::TouchModes::Polling); |
| 88 | +} |
| 89 | + |
| 90 | +Metronome::~Metronome() { |
| 91 | + app->SetTouchMode(DisplayApp::TouchModes::Gestures); |
| 92 | + systemTask.PushMessage(Pinetime::System::SystemTask::Messages::EnableSleeping); |
| 93 | + lv_obj_clean(lv_scr_act()); |
| 94 | +} |
| 95 | + |
| 96 | +bool Metronome::OnTouchEvent(Pinetime::Applications::TouchEvents event) { |
| 97 | + return true; |
| 98 | +} |
| 99 | + |
| 100 | +bool Metronome::Refresh() { |
| 101 | + switch (currentState) { |
| 102 | + case States::Stopped: { |
| 103 | + break; |
| 104 | + } |
| 105 | + case States::Running: { |
| 106 | + if (calculateDelta(startTime, xTaskGetTickCount()) >= (60.0 / bpm)) { |
| 107 | + counter--; |
| 108 | + startTime -= 60.0 / bpm; |
| 109 | + startTime = xTaskGetTickCount(); |
| 110 | + if (counter == 0) { |
| 111 | + counter = bpb; |
| 112 | + motorController.SetDuration(90); |
| 113 | + } else { |
| 114 | + motorController.SetDuration(30); |
| 115 | + } |
| 116 | + } |
| 117 | + break; |
| 118 | + } |
| 119 | + } |
| 120 | + return running; |
| 121 | +} |
| 122 | + |
| 123 | +void Metronome::OnEvent(lv_obj_t* obj, lv_event_t event) { |
| 124 | + switch (event) { |
| 125 | + case LV_EVENT_VALUE_CHANGED: { |
| 126 | + if (obj == bpmArc) { |
| 127 | + bpm = lv_arc_get_value(bpmArc); |
| 128 | + lv_label_set_text_fmt(bpmValue, "%03d", bpm); |
| 129 | + } else if (obj == bpbDropdown) { |
| 130 | + bpb = lv_dropdown_get_selected(obj) + 1; |
| 131 | + } |
| 132 | + break; |
| 133 | + } |
| 134 | + case LV_EVENT_PRESSED: { |
| 135 | + if (obj == bpmTap) { |
| 136 | + float timeDelta = calculateDelta(tappedTime, xTaskGetTickCount()); |
| 137 | + if (tappedTime == 0 || timeDelta > 3) { |
| 138 | + tappedTime = xTaskGetTickCount(); |
| 139 | + } else { |
| 140 | + bpm = ceil(60.0 / timeDelta); |
| 141 | + lv_arc_set_value(bpmArc, bpm); |
| 142 | + lv_label_set_text_fmt(bpmValue, "%03d", bpm); |
| 143 | + tappedTime = xTaskGetTickCount(); |
| 144 | + } |
| 145 | + } |
| 146 | + break; |
| 147 | + } |
| 148 | + case LV_EVENT_CLICKED: { |
| 149 | + if (obj == playPause) { |
| 150 | + currentState = (currentState == States::Stopped ? States::Running : States::Stopped); |
| 151 | + switch (currentState) { |
| 152 | + case States::Stopped: { |
| 153 | + lv_label_set_text(playPauseLabel, Symbols::play); |
| 154 | + systemTask.PushMessage(Pinetime::System::SystemTask::Messages::EnableSleeping); |
| 155 | + break; |
| 156 | + } |
| 157 | + case States::Running: { |
| 158 | + lv_label_set_text(playPauseLabel, Symbols::pause); |
| 159 | + systemTask.PushMessage(Pinetime::System::SystemTask::Messages::DisableSleeping); |
| 160 | + startTime = xTaskGetTickCount(); |
| 161 | + counter = 1; |
| 162 | + break; |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + break; |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments