Skip to content

Commit 2279b2b

Browse files
authored
Merge pull request #17 from PBenja3028/master
Buttons controlling main loop example
2 parents 143f35b + 0ce2428 commit 2279b2b

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

examples/control-main.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env python
2+
import time
3+
import signal
4+
import buttonshim
5+
6+
print("""
7+
Button SHIM: control-main.py
8+
9+
Light up the LED a different color of the rainbow with each button pressed.
10+
""")
11+
12+
@buttonshim.on_press(buttonshim.BUTTON_A)
13+
def button_a(button, pressed):
14+
global button_flag
15+
button_flag = "button_1"
16+
17+
@buttonshim.on_press(buttonshim.BUTTON_B)
18+
def button_b(button, pressed):
19+
global button_flag
20+
button_flag = "button_2"
21+
22+
@buttonshim.on_press(buttonshim.BUTTON_C)
23+
def button_c(button, pressed):
24+
global button_flag
25+
button_flag = "button_3"
26+
27+
@buttonshim.on_press(buttonshim.BUTTON_D)
28+
def button_d(button, pressed):
29+
global button_flag
30+
button_flag = "button_4"
31+
32+
@buttonshim.on_press(buttonshim.BUTTON_E)
33+
def button_e(button, pressed):
34+
global button_flag
35+
button_flag = "button_5"
36+
37+
button_flag = "null"
38+
39+
while True:
40+
time.sleep(.1)
41+
if button_flag == "button_1":
42+
buttonshim.set_pixel(0x94, 0x00, 0xd3)
43+
button_flag = "null"
44+
elif button_flag == "button_2":
45+
buttonshim.set_pixel(0x00, 0x00, 0xff)
46+
button_flag = "null"
47+
elif button_flag == "button_3":
48+
buttonshim.set_pixel(0x00, 0xff, 0x00)
49+
button_flag = "null"
50+
elif button_flag == "button_4":
51+
buttonshim.set_pixel(0xff, 0xff, 0x00)
52+
button_flag = "null"
53+
elif button_flag == "button_5":
54+
buttonshim.set_pixel(0xff, 0x00, 0x00)
55+
button_flag = "null"

0 commit comments

Comments
 (0)