2位拨码开关类,配置开关的触发事件并随时读取开关即时状态。
初始化对象,设置引脚和触发电平(高电平或低电平触发)。pins 为 IO 引脚数组。
返回当前开关的按下状态数组,2个元素,其值为 true 或 false。
为当前开关注册监听对象。observer 为监听对象。当开关状态改变时会触发 observer 对象的 on_dip_switch_2bit_status_changed(self, status) 方法。status 为该触发发生时的开关的状态数组,2个元素,其值为 true 或 false。
为当前开关注销监听对象。observer 为监听对象。
调用方法:
self.dip_switch = entities.DipSwitch2Bit([PINS.DIP_SWITCH_1, PINS.DIP_SWITCH_2], GPIO.LOW) dip_switch_status_changed_handler = None def on_dip_switch_2bit_status_changed(self, status): #print('on_dip_switch_2bit_status_changed') if self.dip_switch_status_changed_handler is not None: self.dip_switch_status_changed_handler(status) def dip_switch_status_changed_handler(status): ''' called while the status of dip switch changed :param status: current status :return: void ''' print('on_dip_switch_status_changed:') print(status) pass SAKS.dip_switch_status_changed_handler = dip_switch_status_changed_handler