-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleds.py
executable file
·158 lines (137 loc) · 4.76 KB
/
leds.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
#!/usr/bin/python3
# File: leds.py
#
# Methods:
# leds.all_on(egpg) turn two red blinker leds on and two "eyes" on bright white
# leds.all_off(egpg) turn two red blinker leds off and two "eyes" off
# wifi_blinker_on(egpg,color=RED) make wifi led blink on/off,on/off...
# pass colors as leds.YELLOW or leds.GREEN ...
# wifi_blinker_off(egpg) turn wifi blinker off
#
# Usage:
# import leds
# egpg=easygopigo3.EasyGoPiGo3()
# leds.all_on(egpg)
# leds.all_off(egpg)
# leds.wifi_blinker_on(egpg,color=leds.ORANGE)
# leds.wifi_blinker_off(egpg)
#
# or from command line:
# > ./leds.py performs test on/off
# > ./leds.py -s on turns leds on
# > ./leds.py -s off turns leds off
# > ./leds.py -c 5 turn all leds green
import sys
sys.path.insert(1,"/home/pi/GoPi5Go/plib")
import noinit_easygopigo3 as easygopigo3
# import myconfig
from time import sleep
import argparse
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 all_on(egpg=None):
egpg.blinker_on("left")
egpg.blinker_on("right")
egpg.led_on("left")
egpg.led_on("right")
egpg.set_eye_color(WHITE_BRIGHT)
egpg.open_eyes()
# can set wifi led to white, only if utils/wifi_led_off.sh has been run
egpg.set_led(egpg.LED_WIFI,255,255,255)
def all_off(egpg=None):
egpg.blinker_off("left")
egpg.blinker_off("right")
egpg.led_off("left")
egpg.led_off("right")
egpg.close_eyes()
# can turn wifi led off, only if utils/wifi_led_off.sh has been run
egpg.set_led(egpg.LED_WIFI,0,0,0)
def all_color(egpg=None, colornum=5):
if colornum < len(COLOR_LIST):
egpg.set_led(egpg.LED_WIFI,COLOR_LIST[colornum][0], COLOR_LIST[colornum][1], COLOR_LIST[colornum][2])
egpg.set_eye_color(COLOR_LIST[ colornum ])
egpg.open_eyes()
else:
print("ERROR: all_color({}) larger than {}".format(colornum,len(COLOR_LIST)))
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
def main():
ap = argparse.ArgumentParser()
ap.add_argument("-s", "--set", action="store", default=None, help="set all leds 'on' or 'off'")
ap.add_argument("-c", "--colornum", type=int, action="store", default=None, help="set all leds to color ")
args = vars(ap.parse_args())
egpg = easygopigo3.EasyGoPiGo3(use_mutex=True, noinit=True)
# myconfig.setParameters(egpg)
set = args["set"]
colornumber = args['colornum']
if set==None:
if colornumber == None:
print("leds.py: Test all_on()")
all_on(egpg)
sleep(5)
print("leds.py: Test all_off()")
all_off(egpg)
sleep(5)
print("leds.py: Test all_color() (green)")
all_color(egpg)
sleep(5)
else:
all_color(egpg,colornumber)
elif set=='on':
all_on(egpg)
else:
all_off(egpg)
print("Test Wifi Blinker")
wifi_blinker_on(egpg,color=RED)
sleep(10)
wifi_blinker_off(egpg)
all_off(egpg)
if (__name__ == '__main__'): main()