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)) loop = asyncio.get_event_loop() loop.run_until_complete(killer())
def test_btncb(): print('Test of pushbutton executing callbacks.') print(helptext) 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, )) loop = asyncio.get_event_loop() loop.run_until_complete(killer())
def test_btn(): print('Test of pushbutton scheduling coroutines.') print(helptext) 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(pulse, (red, 1000)) pb.release_func(pulse, (green, 1000)) pb.double_func(pulse, (yellow, 1000)) pb.long_func(pulse, (blue, 1000)) loop = asyncio.get_event_loop() loop.run_until_complete(killer())
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.relay_property = HomieNodeProperty( id="power", name="Relay", settable=True, retained=True, datatype="bool", default=FALSE, restore=True, ) self.add_property(self.relay_property) self.button = Pushbutton(Pin(14, Pin.IN, Pin.PULL_UP)) self.button.release_func(self.toggle, ()) self.button.long_func(reset, (self.led, )) def off(self): self.r_off(0) sleep_ms(100) self.r_on(1) self.relay_property.data = FALSE def on(self): self.r_on(0) sleep_ms(100) self.r_off(1) self.relay_property.data = TRUE def callback(self, topic, payload, retained): if b"power" in topic: if payload == FALSE: self.off() elif payload == TRUE: self.on() def toggle(self): if self.relay_property.data == TRUE: self.off() else: self.on()
class SmartSocket(HomieNode): def __init__(self): super().__init__(id="relay", name="Relay 16A", type="Gosund SP1 v23") uos.dupterm(None, 1) # disable REPL so we can use the blue led self.led_b = Pin(1, Pin.OUT, value=1) self.led_r = Pin(13, Pin.OUT, value=1) self.relay = Pin(14, Pin.OUT) self.relay_property = HomieNodeProperty( id="power", name="Relay", settable=True, retained=True, datatype="boolean", default=FALSE, restore=True, ) self.add_property(self.relay_property) 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) self.relay_property.data = FALSE def on(self): self.relay(1) self.led_b(1) self.led_r(0) self.relay_property.data = TRUE def callback(self, topic, payload, retained): if b"power" in topic: if payload == FALSE: self.off() elif payload == TRUE: self.on() def toggle(self): if self.relay_property.data == TRUE: self.off() else: self.on()
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.power_property = HomieNodeProperty( id="power", name="Power", settable=True, datatype=BOOLEAN, default=FALSE, ) self.add_property(self.power_property, self.on_power_msg) 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) self.power_property.data = FALSE def on(self): self.relay(1) self.led_b(1) self.led_r(0) self.power_property.data = TRUE def on_power_msg(self, topic, payload, retained): if payload == FALSE: self.off() elif payload == TRUE: self.on() def toggle(self): if self.power_property.data == TRUE: self.off() else: self.on()
async def main(): global thermo set_global_exception() # Debug aid switch = Pushbutton(sw, suppress=True) switch.release_func(switch_display, ()) uasyncio.create_task(thermo.blink()) # Or you might do this #uasyncio.create_task(thermo.temp()) uasyncio.create_task(thermo.temp_thermo()) uasyncio.create_task(thermo.encoder_loop(enc)) uasyncio.create_task(thermo.calc_diff()) uasyncio.create_task(thermo.alert_check()) uasyncio.create_task(thermo.alert_light()) uasyncio.create_task(thermo.show_lcd_initial() ) #makes sure that something shows up initially #await thermo.cancel() # Non-terminating method await thermo.temp()
async def test_btn(pin, suppress=True, lf=True, df=True): ''' handler for aswitch.Pushbutton instances ''' pb = Pushbutton(pin, suppress) print('{} set up'.format(pin)) pb.press_func(button_operation, ( str(pin), "pushed", )) pb.release_func(button_operation, ( str(pin), "released", )) pb.double_func(button_operation, ( str(pin), "double-clicked", )) pb.long_func(button_operation, ( str(pin), "long pressed", ))
class ScooterDisplay(object): def __init__(self, screens): self.screens = screens if not self.screens: self.screens = [DemoScreen()] self._screen = 0 # OLED spi = SPI(2, baudrate=14500000, sck=Pin(PIN_DISPLAY_CLK), mosi=Pin(PIN_DISPLAY_MOSI)) self.display = SafeDisplay(spi, dc=Pin(PIN_DISPLAY_DC), cs=Pin(PIN_DISPLAY_CS), rst=Pin(PIN_DISPLAY_RST)) loop = asyncio.get_event_loop() loop.create_task(self.update_display()) # Encoder rotation self.encoder = RotaryEncoder(PIN_KNOB_CLK, PIN_KNOB_DT, cw=self.encoder_cw, ccw=self.encoder_ccw) # Encoder button self.button = Pushbutton(Pin(PIN_KNOB_SWITCH, Pin.IN)) self.button.release_func(self.encoder_push) self.button.double_func(self.encoder_dblpush) self.button.long_func(self.encoder_longpush) def encoder_cw(self, steps): print("DISPLAY: Encoder CW, {} steps".format(steps)) self._screen += 1 if self._screen > len(self.screens) - 1: self._screen = 0 def encoder_ccw(self, steps): print("DISPLAY: Encoder CCW, {} steps".format(steps)) self._screen -= 1 if self._screen < 0: self._screen = len(self.screens) - 1 def encoder_push(self): print("DISPLAY: Encoder PUSH") self.screens[self._screen].encoder_click() def encoder_dblpush(self): print("DISPLAY: Encoder DBLPUSH") self.screens[self._screen].encoder_dblclick() def encoder_longpush(self): print("DISPLAY: Encoder LONGPUSH") self.screens[self._screen].encoder_longpress() async def update_display(self): current_screen = self._screen screen_changed = True needs_pixel_shift = False while True: if screen_changed or needs_pixel_shift: self.display.clear() if needs_pixel_shift: self.screens[self._screen].pixel_shift() self.screens[self._screen].update(self.display, needs_full_redraw=screen_changed or needs_pixel_shift) current_screen = self._screen await asyncio.sleep_ms(1000) screen_changed = current_screen != self._screen needs_pixel_shift = self.screens[self._screen].needs_pixel_shift()
class switch_plugin: inputtype = "normal" # Default switch type datastore = None # Place where plugin data is stored for reboots def __init__(self): # generic section self._log = core._log self._log.debug("Plugin: switch contruction") self._utils = core._utils self._plugins = core._plugins self._hal = core._hal self._lock = Event() self.dxpin = dxpin # plugin specific section self.valuenames = {} self.valuenames["valueN1"] = "switch" self.valuenames["valueF1"] = "" self.valuenames["valueD1"] = 0 # release lock, ready for next measurement self._lock.clear() def init(self, plugin, device, queue, scriptqueue): self._log.debug("Plugin: switch init") # generic section self._utils.plugin_initdata(self, plugin, device, queue, scriptqueue) self.content = plugin.get('content', content) plugin['dtype'] = dtype plugin['stype'] = stype plugin['template'] = template datastore = self._plugins.readstore(self.devicename) # plugin specific section self.switch_status = bootstate if self.inputtype == 'normal': self._log.debug("Plugin: switch init normal, pin: " + self.dxpin) # Setup switch self.swpin = self._hal.pin(self.dxpin, core.PIN_IN, core.PIN_PULL_UP) self.switch = Switch(self.swpin) # Register coros to launch on contact close and open self.switch.close_func(self.asyncswitchopen) self.switch.open_func(self.asyncswitchclosed) elif self.inputtype == 'low': self._log.debug("Plugin: switch init low, pin: " + self.dxpin) # Setup button active low self.swpin = self._hal.pin(self.dxpin, core.PIN_IN, core.PIN_PULL_UP) self.switch = Pushbutton(self.swpin) self.switch.press_func(self.asyncbuttonpress) self.switch.release_func(self.asyncbuttonrelease) self.switch.double_func(self.asyncbuttondouble) self.switch.long_func(self.asyncbuttonlong) else: self._log.debug("Plugin: switch init high, pin: " + self.dxpin) # Setup button active high self.swpin = self._hal.pin(self.dxpin, core.PIN_IN, core.PIN_PULL_DOWN) self.switch = Pushbutton(self.swpin) self.switch.press_func(self.asyncbuttonpress) self.switch.release_func(self.asyncbuttonrelease) self.switch.double_func(self.asyncbuttondouble) self.switch.long_func(self.asyncbuttonlong) return True def loadform(self, plugindata): self._log.debug("Plugin: switch loadform") # generic section self._utils.plugin_loadform(self, plugindata) # plugin specific section plugindata['inputtype'] = self.inputtype plugindata['dxpin0'] = self.dxpin def saveform(self, plugindata): self._log.debug("Plugin: switch saveform") # generic section self._utils.plugin_saveform(self, plugindata) # plugin specific section self.inputtype = plugindata['inputtype'] self.dxpin = plugindata['dxpin0'] # store values data = {} data["inputtype"] = self.inputtype data["dxpin"] = self.dxpin data["valueN1"] = self.valuenames["valueN1"] data["valueF1"] = self.valuenames["valueF1"] data["valueD1"] = self.valuenames["valueD1"] self._plugins.writestore(self.devicename, data) if self.inputtype == 'normal': # Setup switch self.swpin = self._hal.pin(self.dxpin, core.PIN_IN, core.PIN_PULL_UP) self.switch = Switch(self.swpin) # Register coros to launch on contact close and open self.switch.close_func(self.asyncswitchopen) self.switch.open_func(self.asyncswitchclosed) elif self.inputtype == 'low': # Setup button active low self.swpin = self._hal.pin(self.dxpin, core.PIN_IN, core.PIN_PULL_UP) self.switch = Pushbutton(self.swpin) self.switch.press_func(self.asyncbuttonpress) self.switch.release_func(self.asyncbuttonrelease) self.switch.double_func(self.asyncbuttondouble) self.switch.long_func(self.asyncbuttonlong) else: # Setup button active high self.swpin = self._hal.pin(self.dxpin, core.PIN_IN, core.PIN_PULL_DOWN) self.switch.press_func(self.asyncbuttonpress) self.switch.release_func(self.asyncbuttonrelease) self.switch.double_func(self.asyncbuttondouble) self.switch.long_func(self.asyncbuttonlong) def read(self, values): self._log.debug("Plugin: switch read") # generic section values['valueN1'] = self.valuenames["valueN1"] values['valueD1'] = self.switch_status def write(self, values): self._log.debug("Plugin: switch write") async def asyncprocess(self): self._log.debug("Plugin: switch process") # plugin specific section # If a switch occured if self.switch_status: # send data to protocol and script/rule queues self.valuenames["valueD1"] = self.switch_status self._utils.plugin_senddata(self) # erase status self.switch_status = "" # release lock, ready for next measurement self._lock.clear() # #CUSTOM SENSOR CODE... # async def asyncswitchopen(self): self.switch_status = 'open' # release lock, ready for next measurement self._lock.clear() async def asyncswitchclosed(self): self.switch_status = 'closed' # release lock, ready for next measurement self._lock.clear() async def asyncbuttonpress(self): self.switch_status = 'press' # release lock, ready for next measurement self._lock.clear() async def asyncbuttonrelease(self): self.switch_status = 'release' # release lock, ready for next measurement self._lock.clear() async def asyncbuttondouble(self): self.switch_status = 'double' # release lock, ready for next measurement self._lock.clear() async def asyncbuttonlong(self): self.switch_status = 'long' # release lock, ready for next measurement self._lock.clear()