-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.py
executable file
·127 lines (109 loc) · 3.63 KB
/
clock.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
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/python3
import RPi.GPIO as GPIO
import time
import argparse
import sys, traceback
from pythonosc import dispatcher
from pythonosc import osc_server
from subprocess import call
STEP_DELAY = 0.0025 # STEP_DELAY between steps
STEPS_WAITIR = 50 # Steps to wait until activating IR LED
STEPS_MAX = 1000 # Steps to stop after if IR fails us
OSC_LISTEN = "0.0.0.0" # default listen on address (0.0.0.0 = all)
OSC_PORT = 5005 # default listen on address (0.0.0.0 = all)
# NOTE no need for enable PINs; they can be constantly pulled high
# IN1-4 to control step sequence
COIL_B_1_PIN = 17
COIL_B_2_PIN = 18
COIL_A_1_PIN = 22
COIL_A_2_PIN = 23
# IR Sensor and Paired LED behind '12' to detect minute hand at zero
IRSENSE_PIN = 24
LED_PIN = 25
# Function for step sequence
def setStep(w1, w2, w3, w4):
GPIO.output(COIL_A_1_PIN, w1)
GPIO.output(COIL_A_2_PIN, w2)
GPIO.output(COIL_B_1_PIN, w3)
GPIO.output(COIL_B_2_PIN, w4)
def runBackward(steps):
for i in range(0, steps):
setStep(1,0,1,0)
time.sleep(STEP_DELAY)
setStep(0,1,1,0)
time.sleep(STEP_DELAY)
setStep(0,1,0,1)
time.sleep(STEP_DELAY)
setStep(1,0,0,1)
time.sleep(STEP_DELAY)
def runForward(steps):
for i in range(0, steps):
setStep(1,0,0,1)
time.sleep(STEP_DELAY)
setStep(0,1,0,1)
time.sleep(STEP_DELAY)
setStep(0,1,1,0)
time.sleep(STEP_DELAY)
setStep(1,0,1,0)
time.sleep(STEP_DELAY)
def forward_handler(unused_addr, args, hrs):
print("forward {} for {} hours".format(args, hrs))
GPIO.output(LED_PIN, 1)
for i in range(int(hrs)):
count = 0
while count < STEPS_WAITIR or GPIO.input(IRSENSE_PIN) == 1:
count += 1
runForward(1)
if count > STEPS_MAX:
break
GPIO.output(LED_PIN, 0)
setStep(0,0,0,0)
def backward_handler(unused_addr, args, hrs):
print("backward {} for {} hrs".format(args, hrs))
GPIO.output(LED_PIN, 1)
for i in range(int(hrs)):
count = 0
while count < STEPS_WAITIR or GPIO.input(IRSENSE_PIN) == 1:
count += 1
runBackward(1)
if count > STEPS_MAX:
break
GPIO.output(LED_PIN, 0)
setStep(0,0,0,0)
def shutdown_handler(unused_addr, args, unused_arg):
print("shutdown {}".format(args))
setStep(0,0,0,0)
call("sudo poweroff", shell=True)
def reboot_handler(unused_addr, args, unused_arg):
print("reboot {}".format(args))
setStep(0,0,0,0)
call("sudo reboot", shell=True)
def main():
try:
# Set PIN states
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(COIL_A_1_PIN, GPIO.OUT)
GPIO.setup(COIL_A_2_PIN, GPIO.OUT)
GPIO.setup(COIL_B_1_PIN, GPIO.OUT)
GPIO.setup(COIL_B_2_PIN, GPIO.OUT)
GPIO.setup(IRSENSE_PIN, GPIO.IN)
GPIO.setup(LED_PIN, GPIO.OUT)
d = dispatcher.Dispatcher()
d.map("/forward", forward_handler, "Clock forwards (hours)")
d.map("/backward", backward_handler, "Clock backwards (hours)")
d.map("/shutdown", shutdown_handler, "Shutdown Pi nicely")
d.map("/reboot", reboot_handler, "Reboot Pi nicely")
server = osc_server.ThreadingOSCUDPServer((OSC_LISTEN, OSC_PORT), d)
print("OSC listening on {} port {}".format(OSC_LISTEN, OSC_PORT))
server.serve_forever()
except KeyboardInterrupt:
print("Closing down")
setStep(0,0,0,0)
except Exception:
traceback.print_exc(file=sys.stdout)
setStep(0,0,0,0)
# We'll never get here!
sys.exit(0)
if __name__ == "__main__":
main()