From 7ec7a8730d303d54d8e0dda155640fa5d4b23daf Mon Sep 17 00:00:00 2001 From: foamyguy Date: Sat, 21 Dec 2024 10:29:12 -0600 Subject: [PATCH] use keypad in button remote example --- examples/wiz_buttons_controller.py | 87 ++++++++++++------------------ 1 file changed, 34 insertions(+), 53 deletions(-) diff --git a/examples/wiz_buttons_controller.py b/examples/wiz_buttons_controller.py index 5d4db25..30b578d 100644 --- a/examples/wiz_buttons_controller.py +++ b/examples/wiz_buttons_controller.py @@ -6,11 +6,9 @@ wired to their own pin. """ -import time - import board +import keypad import wifi -from digitalio import DigitalInOut, Direction, Pull from adafruit_wiz import SCENE_IDS, WizConnectedLight @@ -19,22 +17,10 @@ my_lamp = WizConnectedLight(udp_host, udp_port, wifi.radio, debug=True) -# Basic push buttons initialization -btn_1 = DigitalInOut(board.D11) -btn_1.direction = Direction.INPUT -btn_1.pull = Pull.UP - -btn_2 = DigitalInOut(board.D12) -btn_2.direction = Direction.INPUT -btn_2.pull = Pull.UP - -btn_3 = DigitalInOut(board.A1) -btn_3.direction = Direction.INPUT -btn_3.pull = Pull.UP - -btn_4 = DigitalInOut(board.A0) -btn_4.direction = Direction.INPUT -btn_4.pull = Pull.UP +# Basic push buttons initialization with keypad +buttons = keypad.Keys( + (board.D11, board.D12, board.A1, board.A0), value_when_pressed=False, pull=True +) # list of colors to cycle through colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (0, 255, 255), (255, 0, 255)] @@ -47,37 +33,32 @@ cur_temp_index = 0 while True: - # if btn 1 pressed - if not btn_1.value: - print("Button 1") - # toggle the on/off state - my_lamp.state = not my_lamp.state - time.sleep(0.5) - - # if btn 2 pressed - if not btn_2.value: - print("Button 2") - # set the current RGB color - my_lamp.rgb_color = colors[cur_rgb_index] - # increment the index for next time and wrap around to zero as needed - cur_rgb_index = (cur_rgb_index + 1) % len(colors) - time.sleep(0.5) - - # if btn 3 pressed - if not btn_3.value: - print("Button 3") - # set the current light color temperature - my_lamp.temperature = temperatures[cur_temp_index] - # increment the index for next time and wrap around to zero as needed - cur_temp_index = (cur_temp_index + 1) % len(temperatures) - time.sleep(0.5) - - # if btn 4 pressed - if not btn_4.value: - print("Button 4") - # uncomment to see the available scenes - # print(SCENE_IDS.keys()) - - # set the scene - my_lamp.scene = "Party" - time.sleep(0.5) + # check for button press events + event = buttons.events.get() + if event and event.pressed: + if event.key_number == 0: + print("Button 0") + # toggle the on/off state + my_lamp.state = not my_lamp.state + + elif event.key_number == 1: + print("Button 1") + # set the current RGB color + my_lamp.rgb_color = colors[cur_rgb_index] + # increment the index for next time and wrap around to zero as needed + cur_rgb_index = (cur_rgb_index + 1) % len(colors) + + elif event.key_number == 2: + print("Button 2") + # set the current light color temperature + my_lamp.temperature = temperatures[cur_temp_index] + # increment the index for next time and wrap around to zero as needed + cur_temp_index = (cur_temp_index + 1) % len(temperatures) + + elif event.key_number == 3: + print("Button 3") + # uncomment to see the available scenes + # print(SCENE_IDS.keys()) + + # set the scene + my_lamp.scene = "Party"