-
Notifications
You must be signed in to change notification settings - Fork 0
/
catozap.py
101 lines (83 loc) · 2.9 KB
/
catozap.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
#!/usr/bin/env python
import time
from threading import Thread
try:
import RPi.GPIO as GPIO
except:
print("*** Exception importing RPi.GPIO - falling back on rpiDummy ***")
from rpiDummy import GPIO as GPIO
#raise
class CatoZap:
def __init__(self, configObj):
self.pinLst = configObj['waterPins']
self.enabled = configObj['enabled']
self.startFiring = False
self.shutdown = False
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
for pinNo in self.pinLst:
print("CatoZap.__init__() - setting up pin %s" % pinNo)
GPIO.setup(pinNo, GPIO.OUT)
GPIO.output(pinNo, GPIO.LOW)
def _fire(self):
if (self.enabled):
print("CatoZap._fire()")
try:
for n in range(0, 10):
GPIO.output(self.pinLst[0], GPIO.HIGH)
for pinNo in self.pinLst[1:]:
GPIO.output(pinNo, GPIO.HIGH)
time.sleep(0.2)
GPIO.output(pinNo, GPIO.LOW)
time.sleep(0.2)
GPIO.output(self.pinLst[0], GPIO.LOW)
print("CatoZap._fire() - water off")
except:
print("CatoZap._fire() - exception - calling stopFiring()")
self.stopFiring()
raise
else:
print("CatoZap._fire() - CatoZab not enabled in configuration object")
def stopFiring(self):
''' Shut down the water - should be called if an exception is raised
such as a keyboard interrupt so that we exit with the water off.
'''
print("Catozap.stopFiring()")
for pinNo in self.pinLst:
GPIO.output(pinNo, GPIO.LOW)
def zapLoop(self):
print("CatoZap.zapLoop()")
while(1):
if self.startFiring:
self.startFiring = False
self._fire()
if self.shutdown:
break
print("CatoZap.zapLoop exiting")
def start(self):
''' Run the zapLoop() function in a background thread - it waits
for the fire() function to be called to initiate the firing
sequence.
'''
print("CatoZap.start()")
self.thread = Thread(target=self.zapLoop)
self.thread.start()
def stop(self):
print("CatoZap.stop()")
self.shutdown = True
print("CatoZap.stop() - waiting for zapLoop to exit...")
self.thread.join()
def fire(self):
self.startFiring = True
if __name__ == "__main__":
print("catozap main()")
cz = CatoZap({"waterPins": [17, 27, 22], "enabled": 1})
cz.start()
for n in range(0,10):
cz.fire()
time.sleep(2)
print("sleeping")
time.sleep(3)
print("shutting down cz")
cz.stop()
print("finished..")