summaryrefslogtreecommitdiff
path: root/src/ui/screen_settings.cpp
blob: d8c867dc33d3526ff5dcec91297395e16286664b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
 * Copyright 2023 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#include "screen_settings.hpp"
#include <stdint.h>
#include <string>

#include "audio_events.hpp"
#include "core/lv_event.h"
#include "core/lv_obj.h"
#include "display.hpp"
#include "esp_log.h"

#include "core/lv_group.h"
#include "core/lv_obj_pos.h"
#include "event_queue.hpp"
#include "extra/layouts/flex/lv_flex.h"
#include "extra/widgets/list/lv_list.h"
#include "extra/widgets/menu/lv_menu.h"
#include "extra/widgets/spinbox/lv_spinbox.h"
#include "extra/widgets/spinner/lv_spinner.h"
#include "hal/lv_hal_disp.h"
#include "index.hpp"
#include "misc/lv_anim.h"
#include "misc/lv_area.h"
#include "nvs.hpp"
#include "screen.hpp"
#include "ui_events.hpp"
#include "ui_fsm.hpp"
#include "widget_top_bar.hpp"
#include "widgets/lv_bar.h"
#include "widgets/lv_btn.h"
#include "widgets/lv_dropdown.h"
#include "widgets/lv_label.h"
#include "widgets/lv_slider.h"
#include "widgets/lv_switch.h"
#include "wm8523.hpp"

namespace ui {
namespace screens {

using Page = internal::ShowSettingsPage::Page;

static void open_sub_menu_cb(lv_event_t* e) {
  Page next_page = static_cast<Page>(reinterpret_cast<uintptr_t>(e->user_data));
  events::Ui().Dispatch(internal::ShowSettingsPage{
      .page = next_page,
  });
}

static void sub_menu(lv_obj_t* list,
                     lv_group_t* group,
                     const std::string& text,
                     Page page) {
  lv_obj_t* item = lv_list_add_btn(list, NULL, text.c_str());
  lv_group_add_obj(group, item);
  lv_obj_add_event_cb(item, open_sub_menu_cb, LV_EVENT_CLICKED,
                      reinterpret_cast<void*>(static_cast<uintptr_t>(page)));
}

Settings::Settings() : MenuScreen("Settings") {
  lv_obj_t* list = lv_list_create(content_);
  lv_obj_set_size(list, lv_pct(100), lv_pct(100));

  lv_list_add_text(list, "Audio");
  sub_menu(list, group_, "Bluetooth", Page::kBluetooth);
  sub_menu(list, group_, "Headphones", Page::kHeadphones);

  lv_list_add_text(list, "Interface");
  sub_menu(list, group_, "Appearance", Page::kAppearance);
  sub_menu(list, group_, "Input Method", Page::kInput);

  lv_list_add_text(list, "System");
  sub_menu(list, group_, "Storage", Page::kStorage);
  sub_menu(list, group_, "Firmware Update", Page::kFirmwareUpdate);
  sub_menu(list, group_, "About", Page::kAbout);
}

static auto settings_container(lv_obj_t* parent) -> lv_obj_t* {
  lv_obj_t* res = lv_obj_create(parent);
  lv_obj_set_layout(res, LV_LAYOUT_FLEX);
  lv_obj_set_size(res, lv_pct(100), LV_SIZE_CONTENT);
  lv_obj_set_flex_flow(res, LV_FLEX_FLOW_ROW);
  lv_obj_set_flex_align(res, LV_FLEX_ALIGN_SPACE_EVENLY, LV_FLEX_ALIGN_CENTER,
                        LV_FLEX_ALIGN_START);
  return res;
}

static auto label_pair(lv_obj_t* parent,
                       const std::string& left,
                       const std::string& right) -> lv_obj_t* {
  lv_obj_t* container = settings_container(parent);
  lv_obj_t* left_label = lv_label_create(container);
  lv_label_set_text(left_label, left.c_str());
  lv_obj_t* right_label = lv_label_create(container);
  lv_label_set_text(right_label, right.c_str());
  return right_label;
}

Bluetooth::Bluetooth() : MenuScreen("Bluetooth") {
  lv_obj_t* toggle_container = settings_container(content_);
  lv_obj_t* toggle_label = lv_label_create(toggle_container);
  lv_label_set_text(toggle_label, "Enable");
  lv_obj_set_flex_grow(toggle_label, 1);
  lv_obj_t* toggle = lv_switch_create(toggle_container);
  lv_group_add_obj(group_, toggle);

  lv_obj_t* devices_label = lv_label_create(content_);
  lv_label_set_text(devices_label, "Devices");

  lv_obj_t* devices_list = lv_list_create(content_);
  lv_list_add_text(devices_list, "My Headphones");
  lv_group_add_obj(group_,
                   lv_list_add_btn(devices_list, NULL, "Something else"));
  lv_group_add_obj(group_, lv_list_add_btn(devices_list, NULL, "A car??"));
  lv_group_add_obj(group_,
                   lv_list_add_btn(devices_list, NULL, "OLED TV ANDROID"));
  lv_group_add_obj(
      group_, lv_list_add_btn(devices_list, NULL, "there could be another"));
  lv_group_add_obj(group_, lv_list_add_btn(devices_list, NULL,
                                           "this one has a really long name"));
}

static void change_vol_limit_cb(lv_event_t* ev) {
  int selected_index = lv_dropdown_get_selected(ev->target);
  Headphones* instance = reinterpret_cast<Headphones*>(ev->user_data);
  instance->ChangeMaxVolume(selected_index);
}

static void increase_vol_limit_cb(lv_event_t* ev) {
  Headphones* instance = reinterpret_cast<Headphones*>(ev->user_data);
  instance->ChangeCustomVolume(2);
}

static void decrease_vol_limit_cb(lv_event_t* ev) {
  Headphones* instance = reinterpret_cast<Headphones*>(ev->user_data);
  instance->ChangeCustomVolume(-2);
}

Headphones::Headphones(drivers::NvsStorage& nvs)
    : MenuScreen("Headphones"), nvs_(nvs), custom_limit_(0) {
  uint16_t reference = drivers::wm8523::kLineLevelReferenceVolume;
  index_to_level_.push_back(reference - (10 * 4));
  index_to_level_.push_back(reference + (6 * 4));
  index_to_level_.push_back(reference + (9.5 * 4));

  lv_obj_t* vol_label = lv_label_create(content_);
  lv_label_set_text(vol_label, "Volume Limit");
  lv_obj_t* vol_dropdown = lv_dropdown_create(content_);
  lv_dropdown_set_options(vol_dropdown,
                          "Line Level (-10 dB)\nCD Level (+6 dB)\nMax "
                          "before clipping (+10dB)\nCustom");
  lv_group_add_obj(group_, vol_dropdown);

  uint16_t level = nvs.AmpMaxVolume().get();
  for (int i = 0; i < index_to_level_.size() + 1; i++) {
    if (i == index_to_level_.size() || index_to_level_[i] == level) {
      lv_dropdown_set_selected(vol_dropdown, i);
      break;
    }
  }

  lv_obj_add_event_cb(vol_dropdown, change_vol_limit_cb, LV_EVENT_VALUE_CHANGED,
                      this);

  custom_vol_container_ = settings_container(content_);

  lv_obj_t* decrease_btn = lv_btn_create(custom_vol_container_);
  lv_obj_t* btn_label = lv_label_create(decrease_btn);
  lv_label_set_text(btn_label, "-");
  lv_obj_add_event_cb(decrease_btn, decrease_vol_limit_cb, LV_EVENT_CLICKED,
                      this);

  custom_vol_label_ = lv_label_create(custom_vol_container_);
  UpdateCustomVol(level);

  lv_obj_t* increase_btn = lv_btn_create(custom_vol_container_);
  btn_label = lv_label_create(increase_btn);
  lv_label_set_text(btn_label, "+");
  lv_obj_add_event_cb(increase_btn, increase_vol_limit_cb, LV_EVENT_CLICKED,
                      this);

  if (lv_dropdown_get_selected(vol_dropdown) != index_to_level_.size()) {
    lv_obj_add_flag(custom_vol_container_, LV_OBJ_FLAG_HIDDEN);
  }

  lv_obj_t* balance_label = lv_label_create(content_);
  lv_label_set_text(balance_label, "Left/Right Balance");
  lv_obj_t* balance = lv_slider_create(content_);
  lv_obj_set_width(balance, lv_pct(100));
  lv_slider_set_range(balance, -10, 10);
  lv_slider_set_value(balance, 0, LV_ANIM_OFF);
  lv_slider_set_mode(balance, LV_SLIDER_MODE_SYMMETRICAL);
  lv_group_add_obj(group_, balance);
  lv_obj_t* current_balance_label = lv_label_create(content_);
  lv_label_set_text(current_balance_label, "0dB");
  lv_obj_set_size(current_balance_label, lv_pct(100), LV_SIZE_CONTENT);
}

auto Headphones::ChangeMaxVolume(uint8_t index) -> void {
  if (index >= index_to_level_.size()) {
    lv_obj_clear_flag(custom_vol_container_, LV_OBJ_FLAG_HIDDEN);
    return;
  }
  auto vol = index_to_level_[index];
  lv_obj_add_flag(custom_vol_container_, LV_OBJ_FLAG_HIDDEN);
  UpdateCustomVol(vol);
  events::Audio().Dispatch(audio::ChangeMaxVolume{.new_max = vol});
}

auto Headphones::ChangeCustomVolume(int8_t diff) -> void {
  UpdateCustomVol(custom_limit_ + diff);
}

auto Headphones::UpdateCustomVol(uint16_t level) -> void {
  custom_limit_ = level;
  int16_t db = (static_cast<int32_t>(level) -
                drivers::wm8523::kLineLevelReferenceVolume) /
               4;
  int16_t db_parts = (static_cast<int32_t>(level) -
                      drivers::wm8523::kLineLevelReferenceVolume) %
                     4;

  std::ostringstream builder;
  if (db >= 0) {
    builder << "+";
  }
  builder << db << ".";
  builder << (db_parts * 100 / 4);
  builder << " dBV";

  lv_label_set_text(custom_vol_label_, builder.str().c_str());
}

static void change_brightness_cb(lv_event_t* ev) {
  Appearance* instance = reinterpret_cast<Appearance*>(ev->user_data);
  instance->ChangeBrightness(lv_slider_get_value(ev->target));
}

static void release_brightness_cb(lv_event_t* ev) {
  Appearance* instance = reinterpret_cast<Appearance*>(ev->user_data);
  instance->CommitBrightness();
}

static auto brightness_str(uint_fast8_t percent) -> std::string {
  return std::to_string(percent) + "%";
}

Appearance::Appearance(drivers::NvsStorage& nvs, drivers::Display& display)
    : MenuScreen("Appearance"), nvs_(nvs), display_(display) {
  lv_obj_t* toggle_container = settings_container(content_);
  lv_obj_t* toggle_label = lv_label_create(toggle_container);
  lv_obj_set_flex_grow(toggle_label, 1);
  lv_label_set_text(toggle_label, "Dark Mode");
  lv_obj_t* toggle = lv_switch_create(toggle_container);
  lv_group_add_obj(group_, toggle);

  uint_fast8_t initial_brightness = nvs_.ScreenBrightness().get();

  lv_obj_t* brightness_label = lv_label_create(content_);
  lv_label_set_text(brightness_label, "Brightness");
  lv_obj_t* brightness = lv_slider_create(content_);
  lv_obj_set_width(brightness, lv_pct(100));
  lv_slider_set_range(brightness, 10, 100);
  lv_slider_set_value(brightness, initial_brightness, LV_ANIM_OFF);
  lv_group_add_obj(group_, brightness);
  current_brightness_label_ = lv_label_create(content_);
  lv_label_set_text(current_brightness_label_,
                    brightness_str(initial_brightness).c_str());
  lv_obj_set_size(current_brightness_label_, lv_pct(100), LV_SIZE_CONTENT);

  lv_obj_add_event_cb(brightness, change_brightness_cb, LV_EVENT_VALUE_CHANGED,
                      this);
  lv_obj_add_event_cb(brightness, release_brightness_cb, LV_EVENT_RELEASED,
                      this);
}

auto Appearance::ChangeBrightness(uint_fast8_t new_level) -> void {
  current_brightness_ = new_level;
  display_.SetBrightness(new_level);
  lv_label_set_text(current_brightness_label_,
                    brightness_str(new_level).c_str());
}

auto Appearance::CommitBrightness() -> void {
  nvs_.ScreenBrightness(current_brightness_);
}

InputMethod::InputMethod() : MenuScreen("Input Method") {
  lv_obj_t* wheel_label = lv_label_create(content_);
  lv_label_set_text(wheel_label, "What does the wheel do?");
  lv_obj_t* wheel_dropdown = lv_dropdown_create(content_);
  lv_dropdown_set_options(wheel_dropdown, "Scroll\nDirectional\nBig Button");
  lv_group_add_obj(group_, wheel_dropdown);

  lv_obj_t* buttons_label = lv_label_create(content_);
  lv_label_set_text(buttons_label, "What do the buttons do?");
  lv_obj_t* buttons_dropdown = lv_dropdown_create(content_);
  lv_dropdown_set_options(buttons_dropdown, "Volume\nScroll");
  lv_group_add_obj(group_, buttons_dropdown);
}

Storage::Storage() : MenuScreen("Storage") {
  label_pair(content_, "Storage Capacity:", "32 GiB");
  label_pair(content_, "Currently Used:", "6 GiB");
  label_pair(content_, "DB Size:", "1.2 MiB");

  lv_obj_t* usage_bar = lv_bar_create(content_);
  lv_bar_set_range(usage_bar, 0, 32);
  lv_bar_set_value(usage_bar, 6, LV_ANIM_OFF);

  lv_obj_t* reset_btn = lv_btn_create(content_);
  lv_obj_t* reset_label = lv_label_create(reset_btn);
  lv_label_set_text(reset_label, "Reset Database");
  lv_group_add_obj(group_, reset_btn);
}

FirmwareUpdate::FirmwareUpdate() : MenuScreen("Firmware Update") {
  label_pair(content_, "ESP32 FW:", "vIDKLOL");
  label_pair(content_, "SAMD21 FW:", "vIDKLOL");

  lv_obj_t* flash_esp_btn = lv_btn_create(content_);
  lv_obj_t* flash_esp_label = lv_label_create(flash_esp_btn);
  lv_label_set_text(flash_esp_label, "Update ESP32");
  lv_group_add_obj(group_, flash_esp_btn);

  lv_obj_t* flash_samd_btn = lv_btn_create(content_);
  lv_obj_t* flash_samd_label = lv_label_create(flash_samd_btn);
  lv_label_set_text(flash_samd_label, "Update SAMD21");
  lv_group_add_obj(group_, flash_samd_btn);
}

About::About() : MenuScreen("About") {
  lv_obj_t* label = lv_label_create(content_);
  lv_label_set_text(label, "Some licenses or whatever");
}

}  // namespace screens
}  // namespace ui