Skip to content

Commit f2a2808

Browse files
committed
Fix threading for python gui
1 parent ad3a034 commit f2a2808

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

python/inputmodule/gui/gui_threading.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
# Global GUI variables
22
STOP_THREAD = False
33
DISCONNECTED_DEVS = []
4+
STATUS = ''
45

6+
def set_status(status):
7+
global STATUS
8+
STATUS = status
9+
10+
def get_status():
11+
global STATUS
12+
return STATUS
513

614
def stop_thread():
715
global STOP_THREAD

python/inputmodule/gui/ledmatrix.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,26 @@
66
reset_thread,
77
is_thread_stopped,
88
is_dev_disconnected,
9+
set_status,
10+
get_status,
911
)
1012
from inputmodule.inputmodule.ledmatrix import (
1113
light_leds,
1214
show_string,
1315
eq,
1416
breathing,
17+
animate,
1518
)
1619
from inputmodule.inputmodule import brightness
1720

18-
1921
def countdown(dev, seconds):
2022
"""Run a countdown timer. Lighting more LEDs every 100th of a seconds.
2123
Until the timer runs out and every LED is lit"""
24+
animate(dev, False)
25+
set_status('countdown')
2226
start = datetime.now()
2327
target = seconds * 1_000_000
24-
while True:
28+
while True and get_status() == 'countdown':
2529
if is_thread_stopped() or is_dev_disconnected(dev.device):
2630
reset_thread()
2731
return
@@ -37,15 +41,17 @@ def countdown(dev, seconds):
3741

3842
time.sleep(0.01)
3943

40-
light_leds(dev, 306)
41-
breathing(dev)
42-
# blinking(dev)
44+
if get_status() == 'countdown':
45+
light_leds(dev, 306)
46+
breathing(dev)
47+
# blinking(dev)
4348

4449

4550
def blinking(dev):
4651
"""Blink brightness high/off every second.
4752
Keeps currently displayed grid"""
48-
while True:
53+
set_status('blinking')
54+
while True and get_status() == 'blinking':
4955
if is_thread_stopped() or is_dev_disconnected(dev.device):
5056
reset_thread()
5157
return
@@ -57,7 +63,9 @@ def blinking(dev):
5763

5864
def random_eq(dev):
5965
"""Display an equlizer looking animation with random values."""
60-
while True:
66+
animate(dev, False)
67+
set_status('random_eq')
68+
while True and get_status() == 'random_eq':
6169
if is_thread_stopped() or is_dev_disconnected(dev.device):
6270
reset_thread()
6371
return
@@ -72,7 +80,9 @@ def random_eq(dev):
7280
def clock(dev):
7381
"""Render the current time and display.
7482
Loops forever, updating every second"""
75-
while True:
83+
animate(dev, False)
84+
set_status('clock')
85+
while True and get_status() == 'clock':
7686
if is_thread_stopped() or is_dev_disconnected(dev.device):
7787
reset_thread()
7888
return

python/inputmodule/inputmodule/ledmatrix.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
send_serial,
1212
brightness,
1313
)
14+
from inputmodule.gui.gui_threading import get_status, set_status
1415

1516
WIDTH = 9
1617
HEIGHT = 34
@@ -69,6 +70,8 @@ def percentage(dev, p):
6970
def animate(dev, b: bool):
7071
"""Tell the firmware to start/stop animation.
7172
Scrolls the currently saved grid vertically down."""
73+
if b:
74+
set_status('animate')
7275
send_command(dev, CommandVals.Animate, [b])
7376

7477

@@ -104,6 +107,7 @@ def image_bl(dev, image_file):
104107

105108
def camera(dev):
106109
"""Play a live view from the webcam, for fun"""
110+
set_status('camera')
107111
with serial.Serial(dev.device, 115200) as s:
108112
import cv2
109113

@@ -120,7 +124,7 @@ def camera(dev):
120124
end_x = min(dim[1], start_x + WIDTH)
121125

122126
# Pre-process the video into resized, cropped, grayscale frames
123-
while True:
127+
while True and get_status() == 'camera':
124128
ret, frame = capture.read()
125129
if not ret:
126130
print("Failed to capture video frames")
@@ -142,6 +146,7 @@ def camera(dev):
142146

143147

144148
def video(dev, video_file):
149+
set_status('video')
145150
"""Resize and play back a video"""
146151
with serial.Serial(dev.device, 115200) as s:
147152
import cv2
@@ -161,7 +166,7 @@ def video(dev, video_file):
161166
processed = []
162167

163168
# Pre-process the video into resized, cropped, grayscale frames
164-
while True:
169+
while True and get_status() == 'video':
165170
ret, frame = capture.read()
166171
if not ret:
167172
print("Failed to read video frames")
@@ -294,8 +299,9 @@ def all_brightnesses(dev):
294299
def breathing(dev):
295300
"""Animate breathing brightness.
296301
Keeps currently displayed grid"""
302+
set_status('breathing')
297303
# Bright ranges appear similar, so we have to go through those faster
298-
while True:
304+
while True and get_status() == 'breathing':
299305
# Go quickly from 250 to 50
300306
for i in range(10):
301307
time.sleep(0.03)

0 commit comments

Comments
 (0)