4 Commits

Author SHA1 Message Date
Miguel Grinberg
db29624a45 Release 0.2.0 2019-04-19 20:22:18 +01:00
Miguel Grinberg
0f2c749f6d error handlers 2019-04-19 20:21:51 +01:00
Miguel Grinberg
52f2d0c491 fleshed out example GPIO application 2019-04-19 19:35:00 +01:00
Miguel Grinberg
2f58c41cc8 more robust parsing of cookie header 2019-04-19 19:32:47 +01:00
4 changed files with 129 additions and 23 deletions

View File

@@ -2,14 +2,75 @@
<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');
if (form) {
form = form.split(',')
document.getElementById('pin').selectedIndex = parseInt(form[0]);
document.getElementById(form[1]).checked = true;
}
}
</script>
</head>
<body>
<h1>Microdot GPIO Example</h1>
<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">
</form>
<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:
<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()

View File

@@ -63,7 +63,7 @@ class Request():
self.content_type = value
elif header == 'Cookie':
for cookie in self.headers['Cookie'].split(';'):
name, value = cookie.split('=', 1)
name, value = cookie.strip().split('=', 1)
self.cookies[name] = value
# body
@@ -243,6 +243,7 @@ class URLPattern():
class Microdot():
def __init__(self) :
self.url_map = []
self.error_handlers = {}
def route(self, url_pattern, methods=None):
def decorated(f):
@@ -251,6 +252,12 @@ class Microdot():
return f
return decorated
def errorhandler(self, status_code_or_exception_class):
def decorated(f):
self.error_handlers[status_code_or_exception_class] = f
return f
return decorated
def run(self, host='0.0.0.0', port=5000):
s = socket.socket()
ai = socket.getaddrinfo(host, port)
@@ -271,14 +278,28 @@ class Microdot():
if args is not None:
f = route_handler
break
if f:
resp = f(req, **args)
if isinstance(resp, tuple):
resp = Response(*resp)
elif not isinstance(resp, Response):
resp = Response(resp)
resp.write(req.client_stream)
req.close()
try:
if f:
resp = f(req, **args)
elif 404 in self.error_handlers:
resp = self.error_handlers[404](req)
else:
resp = 'Not found', 404
except Exception as exc:
resp = None
if exc.__class__ in self.error_handlers:
try:
resp = self.error_handlers[exc.__class__](req, exc)
except:
pass
if resp is None:
resp = 'Internal server error', 500
if isinstance(resp, tuple):
resp = Response(*resp)
elif not isinstance(resp, Response):
resp = Response(resp)
resp.write(req.client_stream)
req.close()
redirect = Response.redirect

View File

@@ -8,7 +8,7 @@ from setuptools import setup
setup(
name='microdot',
version='0.1.1',
version='0.2.0',
url='http://github.com/miguelgrinberg/microdot/',
license='MIT',
author='Miguel Grinberg',