forked from mattvenn/machinekit-bipod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutton.py
executable file
·50 lines (43 loc) · 1.2 KB
/
button.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
#!/usr/bin/python
import Adafruit_BBIO.GPIO as GPIO
import logging
from control import send
import time
import socket
# create log file handler and set to debug
log = logging.getLogger('')
log_format = logging.Formatter('%(asctime)s - %(levelname)-8s - %(message)s')
fh = logging.FileHandler('button.log')
fh.setFormatter(log_format)
log.addHandler(fh)
# button connected to this pin and +3.3v, with pulldown
# so if high, button is pressed
pin = "P9_12"
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# wait for bipod process to start
log.warning("waiting for bipod process to start")
while True:
try:
send('programs')
break
except socket.error:
pass
time.sleep(1)
def wait_for_button():
log.warning("waiting for button press")
while True:
GPIO.wait_for_edge(pin, GPIO.RISING)
# try to avoid noise issues that cause false triggers
time.sleep(0.25)
# if button still pressed
if GPIO.input(pin):
break
# first button we will send start
wait_for_button()
log.warning("button pressed, starting")
send('start')
# second button press will halt
wait_for_button()
log.warning("button pressed, halting")
send('halt')
GPIO.cleanup()