Simplifying setup function, using setup function for start and stop endpoints, making HTML valid

This commit is contained in:
2022-10-27 00:50:32 -04:00
parent ffb27664f4
commit 6d63be70c4
2 changed files with 12 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from flask import Flask, render_template, request from flask import Flask, render_template
from paste.translogger import TransLogger from paste.translogger import TransLogger
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
from waitress import serve from waitress import serve
@@ -10,11 +10,8 @@ app = Flask(__name__)
def setup(): def setup():
print('Running setup')
GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BCM)
GPIO.setup(RELAIS_1_GPIO, GPIO.OUT) GPIO.setup(RELAIS_1_GPIO, GPIO.OUT)
GPIO.output(RELAIS_1_GPIO, GPIO.HIGH) # Defaulting to high/off
print('GPIO setup, pin set to off')
@app.route('/') @app.route('/')
@@ -24,16 +21,14 @@ def home():
@app.route('/start') @app.route('/start')
def starthorn(): def starthorn():
GPIO.setmode(GPIO.BCM) setup()
GPIO.setup(RELAIS_1_GPIO, GPIO.OUT)
GPIO.output(RELAIS_1_GPIO, GPIO.LOW) # Toggle switch to low/on GPIO.output(RELAIS_1_GPIO, GPIO.LOW) # Toggle switch to low/on
return('Sending power to the relay switch, hopefully toggling a horn...') return('Sending power to the relay switch, hopefully toggling a horn...')
@app.route('/stop') @app.route('/stop')
def stophorn(): def stophorn():
GPIO.setmode(GPIO.BCM) setup()
GPIO.setup(RELAIS_1_GPIO, GPIO.OUT)
GPIO.output(RELAIS_1_GPIO, GPIO.HIGH) # Toggle switch to high/off GPIO.output(RELAIS_1_GPIO, GPIO.HIGH) # Toggle switch to high/off
GPIO.cleanup() GPIO.cleanup()
return('Ceasing power to the relay switch, hopefully silencing a horn...') return('Ceasing power to the relay switch, hopefully silencing a horn...')
@@ -46,7 +41,10 @@ def destroy():
if __name__ == '__main__': # Program entrance if __name__ == '__main__': # Program entrance
try: try:
print('Running setup')
setup() setup()
GPIO.output(RELAIS_1_GPIO, GPIO.HIGH) # Defaulting to high/off
print('GPIO setup, pin set to off')
serve(TransLogger(app, setup_console_handler=False), listen='*:5000') serve(TransLogger(app, setup_console_handler=False), listen='*:5000')
finally: finally:
destroy() destroy()

View File

@@ -1,7 +1,7 @@
<!doctype html> <!doctype html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>Klaxon horn jump scare button</title> <title>Klaxon horn jump scare button</title>
<script src="http://code.jquery.com/jquery-latest.js"></script> <script src="http://code.jquery.com/jquery-latest.js"></script>
@@ -10,13 +10,16 @@
</head> </head>
<body> <body>
<section class="section hero is-fullheight" style='background-image: url("{{ url_for('static', filename='pumpkin.png' ) }}");'> <section class="section hero is-fullheight" style='background-image: url("{{ url_for('static', filename='pumpkin.png' ) }}");'>
<div class="container has-text-centered"> <div class="container has-text-centered is-large">
<h1 class="title">
Klaxon Horn Jump Scare Control
</h1>
<button class="button is-dark is-large is-fullwidth" id="spoop"> <button class="button is-dark is-large is-fullwidth" id="spoop">
<i class="fa-solid fa-skull m-3"></i> SPOOP <i class="fa-solid fa-bullhorn m-3"></i> <i class="fa-solid fa-skull m-3"></i> SPOOP <i class="fa-solid fa-bullhorn m-3"></i>
</button> </button>
</div> </div>
</section> </section>
<script type='text/javascript'> <script>
var isTouchDevice = 'ontouchstart' in document.documentElement; var isTouchDevice = 'ontouchstart' in document.documentElement;
var starturl = '/start' var starturl = '/start'
var stopurl = '/stop' var stopurl = '/stop'