def test_btn(suppress=False, lf=True, df=True): s = ''' press pulses red release pulses green double click pulses yellow long press pulses blue ''' print('Test of pushbutton scheduling coroutines.') print(helptext) print(s) pin = Pin('X1', Pin.IN, Pin.PULL_UP) red = LED(1) green = LED(2) yellow = LED(3) blue = LED(4) pb = Pushbutton(pin, suppress) pb.press_func(pulse, (red, 1000)) pb.release_func(pulse, (green, 1000)) if df: print('Doubleclick enabled') pb.double_func(pulse, (yellow, 1000)) if lf: print('Long press enabled') pb.long_func(pulse, (blue, 1000)) run()
async def __setup_button(self): hw.log.debug("Setting up system button ...") pin = Pin(hw.SYS_BUTTON_PIN, Pin.IN, Pin.PULL_UP) pb = Pushbutton(pin) pb.long_func(self.__long_press) pb.double_func(self.__double_press) create_task(self.__state_clean())
def test_btn(suppress=False, lf=True, df=True): pin = Pin(2, Pin.IN, Pin.PULL_UP) pb = Pushbutton(pin, suppress) if df: print('Doubleclick enabled') pb.double_func(double_press, ()) if lf: print('Long press enabled') pb.long_func(long_press, ()) run()
class SmartSocket(HomieNode): def __init__(self): super().__init__(id="relay", name="Wifi Power Socket", type="OW8266-02Q") self.led = Pin(4, Pin.OUT, value=1) self.r_on = Pin(12, Pin.OUT) self.r_off = Pin(5, Pin.OUT) self.p_power = HomieProperty( id="power", name="Relay", settable=True, retained=True, datatype=BOOLEAN, default=FALSE, restore=True, on_message=self.on_power_msg, ) self.add_property(self.p_power) self.button = Pushbutton(Pin(14, Pin.IN, Pin.PULL_UP)) self.button.release_func(self.toggle, ()) self.button.long_func(reset, (self.led, )) async def off(self): self.r_off(0) await asyncio.sleep_ms(100) self.r_on(1) async def on(self): self.r_on(0) await asyncio.sleep_ms(100) self.r_off(1) def on_power_msg(self, topic, payload, retained): if payload == FALSE: self.off() elif payload == TRUE: self.on() async def toggle(self): if self.p_power.value == TRUE: await self.off() self.p_power.value = FALSE else: await self.on() self.p_power.value = TRUE
class SmartSocket(HomieNode): def __init__(self, name="Relay 16A"): super().__init__(id="relay", name=name, type="Gosund SP1") # disable REPL so we can use the blue led uos.dupterm(None, 1) self.led_b = Pin(1, Pin.OUT, value=1) # Blue LED self.led_r = Pin(13, Pin.OUT, value=1) # Red LED self.relay = Pin(14, Pin.OUT) self.p_power = HomieProperty( id="power", name="Power", settable=True, datatype=BOOLEAN, default=FALSE, on_message=self.on_power_msg, ) self.add_property(self.p_power) self.button = Pushbutton(Pin(3, Pin.IN)) self.button.release_func(self.toggle, ()) self.button.long_func(reset, (self.led_r, )) def off(self): self.relay(0) self.led_b(0) self.led_r(1) def on(self): self.relay(1) self.led_b(1) self.led_r(0) def on_power_msg(self, topic, payload, retained): if payload == FALSE: self.off() elif payload == TRUE: self.on() def toggle(self): if self.p_power.value == TRUE: self.off() self.p_power.value = FALSE else: self.on() self.p_power.value = TRUE
def btn_dynamic(): s = ''' press pulses red release pulses green double click pulses yellow long press changes button functions. ''' print('Test of pushbutton scheduling coroutines.') print(helptext) print(s) pin = Pin('X1', Pin.IN, Pin.PULL_UP) red = LED(1) green = LED(2) yellow = LED(3) blue = LED(4) pb = Pushbutton(pin) setup(pb, red, green, yellow, None) pb.long_func(setup, (pb, blue, red, green, yellow, 2000)) run()
def test_btncb(): s = ''' press toggles red release toggles green double click toggles yellow long press toggles blue ''' print('Test of pushbutton executing callbacks.') print(helptext) print(s) pin = Pin('X1', Pin.IN, Pin.PULL_UP) red = LED(1) green = LED(2) yellow = LED(3) blue = LED(4) pb = Pushbutton(pin) pb.press_func(toggle, (red,)) pb.release_func(toggle, (green,)) pb.double_func(toggle, (yellow,)) pb.long_func(toggle, (blue,)) run()
def test_btncb(): pin = Pin(2, Pin.IN, Pin.PULL_UP) pb = Pushbutton(pin) pb.double_func(double_press, ()) pb.long_func(long_press, ()) run()
async def __setup_button(): pin = Pin(32, Pin.IN, Pin.PULL_UP) pb = Pushbutton(pin) pb.long_func(__long_press) pb.double_func(__double_press)