Skip to content

Commit a41e41b

Browse files
committed
Improvements to the multi-plugin example
* adds reset button logic * adds support for upt to 16 plugs Add ability to configure up to 16 plugs Add factory reset via GPIO Use bsp instead of device_hal for button initialization Check if reset button IO pin # conflicts with plugs Update default IO to be usable accross all supported targets Updates readme Adds reset_gpio out arg. Fixed error code usage. Fixed non-static variable names
1 parent 305d81f commit a41e41b

File tree

7 files changed

+347
-33
lines changed

7 files changed

+347
-33
lines changed

examples/multiple_on_off_plugin_units/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ include(${ESP_MATTER_PATH}/examples/common/cmake_common/components_include.cmake
1919
set(EXTRA_COMPONENT_DIRS
2020
"${MATTER_SDK_PATH}/config/esp32/components"
2121
"${ESP_MATTER_PATH}/components"
22+
"${ESP_MATTER_PATH}/examples/common"
2223
${extra_components_dirs_append})
2324

2425
project(multiple_on_off_plugin_units)

examples/multiple_on_off_plugin_units/README.md

+27-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,33 @@ To update the existing CONFIG_GPIO_PLUG values, follow these steps:
1111
1. Run the following command to access the configuration menu:
1212
`idf.py menuconfig`
1313
1. Navigate to the "Plugin manager" menu.
14-
1. Update the GPIO pin number values (**Use only available GPIO pins as per the target chip**).
15-
16-
17-
You can update maximum configurable plugin units from same menu.
14+
1. Update the GPIO pin number used for the factory reset button and plug output (**Use only available GPIO pins as per the target chip**).
15+
16+
You can update the number of plugin units from same menu.
17+
18+
The following table defines the default GPIO pin numbers for each supported target device.
19+
20+
| IO Function | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S3 |
21+
|----------------------|-------|----------|----------|----------|----------|----------|
22+
| Factory Reset Button | 0 | 9 | 9 | 9 | 9 | 0 |
23+
| Plug 1 | 2 | 2 | 2 | 2 | 2 | 2 |
24+
| Plug 2 | 4 | 4 | 4 | 4 | 4 | 4 |
25+
| Plug 3 | 5 | 5 | 5 | 5 | 5 | 5 |
26+
| Plug 4 | 12 | 6 | 6 | 12 | 12 | 12 |
27+
| Plug 5 | 13 | 7 | 7 | 13 | 13 | 13 |
28+
| Plug 6 | 14 | 8 | 8 | 15 | 14 | 14 |
29+
| Plug 7 | 15 | 10 | 10 | 18 | 22 | 15 |
30+
| Plug 8 | 16 | 18 | 18 | 19 | 25 | 16 |
31+
| Plug 9 | 17 | 0 | 19 | 20 | 26 | 17 |
32+
| Plug 10 | 18 | 1 | 0 | 21 | 27 | 18 |
33+
| Plug 11 | 19 | 3 | 1 | 22 | 0 | 19 |
34+
| Plug 12 | 21 | | 3 | 23 | 1 | 20 |
35+
| Plug 13 | 22 | | | 0 | 3 | 21 |
36+
| Plug 14 | 23 | | | 1 | 8 | 35 |
37+
| Plug 15 | 25 | | | 3 | 10 | 36 |
38+
| Plug 16 | 26 | | | 6 | 11 | 37 |
39+
40+
**Note**: ESP32-C2 and ESP32-C3 do not have enough IO to use all 16 plugs
1841

1942
See the [docs](https://docs.espressif.com/projects/esp-matter/en/latest/esp32/developing.html) for more information about building and flashing the firmware.
2043

Original file line numberDiff line numberDiff line change
@@ -1,25 +1,174 @@
11
menu "Plugin manager"
2-
config MAX_CONFIGURABLE_PLUGS
3-
int "Maximum virtual configurable plugs"
4-
default 5
2+
config USER_BUTTON
3+
bool "Use custom GPIO for factory reset button"
4+
default n
5+
6+
config USER_BUTTON_GPIO
7+
int "GPIO pin number for factory reset button"
8+
default 0 if IDF_TARGET_ESP32 || IDF_TARGET_ESP32S3
9+
default 9
10+
depends on USER_BUTTON
11+
12+
config USER_BUTTON_LEVEL
13+
int "GPIO level when factory reset button is active"
14+
default 0
15+
range 0 1
16+
depends on USER_BUTTON
17+
18+
config NUM_VIRTUAL_PLUGS
19+
int "Number of virtual plugs"
20+
default 3
21+
range 1 11 if IDF_TARGET_ESP32C2
22+
range 1 12 if IDF_TARGET_ESP32C3
523
range 1 16
624

725
config GPIO_PLUG_1
826
int "GPIO pin number for plug 1"
9-
default 3 if IDF_TARGET_ESP32S3
10-
default 2 if !IDF_TARGET_ESP32S3
27+
default 2
28+
depends on (NUM_VIRTUAL_PLUGS >= 1)
1129
help
1230
Set GPIO pin value for target chip to create plugin unit
1331

1432
config GPIO_PLUG_2
1533
int "GPIO pin number for plug 2"
1634
default 4
35+
depends on (NUM_VIRTUAL_PLUGS >= 2)
1736
help
1837
Set GPIO pin value for target chip to create plugin unit
1938

2039
config GPIO_PLUG_3
2140
int "GPIO pin number for plug 3"
2241
default 5
42+
depends on (NUM_VIRTUAL_PLUGS >= 3)
43+
help
44+
Set GPIO pin value for target chip to create plugin unit
45+
46+
config GPIO_PLUG_4
47+
int "GPIO pin number for plug 4"
48+
default 6 if IDF_TARGET_ESP32C2 || IDF_TARGET_ESP32C3
49+
default 12
50+
depends on (NUM_VIRTUAL_PLUGS >= 4)
51+
help
52+
Set GPIO pin value for target chip to create plugin unit
53+
54+
config GPIO_PLUG_5
55+
int "GPIO pin number for plug 5"
56+
default 7 if IDF_TARGET_ESP32C2 || IDF_TARGET_ESP32C3
57+
default 13
58+
depends on (NUM_VIRTUAL_PLUGS >= 5)
59+
help
60+
Set GPIO pin value for target chip to create plugin unit
61+
62+
config GPIO_PLUG_6
63+
int "GPIO pin number for plug 6"
64+
default 8 if IDF_TARGET_ESP32C2 || IDF_TARGET_ESP32C3
65+
default 15 if IDF_TARGET_ESP32C6
66+
default 14
67+
depends on (NUM_VIRTUAL_PLUGS >= 6)
68+
help
69+
Set GPIO pin value for target chip to create plugin unit
70+
71+
config GPIO_PLUG_7
72+
int "GPIO pin number for plug 7"
73+
default 10 if IDF_TARGET_ESP32C2 || IDF_TARGET_ESP32C3
74+
default 18 if IDF_TARGET_ESP32C6
75+
default 22 if IDF_TARGET_ESP32H2
76+
default 15
77+
depends on (NUM_VIRTUAL_PLUGS >= 7)
78+
help
79+
Set GPIO pin value for target chip to create plugin unit
80+
81+
config GPIO_PLUG_8
82+
int "GPIO pin number for plug 8"
83+
default 18 if IDF_TARGET_ESP32C2 || IDF_TARGET_ESP32C3
84+
default 19 if IDF_TARGET_ESP32C6
85+
default 25 if IDF_TARGET_ESP32H2
86+
default 16
87+
depends on (NUM_VIRTUAL_PLUGS >= 8)
88+
help
89+
Set GPIO pin value for target chip to create plugin unit
90+
91+
config GPIO_PLUG_9
92+
int "GPIO pin number for plug 9"
93+
default 0 if IDF_TARGET_ESP32C2
94+
default 19 if IDF_TARGET_ESP32C3
95+
default 20 if IDF_TARGET_ESP32C6
96+
default 26 if IDF_TARGET_ESP32H2
97+
default 17
98+
depends on (NUM_VIRTUAL_PLUGS >= 9)
99+
help
100+
Set GPIO pin value for target chip to create plugin unit
101+
102+
config GPIO_PLUG_10
103+
int "GPIO pin number for plug 10"
104+
default 1 if IDF_TARGET_ESP32C2
105+
default 0 if IDF_TARGET_ESP32C3
106+
default 21 if IDF_TARGET_ESP32C6
107+
default 27 if IDF_TARGET_ESP32H2
108+
default 18
109+
depends on (NUM_VIRTUAL_PLUGS >= 10)
110+
help
111+
Set GPIO pin value for target chip to create plugin unit
112+
113+
config GPIO_PLUG_11
114+
int "GPIO pin number for plug 11"
115+
default 3 if IDF_TARGET_ESP32C2
116+
default 1 if IDF_TARGET_ESP32C3
117+
default 22 if IDF_TARGET_ESP32C6
118+
default 0 if IDF_TARGET_ESP32H2
119+
default 19
120+
depends on (NUM_VIRTUAL_PLUGS >= 11)
121+
help
122+
Set GPIO pin value for target chip to create plugin unit
123+
124+
config GPIO_PLUG_12
125+
int "GPIO pin number for plug 12"
126+
default 3 if IDF_TARGET_ESP32C3
127+
default 23 if IDF_TARGET_ESP32C6
128+
default 1 if IDF_TARGET_ESP32H2
129+
default 20 if IDF_TARGET_ESP32S3
130+
default 21
131+
depends on (NUM_VIRTUAL_PLUGS >= 12)
132+
help
133+
Set GPIO pin value for target chip to create plugin unit
134+
135+
config GPIO_PLUG_13
136+
int "GPIO pin number for plug 13"
137+
default 0 if IDF_TARGET_ESP32C6
138+
default 3 if IDF_TARGET_ESP32H2
139+
default 21 if IDF_TARGET_ESP32S3
140+
default 22
141+
depends on (NUM_VIRTUAL_PLUGS >= 13)
142+
help
143+
Set GPIO pin value for target chip to create plugin unit
144+
145+
config GPIO_PLUG_14
146+
int "GPIO pin number for plug 14"
147+
default 1 if IDF_TARGET_ESP32C6
148+
default 8 if IDF_TARGET_ESP32H2
149+
default 35 if IDF_TARGET_ESP32S3
150+
default 23
151+
depends on (NUM_VIRTUAL_PLUGS >= 14)
152+
help
153+
Set GPIO pin value for target chip to create plugin unit
154+
155+
config GPIO_PLUG_15
156+
int "GPIO pin number for plug 15"
157+
default 3 if IDF_TARGET_ESP32C6
158+
default 10 if IDF_TARGET_ESP32H2
159+
default 36 if IDF_TARGET_ESP32S3
160+
default 25
161+
depends on (NUM_VIRTUAL_PLUGS >= 15)
162+
help
163+
Set GPIO pin value for target chip to create plugin unit
164+
165+
config GPIO_PLUG_16
166+
int "GPIO pin number for plug 16"
167+
default 6 if IDF_TARGET_ESP32C6
168+
default 11 if IDF_TARGET_ESP32H2
169+
default 37 if IDF_TARGET_ESP32S3
170+
default 26
171+
depends on (NUM_VIRTUAL_PLUGS >= 16)
23172
help
24173
Set GPIO pin value for target chip to create plugin unit
25174
endmenu

examples/multiple_on_off_plugin_units/main/app_driver.cpp

+58-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66
CONDITIONS OF ANY KIND, either express or implied.
77
*/
88

9+
#include <bsp/esp-bsp.h>
910
#include <esp_log.h>
11+
#include <iot_button.h>
1012
#include <stdlib.h>
1113
#include <string.h>
14+
#include "bsp/esp_bsp_devkit.h"
1215
#include "driver/gpio.h"
16+
#include "soc/gpio_num.h"
17+
#include "support/CodeUtils.h"
1318

1419
#include <esp_matter.h>
1520

@@ -57,9 +62,9 @@ esp_err_t app_driver_plugin_unit_init(const gpio_plug* plug)
5762
gpio_num_t get_gpio(uint16_t endpoint_id)
5863
{
5964
gpio_num_t gpio_pin = GPIO_NUM_NC;
60-
for (int i = 0; i < s_configure_plugs; i++) {
61-
if (s_plugin_unit_list[i].endpoint_id == endpoint_id) {
62-
gpio_pin = s_plugin_unit_list[i].plug;
65+
for (int i = 0; i < configure_plugs; i++) {
66+
if (plugin_unit_list[i].endpoint_id == endpoint_id) {
67+
gpio_pin = plugin_unit_list[i].plug;
6368
}
6469
}
6570
return gpio_pin;
@@ -85,4 +90,54 @@ esp_err_t app_driver_attribute_update(app_driver_handle_t driver_handle, uint16_
8590
return err;
8691
}
8792

93+
app_driver_handle_t app_driver_button_init(gpio_num_t * reset_gpio)
94+
{
95+
VerifyOrReturnValue((reset_gpio), (app_driver_handle_t)NULL, ESP_LOGE(TAG, "reset_gpio cannot be NULL"));
96+
#ifdef CONFIG_USER_BUTTON
97+
*reset_gpio = (gpio_num_t)CONFIG_USER_BUTTON_GPIO;
98+
#elif CONFIG_BSP_BUTTONS_NUM >= 1
99+
*reset_gpio = (gpio_num_t)BSP_BUTTON_1_IO;
100+
#else
101+
*reset_gpio = gpio_num_t::GPIO_NUM_NC;
102+
return (app_driver_handle_t)NULL;
103+
#endif
104+
ESP_LOGI(TAG, "Initializing reset button with gpio pin %d ...", (int)*reset_gpio);
105+
106+
// Make sure button's IO pin isn't assigned to a plug's IO pin
107+
for (int i = 0; i < configure_plugs; i++) {
108+
if (plugin_unit_list[i].plug == *reset_gpio) {
109+
ESP_LOGE(TAG, "Button's gpio pin %d is already configured for plug %d", *reset_gpio, i);
110+
*reset_gpio = gpio_num_t::GPIO_NUM_NC;
111+
return (app_driver_handle_t)NULL;
112+
}
113+
}
114+
115+
/* Initialize button */
116+
app_driver_handle_t reset_handle = NULL;
117+
#ifdef CONFIG_USER_BUTTON
118+
button_config_t config = {
119+
.type = BUTTON_TYPE_GPIO,
120+
.gpio_button_config = {
121+
.gpio_num = CONFIG_USER_BUTTON_GPIO,
122+
.active_level = CONFIG_USER_BUTTON_LEVEL,
123+
}
124+
};
125+
reset_handle = (app_driver_handle_t)iot_button_create(&config);
126+
#else
127+
button_handle_t bsp_buttons[BSP_BUTTON_NUM];
128+
int btn_cnt = 0;// will contain # of buttons that were created by BSP
129+
bsp_iot_button_create(bsp_buttons, &btn_cnt, BSP_BUTTON_NUM);
130+
if (btn_cnt >= 1) {
131+
// return handle to dev board's 1st built-in button
132+
reset_handle = (app_driver_handle_t)bsp_buttons[0];
133+
} else {
134+
ESP_LOGE(TAG, "bsp_iot_button_create() didn't return a usable button count: %d", btn_cnt);
135+
}
136+
#endif
137+
138+
if (!reset_handle) {
139+
*reset_gpio = gpio_num_t::GPIO_NUM_NC;
140+
}
141+
return reset_handle;
142+
}
88143

0 commit comments

Comments
 (0)