109 lines
2.9 KiB
Python
109 lines
2.9 KiB
Python
from machine import I2C, Pin, Timer, RTC
|
|
from network import STA_IF, WLAN
|
|
import ssd1306
|
|
import time
|
|
import utime
|
|
from mpu6050 import accel
|
|
import picoweb
|
|
|
|
# Set Up Screen for output
|
|
screen = {}
|
|
screen['rst'] = Pin(16, Pin.OUT)
|
|
screen['rst'].value(1)
|
|
screen['scl'] = Pin(15, Pin.OUT, Pin.PULL_UP)
|
|
screen['sda'] = Pin(4, Pin.OUT, Pin.PULL_UP)
|
|
screen['i2c'] = I2C(scl=screen['scl'], sda=screen['sda'], freq=450000)
|
|
oled = ssd1306.SSD1306_I2C(128, 64, screen['i2c'], addr=0x3c)
|
|
|
|
oled.fill(0)
|
|
oled.text('Initializing',5,5)
|
|
oled.show()
|
|
|
|
# Set up MPU communication
|
|
mpu = {}
|
|
mpu['scl'] = Pin(19, Pin.OUT, Pin.PULL_UP)
|
|
mpu['sda'] = Pin(23, Pin.OUT, Pin.PULL_UP)
|
|
mpu['i2c'] = I2C(scl=mpu['scl'], sda=mpu['sda'])
|
|
accelerometer = accel(mpu['i2c'])
|
|
|
|
# Gets Date/Time, maps to EDT.
|
|
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)
|
|
)
|
|
|
|
# Get the network address of the Wireless Lan
|
|
def ip():
|
|
return WLAN(STA_IF).ifconfig()[0]
|
|
|
|
last_pos = 'UN'
|
|
|
|
# Get the current orientation of the 'dice'
|
|
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)
|
|
}
|
|
|
|
# Write the current information to the screen
|
|
def output_positions(data):
|
|
oled.fill(0)
|
|
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 = {}
|
|
|
|
# Function to update current data and display
|
|
def run_cycle():
|
|
global current_data
|
|
current_data = build_data(accelerometer.get_values())
|
|
output_positions(current_data)
|
|
|
|
# Here we're setting up a timer to call run_cycle every 1000ms (1s)
|
|
timer = Timer(0)
|
|
timer.init(period=1000, mode=Timer.PERIODIC, callback=lambda t:run_cycle())
|
|
|
|
# This is setting up the web application to report values
|
|
app = picoweb.WebApp('main')
|
|
|
|
@app.route("/")
|
|
def index(req,resp):
|
|
yield from picoweb.start_response(resp)
|
|
yield from resp.awrite("""
|
|
<table>
|
|
<tr><th>X</th><td>%(x)+05d</td></tr>
|
|
<tr><th>Y</th><td>%(y)+05d</td></tr>
|
|
<tr><th>Z</th><td>%(z)+05d</td></tr>
|
|
<tr><th>Position</th><td>%(position)s</td></tr>
|
|
</table>
|
|
""" % current_data)
|
|
|
|
app.run(debug=True,host=ip(),port=80)
|
|
|