Пример #1
0
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()
Пример #2
0
class LED(HomieNode):

    # Reversed values for the esp8266 boards onboard led
    ONOFF = {FALSE: 1, TRUE: 0}

    def __init__(self, name="Onboard LED", pin=0):
        super().__init__(id="led", name=name, type="LED")
        self.led = Pin(pin, Pin.OUT, value=1)

        # Boot button on some dev boards
        self.btn = Pushbutton(Pin(pin, Pin.IN, Pin.PULL_UP))
        self.btn.press_func(self.toggle_led)

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

    def on_power_msg(self, topic, payload, retained):
        self.led(self.ONOFF[payload])

    def toggle_led(self):
        if self.p_power.value == TRUE:
            self.led(1)
            self.p_power.value = False
        else:
            self.led(0)
            self.p_power.value = True
Пример #3
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()
Пример #4
0
    """
    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'
        if 0 <= player < len(players_led):