-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmycamera.py
116 lines (92 loc) · 2.57 KB
/
mycamera.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
#
# Copyright (C) 2016 David Brookshire <[email protected]>
#
import time
# import picamera
import datetime
import os
import RPi.GPIO as GPIO
BUTTON_GPIO = 4
LED1_GPIO = 17
LED2_GPIO = 18
FLASH_GPIO = 27
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)
class CamCapper():
def __init__(self,
prefix="wc",
delay=2,
target_dir="/tmp",
rotate=False):
self.prefix = prefix
self.delay = delay
self.target_dir = target_dir
# self.cam = picamera.PiCamera()
# self.cam.led = True
if rotate:
self.cam.rotation = 180
def take_snapshot(self):
ds = datetime.datetime.now().strftime("%Y.%m.%d.%H.%M.%S")
fname = os.path.join(self.target_dir,
self.prefix + "." + ds + ".jpg")
# self.cam.capture(fname)
# time.sleep(self.delay)
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 warn_message(*leds):
if len(leds) >= 2:
do_blink(leds[0], 4, 0.5)
do_blink(leds[1], 1)
else:
do_blink(leds[0], 4, 0.5)
do_blink(leds[0], 1, 1)
# 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)
flash = Lamp(FLASH_GPIO)
# cam = CamCapper(target_dir='/var/www/html',
# rotate=True)
try:
while True:
input_state = trigger.get_state()
if not input_state:
print("Button pressed")
flash.on()
warn_message(lamp1, lamp2)
# cam.take_snapshot()
do_blink(lamp1, 2, 0.02)
flash.off()
except KeyboardInterrupt:
print("Shutting down")
do_blink(lamp2, 4)
GPIO.cleanup()
print("Exiting")