Along the lines of a first look at threading - an exercise could adapt the following “WiFi LED Blinker” excerpt to dex_eyes.py for the eyes. (I have disabled antenna_wifi.service so that I can use the WiFi LED, but the concept works for the “Eyes” as well.)
import threading
WHITE_BRIGHT = (255, 255, 255) # color 0
RED = (255, 0, 0) # color 1
ORANGE = (255, 125, 0) # color 2
YELLOW = (255, 255, 0) # color 3
YELLOW_GREEN = (125, 255, 0) # color 4
GREEN = (0, 255, 0) # color 5
TURQUOISE = (0, 255, 125) # color 6
CYAN = (0, 255, 255) # color 7 light blue
CYAN_BLUE = (0, 125, 255) # color 8
BLUE = (0, 0, 255) # color 9
VIOLET = (125, 0, 255) # color 10
MAGENTA = (255, 0, 255) # color 11
MAGENTA_RED = (255, 0, 125) # color 12
COLOR_LIST = [WHITE_BRIGHT, RED, ORANGE, YELLOW, YELLOW_GREEN, GREEN, TURQUOISE, CYAN, CYAN_BLUE, BLUE, VIOLET, MAGENTA, MAGENTA_RED]
def do_wifi_blinking(egpg,color=RED):
global wifi_blinker_thread_quit
try:
r,g,b = color
while wifi_blinker_thread_quit is not True:
egpg.set_led(egpg.LED_WIFI,r,g,b)
sleep(1)
egpg.set_led(egpg.LED_WIFI,0,0,0)
sleep(1)
except Exception as e:
print("do_wifi_blinking: Exception {}".format(str(e)))
raise e
# print("do_wifi_blinking() exiting")
wifi_blinker_thread_quit = False
wifi_blinker_thread = None
wifi_blinker_thread_quit = False
def wifi_blinker_on(egpg,color=RED):
global wifi_blinker_thread,wifi_blinker_thread_quit
if wifi_blinker_thread:
pass
else: # need to start thread
wifi_blinker_thread_quit = False
wifi_blinker_thread = threading.Thread(target=do_wifi_blinking, args=(egpg,color,), daemon=True)
wifi_blinker_thread.start()
def wifi_blinker_off(egpg):
global wifi_blinker_thread,wifi_blinker_thread_quit
if wifi_blinker_thread:
wifi_blinker_thread_quit = True # tell thread to quit
# wifi_blinker_thread.join() # wait for thread to quit
timer = 0
while wifi_blinker_thread_quit and (timer < 5):
sleep(1)
timer+=1
wifi_blinker_thread_quit = False
wifi_blinker_thread = None
else:
pass
You’ve got a lot on your plate with teaching - I’m sorry for going on here. What you have done looks great. Glad you have chosen GoPiGo3 to be a part of your pedagogy.