# HG changeset patch # User Matt Johnston # Date 1432137669 -28800 # Node ID c9b20d3d393abe482f6996ee0ff8e8519b038be5 # Parent 2d46d7f1ccf0d4c90a445b4e53845d84d3b6a47b# Parent 2caee09f41c4008f6df66c73eec0987050ed432e merge diff -r 2caee09f41c4 -r c9b20d3d393a py/gpio_rpi.py --- a/py/gpio_rpi.py Wed May 20 00:03:53 2015 +0800 +++ b/py/gpio_rpi.py Thu May 21 00:01:09 2015 +0800 @@ -7,15 +7,43 @@ __all__ = ["Gpio"] class Gpio(object): - def __init__(self, pin, name): - self.pin = pin - self.name = name - GPIO.setmode(GPIO.BOARD) - GPIO.setup(self.pin, GPIO.OUT) + SYS_GPIO_BASE = '/sys/class/gpio/gpio' + def __init__(self, pin, name): + self.pin = pin + self.name = name + + dir_fn = '%s%d/direction' % (self.SYS_GPIO_BASE, pin) + with open(dir_fn, 'w') as f: + # make sure it doesn't start "on" + f.write('low') + val_fn = '%s%d/value' % (self.SYS_GPIO_BASE, pin) + self.value_file = open(val_fn, 'r+') + + def turn(self, value): + self.value_file.seek(0) + self.value_file.write('1' if value else '0') + self.value_file.flush() - def turn(self, value): - self.state = bool(value) - GPIO.output(self.pin, self.state) + def get_state(self): + self.value_file.seek(0) + buf = self.value_file.read().strip() + if buf == '0': + return False + if buf != '1': + E("Bad value read from gpio '%s': '%s'" + % (self.value_file.name, buf)) + return True + - def get_state(self): - return GPIO.input(self.pin) +def main(): + g = Gpio(17, 'f') + g.turn(1) + + print(g.get_state()) + + g.turn(0) + + print(g.get_state()) + +if __name__ == '__main__': + main() diff -r 2caee09f41c4 -r c9b20d3d393a web/config.py --- a/web/config.py Wed May 20 00:03:53 2015 +0800 +++ b/web/config.py Thu May 21 00:01:09 2015 +0800 @@ -16,9 +16,11 @@ UPDATE_URL = 'http://evil.ucc.asn.au/~matt/templog/update' -GRAPH_WIDTH = 1200 -GRAPH_HEIGHT = 600 +GRAPH_WIDTH = 600 +GRAPH_HEIGHT = 700 ZOOM = 1 +# determine by viewing the image +GRAPH_LEFT_MARGIN = 65 LINE_WIDTH = 2 @@ -50,8 +52,6 @@ GRAPH_FONT = "Prociono" #GRAPH_FONT = "URW Gothic L" -# determine by zooming in an image viewer -GRAPH_LEFT_MARGIN = 63 # 1 hour CSRF_TIMEOUT = 3600 diff -r 2caee09f41c4 -r c9b20d3d393a web/templog.py --- a/web/templog.py Wed May 20 00:03:53 2015 +0800 +++ b/web/templog.py Thu May 21 00:01:09 2015 +0800 @@ -51,15 +51,22 @@ return "OK" +def make_graph(length, end): + length_minutes = int(length) + end = datetime.strptime(end, DATE_FORMAT) + start = end - timedelta(minutes=length_minutes) + + start_epoch = time.mktime(start.timetuple()) + return log.graph_png(start_epoch, length_minutes * 60) + +def encode_data(data, mimetype): + return 'data:%s;base64,%s' % (mimetype, binascii.b2a_base64(data).rstrip()) + + @route('/graph.png') def graph(): - length_minutes = int(request.query.length) - end = datetime.strptime(request.query.end, DATE_FORMAT) - start = end - timedelta(minutes=length_minutes) - response.set_header('Content-Type', 'image/png') - start_epoch = time.mktime(start.timetuple()) - return log.graph_png(start_epoch, length_minutes * 60) + return make_graph(request.query.length, request.query.end) @route('/set/update', method='post') def set_update(): @@ -121,9 +128,12 @@ request.query.replace('end', end.strftime(DATE_FORMAT)) urlparams = urllib.urlencode(request.query) + graphdata = encode_data(make_graph(request.query.length, request.query.end), 'image/png') return bottle.template('top', urlparams=urlparams, end = end.strftime(DATE_FORMAT), - length = minutes) + length = minutes, + graphwidth = config.GRAPH_WIDTH, + graphdata = graphdata) @route('/debug') def debuglog(): diff -r 2caee09f41c4 -r c9b20d3d393a web/views/top.tpl --- a/web/views/top.tpl Wed May 20 00:03:53 2015 +0800 +++ b/web/views/top.tpl Thu May 21 00:01:09 2015 +0800 @@ -2,6 +2,7 @@ Wort Temperature Log +