diff --git a/main.py b/main.py index 506b667..b57bdd9 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,7 @@ import ssd1306 import time import utime from mpu6050 import accel +import picoweb screen = {} screen['rst'] = Pin(16, Pin.OUT) @@ -26,24 +27,73 @@ accelerometer = accel(mpu['i2c']) def date_time(): (y,mo,d,wd,h,mi,s,*v) = RTC().datetime() (y,mo,d,h,mi,s,wd,yd) = utime.localtime(utime.mktime((y,mo,d,h,mi,s,wd,0))-4*3600) - return ('%02d/%02d/%02d' % (mo,d,y-2000), '%02d:%02d:%02d' % (h,mi,s)) + return ( + '%02d/%02d/%02d' % (mo,d,y-2000), + '%02d:%02d:%02d' % (h,mi,s) + ) def ip(): return WLAN(STA_IF).ifconfig()[0] -def output_positions(arg): +last_pos = 'UN' + +def position(x,y,z): + global last_pos + if abs(z) > 8192 and abs(y) < 4096 and abs(x) < 4096: + last_pos = 'TOP' if z > 0 else 'BOT' + if abs(y) > 8192 and abs(z) < 4096 and abs(x) < 4096: + last_pos = 'RGT' if y > 0 else 'LFT' + if abs(x) > 8192 and abs(z) < 4096 and abs(y) < 4096: + last_pos = 'FRO' if x > 0 else 'BAK' + return last_pos + +def build_data(arg): + [x,y,z] = [arg[idx] for idx in ['AcX','AcY','AcZ']] + (date, time) = date_time() + return { + 'x': x, + 'y': y, + 'z': z, + 'date': date, + 'time': time, + 'ip': ip(), + 'position': position(x,y,z) + } + +def output_positions(data): oled.fill(0) - oled.text('X: %(AcX)+05d' % arg, 5, 5) - oled.text('Y: %(AcY)+05d' % arg, 5, 15) - oled.text('Z: %(AcZ)+05d' % arg, 5, 25) - (d,t) = date_time() - oled.text(d,5,35) - oled.text(t,5,45) - oled.text(ip(),5,55) + oled.text('X: %(x)+05d' % data, 5, 5) + oled.text('Y: %(y)+05d' % data, 5, 15) + oled.text('Z: %(z)+05d' % data, 5, 25) + oled.text(data['date'],5,35) + oled.text(data['position'],80,35) + oled.text(data['time'],5,45) + oled.text(data['ip'],5,55) oled.show() +current_data = {} + def run_cycle(): - output_positions(accelerometer.get_values()) + global current_data + current_data = build_data(accelerometer.get_values()) + output_positions(current_data) timer = Timer(0) timer.init(period=1000, mode=Timer.PERIODIC, callback=lambda t:run_cycle()) + +app = picoweb.WebApp('main') + +@app.route("/") +def index(req,resp): + yield from picoweb.start_response(resp) + yield from resp.awrite(""" +
| X | %(x)+05d |
|---|---|
| Y | %(y)+05d |
| Z | %(z)+05d |
| Position | %(position)s |