#MicroPython动手做(15)——掌控板之AB按键
#A、B键控制图片转换显示
from mpython import *
import music
def callback_button_a_pressed():
oled.fill(0)
bmp_wZZv = bytearray()
oled.Bitmap(0, 0, bmp_wZZv, 128, 64, 1)
oled.show()
def callback_button_b_pressed():
oled.fill(0)
bmp_h61P = bytearray()
oled.Bitmap(0, 0, bmp_h61P, 128, 64, 1)
oled.show()
def on_button_a_pressed():
while True:
if button_a.value() == 0:
yield callback_button_a_pressed()
else:
yield
def on_button_b_pressed():
while True:
if button_b.value() == 0:
yield callback_button_b_pressed()
else:
yield
func_button_a_pressed = on_button_a_pressed()
func_button_b_pressed = on_button_b_pressed()
while True:
next(func_button_a_pressed)
next(func_button_b_pressed) Mind+ 图形编程
11、AB按键切换声光值与三轴数值
#MicroPython动手做(15)——掌控板之AB按键
#AB按键切换声光值与三轴数值
#MicroPython动手做(15)——掌控板之AB按键
#AB按键切换显示声光值与三轴数值
from mpython import *
import time
def on_button_a_down(_):
global sensor, switch
time.sleep_ms(10)
if button_a.value() == 1: return
switch = switch + -1
if switch < 0:
switch = 2
def on_button_b_down(_):
global sensor, switch
time.sleep_ms(10)
if button_b.value() == 1: return
switch = switch + 1
if switch > 2:
switch = 0
button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)
button_b.irq(trigger=Pin.IRQ_FALLING, handler=on_button_b_down)
switch = 0
oled.fill(0)
oled.DispChar("上一个", 0, 0, 1)
oled.DispChar("下一个", 91, 0, 1)
while True:
oled.fill_rect(0, 16, 128, 48, 0)
if switch == 1:
oled.DispChar((str("声音值:") + str(sound.read())), 30, 28, 1)
elif switch == 2:
oled.DispChar((str(" 光线值:") + str(light.read())), 25, 28, 1)
else:
oled.DispChar((str("X 轴:") + str(accelerometer.get_x())), 15, 15, 1)
oled.DispChar((str("Y 轴:") + str(accelerometer.get_y())), 15, 30, 1)
oled.DispChar((str("Z 轴:") + str(accelerometer.get_z())), 15, 45, 1)
oled.show() mPython X 图形编程
12、AB按键秒表计时器
秒表主要是用来统计秒数的。它有一般有两个按键。A键和B键。
第一次按下A键时,开始计时。
第二次按下A键时,暂停计时。
再按下A键时,继续上一步暂停的计时。
按下B键,归零。
#MicroPython动手做(15)——掌控板之AB按键
#AB按键秒表计时器
#MicroPython动手做(15)——掌控板之AB按键
#AB按键秒表计时器from mpython import *
import framebuf
import font.digiface_44
import time
from machine import Timer
def timer1_tick(_):
global aaa, ttt
ttt = ttt + 1
oled.fill(0)
display_font(font.digiface_44, (''.join(])), 2, 8, False, 2)
oled.show()
tim1 = Timer(1)
def on_button_a_down(_):
global aaa, ttt
time.sleep_ms(10)
if button_a.value() == 1: return
if aaa == 1:
aaa = 0
tim1.init(period=1000, mode=Timer.PERIODIC, callback=timer1_tick)
else:
aaa = 1
tim1.deinit()
def on_button_b_down(_):
global aaa, ttt
time.sleep_ms(10)
if button_b.value() == 1: return
ttt = 0
oled.fill(0)
display_font(font.digiface_44, '00:00', 2, 8, False, 2)
oled.show()
aaa = 1
tim1.deinit()
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: _x = _start; _y += _d
if _c == '1' and _z > 0: oled.fill_rect(_x, _y, _d, _d, 0)
oled.blit(framebuf.FrameBuffer(bytearray(_d), _d, _d,
framebuf.MONO_HLSB), (_x+int(_d/_z)) if _c=='1' and _z>0 else _x, _y)
_x += _d
button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)
button_b.irq(trigger=Pin.IRQ_FALLING, handler=on_button_b_down)
oled.fill(0)
display_font(font.digiface_44, '00:00', 2, 8, False, 2)
oled.show()
ttt = 0
aaa = 1 mPython 图形编程
页:
1
[2]