-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
58 lines (46 loc) · 1.98 KB
/
Copy pathcontroller.py
File metadata and controls
58 lines (46 loc) · 1.98 KB
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
import time
import logging
import usb
from display import USBDisplay
from monitor import MetricsMonitor
UPDATE_INTERVAL = 1
class DisplayController:
"""Orchestrates the monitor and the display."""
def __init__(self, use_watts, use_fahrenheit, update_interval = UPDATE_INTERVAL):
self.display = USBDisplay()
self.monitor = MetricsMonitor(update_interval, use_watts)
self.use_fahrenheit = use_fahrenheit
self.update_interval = update_interval
self.flag_byte = self._calc_flags(use_watts, use_fahrenheit)
def _calc_flags(self, use_watts, use_fahrenheit):
flag = 0b00000000
if not use_watts: flag |= 0b00010000
if use_fahrenheit: flag |= 0b00000001
return flag
def run(self):
while True:
if not self.display.connect():
time.sleep(5); continue
try:
while True:
temp = self.monitor.get_temp()
if self.use_fahrenheit: temp = (temp * 9/5) + 32
load = self.monitor.get_load()
self._update_ui(int(temp), int(load))
time.sleep(self.update_interval)
except (usb.core.USBError, Exception) as e:
logging.error(f"Device error: {e}. Reconnecting in 5s...")
self.display.close()
time.sleep(5)
except KeyboardInterrupt:
logging.debug("Program shutting down (signal received).")
self.display.close()
break
def _update_ui(self, temp, load):
temp_limit = max(0, min(temp, 99))
t_tens, t_unit = divmod(temp_limit, 10)
load_limit = max(0, min(load, 999))
l_hundreds, remainder = divmod(load_limit, 100)
l_tens, l_unit = divmod(remainder, 10)
report = [0x07, 0x00, t_tens, t_unit, self.flag_byte, l_hundreds, l_tens, l_unit] + [0x00] * 56
self.display.send_report(report)