-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblinkControl.py
52 lines (41 loc) · 1.19 KB
/
blinkControl.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
import RPi.GPIO as GPIO
import sys, termios, tty, os, time
def getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.OUT) # Set up GPIO pins as outputs
GPIO.setup(12,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(15,GPIO.OUT)
try:
print("Press 'r', 'g', 'b' or 'w'")
while True:
x = getch()
GPIO.output(11, GPIO.LOW)
GPIO.output(12, GPIO.LOW)
GPIO.output(13, GPIO.LOW)
GPIO.output(15, GPIO.LOW)
x = x.lower()
if (x == 'r'):
GPIO.output(11, GPIO.HIGH)
elif (x == 'b'):
GPIO.output(12, GPIO.HIGH)
elif(x == 'w'):
GPIO.output(13, GPIO.HIGH)
elif(x == 'g'):
GPIO.output(15, GPIO.HIGH)
elif(x == 'p'):
raise KeyboardInterrupt
except KeyboardInterrupt:
print("\nA keyboard interrupt has been detected!")
except:
print("\nAn error or exception has occurred!")
finally:
GPIO.cleanup()