summaryrefslogtreecommitdiff
path: root/lua/settings.lua
blob: e6c7546518202225c0dfe72061bb789a0bc30b08 (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
local lvgl = require("lvgl")
local backstack = require("backstack")
local widgets = require("widgets")
local theme = require("theme")
local volume = require("volume")
local display = require("display")
local controls = require("controls")
local bluetooth = require("bluetooth")
local database = require("database")

local settings = {}

local function SettingsScreen(title)
  local menu = widgets.MenuScreen {
    show_back = true,
    title = title,
  }
  menu.content = menu.root:Object {
    flex = {
      flex_direction = "column",
      flex_wrap = "nowrap",
      justify_content = "flex-start",
      align_items = "flex-start",
      align_content = "flex-start",
    },
    w = lvgl.PCT(100),
    flex_grow = 1,
    pad_left = 4,
    pad_right = 4,
  }
  return menu
end

function settings.bluetooth()
  local menu = SettingsScreen("Bluetooth")

  local enable_container = menu.content:Object {
    flex = {
      flex_direction = "row",
      justify_content = "flex-start",
      align_items = "content",
      align_content = "flex-start",
    },
    w = lvgl.PCT(100),
    h = lvgl.SIZE_CONTENT,
    pad_bottom = 1,
  }
  enable_container:Label { text = "Enable", flex_grow = 1 }
  local enable_sw = enable_container:Switch {}
  enable_sw:onevent(lvgl.EVENT.VALUE_CHANGED, function()
    local enabled = enable_sw:enabled()
    bluetooth.enabled:set(enabled)
  end)

  menu.content:Label {
    text = "Paired Device",
    pad_bottom = 1,
  }:add_style(theme.settings_title)

  local paired_container = menu.content:Object {
    flex = {
      flex_direction = "row",
      justify_content = "flex-start",
      align_items = "flex-start",
      align_content = "flex-start",
    },
    w = lvgl.PCT(100),
    h = lvgl.SIZE_CONTENT,
    pad_bottom = 2,
  }

  local paired_device = paired_container:Label {
    flex_grow = 1,
  }
  local clear_paired = paired_container:Button {}
  clear_paired:Label { text = "x" }
  clear_paired:onClicked(function()
    bluetooth.paired_device:set()
  end)

  menu.content:Label {
    text = "Nearby Devices",
    pad_bottom = 1,
  }:add_style(theme.settings_title)

  local devices = menu.content:List {
    w = lvgl.PCT(100),
    h = lvgl.SIZE_CONTENT,
  }

  menu.bindings = {
    bluetooth.enabled:bind(function(en)
      enable_sw:set { enabled = en }
    end),
    bluetooth.paired_device:bind(function(device)
      if device then
        paired_device:set { text = device.name }
        clear_paired:clear_flag(lvgl.FLAG.HIDDEN)
      else
        paired_device:set { text = "None" }
        clear_paired:add_flag(lvgl.FLAG.HIDDEN)
      end
    end),
    bluetooth.devices:bind(function(devs)
      devices:clean()
      for _, dev in pairs(devs) do
        devices:add_btn(nil, dev.name):onClicked(function()
          bluetooth.paired_device:set(dev)
        end)
      end
    end)
  }
end

function settings.headphones()
  local menu = SettingsScreen("Headphones")

  menu.content:Label {
    text = "Maximum volume limit",
  }:add_style(theme.settings_title)

  local volume_chooser = menu.content:Dropdown {
    options = "Line Level (-10 dB)\nCD Level (+6 dB)\nMaximum (+10dB)",
    selected = 1,
  }
  local limits = { -10, 6, 10 }
  volume_chooser:onevent(lvgl.EVENT.VALUE_CHANGED, function()
    -- luavgl dropdown binding uses 0-based indexing :(
    local selection = volume_chooser:get('selected') + 1
    volume.limit_db:set(limits[selection])
  end)

  menu.content:Label {
    text = "Left/Right balance",
  }:add_style(theme.settings_title)

  local balance = menu.content:Slider {
    w = lvgl.PCT(100),
    h = 5,
    range = { min = -100, max = 100 },
    value = 0,
  }
  balance:onevent(lvgl.EVENT.VALUE_CHANGED, function()
    volume.left_bias:set(balance:value())
  end)

  local balance_label = menu.content:Label {}

  menu.bindings = {
    volume.limit_db:bind(function(limit)
      for i = 1, #limits do
        if limits[i] == limit then
          volume_chooser:set { selected = i - 1 }
        end
      end
    end),
    volume.left_bias:bind(function(bias)
      balance:set {
        value = bias
      }
      if bias < 0 then
        balance_label:set {
          text = string.format("Left %.2fdB", bias / 4)
        }
      elseif bias > 0 then
        balance_label:set {
          text = string.format("Right %.2fdB", -bias / 4)
        }
      else
        balance_label:set { text = "Balanced" }
      end
    end),
  }

  return menu
end

function settings.display()
  local menu = SettingsScreen("Display")

  local brightness_title = menu.content:Object {
    flex = {
      flex_direction = "row",
      justify_content = "flex-start",
      align_items = "flex-start",
      align_content = "flex-start",
    },
    w = lvgl.PCT(100),
    h = lvgl.SIZE_CONTENT,
  }
  brightness_title:Label { text = "Brightness", flex_grow = 1 }
  local brightness_pct = brightness_title:Label {}
  brightness_pct:add_style(theme.settings_title)

  local brightness = menu.content:Slider {
    w = lvgl.PCT(100),
    h = 5,
    range = { min = 0, max = 100 },
    value = display.brightness:get(),
  }
  brightness:onevent(lvgl.EVENT.VALUE_CHANGED, function()
    display.brightness:set(brightness:value())
  end)

  menu.bindings = {
    display.brightness:bind(function(b)
      brightness_pct:set { text = tostring(b) .. "%" }
    end)
  }

  return menu
end

function settings.input()
  local menu = SettingsScreen("Input Method")

  menu.content:Label {
    text = "Control scheme",
  }:add_style(theme.settings_title)

  local schemes = controls.schemes()
  local option_to_scheme = {}
  local scheme_to_option = {}

  local option_idx = 0
  local options = ""

  for i, v in pairs(schemes) do
    option_to_scheme[option_idx] = i
    scheme_to_option[i] = option_idx
    if option_idx > 0 then
      options = options .. "\n"
    end
    options = options .. v
    option_idx = option_idx + 1
  end

  local controls_chooser = menu.content:Dropdown {
    options = options,
  }

  menu.bindings = {
    controls.scheme:bind(function(s)
      local option = scheme_to_option[s]
      controls_chooser:set({ selected = option })
    end)
  }

  controls_chooser:onevent(lvgl.EVENT.VALUE_CHANGED, function()
    local option = controls_chooser:get('selected')
    local scheme = option_to_scheme[option]
    controls.scheme:set(scheme)
  end)

  return menu
end

function settings.database()
  local menu = SettingsScreen("Database")
  local db = require("database")
  widgets.Row(menu.content, "Schema version", db.version())
  widgets.Row(menu.content, "Size on disk", string.format("%.1f KiB", db.size() / 1024))

  local actions_container = menu.content:Object {
    w = lvgl.PCT(100),
    h = lvgl.SIZE_CONTENT,
    flex = {
      flex_direction = "row",
      justify_content = "center",
      align_items = "space-evenly",
      align_content = "center",
    },
    pad_top = 4,
    pad_column = 4,
  }
  actions_container:add_style(theme.list_item)

  local update = actions_container:Button {}
  update:Label { text = "Update" }
  update:onClicked(function()
    database.update()
  end)
end

function settings.firmware()
  local menu = SettingsScreen("Firmware")
  local version = require("version")
  widgets.Row(menu.content, "ESP32", version.esp())
  widgets.Row(menu.content, "SAMD21", version.samd())
  widgets.Row(menu.content, "Collator", version.collator())
end

function settings.root()
  local menu = widgets.MenuScreen {
    show_back = true,
    title = "Settings",
  }
  menu.list = menu.root:List {
    w = lvgl.PCT(100),
    h = lvgl.PCT(100),
    flex_grow = 1,
  }

  local function section(name)
    menu.list:add_text(name):add_style(theme.list_heading)
  end

  local function submenu(name, fn)
    local item = menu.list:add_btn(nil, name)
    item:onClicked(function()
      backstack.push(fn)
    end)
    item:add_style(theme.list_item)
  end

  section("Audio")
  submenu("Bluetooth", settings.bluetooth)
  submenu("Headphones", settings.headphones)

  section("Interface")
  submenu("Display", settings.display)
  submenu("Input Method", settings.input)

  section("System")
  submenu("Database", settings.database)
  submenu("Firmware", settings.firmware)
  submenu("Licenses", function()
    return require("licenses")()
  end)

  return menu
end

return settings