Skip to content

Commit 4488669

Browse files
authored
Merge pull request #3 from FoamyGuy/use_keypad_in_btn_example
use keypad in button remote example
2 parents 844de23 + 7ec7a87 commit 4488669

File tree

1 file changed

+34
-53
lines changed

1 file changed

+34
-53
lines changed

examples/wiz_buttons_controller.py

+34-53
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
wired to their own pin.
77
"""
88

9-
import time
10-
119
import board
10+
import keypad
1211
import wifi
13-
from digitalio import DigitalInOut, Direction, Pull
1412

1513
from adafruit_wiz import SCENE_IDS, WizConnectedLight
1614

@@ -19,22 +17,10 @@
1917

2018
my_lamp = WizConnectedLight(udp_host, udp_port, wifi.radio, debug=True)
2119

22-
# Basic push buttons initialization
23-
btn_1 = DigitalInOut(board.D11)
24-
btn_1.direction = Direction.INPUT
25-
btn_1.pull = Pull.UP
26-
27-
btn_2 = DigitalInOut(board.D12)
28-
btn_2.direction = Direction.INPUT
29-
btn_2.pull = Pull.UP
30-
31-
btn_3 = DigitalInOut(board.A1)
32-
btn_3.direction = Direction.INPUT
33-
btn_3.pull = Pull.UP
34-
35-
btn_4 = DigitalInOut(board.A0)
36-
btn_4.direction = Direction.INPUT
37-
btn_4.pull = Pull.UP
20+
# Basic push buttons initialization with keypad
21+
buttons = keypad.Keys(
22+
(board.D11, board.D12, board.A1, board.A0), value_when_pressed=False, pull=True
23+
)
3824

3925
# list of colors to cycle through
4026
colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (0, 255, 255), (255, 0, 255)]
@@ -47,37 +33,32 @@
4733
cur_temp_index = 0
4834

4935
while True:
50-
# if btn 1 pressed
51-
if not btn_1.value:
52-
print("Button 1")
53-
# toggle the on/off state
54-
my_lamp.state = not my_lamp.state
55-
time.sleep(0.5)
56-
57-
# if btn 2 pressed
58-
if not btn_2.value:
59-
print("Button 2")
60-
# set the current RGB color
61-
my_lamp.rgb_color = colors[cur_rgb_index]
62-
# increment the index for next time and wrap around to zero as needed
63-
cur_rgb_index = (cur_rgb_index + 1) % len(colors)
64-
time.sleep(0.5)
65-
66-
# if btn 3 pressed
67-
if not btn_3.value:
68-
print("Button 3")
69-
# set the current light color temperature
70-
my_lamp.temperature = temperatures[cur_temp_index]
71-
# increment the index for next time and wrap around to zero as needed
72-
cur_temp_index = (cur_temp_index + 1) % len(temperatures)
73-
time.sleep(0.5)
74-
75-
# if btn 4 pressed
76-
if not btn_4.value:
77-
print("Button 4")
78-
# uncomment to see the available scenes
79-
# print(SCENE_IDS.keys())
80-
81-
# set the scene
82-
my_lamp.scene = "Party"
83-
time.sleep(0.5)
36+
# check for button press events
37+
event = buttons.events.get()
38+
if event and event.pressed:
39+
if event.key_number == 0:
40+
print("Button 0")
41+
# toggle the on/off state
42+
my_lamp.state = not my_lamp.state
43+
44+
elif event.key_number == 1:
45+
print("Button 1")
46+
# set the current RGB color
47+
my_lamp.rgb_color = colors[cur_rgb_index]
48+
# increment the index for next time and wrap around to zero as needed
49+
cur_rgb_index = (cur_rgb_index + 1) % len(colors)
50+
51+
elif event.key_number == 2:
52+
print("Button 2")
53+
# set the current light color temperature
54+
my_lamp.temperature = temperatures[cur_temp_index]
55+
# increment the index for next time and wrap around to zero as needed
56+
cur_temp_index = (cur_temp_index + 1) % len(temperatures)
57+
58+
elif event.key_number == 3:
59+
print("Button 3")
60+
# uncomment to see the available scenes
61+
# print(SCENE_IDS.keys())
62+
63+
# set the scene
64+
my_lamp.scene = "Party"

0 commit comments

Comments
 (0)