diff --git a/beertap/views.py b/beertap/views.py index 51bc742..072d550 100644 --- a/beertap/views.py +++ b/beertap/views.py @@ -1,7 +1,7 @@ import json from flask import Blueprint, render_template, request, flash, jsonify -from gpio_handler import gpio_test, draw_beer +from gpio_handler import gpio_set_pulse views = Blueprint('views', __name__) @@ -22,10 +22,10 @@ def render(): def choice(): if request.form['submit_button'] == 'option_1': print("Eis usem lingge Gütterli uselah!") - draw_beer(channel=r_ch1, wait=t_large_beer) + gpio_set_pulse(channel=r_ch1, time_on=t_large_beer) if request.form['submit_button'] == 'option_2': print("Eis usem rächte Gütterli uselah!") - draw_beer(channel=r_ch2, wait=t_large_beer) + gpio_set_pulse(channel=r_ch2, time_on=t_large_beer) return render_template('beertap.html') @views.route('/about', methods=['GET']) diff --git a/gpio_handler/__init__.py b/gpio_handler/__init__.py index bfd4c42..cb13bfb 100644 --- a/gpio_handler/__init__.py +++ b/gpio_handler/__init__.py @@ -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 diff --git a/gpio_handler/gpio.py b/gpio_handler/gpio.py new file mode 100644 index 0000000..2066e09 --- /dev/null +++ b/gpio_handler/gpio.py @@ -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) diff --git a/main.py b/main.py index eb62b30..9ec248f 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,9 @@ from beertap import create_app -from gpio_handler import gpio_test +from gpio_handler import create_output app = create_app() +output = create_output() if __name__ == "__main__": - gpio_test() + output.gpio_test() app.run(debug=True)