|
6 | 6 | wired to their own pin.
|
7 | 7 | """
|
8 | 8 |
|
9 |
| -import time |
10 |
| - |
11 | 9 | import board
|
| 10 | +import keypad |
12 | 11 | import wifi
|
13 |
| -from digitalio import DigitalInOut, Direction, Pull |
14 | 12 |
|
15 | 13 | from adafruit_wiz import SCENE_IDS, WizConnectedLight
|
16 | 14 |
|
|
19 | 17 |
|
20 | 18 | my_lamp = WizConnectedLight(udp_host, udp_port, wifi.radio, debug=True)
|
21 | 19 |
|
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 | +) |
38 | 24 |
|
39 | 25 | # list of colors to cycle through
|
40 | 26 | colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (0, 255, 255), (255, 0, 255)]
|
|
47 | 33 | cur_temp_index = 0
|
48 | 34 |
|
49 | 35 | 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