documenting main.py
This commit is contained in:
parent
bd05b3dd02
commit
3a5edaf172
11
main.py
11
main.py
@ -6,6 +6,7 @@ import utime
|
|||||||
from mpu6050 import accel
|
from mpu6050 import accel
|
||||||
import picoweb
|
import picoweb
|
||||||
|
|
||||||
|
# Set Up Screen for output
|
||||||
screen = {}
|
screen = {}
|
||||||
screen['rst'] = Pin(16, Pin.OUT)
|
screen['rst'] = Pin(16, Pin.OUT)
|
||||||
screen['rst'].value(1)
|
screen['rst'].value(1)
|
||||||
@ -18,12 +19,14 @@ oled.fill(0)
|
|||||||
oled.text('Initializing',5,5)
|
oled.text('Initializing',5,5)
|
||||||
oled.show()
|
oled.show()
|
||||||
|
|
||||||
|
# Set up MPU communication
|
||||||
mpu = {}
|
mpu = {}
|
||||||
mpu['scl'] = Pin(19, Pin.OUT, Pin.PULL_UP)
|
mpu['scl'] = Pin(19, Pin.OUT, Pin.PULL_UP)
|
||||||
mpu['sda'] = Pin(23, Pin.OUT, Pin.PULL_UP)
|
mpu['sda'] = Pin(23, Pin.OUT, Pin.PULL_UP)
|
||||||
mpu['i2c'] = I2C(scl=mpu['scl'], sda=mpu['sda'])
|
mpu['i2c'] = I2C(scl=mpu['scl'], sda=mpu['sda'])
|
||||||
accelerometer = accel(mpu['i2c'])
|
accelerometer = accel(mpu['i2c'])
|
||||||
|
|
||||||
|
# Gets Date/Time, maps to EDT.
|
||||||
def date_time():
|
def date_time():
|
||||||
(y,mo,d,wd,h,mi,s,*v) = RTC().datetime()
|
(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)
|
(y,mo,d,h,mi,s,wd,yd) = utime.localtime(utime.mktime((y,mo,d,h,mi,s,wd,0))-4*3600)
|
||||||
@ -32,11 +35,13 @@ def date_time():
|
|||||||
'%02d:%02d:%02d' % (h,mi,s)
|
'%02d:%02d:%02d' % (h,mi,s)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Get the network address of the Wireless Lan
|
||||||
def ip():
|
def ip():
|
||||||
return WLAN(STA_IF).ifconfig()[0]
|
return WLAN(STA_IF).ifconfig()[0]
|
||||||
|
|
||||||
last_pos = 'UN'
|
last_pos = 'UN'
|
||||||
|
|
||||||
|
# Get the current orientation of the 'dice'
|
||||||
def position(x,y,z):
|
def position(x,y,z):
|
||||||
global last_pos
|
global last_pos
|
||||||
if abs(z) > 8192 and abs(y) < 4096 and abs(x) < 4096:
|
if abs(z) > 8192 and abs(y) < 4096 and abs(x) < 4096:
|
||||||
@ -60,6 +65,7 @@ def build_data(arg):
|
|||||||
'position': position(x,y,z)
|
'position': position(x,y,z)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Write the current information to the screen
|
||||||
def output_positions(data):
|
def output_positions(data):
|
||||||
oled.fill(0)
|
oled.fill(0)
|
||||||
oled.text('X: %(x)+05d' % data, 5, 5)
|
oled.text('X: %(x)+05d' % data, 5, 5)
|
||||||
@ -72,15 +78,18 @@ def output_positions(data):
|
|||||||
oled.show()
|
oled.show()
|
||||||
|
|
||||||
current_data = {}
|
current_data = {}
|
||||||
|
|
||||||
|
# Function to update current data and display
|
||||||
def run_cycle():
|
def run_cycle():
|
||||||
global current_data
|
global current_data
|
||||||
current_data = build_data(accelerometer.get_values())
|
current_data = build_data(accelerometer.get_values())
|
||||||
output_positions(current_data)
|
output_positions(current_data)
|
||||||
|
|
||||||
|
# Here we're setting up a timer to call run_cycle every 1000ms (1s)
|
||||||
timer = Timer(0)
|
timer = Timer(0)
|
||||||
timer.init(period=1000, mode=Timer.PERIODIC, callback=lambda t:run_cycle())
|
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 = picoweb.WebApp('main')
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user