diff --git a/examples/light-switch-app/esp32/README.md b/examples/light-switch-app/esp32/README.md index ef79f82c709d00..9616c250526933 100644 --- a/examples/light-switch-app/esp32/README.md +++ b/examples/light-switch-app/esp32/README.md @@ -118,13 +118,16 @@ make sure the IDF_PATH has been exported(See the manual setup steps above). $ ./out/debug/chip-tool accesscontrol write acl '[{"fabricIndex": 1, "privilege": 5, "authMode": 2, "subjects": [112233], "targets": null },{"fabricIndex": 1, "privilege": 3, "authMode": 2, "subjects": [12344320], "targets": null }]' 12344321 0 -- we use matter shell to bind and test. +- After successful commissioning, use the chip-tool for binding in + Lighting-switch. - binding: + $ ./out/debug/chip-tool binding write binding '[{"fabricIndex": 1, "node":20836, "endpoint":1, "cluster":6}]' 12344320 1 - matter switch binding unicast 1 12344321 1 +- Test toggle: - on: + Press `boot` button to toggle LED. + + Using matter shell on: matter switch onoff on diff --git a/examples/light-switch-app/esp32/main/AppTask.cpp b/examples/light-switch-app/esp32/main/AppTask.cpp index 309e6d9f92476d..f29b4a0927f783 100644 --- a/examples/light-switch-app/esp32/main/AppTask.cpp +++ b/examples/light-switch-app/esp32/main/AppTask.cpp @@ -31,6 +31,8 @@ using namespace chip; static const char * TAG = "app-task"; +Button AppButton; + namespace { QueueHandle_t sAppEventQueue; @@ -59,6 +61,10 @@ CHIP_ERROR AppTask::Init() { CHIP_ERROR err = CHIP_NO_ERROR; + AppButton.Init(); + + AppButton.SetButtonPressCallback(ButtonPressCallback); + return err; } @@ -132,15 +138,10 @@ void AppTask::SwitchActionEventHandler(AppEvent * aEvent) } } -void AppTask::ButtonEventHandler(const uint8_t buttonHandle, uint8_t btnAction) +void AppTask::ButtonPressCallback() { - AppEvent button_event = {}; - button_event.Type = AppEvent::kEventType_Button; - button_event.ButtonEvent.Action = btnAction; - - if (buttonHandle == APP_LIGHT_SWITCH && btnAction == BUTTON_PRESSED) - { - button_event.mHandler = SwitchActionEventHandler; - sAppTask.PostEvent(&button_event); - } + AppEvent button_event; + button_event.Type = AppEvent::kEventType_Button; + button_event.mHandler = AppTask::SwitchActionEventHandler; + sAppTask.PostEvent(&button_event); } diff --git a/examples/light-switch-app/esp32/main/Button.cpp b/examples/light-switch-app/esp32/main/Button.cpp new file mode 100644 index 00000000000000..5ee8d6886c079f --- /dev/null +++ b/examples/light-switch-app/esp32/main/Button.cpp @@ -0,0 +1,66 @@ +/* + * + * Copyright (c) 2022 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Button.h" + +#define GPIO_INPUT_IO_0 CONFIG_EXAMPLE_BOARD_BUTTON_GPIO + +#define GPIO_INPUT_PIN_SEL (1ULL << GPIO_INPUT_IO_0) +#define ESP_INTR_FLAG_DEFAULT 0 + +static const char * TAG = "Button"; + +static Button::ButtonPressCallback button_press_handler = nullptr; + +static void IRAM_ATTR gpio_isr_handler(void * arg) +{ + if (button_press_handler != nullptr) + { + button_press_handler(); + } +} + +void Button::Init() +{ + /* Initialize button interrupt*/ + // zero-initialize the config structure. + gpio_config_t io_conf = {}; + // interrupt of rising edge + io_conf.intr_type = GPIO_INTR_NEGEDGE; + // bit mask of the pins, use GPIO4/5 here + io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL; + // set as input mode + io_conf.mode = GPIO_MODE_INPUT; + // enable pull-up mode + io_conf.pull_up_en = GPIO_PULLUP_ENABLE; + gpio_config(&io_conf); + + // install gpio isr service + gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT); + // hook isr handler for specific gpio pin + gpio_isr_handler_add(static_cast(GPIO_INPUT_IO_0), gpio_isr_handler, (void *) GPIO_INPUT_IO_0); + + ESP_LOGI(TAG, "Button initialized.."); +} + +void Button::SetButtonPressCallback(ButtonPressCallback button_callback) +{ + if (button_callback != nullptr) + { + button_press_handler = button_callback; + } +} diff --git a/examples/light-switch-app/esp32/main/Kconfig.projbuild b/examples/light-switch-app/esp32/main/Kconfig.projbuild index 9fbfa68ab1a7aa..79964c0e2b1f9c 100644 --- a/examples/light-switch-app/esp32/main/Kconfig.projbuild +++ b/examples/light-switch-app/esp32/main/Kconfig.projbuild @@ -71,5 +71,12 @@ menu "Demo" default 2 if RENDEZVOUS_MODE_BLE default 4 if RENDEZVOUS_MODE_THREAD default 8 if RENDEZVOUS_MODE_ETHERNET - + + config EXAMPLE_BOARD_BUTTON_GPIO + int "Boot Button GPIO" + default 0 if !IDF_TARGET_ESP32C3 + default 9 if IDF_TARGET_ESP32C3 + help + GPIO number on which the "Boot" button is connected. This is generally used + by the application for custom operations like toggling states. endmenu diff --git a/examples/light-switch-app/esp32/main/include/AppTask.h b/examples/light-switch-app/esp32/main/include/AppTask.h index 7b2c813ca362e8..b07b5e67bd83f9 100644 --- a/examples/light-switch-app/esp32/main/include/AppTask.h +++ b/examples/light-switch-app/esp32/main/include/AppTask.h @@ -22,6 +22,7 @@ #include #include "AppEvent.h" +#include "Button.h" #include "freertos/FreeRTOS.h" #include @@ -33,6 +34,8 @@ #define APP_ERROR_START_TIMER_FAILED CHIP_APPLICATION_ERROR(0x05) #define APP_ERROR_STOP_TIMER_FAILED CHIP_APPLICATION_ERROR(0x06) +extern Button AppButton; + class AppTask { @@ -40,8 +43,7 @@ class AppTask CHIP_ERROR StartAppTask(); static void AppTaskMain(void * pvParameter); void PostEvent(const AppEvent * event); - - void ButtonEventHandler(const uint8_t buttonHandle, uint8_t btnAction); + static void ButtonPressCallback(); private: friend AppTask & GetAppTask(void); diff --git a/examples/light-switch-app/esp32/main/include/Button.h b/examples/light-switch-app/esp32/main/include/Button.h new file mode 100644 index 00000000000000..df70ef4ed93649 --- /dev/null +++ b/examples/light-switch-app/esp32/main/include/Button.h @@ -0,0 +1,30 @@ +/* + * + * Copyright (c) 2022 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include "driver/gpio.h" +#include "esp_log.h" + +class Button +{ +public: + typedef void (*ButtonPressCallback)(void); + + void Init(void); + void SetButtonPressCallback(ButtonPressCallback button_callback); +};