Created
April 21, 2018 15:15
-
-
Save devSarry/73cc2dc26b784f091827d4a4f4de71f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This file is executed on every boot (including wake-boot from deepsleep) | |
from machine import Pin,I2C | |
from mpu9250 import MPU9250 | |
import ssd1306 | |
import time | |
# Initilize i2c | |
i2c = I2C(scl=Pin(4), sda=Pin(5), freq=1000000) | |
# Setup | |
imu = MPU9250(i2c) | |
lcd=ssd1306.SSD1306_I2C(128, 64, i2c) | |
lcd.text('starting....',0,0) | |
lcd.show() | |
time.sleep(2) | |
button = Pin(16, Pin.IN) #create Button object | |
SENSOR_STATE = 0 | |
def changeSensor(v): | |
global SENSOR_STATE | |
time.sleep_ms(50) | |
if(button.value() == 0): #When the button is not pressed, return | |
return | |
while(button.value() == 1): #If press the button, it will wait until released | |
time.sleep_ms(100) | |
time.sleep_ms(100) | |
SENSOR_STATE = (SENSOR_STATE + 1) % 3 | |
print("SENSOR_STATE: ",SENSOR_STATE) | |
while True: | |
try: | |
button.irq(trigger=Pin.IRQ_RISING, handler=changeSensor) #init irq,set interrupt on rising edge,and callback function is func | |
while True: | |
lcd.fill(0) | |
if SENSOR_STATE == 0: | |
x,y,z = imu.accel.xyz | |
elevation = imu.accel.elevation | |
inclination = imu.accel.inclination | |
lcd.text("accel_x %.4f " % x, 0, 0) | |
lcd.text("accel_y %.4f " % y, 0, 10) | |
lcd.text("accel_z %.4f " % z, 0, 20) | |
lcd.text("elevation %.4f " % elevation, 0, 30) | |
lcd.text("inclination %.4f " % inclination, 0, 40) | |
elif SENSOR_STATE == 1: | |
x,y,z = imu.gyro.xyz | |
lcd.text("gyro_x %.4f " % x, 0, 0) | |
lcd.text("gyro_y %.4f " % y, 0, 10) | |
lcd.text("gyro_z %.4f " % z, 0, 20) | |
else: | |
x,y,z = imu.mag.xyz | |
lcd.text("mag_x %.4f " % x, 0, 0) | |
lcd.text("mag_y %.4f " % y, 0, 10) | |
lcd.text("mag_z %.4f " % z, 0, 20) | |
lcd.show() | |
time.sleep_ms(100) | |
pass | |
except: | |
machine.disable_irq() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment