71 lines
1.9 KiB
Python
71 lines
1.9 KiB
Python
'''
|
|
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)
|