600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > mpython掌控板编程_MicroPython动手做(20)——掌控板之三轴加速度

mpython掌控板编程_MicroPython动手做(20)——掌控板之三轴加速度

时间:2018-08-01 20:22:41

相关推荐

mpython掌控板编程_MicroPython动手做(20)——掌控板之三轴加速度

10、使用“摇晃”指令的计步器

#MicroPython动手做(20)——掌控板之三轴加速度

#使用“摇晃”指令的计步器

[mw_shl_code=arduino,true]#MicroPython动手做(20)——掌控板之三轴加速度

#使用“摇晃”指令的计步器

from mpython import *

import framebuf

import font.digiface_30

import time

def on_button_a_down(_):

global a, b

time.sleep_ms(10)

if button_a.value() == 1: return

a = 1

def on_button_b_down(_):

global a, b

time.sleep_ms(10)

if button_b.value() == 1: return

a = 0

from machine import Timer

_is_shaked = _is_thrown = False

_last_x = _last_y = _last_z = _count_shaked = _count_thrown = 0

def on_shaked():pass

def on_thrown():pass

tim11 = Timer(11)

def timer11_tick(_):

global _is_shaked, _is_thrown, _last_x, _last_y, _last_z, _count_shaked, _count_thrown

if _is_shaked:

_count_shaked += 1

if _count_shaked == 5: _count_shaked = 0

if _is_thrown:

_count_thrown += 1

if _count_thrown == 10: _count_thrown = 0

if _count_thrown > 0: return

x=accelerometer.get_x(); y=accelerometer.get_y(); z=accelerometer.get_z()

_is_thrown = (x * x + y * y + z * z < 0.25)

if _is_thrown: on_thrown();return

if _last_x == 0 and _last_y == 0 and _last_z == 0:

_last_x = x; _last_y = y; _last_z = z; return

diff_x = x - _last_x; diff_y = y - _last_y; diff_z = z - _last_z

_last_x = x; _last_y = y; _last_z = z

if _count_shaked > 0: return

_is_shaked = (diff_x * diff_x + diff_y * diff_y + diff_z * diff_z > 1)

if _is_shaked: on_shaked()

tim11.init(period=100, mode=Timer.PERIODIC, callback=timer11_tick)

def on_shaked():

global a, b

if a == 1:

b = b + 1

def display_font(_font, _str, _x, _y, _wrap, _z=0):

_start = _x

for _c in _str:

_d = _font.get_ch(_c)

if _wrap and _x > 128 - _d[2]: _x = _start; _y += _d[1]

if _c == '1' and _z > 0: oled.fill_rect(_x, _y, _d[2], _d[1], 0)

oled.blit(framebuf.FrameBuffer(bytearray(_d[0]), _d[2], _d[1],

framebuf.MONO_HLSB), (_x+int(_d[2]/_z)) if _c=='1' and _z>0 else _x, _y)

_x += _d[2]

button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)

button_b.irq(trigger=Pin.IRQ_FALLING, handler=on_button_b_down)

a = 0

b = 0

while True:

if a == 0:

oled.fill(0)

oled.DispChar('计步器', 0, 0, 1)

oled.DispChar('每天1万步,活出好身体', 0, 16, 1)

oled.DispChar('按下A键开始计步', 0, 32, 1)

oled.DispChar('按下B键步数清零', 0, 48, 1)

oled.show()

b = 0

elif a == 1:

oled.fill(0)

oled.DispChar('步数', 0, 15, 1)

display_font(font.digiface_30, (str(b)), 30, 10, False, 2)

oled.show()

if b <= 10000:

oled.fill(0)

oled.DispChar((''.join([str(x) for x in ['还差', 10000 - b, '加油加油!']])), 0, 48, 1)

oled.show()

else:

oled.fill(0)

oled.DispChar('目标完成,你真棒!', 0, 48, 1)

oled.show()

time.sleep(1)[/mw_shl_code]

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。