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

View File

@@ -5,9 +5,9 @@ name = "pypi"
[packages] [packages]
Flask = "*" Flask = "*"
"RPi.GPIO" = "*"
[dev-packages] [dev-packages]
[requires] [requires]
python_version = "3.10" python_version = "3.9"

38
Pipfile.lock generated
View File

@@ -1,11 +1,11 @@
{ {
"_meta": { "_meta": {
"hash": { "hash": {
"sha256": "6de13d2ea8dca5da51cef01797133e7ef9f87c26c51503e42f0698490cb97b2d" "sha256": "546f2640086b0cce7cdfc40c1f433e02e8d96e7fec695697c950a409ea9498cb"
}, },
"pipfile-spec": 6, "pipfile-spec": 6,
"requires": { "requires": {
"python_version": "3.10" "python_version": "3.9"
}, },
"sources": [ "sources": [
{ {
@@ -21,7 +21,6 @@
"sha256:7682dc8afb30297001674575ea00d1814d808d6a36af415a82bd481d37ba7b8e", "sha256:7682dc8afb30297001674575ea00d1814d808d6a36af415a82bd481d37ba7b8e",
"sha256:bb4d8133cb15a609f44e8213d9b391b0809795062913b383c62be0ee95b1db48" "sha256:bb4d8133cb15a609f44e8213d9b391b0809795062913b383c62be0ee95b1db48"
], ],
"markers": "python_version >= '3.7'",
"version": "==8.1.3" "version": "==8.1.3"
}, },
"flask": { "flask": {
@@ -32,12 +31,19 @@
"index": "pypi", "index": "pypi",
"version": "==2.1.3" "version": "==2.1.3"
}, },
"importlib-metadata": {
"hashes": [
"sha256:637245b8bab2b6502fcbc752cc4b7a6f6243bb02b31c5c26156ad103d3d45670",
"sha256:7401a975809ea1fdc658c3aa4f78cc2195a0e019c5cbc4c06122884e9ae80c23"
],
"markers": "python_version < '3.10'",
"version": "==4.12.0"
},
"itsdangerous": { "itsdangerous": {
"hashes": [ "hashes": [
"sha256:2c2349112351b88699d8d4b6b075022c0808887cb7ad10069318a8b0bc88db44", "sha256:2c2349112351b88699d8d4b6b075022c0808887cb7ad10069318a8b0bc88db44",
"sha256:5dbbc68b317e5e42f327f9021763545dc3fc3bfe22e6deb96aaf1fc38874156a" "sha256:5dbbc68b317e5e42f327f9021763545dc3fc3bfe22e6deb96aaf1fc38874156a"
], ],
"markers": "python_version >= '3.7'",
"version": "==2.1.2" "version": "==2.1.2"
}, },
"jinja2": { "jinja2": {
@@ -45,7 +51,6 @@
"sha256:31351a702a408a9e7595a8fc6150fc3f43bb6bf7e319770cbc0db9df9437e852", "sha256:31351a702a408a9e7595a8fc6150fc3f43bb6bf7e319770cbc0db9df9437e852",
"sha256:6088930bfe239f0e6710546ab9c19c9ef35e29792895fed6e6e31a023a182a61" "sha256:6088930bfe239f0e6710546ab9c19c9ef35e29792895fed6e6e31a023a182a61"
], ],
"markers": "python_version >= '3.7'",
"version": "==3.1.2" "version": "==3.1.2"
}, },
"markupsafe": { "markupsafe": {
@@ -91,16 +96,35 @@
"sha256:f121a1420d4e173a5d96e47e9a0c0dcff965afdf1626d28de1460815f7c4ee7a", "sha256:f121a1420d4e173a5d96e47e9a0c0dcff965afdf1626d28de1460815f7c4ee7a",
"sha256:fc7b548b17d238737688817ab67deebb30e8073c95749d55538ed473130ec0c7" "sha256:fc7b548b17d238737688817ab67deebb30e8073c95749d55538ed473130ec0c7"
], ],
"markers": "python_version >= '3.7'",
"version": "==2.1.1" "version": "==2.1.1"
}, },
"rpi.gpio": {
"hashes": [
"sha256:15311d3b063b71dee738cd26570effc9985a952454d162937c34e08c0fc99902",
"sha256:26b2ade4bb353bbe5417a64e40b45cdf00a82f27c2d320c0dd46c59751959d77",
"sha256:29226823da8b5ccb9001d795a944f2e00924eeae583490f0bc7317581172c624",
"sha256:57b6c044ef5375a78c8dda27cdfadf329e76aa6943cd6cffbbbd345a9adf9ca5",
"sha256:77afb817b81331ce3049a4b8f94a85e41b7c404d8e56b61ac0f1eb75c3120868",
"sha256:96ea7e5bf6bf592828487bfa7d3d1d0e432d5d1682e9e33b4dab398914628fcd",
"sha256:b86b66dc02faa5461b443a1e1f0c1d209d64ab5229696f32fb3b0215e0600c8c",
"sha256:cd61c4b03c37b62bba4a5acfea9862749c33c618e0295e7e90aa4713fb373b70"
],
"index": "pypi",
"version": "==0.7.1"
},
"werkzeug": { "werkzeug": {
"hashes": [ "hashes": [
"sha256:1ce08e8093ed67d638d63879fd1ba3735817f7a80de3674d293f5984f25fb6e6", "sha256:1ce08e8093ed67d638d63879fd1ba3735817f7a80de3674d293f5984f25fb6e6",
"sha256:72a4b735692dd3135217911cbeaa1be5fa3f62bffb8745c5215420a03dc55255" "sha256:72a4b735692dd3135217911cbeaa1be5fa3f62bffb8745c5215420a03dc55255"
], ],
"markers": "python_version >= '3.7'",
"version": "==2.1.2" "version": "==2.1.2"
},
"zipp": {
"hashes": [
"sha256:05b45f1ee8f807d0cc928485ca40a07cb491cf092ff587c0df9cb1fd154848d2",
"sha256:47c40d7fe183a6f21403a199b3e4192cca5774656965b0a4988ad2f8feb5f009"
],
"version": "==3.8.1"
} }
}, },
"develop": {} "develop": {}

View File

@@ -1,165 +0,0 @@
#!/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_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 cli_args_parser():
"""
Argument parser configuration
"""
parser = argparse.ArgumentParser(
description='Lightning beer tap cli',
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
'-p',
'--products',
action='store',
dest='products',
help="Product description, beer size [small, large]"
)
parser.add_argument(
'-t',
'--test',
action='store_true',
help="Test mode which tests all available channels"
)
parser.add_argument(
'-f1',
'--flush1',
action='store_true',
help="Flush tap 1 for 20s!"
)
parser.add_argument(
'-f2',
'--flush2',
action='store_true',
help="Flush tap 2 for 20s!"
)
parser.add_argument(
'-f3',
'--flush3',
action='store_true',
help="Flush tap 3 for 20s!"
)
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)
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)
if __name__ == "__main__":
"""
Main if not loaded as module
"""
# parse arguments
args = cli_args_parser()
print(args.products)
# call functions according to the given arguments
if args.test:
print("Test mode enabled")
gpio_test()
elif args.flush1:
print("Choice: Flush tap1")
draw_beer(r_ch1, t_flush)
elif args.flush2:
print("Choice: Flush tap2")
draw_beer(r_ch2, t_flush)
elif args.flush3:
print("Choice: Flush tap3")
draw_beer(r_ch3, t_flush)
elif args.products == "LARGE":
print("Choice: Large beer")
draw_beer(r_ch1, t_large_beer)
elif args.products == "SMALL":
print("Choice: Small beer")
draw_beer(r_ch1, t_small_beer)
elif args.products == "LEFT_TAP":
print("Choice: left tap")
draw_beer(r_ch1, t_left_tap)
elif args.products == "RIGHT_TAP":
print("Choice: right tap")
draw_beer(r_ch2, t_right_tap)
else:
print("RTFM!")

View File

@@ -1,9 +1,19 @@
import json import json
from flask import Blueprint, render_template, request, flash, jsonify from flask import Blueprint, render_template, request, flash, jsonify
from gpio_handler import gpio_test, draw_beer
views = Blueprint('views', __name__) views = Blueprint('views', __name__)
# gpio channels
r_ch1 = 26
r_ch2 = 20
r_ch3 = 21
# wait times
t_large_beer = 6
t_small_beer = 2
@views.route('/', methods=['GET']) @views.route('/', methods=['GET'])
def render(): def render():
return render_template('beertap.html') return render_template('beertap.html')
@@ -11,9 +21,11 @@ def render():
@views.route('/', methods=['POST']) @views.route('/', methods=['POST'])
def choice(): def choice():
if request.form['submit_button'] == 'option_1': if request.form['submit_button'] == 'option_1':
print("Es Grosses uselah!") print("Eis usem lingge Gütterli uselah!")
draw_beer(channel=r_ch1, wait=t_large_beer)
if request.form['submit_button'] == 'option_2': if request.form['submit_button'] == 'option_2':
print("Es Chliises uselah!") print("Eis usem rächte Gütterli uselah!")
draw_beer(channel=r_ch2, wait=t_large_beer)
return render_template('beertap.html') return render_template('beertap.html')
@views.route('/about', methods=['GET']) @views.route('/about', methods=['GET'])

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)

View File

@@ -1,6 +1,8 @@
from beertap import create_app from beertap import create_app
from gpio_handler import gpio_test
app = create_app() app = create_app()
if __name__ == "__main__": if __name__ == "__main__":
gpio_test()
app.run(debug=True) app.run(debug=True)