-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgpio_handler.py
131 lines (110 loc) · 2.85 KB
/
gpio_handler.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
127
128
129
130
131
# https://github.com/puzzle/lightning-beer-tap/blob/master/gpio_handler/gpio_handler.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
###################
# P26 ----> r_ch1 #
# P20 ----> r_ch2 #
# P21 ----> r_ch3 #
###################
import RPi.GPIO as GPIO
import argparse
import time
# Channels
r_ch1 = 26
r_ch2 = 20
r_ch3 = 21
# time constants in seconds
# single tap
t_beer = 1.5
t_cocktail = 1.5
t_flush = 10
# Syntax suger because of negative logic
S_ON = GPIO.LOW
S_OFF = GPIO.HIGH
def cli_args_parser():
"""
Argument parser configuration
"""
parser = argparse.ArgumentParser(
description='Bitcoin Bay Bartender cli',
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
'-p',
'--products',
action='store',
dest='products',
help="Drink choice [beer, cocktail]"
)
parser.add_argument(
'-t',
'--test',
action='store_true',
help="Test mode which tests all available channels"
)
parser.add_argument(
'-f',
'--flush',
action='store_true',
help="Flush tap for 10s!"
)
return parser.parse_args()
def __setup_GPIO(channel=r_ch1):
"""
Setup all GPIOs, set output mode, and set gpio mode to bcm
"""
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.OUT)
__set_gpio(channel, S_OFF)
def __set_gpio(channel=r_ch1, value=S_OFF):
"""
Try to safely change the value of a gpio, catch exception if it fails
TODO: Exception Handling
"""
try:
GPIO.output(channel, value)
except:
print("GPIO Error")
GPIO.cleanup()
def gpio_test():
"""
Test all channels
"""
for i, gpio in enumerate([r_ch1, r_ch2, r_ch3], start=1):
# Setup gpio pin
__setup_GPIO(gpio)
__set_gpio(gpio, S_ON)
time.sleep(0.1)
__set_gpio(gpio, S_OFF)
time.sleep(0.1)
def draw_beer(channel=r_ch1, wait=t_beer):
"""
Draw a delicious beer, keep the tap on for n_wait seconds
"""
# Setup gpio pin
__setup_GPIO(channel)
__set_gpio(channel, S_ON)
time.sleep(wait)
__set_gpio(channel, S_OFF)
if __name__ == "__main__":
"""
Main if not loaded as module
"""
# parse arguments
args = cli_args_parser()
# call functions according to the given arguments
if args.test:
print("Test mode enabled")
gpio_test()
elif args.flush:
print("Choice: Flush tap")
draw_beer(r_ch1, t_flush)
elif args.products == "beer":
print("Choice: Beer")
draw_beer(r_ch1, t_beer)
elif args.products == "cocktail":
print("Choice: Cocktail")
draw_beer(r_ch1, t_cocktail)
else:
print("sumting wong")