-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathled_press3.py
85 lines (60 loc) · 1.52 KB
/
led_press3.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
#
# Copyright (C) 2016 David Brookshire <[email protected]>
#
import time
import RPi.GPIO as GPIO
BUTTON_GPIO = 4
LED1_GPIO = 17
LED2_GPIO = 18
class Lamp():
state = False # True==On, False==Off
gpio = None
def __init__(self, gpio):
self.gpio = gpio
GPIO.setup(self.gpio, GPIO.OUT)
def __repr__(self):
return "LAMP-%d" % self.gpio
def on(self):
GPIO.output(self.gpio, True)
def off(self):
GPIO.output(self.gpio, False)
class Button():
gpio = None
def __init__(self, gpio):
self.gpio = gpio
GPIO.setup(BUTTON_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def __repr__(self):
return "BUTTON-%d" % self.gpio
def get_state(self):
return GPIO.input(self.gpio)
def do_blink(lamp, count=1, delay=0.5):
while count:
lamp.on()
time.sleep(delay)
lamp.off()
time.sleep(0.2)
count -= 1
def do_shutdown(*leds):
if len(leds) >= 2:
do_blink(leds[0], 4)
do_blink(leds[1], 1, 4)
else:
do_blink(leds[0], 4)
do_blink(leds[0], 1, 4)
for l in leds:
l.off()
if __name__ == '__main__':
# GPIO.cleanup()
GPIO.setmode(GPIO.BCM)
lamp1 = Lamp(LED1_GPIO)
lamp2 = Lamp(LED2_GPIO)
trigger = Button(BUTTON_GPIO)
try:
while True:
input_state = trigger.get_state()
if not input_state:
break
except KeyboardInterrupt:
pass
do_shutdown(lamp1, lamp2)
GPIO.cleanup()