API 速览
与硬件直接交互的功能都集中在 bibo 模块中。为方便使用,建议在脚本开头写入:
from bibo import *
下文默认你已这样导入。
顶层函数
可直接调用的函数:
# 休眠指定毫秒数
sleep(ms)
# 返回自 micro:bit 上次开机以来的毫秒数
running_time()
# 返回摄氏温度
temperature()
# 将数值从一个范围映射到另一个范围
scale(value_to_convert, from_=(min_value, max_value), to=(min_value, max_value))
# 进入 panic 模式(通常因内存耗尽触发,屏幕会显示 sad 表情);error_code 可为任意整数
panic(error_code)
# 重启 micro:bit
reset()
# 设置 micro:bit 扬声器及边缘连接器外接扬声器/耳机的输出音量(0–255)
set_volume(128)
其余功能通过 bibo 模块中的对象与类提供,详见各专题页面。
按钮
板载两个按钮:
button_a
button_b
常用方法:
# 调用时按钮是否被按下
button.is_pressed()
# 自开机或上次调用以来是否曾被按下
button.was_pressed()
# 返回累计按压次数,并将计数清零
button.get_presses()
显示屏
LED 点阵通过 display 对象访问:
# 读取像素 (x,y) 亮度,0(灭)到 9(最亮)
display.get_pixel(x, y)
# 设置像素亮度
display.set_pixel(x, y, val)
# 清空屏幕
display.clear()
# 显示图像
display.show(image, delay=0, wait=True, loop=False, clear=False)
# 依次显示可迭代对象中的图像或字母,间隔 delay 毫秒
display.show(iterable, delay=400, wait=True, loop=False, clear=False)
# 滚动显示字符串
display.scroll(string, delay=400)
声音事件
声音事件描述麦克风听到的声音变化:
# 从 quiet 到 loud 的过渡,如拍手、喊叫
SoundEvent.LOUD = SoundEvent('loud')
# 从 loud 到 quiet 的过渡,如说话结束、背景音减弱
SoundEvent.QUIET = SoundEvent('quiet')
详见 麦克风。
麦克风
通过 microphone 对象访问:
# 最近一次记录的声音事件名称
microphone.current_event()
# 自上次调用以来是否至少发生过一次指定事件
microphone.was_event(event)
# 返回事件历史元组(最新在最后),并清空历史
microphone.get_events()
# 设置阈值 0–255,例如 set_threshold(SoundEvent.LOUD, 250) 仅在很响时触发
microphone.set_threshold(event, threshold)
# 返回 0–255 范围内的声压级
microphone.sound_level()
引脚
边缘连接器引脚、板载 Logo 与扬声器引脚提供数字/模拟输入输出。部分引脚与 LED 矩阵、按钮内部相连。
引脚对象位于 bibo 模块顶层,例如:
pin0…pin16pin19、pin20pin_logo(Logo 触摸)pin_speaker(扬声器)
MicroBitPin 类常用 API:
pin.write_digital(value) # 0、1、False、True
pin.read_digital() # 返回 0 或 1
pin.write_analog(value) # 0–1023
pin.read_analog() # 返回 0–1023
pin.set_analog_period(int) # PWM 周期(毫秒)
pin.set_analog_period_microseconds(int) # PWM 周期(微秒)
pin.is_touched() # 触摸引脚 0、1、2 及 pin_logo
pin.set_touch_mode(value) # RESISTIVE 或 CAPACITIVE
pin_speaker 与 MicroBitPin 类似,但不支持 is_touched()。
图像
image = Image() # 空 5×5 图像
image = Image('90009:09090:00900:09090:90009:') # 从字符串创建
image = Image(width, height) # 指定尺寸空图像
image = Image(width, height, buffer) # 从缓冲区创建
image.width()
image.height()
image.set_pixel(x, y, value)
image.get_pixel(x, y)
image.shift_left(n)
image.shift_right(n)
image.shift_up(n)
image.shift_down(n)
repr(image)
str(image)
image + image # 叠加
image * n # 亮度缩放
内置图像包括 Image.HEART、Image.HAPPY、Image.ARROW_N 等;时钟与箭头序列见 Image.ALL_CLOCKS、Image.ALL_ARROWS。完整列表见 图像。
加速度计
accelerometer.get_x() # X 轴,单位 milli-g
accelerometer.get_y()
accelerometer.get_z()
accelerometer.get_values() # (X, Y, Z) 元组
accelerometer.current_gesture()
accelerometer.is_gesture(name)
accelerometer.was_gesture(name)
accelerometer.get_gestures()
可识别手势:up、down、left、right、face up、face down、freefall、3g、6g、8g、shake。
指南针
compass.calibrate()
compass.heading() # 相对「北」的度数
compass.get_field_strength()
compass.is_calibrated()
compass.clear_calibration()
I²C
i2c.read(addr, n, repeat=False)
i2c.write(addr, buf, repeat=False)
内置音效
可通过 bibo 模块访问,并用 audio 模块播放:
Sound.GIGGLE、Sound.HAPPY、Sound.HELLO、Sound.MYSTERIOUS、Sound.SAD、Sound.SLIDE、Sound.SOARING、Sound.SPRING、Sound.TWINKLE、Sound.YAWN
扬声器
内置扬声器默认开启:
speaker.off()
speaker.on()
speaker.is_on()
UART
与串口设备通信:
uart.init() # 默认 pin0(TX)、pin1(RX),波特率 9600
uart.any()
uart.read(n)
uart.read()
uart.readline()
uart.readinto(buffer)
uart.write(buffer)