Microbit有两个按键,在主板上用A和B标记, 按键A对应引脚5, 按键B对应引脚11.
针对按键编写程序, 就可以在按下按键的时候触发程序, 这样可以实现控制或者类似于事件一样的机制.
microbit命名空间中有两个特殊的名称 button_a 和 button_b 用来指代两个按键. 然后提供了一系列函数:
# 按键是否被按下 button_a.is_pressed() # 按键是否被按过 button_a.was_pressed() # 获取按键的情况 button_a.get_presses()
下边就来看一下如何使用这些方法针对按键编程.
按键
按键按下, 如果是专门针对按键的程序, 可以使用 button_a.is_pressed()
方法, 在一个循环中检测, 比如:
from microbit import * while True: if button_a.is_pressed() and button_b.is_pressed(): display.show(Image.GIRAFFE) elif button_a.is_pressed(): display.show(Image.PACMAN) elif button_b.is_pressed(): break; display.show(Image.SWORD) display.clear()
这是一个同时按下两个按键, 显示长颈鹿, 单独按A显示吃豆人, 单独按B退出的程序.
button_a.was_pressed()
是按过之后返回True:
from microbit import * while True: if button_a.was_pressed(): display.show(Image.PACMAN) elif button_b.was_pressed(): break; else: display.show(Image.TORTOISE) sleep(3000) display.show(Image.SWORD) sleep(1000) display.clear()
这个代码正常的时候显示乌龟, 然后睡眠3秒钟, 这3秒钟之内如果按了A, 下一次循环的时候就会进入显示吃豆人的分支, 如果按了B就会进入结束分支.
最后一个get_presses()
是返回一段时间内按键的次数, 这会被记录在某一个地方:
from microbit import * while True: sleep(5000) display.scroll(str(button_a.get_presses()))
加速度计
内置的加速度计可以检测microbit在三个方向上的加速度. 其x表示的是正对LED阵列时候的左右方向. 而y表示上下方向, z表示垂直于microbit所在平面的方向.
使用accelerometer对象即可获取这三个方向上的读数, 可以单独读取也可以读成一个tuple:
while True: x = accelerometer.get_x() y = accelerometer.get_y() z = accelerometer.get_z() display.scroll(str(x)) sleep(500) while True: result = accelerometer.get_values() print("Values:", result) sleep(500)
这里的print()需要结合MU 编辑器来使用, 可以实时打印出检测到的结果. 实际尝试了一下, 首先把程序写入Microbit, 然后打开REPL界面, 之后重启Microbit, 就可以正常显示了.
还有手势, 这个是什么意思, 看一下代码:
from microbit import * last_gesture = "" while True: current_gesture = accelerometer.current_gesture() sleep(100) if current_gesture is not last_gesture: last_gesture = current_gesture print('>{g:s}<'.format(g=current_gesture))
原来这个就是可以显示face up, face down, 左和右等Microbit的姿态.
姿态名称有:
up down left right face up face down freefall shake 3g 6g 8g
可以直接用字符串来判断是否是指定的姿势:
from microbit import * last_gesture = "" while True: current_gesture = accelerometer.current_gesture() if current_gesture == 'face up': display.show(Image.HAPPY) else: display.show(Image.ANGRY)
也可以直接使用accelerometer.is_gesture("face up")
来进行判断.
from microbit import * gestList = [] while True: gestures = accelerometer.get_gestures() print(len(gestures)) if len(gestures) == 1: gestList.append(gestures[0]) sleep(500) print("History: "+str(gestList))
还可以记录手势历史. 可以使用一些特别的玩意.
罗盘
罗盘设备的名称是compass, microbit的罗盘是磁力罗盘, 类似于真正的指南针一样. 可以读出X和Y的值. 罗盘的名称是compass.
calibrate()
方法会启动校准程序, 将Microbit拿在手里绕圈, 直到LED阵列全部变红即可.
读出x和y的值:
from microbit import * compass.calibrate() while True: x = compass.get_x() y = compass.get_y() print("x reading: ", x, ", y reading: ", y) sleep(500)
会打印出实际的数值, 如果要获取具体角度, 则需要进行数学运算, 北方是0:
import math from microbit import * compass.calibrate() while True: x = compass.get_x() y = compass.get_y() angle = math.atan2(y,x) *180/math.pi print("x", x, " y", y) print("Direction: ", angle) sleep(500)
可以使用compass.heading()
来直接得到角度. 如果需要校准, 则会返回 -1004:
from microbit import * while True: heading = compass.heading() print("heading: ", heading) sleep(500)
不过还是不大能搞明白方向和数值之间的关系, 抄了一个显示北方的函数:
while True: sleep(100) needle = ((15 - compass.heading()) // 30) % 12 display.show(Image.ALL_CLOCKS[needle])
在水平持有Microbit的时候, 指针指向的方位就是北方.
播放曲子和编写曲子
Microbit的0号引脚可以发出声音信号, 将喇叭的正极连接到Microbit的0号引脚, 负极连接到GND, 耳机也可以.
要播放音乐, 需要导入 micropython的music库. 然后播放音乐:
from microbit import * import music music.play(music.BIRTHDAY, loop=True)
我的鳄鱼夹耳机接口还没到, 所以只能先把代码记录下来.
此外还可以指定具体的引脚, 比如:
music.play(music.BIRTHDAY, pin=pin1, loop=True)
就需要将线连接到1号引脚.
还可以编写曲子, music.play()
除了可以播放music对象中存储好的音乐, 还可以具体来播放指定的音. 规则如下:
- 可以使用单个字符 ‘CDEFGAB’ 来播放指定的音, 默认播放的是中央C开始的7个音
- Microbit支持四组音高, 分别从2-5, 用音名+数字来表示,
music.play('c3')
就是低八度的中央C, 如果不加数字, 默认就是4. - 升降调用小写的’b’和’#’号来表示, 加在音名之后, 这样Microbit实际上能模拟一个简单的4个音域的键盘了.
- 设置节奏, 可以设置一分钟多少拍, 也就是bpm. 使用
music.set_tempo(bpm=180)
- 还可以设置一个tick,
music.set_tempo(ticks=8)
表示一个拍可以分成8份. tick是电子乐器中计量最短时间的一个单位, 可以不用设置. - 设置一个音演奏几拍(duration), 可以使用:
music.play('C4:3')
, 默认一个音演奏四拍. music.pitch(440, 1000)
,用来直接演奏指定的频率, 比如440赫兹就是中央C, 第二个参数是毫秒表示的时间长度. 如果要一直播放, 可以设置为-1.
有了这些操作, 就可以演奏音乐了, 不过由于Microbit无法演奏和弦, 所以表现力还是不太行.
节奏可以通过tempo = music.get_tempo()
获取.
调用music.reset()
可以将music相关的所有设置复原, 默认的tick=4, bpm=120, duration=4, octave=4
有了这个东西, 加上播放器, 似乎可以自己来做点音乐播放一下了. 不过不知道Microbit能不能蓝牙播放…
剩下基本上就是看一下无线设备如何使用了.