Micro:bit参考

Micro:bit参考 > Micropython API > Microbit Module(模块)

Microbit 模块

The microbit module gives you access to all the hardware that is built-in into your board.
microbit 模块允许您访问电路板中内置的所有硬件。

Functions 函数

microbit.panic(n)

Enter a panic mode that stops all execution, scrolls an error code in the micro:bit display and requires restart:
进入一个 panic 模式,停止所有执行,在 micro:bit 显示屏中滚动错误代码并要求重启:

microbit.panic(255) 
Parameters:

n – An arbitrary integer between 0 and 255 to indicate an error code.
n – 介于 0 和 255 之间的任意整数,用于指示错误代码。


microbit.reset()

Restart the board.
重新启动开发板。


microbit.running_time()

Returns:

The number of milliseconds since the board was switched on or restarted.
自板打开或重新启动以来的毫秒数。


microbit.scale(valuefrom_to)

Converts a value from a range to another range.
将一个值从一个范围转换为另一个范围。

For example, to convert 30 degrees from Celsius to Fahrenheit:
例如,要将 30 度从摄氏度转换为华氏度:

temp_fahrenheit = scale(30, from_=(0.0, 100.0), to=(32.0, 212.0)) 

This can be useful to convert values between inputs and outputs, for example an accelerometer x value to a speaker volume.
这对于在输入和输出之间转换值(例如,将加速度计 x 值转换为扬声器音量)非常有用。

If one of the numbers in the to parameter is a floating point (i.e a decimal number like 10.0), this function will return a floating point number. If they are both integers (i.e 10), it will return an integer:
如果 to 参数中的一个数字是浮点数(即像 10.0 这样的十进制数),则此函数将返回浮点数。如果它们都是整数(即 10),它将返回一个整数:

returns_int = scale(accelerometer.get_x(), from_=(-2000, 2000), to=(0, 255)) 

Negative scaling is also supported, for example scale(25, from_=(0, 100), to=(0, -200)) will return -50.
还支持负缩放,例如 scale(25, from_=(0, 100), to=(0, -200)) 将返回 -50。

Parameters:
  • value – A number to convert.
    value – 要转换的数字。

  • from – A tuple to define the range to convert from.
    from — 用于定义要转换的范围的元组。

  • to – A tuple to define the range to convert to.
    to — 用于定义要转换为的范围的元组。

Returns:

The value converted to the to range.
转换为 to 范围的值 。


microbit.set_volume(volume)

(V2 only) Configure the output volume of the micro:bit speaker and pins:
( 仅限 V2)配置 micro:bit 扬声器和引脚的输出音量:

microbit.set_volume(127) 
Parameters:

volume – An integer between 0 and 255 to set the volume.
volume – 一个介于 0 和 255 之间的整数,用于设置音量。


microbit.sleep(n)

Wait for n milliseconds. One second is 1000 milliseconds, so microbit.sleep(1000) will pause the execution for one second.
等待 n 毫秒。1 秒是 1000 毫秒,因此 microbit.sleep(1000) 将暂停执行 1 秒。

Parameters:

n – An integer or floating point number indicating the number of milliseconds to wait.
n – 一个整数或浮点数,指示要等待的毫秒数。


microbit.run_every(callbackdays=Noneh=Nonemin=Nones=Nonems=None)

Schedule to run a function at the interval specified by the time arguments.
计划以 time 参数指定的间隔运行函数。

run_every can be used in two ways:
run_every 可以通过两种方式使用:

  • As a Decorator - placed on top of the function to schedule. For example:
    作为 Decorator - 放置在函数顶部以调度。例如:

    @run_every(days=1, h=1, min=20, s=30, ms=50) 
    def my_function():
        # Do something here 
  • As a Function - passing the callback as a positional argument. For example:
    作为 Function - 将回调作为位置参数传递。例如:

    def my_function():
        # Do something here 
    run_every(my_function, s=30) 

Each argument corresponds to a different time unit and they are additive. So run_every(min=1, s=30) schedules the callback every minute and a half.
每个参数对应于不同的时间单位,并且它们是累加的。因此 ,run_every(min=1, s=30) 安排一次回调。

When an exception is thrown inside the callback function it deschedules the function. To avoid this you can catch exceptions with try/except.
当在回调函数中引发异常时,它会取消调度该函数。为避免这种情况,您可以使用 try/except 捕获异常。

Parameters:
  • callback – Function to call at the provided interval.
    callback – 在提供的时间间隔内调用的函数。

  • days – Sets the days mark for the scheduling.
    days – 设置计划的天数标记。

  • h – Sets the hour mark for the scheduling.
    h – 设置计划的小时标记。

  • min – Sets the minute mark for the scheduling.
    min – 设置计划的分钟标记。

  • s – Sets the second mark for the scheduling.
    s – 设置计划的秒标记。

  • ms – Sets the millisecond mark for the scheduling.
    ms – 设置计划的毫秒标记。


microbit.temperature()

Returns:

An integer with the temperature of the micro:bit in degrees Celcius.
一个整数,其中包含 micro:bit 的温度,以摄氏度为单位。