Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
db29624a45 | ||
|
|
0f2c749f6d | ||
|
|
52f2d0c491 | ||
|
|
2f58c41cc8 |
@@ -2,14 +2,75 @@
|
|||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>Microdot GPIO Example</title>
|
<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>
|
</head>
|
||||||
<body>
|
<body onload="onLoad();">
|
||||||
<h1>Microdot GPIO Example</h1>
|
<div class="container">
|
||||||
<form method="POST" action="">
|
<h1>Microdot GPIO Example</h1>
|
||||||
<p>GPIO Pin: <input type="text" name="pin" size="3"></p>
|
<div class="alert alert-primary" role="alert" id="message">
|
||||||
<input type="submit" name="read" value="Read">
|
</div>
|
||||||
<input type="submit" name="set-low" value="Set Low">
|
<form method="POST" action="">
|
||||||
<input type="submit" name="set-high" value="Set high">
|
<p>
|
||||||
</form>
|
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>
|
||||||
|
<input type="radio" name="pull" value="pulldown" id="pulldown">
|
||||||
|
<label for="pulldown">Pull-Down</label>
|
||||||
|
<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>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -6,14 +6,38 @@ app = Microdot()
|
|||||||
|
|
||||||
@app.route('/', methods=['GET', 'POST'])
|
@app.route('/', methods=['GET', 'POST'])
|
||||||
def index(request):
|
def index(request):
|
||||||
|
form_cookie = None
|
||||||
|
message_cookie = None
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
if 'set-read' in request.form:
|
form_cookie = '{pin},{pull}'.format(pin=request.form['pin'],
|
||||||
pin = machine.Pin(int(request.form['pin']), machine.Pin.IN)
|
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:
|
else:
|
||||||
pin = machine.Pin(int(request.form['pin']), machine.Pin.OUT)
|
pin = machine.Pin(int(request.form['pin']), machine.Pin.OUT)
|
||||||
pin.value(0 if 'set-low' in request.form else 1)
|
value = 0 if 'set-low' in request.form else 1
|
||||||
return redirect('/')
|
pin.value(value)
|
||||||
return send_file('gpio.html')
|
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()
|
app.run()
|
||||||
|
|||||||
39
microdot.py
39
microdot.py
@@ -63,7 +63,7 @@ class Request():
|
|||||||
self.content_type = value
|
self.content_type = value
|
||||||
elif header == 'Cookie':
|
elif header == 'Cookie':
|
||||||
for cookie in self.headers['Cookie'].split(';'):
|
for cookie in self.headers['Cookie'].split(';'):
|
||||||
name, value = cookie.split('=', 1)
|
name, value = cookie.strip().split('=', 1)
|
||||||
self.cookies[name] = value
|
self.cookies[name] = value
|
||||||
|
|
||||||
# body
|
# body
|
||||||
@@ -243,6 +243,7 @@ class URLPattern():
|
|||||||
class Microdot():
|
class Microdot():
|
||||||
def __init__(self) :
|
def __init__(self) :
|
||||||
self.url_map = []
|
self.url_map = []
|
||||||
|
self.error_handlers = {}
|
||||||
|
|
||||||
def route(self, url_pattern, methods=None):
|
def route(self, url_pattern, methods=None):
|
||||||
def decorated(f):
|
def decorated(f):
|
||||||
@@ -251,6 +252,12 @@ class Microdot():
|
|||||||
return f
|
return f
|
||||||
return decorated
|
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):
|
def run(self, host='0.0.0.0', port=5000):
|
||||||
s = socket.socket()
|
s = socket.socket()
|
||||||
ai = socket.getaddrinfo(host, port)
|
ai = socket.getaddrinfo(host, port)
|
||||||
@@ -271,14 +278,28 @@ class Microdot():
|
|||||||
if args is not None:
|
if args is not None:
|
||||||
f = route_handler
|
f = route_handler
|
||||||
break
|
break
|
||||||
if f:
|
try:
|
||||||
resp = f(req, **args)
|
if f:
|
||||||
if isinstance(resp, tuple):
|
resp = f(req, **args)
|
||||||
resp = Response(*resp)
|
elif 404 in self.error_handlers:
|
||||||
elif not isinstance(resp, Response):
|
resp = self.error_handlers[404](req)
|
||||||
resp = Response(resp)
|
else:
|
||||||
resp.write(req.client_stream)
|
resp = 'Not found', 404
|
||||||
req.close()
|
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
|
redirect = Response.redirect
|
||||||
|
|||||||
2
setup.py
2
setup.py
@@ -8,7 +8,7 @@ from setuptools import setup
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='microdot',
|
name='microdot',
|
||||||
version='0.1.1',
|
version='0.2.0',
|
||||||
url='http://github.com/miguelgrinberg/microdot/',
|
url='http://github.com/miguelgrinberg/microdot/',
|
||||||
license='MIT',
|
license='MIT',
|
||||||
author='Miguel Grinberg',
|
author='Miguel Grinberg',
|
||||||
|
|||||||
Reference in New Issue
Block a user