Browse Source

show individual display values and reduce update issues

Thomas B 6 days ago
parent
commit
81134a88ba
2 changed files with 69 additions and 12 deletions
  1. 5
    2
      client/brightness.py
  2. 64
    10
      client/tray.py

+ 5
- 2
client/brightness.py View File

@@ -24,7 +24,9 @@ calibration = {
24 24
 
25 25
 running = True
26 26
 is_active = True
27
+is_unpaused = True
27 28
 last_brightness = 0
29
+disps = None
28 30
 
29 31
 def cal(v, c):
30 32
     # out = out_b + out_a * in_a * max(0, in_b + in)
@@ -42,7 +44,7 @@ def lux_to_disp(name, val):
42 44
     return min(max(val, 0), 100)
43 45
 
44 46
 def main():
45
-    global running, is_active, last_brightness
47
+    global running, is_active, is_unpaused, last_brightness, disps
46 48
 
47 49
     print("usb init")
48 50
     usb = lux.usb_init()
@@ -79,6 +81,7 @@ def main():
79 81
     time_window = time.time()
80 82
 
81 83
     is_active = True
84
+    is_unpaused = True
82 85
 
83 86
     while running:
84 87
         # read brightness at approx. 1Hz with low-pass filtering
@@ -129,7 +132,7 @@ def main():
129 132
             is_active = not info["fullscreen"]
130 133
 
131 134
         # set displays at most every 10s
132
-        if is_active and ((time.time() - time_displays) > 10.0):
135
+        if is_active and is_unpaused and ((time.time() - time_displays) > 10.0):
133 136
             time_displays = time.time()
134 137
 
135 138
             for d in disps:

+ 64
- 10
client/tray.py View File

@@ -14,14 +14,34 @@ from PIL import Image
14 14
 #icon_path = "/usr/share/icons/breeze-dark/status/32/input-keyboard-brightness.svg"
15 15
 icon_path = "/usr/share/icons/breeze-dark/actions/24/brightness-high.svg"
16 16
 
17
-def is_running():
18
-    #print("check1")
19
-    return "Active" if brightness.is_active else "Inactive"
17
+prev_pause = None
18
+prev_active = None
19
+prev_last = None
20
+prev_disp = None
21
+
22
+def is_running_name():
23
+    #print("check 1")
24
+    return "Running" if (brightness.is_active and brightness.is_unpaused) else "Stopped"
25
+
26
+def is_running_checked():
27
+    #print("check 2")
28
+    return True if (brightness.is_active and brightness.is_unpaused) else False
20 29
 
21 30
 def get_value():
22
-    #print("check2")
31
+    #print("check 3")
23 32
     return f"Brightness: {brightness.last_brightness}"
24 33
 
34
+def is_paused_checked():
35
+    #print("check 4")
36
+    return False if brightness.is_unpaused else True
37
+
38
+def is_paused_name():
39
+    #print("check 5")
40
+    return "Unpaused" if brightness.is_unpaused else "Paused"
41
+
42
+def toggle_pause():
43
+    brightness.is_unpaused = False if brightness.is_unpaused else True
44
+
25 45
 def quit(icon):
26 46
     print("stop brightness")
27 47
     brightness.running = False
@@ -30,10 +50,35 @@ def quit(icon):
30 50
     icon.stop()
31 51
 
32 52
 def poll(icon):
53
+    global prev_pause, prev_active, prev_last, prev_disp
54
+
33 55
     while brightness.running:
56
+        time.sleep(1.0)
57
+
58
+        if (prev_pause == brightness.is_unpaused) and (prev_active == brightness.is_active) and (prev_last == brightness.last_brightness) and (prev_disp == brightness.disps):
59
+            #print("skip")
60
+            continue
61
+
62
+        prev_pause = brightness.is_unpaused
63
+        prev_active = brightness.is_active
64
+        prev_last = brightness.last_brightness
65
+        prev_disp = brightness.disps
66
+
34 67
         #print("update")
35 68
         icon.update_menu()
36
-        time.sleep(5.0)
69
+
70
+def display_menu():
71
+    #print("menu")
72
+    if (brightness.disps == None) or (len(brightness.disps) <= 0):
73
+        return (pystray.MenuItem("No displays", None, enabled=False), )
74
+
75
+    display_entries = []
76
+    for d in brightness.disps:
77
+        #s = "{} ({}) @ {}".format(d["name"], d["_id"], d["prev"])
78
+        s = f"{d['prev']} @ {d['name']}"
79
+        display_entries.append(pystray.MenuItem(s, None, enabled=False))
80
+
81
+    return display_entries
37 82
 
38 83
 def main():
39 84
     out = BytesIO()
@@ -48,18 +93,27 @@ def main():
48 93
     icon = pystray.Icon("AutoBrightness", image, "AutoBrightness",
49 94
         menu=pystray.Menu(
50 95
             pystray.MenuItem(
51
-                'Activated',
96
+                lambda icon=pystray.Icon: get_value(),
52 97
                 None,
53 98
                 enabled=False,
54
-                checked=lambda icon=pystray.Icon: is_running(),
55 99
             ),
56
-
57 100
             pystray.MenuItem(
58
-                lambda icon=pystray.Icon: get_value(),
101
+                "Displays",
102
+                pystray.Menu(
103
+                    lambda icon=pystray.Icon: display_menu(),
104
+                )
105
+            ),
106
+            pystray.MenuItem(
107
+                lambda icon=pystray.Icon: is_running_name(),
59 108
                 None,
60 109
                 enabled=False,
110
+                checked=lambda icon=pystray.Icon: is_running_checked(),
111
+            ),
112
+            pystray.MenuItem(
113
+                lambda icon=pystray.Icon: is_paused_name(),
114
+                lambda icon=pystray.Icon: toggle_pause(),
115
+                checked=lambda icon=pystray.Icon: is_paused_checked(),
61 116
             ),
62
-
63 117
             pystray.MenuItem(
64 118
                 "Quit",
65 119
                 lambda icon=pystray.Icon: quit(icon),

Loading…
Cancel
Save