improve gpio handler
This commit is contained in:
@@ -1,75 +1,20 @@
|
||||
import os
|
||||
import time
|
||||
import RPi.GPIO as GPIO
|
||||
'''
|
||||
Raspi pinout:
|
||||
- P26: r_ch1
|
||||
- P20: r_ch2
|
||||
- P21: r_ch3
|
||||
'''
|
||||
|
||||
'''
|
||||
Pinout:
|
||||
* P26 ----> r_ch1
|
||||
* P20 ----> r_ch2
|
||||
* P21 ----> r_ch3
|
||||
'''
|
||||
# Channels
|
||||
r_ch1 = 26
|
||||
r_ch2 = 20
|
||||
r_ch3 = 21
|
||||
channels = [r_ch1, r_ch2, r_ch3]
|
||||
|
||||
# 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)
|
||||
def create_output():
|
||||
'''
|
||||
Sets up the gpios to be used as outputs
|
||||
'''
|
||||
from .gpio import GPIOHandler
|
||||
output_handler = GPIOHandler(channels)
|
||||
return output_handler
|
||||
|
||||
70
gpio_handler/gpio.py
Normal file
70
gpio_handler/gpio.py
Normal file
@@ -0,0 +1,70 @@
|
||||
'''
|
||||
GPIO module to handle the gpios on our beertap.
|
||||
'''
|
||||
|
||||
import time
|
||||
import RPi.GPIO as gpio
|
||||
|
||||
class GPIOHandler:
|
||||
'''
|
||||
Class which takes care of handling the gpio pins.
|
||||
'''
|
||||
|
||||
# constants for syntactical purposes
|
||||
S_ON = gpio.LOW
|
||||
S_OFF = gpio.HIGH
|
||||
|
||||
# gpio list
|
||||
gpio_list=[]
|
||||
|
||||
def __init__(self, gpio_list):
|
||||
'''
|
||||
Contructor which takes a list of gpio pins
|
||||
and sets them all up to be used as output.
|
||||
'''
|
||||
for pin in gpio_list:
|
||||
self.gpio_list.append(pin)
|
||||
self.__setup_gpio(pin)
|
||||
|
||||
def __set_gpio(self, channel=1, value=0):
|
||||
'''
|
||||
Try to safely change the value of a given gpio
|
||||
and cleanup in case of an error.
|
||||
'''
|
||||
try:
|
||||
gpio.output(channel, value)
|
||||
except:
|
||||
print("GPIO Error")
|
||||
gpio.cleanup()
|
||||
|
||||
def __setup_gpio(self, channel=1):
|
||||
'''
|
||||
Takes a gpio channel, sets it to output mode
|
||||
and set gpio mode to BCM.
|
||||
'''
|
||||
print(f"setting up gpio_{channel}")
|
||||
gpio.setwarnings(False)
|
||||
gpio.setmode(gpio.BCM)
|
||||
gpio.setup(channel, gpio.OUT)
|
||||
self.__set_gpio(channel, self.S_OFF)
|
||||
|
||||
def gpio_test(self):
|
||||
'''
|
||||
Test all configured channels
|
||||
'''
|
||||
for pin in self.gpio_list:
|
||||
self.__set_gpio(pin, self.S_ON)
|
||||
print(f"gpio_{pin}: on")
|
||||
time.sleep(0.1)
|
||||
self.__set_gpio(pin, self.S_OFF)
|
||||
print(f"gpio_{pin}: off")
|
||||
time.sleep(0.1)
|
||||
|
||||
def gpio_set_pulse(self, channel=1, time_on=1):
|
||||
'''
|
||||
Sets the state of a given gpio pin to ON for a given amount of time
|
||||
then toggels it back to the previous state.
|
||||
'''
|
||||
self.__set_gpio(channel, self.S_ON)
|
||||
time.sleep(time_on)
|
||||
self.__set_gpio(channel, self.S_OFF)
|
||||
Reference in New Issue
Block a user