BBC MicroPython 中是否有事件回调?

Are there event callbacks in BBC MicroPython?

我正在尝试将以下内容从 Javascript 翻译成 micro:bit 的 MicroPython。这是从 block 翻译成 Javascript.

的 inventor's kit 中的代码示例 3
let light_state = 0
# how do you do this bit?
input.onPinPressed(TouchPin.P0, () => {
    if (light_state == 0) {
        light_state = 1
    } else {
        light_state = 0
    }
})
basic.forever(() => {
    if (light_state == 1) {
        pins.analogWritePin(AnalogPin.P2, pins.analogReadPin(AnalogPin.P1))
    } else {
        pins.digitalWritePin(DigitalPin.P2, 0)
    }
})

我不知道如何将 input.onPinPressed 作为回调事件甚至 lambda 执行。我能想到的最好办法就是反复轮询 pin0。

from microbit import *

light_on = False
while True:
    if pin0.is_touched():
        light_on = not light_on        
        if light_on:
            aval = pin1.read_analog()
            pin2.write_analog(aval)
        else:
            pin2.write_digital(0)

我在 MicroPython 文档中看到了开关回调,但我没有遇到 micro:bit 引脚的任何事件回调。是否有此功能的任何示例代码,即使它没有记录?

编辑:我对代码进行了更正-之前的MicroPython翻译导致LED不断闪烁。

The reply from the micro:bit forum

The MicroPython micro:bit API was designed primarily for teaching and use by school children, and it was decided not to include callbacks in the API because they can lead to complicated bugs. Instead you will need to poll the pin.