Beispiel #1
0
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()
Beispiel #2
0
 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())
Beispiel #3
0
    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, ))
Beispiel #4
0
    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, ))
Beispiel #5
0
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()
    def __init__(self, id, name, pin_up, pin_down, leds):
        super().__init__(id=id, name=name, type="colorlight")
        pin_up.init(mode=Pin.IN, pull=Pin.PULL_UP)
        pin_down.init(mode=Pin.IN, pull=Pin.PULL_UP)
        button_up = Pushbutton(pin_up, suppress=True, sense=1)
        button_down = Pushbutton(pin_down, suppress=True, sense=1)
        self.buttons = (button_up, button_down)
        self.on_buttons_pressed = [None, None]
        self.on_buttons_released = [None, None]
        self.leds = leds

        self.properties_button_pressed = [None, None]
        for i, button_name in enumerate(self.button_names):
            self.properties_button_pressed[i] = HomieProperty(
                id="button_{0}_pressed".format(button_name),
                name="Knöpgen \"{0}\" gedrückt".format(button_name),
                settable=True,
                default=FALSE,
                on_message=self._on_button_pressed_msg,
                datatype=BOOLEAN,
            )
            self.add_property(self.properties_button_pressed[i])
            self.buttons[i].press_func(self._on_buttons_pressed, args=(i, ))
            self.buttons[i].release_func(self._on_buttons_released, args=(i, ))

        self._power = False
        self.property_power = HomieProperty(
            id="power",
            name="Power",
            settable=True,
            datatype=BOOLEAN,
            default=FALSE,
            on_message=self.on_power_msg,
        )
        self.add_property(self.property_power)

        self.brightness = 0.7
        self.property_brightness = HomieProperty(
            id="brightness",
            name="Helligkeit",
            settable=True,
            datatype=FLOAT,
            default=70,
            format="0:100",
            unit="%",
            on_message=self.on_brightness_msg,
        )
        self.add_property(self.property_brightness)

        self.anims = dict([(a.name, (a, i + 1)) for i, a in enumerate(ANIMS)])
        self._animation_num = 0
        self._animation = None
        self.property_animation = HomieProperty(
            id="animation",
            name="Animation",
            settable=True,
            datatype=ENUM,
            format=",".join(["-"] + list(self.anims.keys())),
            default="-",
            on_message=self.on_change_anim_msg,
        )
        self.add_property(self.property_animation)

        self.color = (0, 0, 0)
        self.property_color = HomieProperty(
            id="color",
            name="Solide Farbe",
            settable=True,
            datatype=COLOR,
            format=RGB,
            default="0,0,0",
            on_message=self.on_change_color_msg,
        )
        self.add_property(self.property_color)

        self.change = False
        asyncio.create_task(self._update_data_async())
Beispiel #7
0
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()
Beispiel #8
0
def reset_state():
    """
    Reset the buttons light and allow another press
    """
    global locked  # Prevent creating local variable
    locked = False
    reset_lights()


def reset():
    reset_state()
    # send reset message
    notification_queue.put_nowait("r")


pb_reset = Pushbutton(reset_pin)
pb_reset.press_func(reset)

# Instanciate and attach button to the asyncio button manager
for i, button in enumerate(players_buzzer):
    pb = Pushbutton(button, suppress=True)
    pb.press_func(button_press, (i, ))


def receive_callback(topic: bytes, msg: bytes, retained):
    global locked
    print((topic, msg, retained))
    if msg == b'reset':
        reset_state()
    elif msg.startswith(b'on') and len(msg) == 4:
        player = msg[3] - 48  # 48 = char '0'
Beispiel #9
0
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)