turn handler into module

This commit is contained in:
beertap
2022-07-16 23:22:02 +02:00
parent c906e9fc93
commit f776d0ed71
6 changed files with 124 additions and 176 deletions

75
gpio_handler/__init__.py Normal file
View File

@@ -0,0 +1,75 @@
import os
import time
import RPi.GPIO as GPIO
'''
Pinout:
* P26 ----> r_ch1
* P20 ----> r_ch2
* P21 ----> r_ch3
'''
# Channels
r_ch1 = 26
r_ch2 = 20
r_ch3 = 21
# time constants in seconds
t_large_beer = 6
t_small_beer = 2
# double tap
t_left_tap = 6
t_right_tap = 6
t_flush = 20
# Syntax suger because of negative logic
S_ON = GPIO.LOW
S_OFF = GPIO.HIGH
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)
print("setting up gpio_{}".format(channel))
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)
print("Channel_{}: gpio_{} on".format(i, gpio))
time.sleep(0.1)
__set_gpio(gpio, S_OFF)
print("Channel_{}: gpio_{} off".format(i, gpio))
time.sleep(0.1)
def draw_beer(channel=r_ch1, wait=t_large_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)