fleshed out example GPIO application

This commit is contained in:
Miguel Grinberg
2019-04-19 19:33:43 +01:00
parent 2f58c41cc8
commit 52f2d0c491
2 changed files with 95 additions and 13 deletions

View File

@@ -2,14 +2,72 @@
<html>
<head>
<title>Microdot GPIO Example</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script>
function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2)
return parts.pop().split(";").shift();
}
function showMessage() {
document.getElementById('message').innerHTML = getCookie('message');
}
function onLoad() {
showMessage();
var form = getCookie('form').split(',')
document.getElementById('pin').selectedIndex = parseInt(form[0]);
document.getElementById(form[1]).checked = true;
}
</script>
</head>
<body>
<body onload="onLoad();">
<div class="container">
<h1>Microdot GPIO Example</h1>
<div class="alert alert-primary" role="alert" id="message">
</div>
<form method="POST" action="">
<p>GPIO Pin: <input type="text" name="pin" size="3"></p>
<input type="submit" name="read" value="Read">
<input type="submit" name="set-low" value="Set Low">
<input type="submit" name="set-high" value="Set high">
<p>
GPIO Pin:
<select name="pin" id="pin">
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
</select>
</p>
<div>
<p>
<input type="radio" name="pull" value="pullup" id="pullup">
<label for="pullup">Pull-Up</label>&nbsp;&nbsp;
<input type="radio" name="pull" value="pulldown" id="pulldown">
<label for="pulldown">Pull-Down</label>&nbsp;&nbsp;
<input type="radio" name="pull" value="pullnone" id="pullnone" checked>
<label for="pullnone">None</label>
<br>
<input type="submit" class="btn btn-outline-dark" name="read" value="Read">
</p>
</div>
<div>
<p>
<input type="submit" class="btn btn-outline-dark" name="set-low" value="Set Low">
<input type="submit" class="btn btn-outline-dark" name="set-high" value="Set high">
</p>
</div>
</form>
</div>
</body>
</html>

View File

@@ -6,14 +6,38 @@ app = Microdot()
@app.route('/', methods=['GET', 'POST'])
def index(request):
form_cookie = None
message_cookie = None
if request.method == 'POST':
if 'set-read' in request.form:
pin = machine.Pin(int(request.form['pin']), machine.Pin.IN)
form_cookie = '{pin},{pull}'.format(pin=request.form['pin'],
pull=request.form['pull'])
if 'read' in request.form:
pull = None
if request.form['pull'] == 'pullup':
pull = machine.Pin.PULL_UP
elif request.form['pull'] == 'pulldown':
pull = machine.Pin.PULL_DOWN
pin = machine.Pin(int(request.form['pin']), machine.Pin.IN, pull)
message_cookie = 'Input pin {pin} is {state}.'.format(
pin=request.form['pin'],
state='high' if pin.value() else 'low')
else:
pin = machine.Pin(int(request.form['pin']), machine.Pin.OUT)
pin.value(0 if 'set-low' in request.form else 1)
return redirect('/')
return send_file('gpio.html')
value = 0 if 'set-low' in request.form else 1
pin.value(value)
message_cookie = 'Output pin {pin} is now {state}.'.format(
pin=request.form['pin'],
state='high' if value else 'low')
response = redirect('/')
else:
if 'message' not in request.cookies:
message_cookie = 'Select a pin and an operation below.'
response = send_file('gpio.html')
if form_cookie:
response.set_cookie('form', form_cookie)
if message_cookie:
response.set_cookie('message', message_cookie)
return response
app.run()