-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
113 lines (89 loc) · 2.44 KB
/
main.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
from printer import PrinterResponse
import RPi.GPIO as GPIO
import os
import sys
import time
import datetime
import socket
def start():
# ============
# Settings
# ============
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
ip = ''
# ip = '192.168.1.22'
port = 80
page = '/NameTag/ntap/response'
hostname = 'robostorm-toshiba.local'
#Pre B+
bDonePin = 24
bHaltPin = 26
#B+ and Pi 2
pDonePin = 38
pHaltPin = 40
doneDelay = 0
haltDelay = 1000
# ================
# End Settings
# ================
# Setup
donePin = bDonePin
haltPin = bHaltPin
# Get time in milliseconds
def millis():
return int(round(time.time()*1000))
# Log a mesage
def log(msg):
print(datetime.datetime.now().strftime("%I:%M:%S:%f")+": "+msg)
# Called when done button is pressed
def done():
log("Done")
response.send()
# Called when halt button is pressed
def halt():
log("Halt")
os.system("halt")
if GPIO.RPI_REVISION >= 3:
donePin = pDonePin
haltPin = pHaltPin
print("B+ or Pi 2")
else:
print("Pi 1 not +")
if ip == '':
ip = socket.gethostbyname(hostname)
donePressed = False
haltPressed = False
doneMillis = 0;
haltMillis = 0;
GPIO.setup(donePin, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(haltPin, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.add_event_detect(donePin, GPIO.BOTH)
GPIO.add_event_detect(haltPin, GPIO.BOTH)
response = PrinterResponse(ip, port, page)
# Main Loop
while True:
if GPIO.event_detected(donePin):
if not GPIO.input(donePin):
log("Done Pressed")
doneMillis = millis()
donePressed = True
else:
log("Done Released")
donePressed = False
if GPIO.event_detected(haltPin):
if not GPIO.input(haltPin):
log("Halt Pressed")
haltMillis = millis()
haltPressed = True
else:
log("Halt Released")
haltPressed = False
if donePressed:
if millis() - doneMillis >= doneDelay:
donePressed = False
done()
if haltPressed:
if millis() - haltMillis >= haltDelay:
haltPressed = False
halt()