Switch类 – 开关对象

Switch对象用于控制按钮的开关

用法:

sw = pyb.Switch()       # create a switch object
sw.value()              # get state (True if pressed, False otherwise)
sw()                    # shorthand notation to get the switch state
sw.callback(f)          # register a callback to be called when the
                        #   switch is pressed down
sw.callback(None)       # remove the callback

示例

pyb.Switch().callback(lambda: pyb.LED(1).toggle())

构造函数

class pyb.Switch

创建并返回一个开关对象

方法

Switch.__call__()

调用开关对象,直接获取其状态: True 为按下,False 为未按下。

Switch.value()

获取开关状态: True 为按下,False 为未按下。

Switch.callback(fun)

注册一个回调函数,在开关按下时被调用,如果 funNone 则关闭回调。