forked from hd-zero/hdzero-programmer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
frame_monitor.py
373 lines (306 loc) · 14.6 KB
/
frame_monitor.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import tkinter as tk
from tkinter import ttk
import ctypes
import global_var
import time
import sys
from ch341 import windows_driver, linux_driver
class frame_monitor:
def __init__(self, parent):
self._parent = parent
self._frame = tk.Frame(parent)
parent.add(self._frame, text="Monitor")
self.color_background = "#303030"
self.color_label = "white"
self.heart_cnt = 0
self.addr_usb_heart = 0x13
self.addr_usb_write_fpga_device = 0x65 # 7bit address
self.addr_usb_write_brightness = 0x14
self.addr_usb_write_contrast = 0x15
self.addr_usb_write_saturation = 0x16
self.addr_usb_write_backlight = 0x17
self.addr_usb_write_cell_count = 0x18
self.addr_usb_write_warning_cell_voltage = 0x19
self.addr_usb_write_osd = 0x1A
self.brightness_min = 0
self.brightness_max = 254
self.brightness_default = 136
self.contrast_min = 0
self.contrast_max = 254
self.contrast_default = 86
self.saturation_min = 0
self.saturation_max = 254
self.saturation_default = 143
self.backlight_min = 1
self.backlight_max = 100
self.backlight_default = 80
self.cell_count_min = 1 # 1=auto
self.cell_count_max = 6
self.cell_count_default = 1
self.warning_cell_voltage_min = 28
self.warning_cell_voltage_max = 42
self.warning_cell_voltage_default = 28
self.osd_min = 0
self.osd_max = 1
self.osd_default = 1
self.brightness_scale = 0
self.contrast_scale = 0
self.saturation_scale = 0
self.backlight_scale = 0
self.cell_count_scale = 0
self.warning_cell_voltage_scale = 0
self.osd_checkbutton = 0
self.reset_button = 0
self.osd_var = tk.BooleanVar()
self._frame.grid_rowconfigure(0, weight=1)
self._frame.grid_rowconfigure(1, weight=1)
self._frame.grid_rowconfigure(2, weight=1)
self._frame.grid_rowconfigure(3, weight=1)
self._frame.grid_rowconfigure(4, weight=1)
self._frame.grid_rowconfigure(5, weight=1)
self._frame.grid_rowconfigure(6, weight=1)
self._frame.grid_columnconfigure(0, weight=0)
self._frame.grid_columnconfigure(1, weight=0)
self._frame.grid_columnconfigure(2, weight=0)
self.init_image_setting()
self.init_power_setting()
self.init_osd_setting()
self.init_reset_button()
if sys.platform.startswith('linux'):
self.dll_object = linux_driver()
else:
self.dll_object = windows_driver()
self.dll_object.load()
def usb_heart(self):
self.heart_cnt += 1
if self.heart_cnt == 255:
self.heart_cnt = 0
self.write_i2c(self.addr_usb_heart, self.heart_cnt)
def write_i2c(self, addr, byte):
self.dll_object.write_I2C(self.addr_usb_write_fpga_device, addr, byte)
time.sleep(0.001)
def write_brightness(self, b):
global_var.brightness = b
self.write_i2c(self.addr_usb_write_brightness, b)
# print(f"write_brightness {b}")
def write_contrast(self, c):
global_var.contrast = c
self.write_i2c(self.addr_usb_write_contrast, c)
# print(f"write_contrast {c}")
def write_saturation(self, s):
global_var.saturation = s
self.write_i2c(self.addr_usb_write_saturation, s)
# print(f"write_saturation {s}")
def write_backlight(self, l):
global_var.backlight = l
self.write_i2c(self.addr_usb_write_backlight, l)
# print(f"write_backlight {l}")
def write_cell_count(self, cell):
global_var.cell_count = cell
self.write_i2c(self.addr_usb_write_cell_count, cell)
# print(f"write_cell_count {cell}")
def write_warning_cell_voltage(self, warning_cell):
global_var.warning_cell_voltage = warning_cell
self.write_i2c(self.addr_usb_write_warning_cell_voltage, warning_cell)
# print(f"write_warning_cell_voltage {warning_cell}")
def write_osd(self, osd):
global_var.osd = osd
self.write_i2c(self.addr_usb_write_osd, osd)
def write_setting(self, b, c, s, l, cell, warning_cell, osd):
"""write setting from vrx.
usually used for sync vrx setting.
NOTE: Must run after setting_enable
"""
if b < self.brightness_min or b > self.brightness_max:
b = self.brightness_default
if c < self.contrast_min or c > self.contrast_max:
c = self.contrast_default
if s < self.saturation_min or s > self.saturation_max:
s = self.saturation_default
if l < self.backlight_min or l > self.backlight_max:
l = self.backlight_default
if cell < self.cell_count_min or cell > self.cell_count_max:
cell = self.cell_count_default
if warning_cell < self.warning_cell_voltage_min or warning_cell > self.warning_cell_voltage_max:
warning_cell = self.warning_cell_voltage_default
if osd < self.osd_min or osd > self.osd_max:
osd = self.osd_default
self.write_brightness(b)
self.write_contrast(c)
self.write_saturation(s)
self.write_backlight(l)
self.write_cell_count(cell)
self.write_warning_cell_voltage(warning_cell)
self.write_osd(osd)
# update scale
self.brightness_scale.set(b)
self.contrast_scale.set(c)
self.saturation_scale.set(s)
self.backlight_scale.set(l)
self.cell_count_scale.set(cell)
self.warning_cell_voltage_scale.set(warning_cell)
if osd == 1:
self.osd_var.set(True)
else:
self.osd_var.set(False)
# update label
self.brightness_label.config(text=f'{b}')
self.contrast_label.config(text=f'{c}')
self.saturation_label.config(text=f'{s}')
self.backlight_label.config(text=f'{l}')
option = ["Auto", "2S", "3S", "4S", "5S", "6S"]
self.cell_count_label.config(text=option[cell-1])
self.warning_cell_voltage_label.config(text=f"{warning_cell/10}")
def setting_disable(self):
self.brightness_scale.configure(state="disabled")
self.contrast_scale.configure(state="disabled")
self.saturation_scale.configure(state="disabled")
self.backlight_scale.configure(state="disabled")
self.cell_count_scale.configure(state="disabled")
self.warning_cell_voltage_scale.configure(state="disabled")
self.osd_checkbutton.configure(state="disabled")
self.reset_button.configure(state="disable")
def setting_enable(self):
self.brightness_scale.configure(state="normal")
self.contrast_scale.configure(state="normal")
self.saturation_scale.configure(state="normal")
self.backlight_scale.configure(state="normal")
self.cell_count_scale.configure(state="normal")
self.warning_cell_voltage_scale.configure(state="normal")
self.osd_checkbutton.configure(state="normal")
self.reset_button.configure(state="normal")
def reset_scale(self):
self.brightness_scale.set(self.brightness_min)
self.contrast_scale.set(self.contrast_min)
self.saturation_scale.set(self.saturation_min)
self.backlight_scale.set(self.backlight_min)
self.cell_count_scale.set(self.cell_count_min)
self.warning_cell_voltage_scale.set(self.warning_cell_voltage_min)
self.osd_var.set(False)
self.brightness_label.config(text=f"{int(float(self.brightness_min))}")
self.brightness_label.config(text=f"{int(float(self.contrast_min))}")
self.saturation_label.config(text=f"{int(float(self.saturation_min))}")
self.backlight_label.config(text=f"{int(float(self.brightness_min))}")
self.on_cell_count_scale_changed(self.cell_count_min)
self.on_warning_cell_voltage_scale_changed(
self.warning_cell_voltage_min)
self.osd_var.set(False)
self.on_osd_checkoutbutton_changed()
def frame(self):
return self._frame
def on_brightness_scale_changed(self, value):
self.brightness_label.config(text=f"{int(float(value))}")
self.write_brightness(int(float(value)))
def on_contrast_scale_changed(self, value):
self.contrast_label.config(text=f"{int(float(value))}")
self.write_contrast(int(float(value)))
def on_saturation_scale_changed(self, value):
self.saturation_label.config(text=f"{int(float(value))}")
self.write_saturation(int(float(value)))
def on_backlight_scale_changed(self, value):
self.backlight_label.config(text=f"{int(float(value))}")
self.write_backlight(int(float(value)))
def on_cell_count_scale_changed(self, value):
option = ["Auto", "2S", "3S", "4S", "5S", "6S"]
self.cell_count = int(float(value))
self.cell_count_label.config(text=option[self.cell_count-1])
self.write_cell_count(int(float(value)))
def on_warning_cell_voltage_scale_changed(self, value):
self.warning_cell_voltage = int(float(value))
self.warning_cell_voltage_label.config(
text=f"{self.warning_cell_voltage/10}")
self.write_warning_cell_voltage(int(float(value)))
def on_osd_checkoutbutton_changed(self):
if self.osd_var.get() == True:
self.write_osd(1)
else:
self.write_osd(0)
def on_reset_button_press(self):
self.brightness_scale.set(self.brightness_default)
self.contrast_scale.set(self.contrast_default)
self.saturation_scale.set(self.saturation_default)
self.backlight_scale.set(self.backlight_default)
self.cell_count_scale.set(self.cell_count_default)
self.warning_cell_voltage_scale.set(self.warning_cell_voltage_default)
self.osd_var.set(True)
self.brightness_label.config(
text=f"{int(float(self.brightness_default))}")
self.contrast_label.config(
text=f"{int(float(self.contrast_default))}")
self.saturation_label.config(
text=f"{int(float(self.saturation_default))}")
self.backlight_label.config(
text=f"{int(float(self.backlight_default))}")
self.on_cell_count_scale_changed(self.cell_count_default)
self.on_warning_cell_voltage_scale_changed(
self.warning_cell_voltage_default)
self.osd_var.set(True)
self.on_osd_checkoutbutton_changed()
def init_image_setting(self):
# brighrness
row = 0
label = ttk.Label(self._frame, text="Brightness")
label.grid(row=row, column=0, sticky="w", padx=20)
self.brightness_scale = ttk.Scale(self._frame, from_=self.brightness_min, to=self.brightness_max, orient="horizontal",
length=350, command=self.on_brightness_scale_changed)
self.brightness_scale.grid(row=row, column=1, sticky="w", padx=20)
self.brightness_label = ttk.Label(self._frame, text="0")
self.brightness_label.grid(row=row, column=2, sticky="w", padx=10)
# contrast
row += 1
label = ttk.Label(self._frame, text="Contrast")
label.grid(row=row, column=0, sticky="w", padx=20)
self.contrast_scale = ttk.Scale(self._frame, from_=self.contrast_min, to=self.contrast_max, orient="horizontal",
length=350, command=self.on_contrast_scale_changed)
self.contrast_scale.grid(row=row, column=1, sticky="w", padx=20)
self.contrast_label = ttk.Label(self._frame, text="0")
self.contrast_label.grid(row=row, column=2, sticky="w", padx=10)
# saturation
row += 1
label = ttk.Label(self._frame, text="Saturation")
label.grid(row=row, column=0, sticky="w", padx=20)
self.saturation_scale = ttk.Scale(self._frame, from_=self.saturation_min, to=self.saturation_max, orient="horizontal",
length=350, command=self.on_saturation_scale_changed)
self.saturation_scale.grid(row=row, column=1, sticky="w", padx=20)
self.saturation_label = ttk.Label(self._frame, text="0")
self.saturation_label.grid(row=row, column=2, sticky="w", padx=10)
# Backlight
row += 1
label = ttk.Label(self._frame, text="Backlight")
label.grid(row=row, column=0, sticky="w", padx=20)
self.backlight_scale = ttk.Scale(self._frame, from_=self.backlight_min, to=self.backlight_max, orient="horizontal",
length=350, command=self.on_backlight_scale_changed)
self.backlight_scale.grid(row=row, column=1, sticky="w", padx=20)
self.backlight_label = ttk.Label(self._frame, text="0")
self.backlight_label.grid(row=row, column=2, sticky="w", padx=10)
def init_power_setting(self):
row = 4
label = ttk.Label(self._frame, text="Cell Count")
label.grid(row=row, column=0, sticky="w", padx=20)
self.cell_count_scale = ttk.Scale(self._frame, from_=self.cell_count_min, to=self.cell_count_max, orient="horizontal",
length=350, command=self.on_cell_count_scale_changed)
self.cell_count_scale.grid(row=row, column=1, sticky="w", padx=20)
self.cell_count_label = ttk.Label(self._frame, text="Auto")
self.cell_count_label.grid(row=row, column=2, sticky="w", padx=10)
row += 1
label = ttk.Label(self._frame, text="Warning Cell Voltage")
label.grid(row=row, column=0, sticky="w", padx=20)
self.warning_cell_voltage_scale = ttk.Scale(self._frame, from_=self.warning_cell_voltage_min, to=self.warning_cell_voltage_max,
orient="horizontal", length=350, command=self.on_warning_cell_voltage_scale_changed)
self.warning_cell_voltage_scale.grid(
row=row, column=1, sticky="w", padx=20)
self.warning_cell_voltage_label = ttk.Label(self._frame, text="2.8")
self.warning_cell_voltage_label.grid(
row=row, column=2, sticky="w", padx=10)
def init_osd_setting(self):
row = 6
label = ttk.Label(self._frame, text="Enable OSD")
label.grid(row=row, column=0, sticky="w", padx=20)
self.osd_checkbutton = ttk.Checkbutton(
self._frame, variable=self.osd_var, text="", command=self.on_osd_checkoutbutton_changed)
self.osd_checkbutton.grid(row=row, column=0, sticky="w", padx=100)
def init_reset_button(self):
row = 6
self.reset_button = tk.Button(
self._frame, text="Reset settings", command=self.on_reset_button_press)
self.reset_button.grid(row=row, column=1, sticky="w", padx=100)