-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathweb_gui.py
355 lines (307 loc) · 12.5 KB
/
web_gui.py
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
343
344
345
346
347
348
349
350
351
352
353
354
355
import os, sys
sys.path.append(os.path.dirname(__file__))
from controllers.ES9038Q2M import DAC_9038Q2M_Control
from flexx import app, event, ui, flx
class TreeWithControls(flx.TreeWidget):
"""Adds a key press handler to allow controlling the TreeWidget with
the arrow keys, space, and enter.
"""
def init(self, py):
self.py = py
@flx.emitter
def key_down(self, e):
"""Overload key_down emitter to prevent browser scroll."""
ev = self._create_key_event(e)
if ev.key.startswith("Arrow"):
e.preventDefault()
return ev
@flx.reaction("key_down")
def _handle_highlighting(self, *events):
for ev in events:
if ev.modifiers:
continue
if ev.key == "Escape":
self.highlight_hide()
elif ev.key == " ":
if self.max_selected == 0: # space also checks if no selection
self.highlight_toggle_checked()
else:
self.highlight_toggle_selected()
elif ev.key == "Enter":
self.highlight_toggle_checked()
elif ev.key == "ArrowRight":
item = self.highlight_get()
if item and item.items:
item.collapsed = None
elif ev.key == "ArrowLeft":
item = self.highlight_get()
if item and item.items:
item.collapsed = True
elif ev.key == "ArrowDown":
self.highlight_show(1)
elif ev.key == "ArrowUp":
self.highlight_show(-1)
@flx.reaction("children**.checked", "children**.selected", "children**.collapsed")
def on_event(self, *events):
self.send_data(events)
@event.action
def send_data(self, data):
self.py.on_tree_select(data)
@event.action
def clear(self):
for c in self.get_all_items():
c.dispose()
class FileInput(flx.LineEdit):
def _create_dom(self):
global document, FileReader
self.file = document.createElement('input')
self.file.type = 'file'
self.file.style = 'display: none'
self.file.addEventListener('change', self._handle_file)
node = super()._create_dom()
node.appendChild(self.file)
node.style = 'display: none'
self.reader = FileReader()
self.reader.onload = self.file_loaded
return node
def _handle_file(self):
self.node.value = self.file.files[0].name
self.file_selected()
def select_file(self):
self.file.click()
def load(self):
if self.file.files.length > 0:
self.reader.readAsText(self.file.files[0])
@flx.emitter
def file_loaded(self, event):
return { 'filedata': event.target.result }
@flx.emitter
def file_selected(self):
return { 'filename': self.node.value }
class WebFrontend(flx.Widget):
def init(self, py):
self.py = py
self.file_name = flx.StringProp('')
self.file_input = FileInput()
with flx.VBox():
flx.Label(style="background:#cfc;", wrap=1, text="")
with flx.HSplit(flex=1):
with flx.VBox(style="border:1px solid #777;",):
flx.Label(text="Properties Tree")
self.tree = TreeWithControls(py, flex=1, max_selected=1)
with flx.VBox(style="border:1px solid #777;"):
flx.Label(text="Property", style="")
with flx.VBox(flex=1):
with flx.VBox(flex=1, style=""):
self.combo = ui.ComboBox(editable=True, options=())
self.field = ui.LineEdit(placeholder_text="type here")
with flx.HBox(flex=1, style="max-height: 20px;"):
self.pick_file = ui.Button(text='...')
self.do_upload = ui.Button(text='Upload', disabled=True)
with flx.VBox(flex=5):
flx.Label(text="Info", style="")
self.info = flx.Label(
text="", style="white-space: pre-line;"
)
with flx.VBox(style="border:1px solid #777;", flex=1):
flx.Label(text="Raw", style="")
self.rawLabel = flx.Label(
text="", style="white-space: pre-line;"
)
self.update_btn = ui.Button(text="Apply", style="width: 100px;")
@flx.reaction('file_input.file_selected')
def handle_file_selected(self, *events):
self.set_file_to_upload(events[-1]['filename'])
@flx.reaction('file_input.file_loaded')
def handle_file_loaded(self, *events):
self.file_loaded(events[-1]['filedata'])
@flx.reaction('pick_file.pointer_click')
def on_pick_file(self, *events):
self.file_input.select_file()
@flx.reaction('do_upload.pointer_click')
def on_do_upload(self, *events):
self.file_input.load()
@flx.action
def set_file_to_upload(self, value):
self.do_upload._mutate_disabled(value == '')
# self._mutate_file_name(value)
pass
@flx.emitter
def file_loaded(self, data):
return {'filedata': data }
#Others
def field_visibility(self, status):
if status == False:
self.field.apply_style("display: none;")
else:
self.field.apply_style("display: inline-block;")
def combo_visibility(self, status):
if status == False:
self.combo.apply_style("display:none;")
else:
self.combo.apply_style("display: inline-block;")
@event.action
def upload_visibility(self, status):
if status == False:
self.pick_file.apply_style("display: none;")
self.do_upload.apply_style("display: none;")
else:
self.pick_file.apply_style("display: inline-block;")
self.do_upload.apply_style("display: inline-block;")
@event.action
def update_combo(self, options, active_idx):
if len(options) > 0:
self.field_visibility(False)
self.combo.set_options(options)
self.combo.set_selected_index(active_idx)
self.combo_visibility(True)
else:
self.combo_visibility(False)
self.field.set_text(str(active_idx))
self.field_visibility(True)
@event.action
def update_raw(self, text):
self.rawLabel.set_text(text)
self.rawLabel.apply_style("white-space: pre-line;")
@event.action
def tree_recursive(self, top, e):
with top:
if isinstance(e, dict):
for n, v in e.items():
with flx.TreeItem(text=n) as top2:
self.tree_recursive(top2, v)
elif isinstance(e, list):
for e0 in e:
flx.TreeItem(title=str(e0), css_class="selectItem")
else:
flx.TreeItem(title=str(e), css_class="selectItem")
@event.action
def update_tree(self, treeDict):
self.tree.clear()
self.tree_recursive(self.tree, treeDict)
@event.action
def update_info(self, text):
self.info.set_text(text)
self.info.apply_style("white-space: pre-line;")
@flx.reaction("combo.user_selected")
def update_selected_index(self, *events):
self.py.on_dropdown_change(events)
@flx.reaction("field.user_text")
def update_field(self, *events):
self.py.on_dropdown_change(events)
@flx.reaction("update_btn.pointer_click")
def update_btn_clicked(self, *events):
self.py.update_i2c()
class ControlApp(app.PyComponent):
def init(self, mappers, locked):
self.mappers = mappers
self.locked = locked
self.widget = WebFrontend(self)
self.autoupdate = False
mapper = self.mappers[0]
self.widget.update_tree(
{
"{0}: {1}".format(
", ".join(map(str, mapper.get(r).registers)), r
): mapper
.get(r)
.mnemonicNames
for r in mapper.registerNames
}
)
self.currentSelect = None
self.fir_filter = "fir0"
@flx.reaction('widget.file_loaded')
def handle_file_upload(self, *events):
filedata = events[-1]['filedata']
data = [min(2**23-1, (max(-2**23, int(float(f.replace("\r", ""))*2**23)))) for f in filedata.split("\n") if len(f) > 0]
print("Start Fir Update: {}".format(self.fir_filter))
for m in self.mappers:
m.fir_update(data, filter=self.fir_filter)
@event.action
def on_tree_select(self, data):
select = (
data[-1]["source"].parent.text.split(": ", 1)[1],
data[-1]["source"].title,
)
register = self.mappers[0].get(select[0])
mnemonic = register.get(select[1])
self.currentSelect = select
if self.currentSelect[1] == "prog_coeff_data":
self.widget.upload_visibility(True)
self.widget.update_info(mnemonic.description)
self.widget.update_combo(["fir1", "fir2"], None)
self.widget.update_raw(str(register))
else:
self.widget.upload_visibility(False)
self.widget.update_combo(
mnemonic.possible_values,
mnemonic.possible_values.index(mnemonic.value)
if len(mnemonic.possible_values) > 0
else mnemonic.value,
)
self.widget.update_info(mnemonic.description)
self.widget.update_raw(str(register))
@event.action
def on_dropdown_change(self, data):
for m in self.mappers:
if self.currentSelect not in self.locked and (
(self.currentSelect[0], "*") not in self.locked
):
mnemonic = m.get(self.currentSelect[0]).get(self.currentSelect[1])
if mnemonic.name == "prog_coeff_data":
self.fir_filter = data[0]["key"]
elif len(mnemonic.possible_values) > 0:
setattr(
m.get(self.currentSelect[0]),
self.currentSelect[1],
data[0]["key"],
)
elif isinstance(mnemonic.value, float):
setattr(
m.get(self.currentSelect[0]),
self.currentSelect[1],
float(data[0]["new_value"]),
)
elif isinstance(mnemonic.value, int):
setattr(
m.get(self.currentSelect[0]),
self.currentSelect[1],
int(data[0]["new_value"]),
)
@event.action
def update_i2c(self):
for m in self.mappers:
m.i2c_update()
def import_defaults(path, mapper):
print("Import defaults")
config_file = os.path.join(path, "ES9038Q2M_{0}.yaml".format(hex(mapper.i2cAddr)))
fir1_file = os.path.join(path, "FIR1.txt")
fir2_file = os.path.join(path, "FIR2.txt")
if os.path.isfile(config_file):
print("Apply config" + "ES9038Q2M_{0}.yaml".format(hex(mapper.i2cAddr)))
mapper.importYaml(config_file)
if all([os.path.isfile(f) for f in [fir1_file, fir2_file]]):
for fir in [(fir2_file, "fir2"), (fir1_file, "fir1")]:
with open(fir[0], "r") as fil:
filedata = fil.read()
data = [min(2**23-1, (max(-2**23, int(float(f.replace("\r", ""))*2**23)))) for f in filedata.split("\n") if len(f) > 0]
mapper.fir_update(data, fir[1])
if __name__ == "__main__":
import_defaults_active = True
lock_settings = [("Mixing, Serial Data and Automute Configuration", "*")]
mappers = [DAC_9038Q2M_Control(0x48), DAC_9038Q2M_Control(0x49)]
defaults = os.path.join(os.path.dirname(__file__), "configs", "default")
for m in mappers:
m.i2c_init()
if import_defaults_active:
import_defaults(defaults, m)
# m.importYaml(
# r"C:\Users\webco\Documents\Projects\SABRE_I2C_Controller\configs\device_0x48_config_std.yml"
# )
pass
flx_app = flx.App(ControlApp, mappers, lock_settings)
# app.launch("app", title="ES9038Control") # to run as a desktop app
app.create_server(host="", port=5000, backend="tornado")
flx_app.launch("browser") # to open in the browser
flx.start() # mainloop will exit when the