diff --git a/examples/espidf-arduino-matter-light/.gitignore b/examples/espidf-arduino-matter-light/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/examples/espidf-arduino-matter-light/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/examples/espidf-arduino-matter-light/README.md b/examples/espidf-arduino-matter-light/README.md index 694ae22..c697678 100644 --- a/examples/espidf-arduino-matter-light/README.md +++ b/examples/espidf-arduino-matter-light/README.md @@ -1,65 +1,20 @@ -| Supported Targets | ESP32-C3 | ESP32-C6 | -| ----------------- | -------- | -------- | - - -# Managed Component Light - -This example is configured by default to work with the ESP32-C6, which has the RGB LED GPIO set as pin 8 and the BOOT button on GPIO 9. - -This example creates a Color Temperature Light device using the esp_matter component automatically downloaded from the [Espressif Component Registry](https://components.espressif.com/). See the [docs](https://docs.espressif.com/projects/esp-matter/en/latest/esp32/developing.html) for more information about matter. - -The code is based on the Arduino API and uses Arduino as an IDF Component. - -## How to use it - -Once the device runs for the first time, it must be commissioned to the Matter Fabric of the available Matter Environment. -Possible Matter Environments are: -- Amazon Alexa -- Google Home Assistant (*) -- Apple Home -- Open Source Home Assistant - -(*) Google Home Assistant requires the user to set up a Matter Light using the [Google Home Developer Console](https://developers.home.google.com/codelabs/matter-device#2). It is necessary to create a Matter Light device with VID = 0xFFF1 and PID = 0x8000. Otherwise, the Light won't show up in the GHA APP. This action is necessary because the Firmware uses Testing credentials and Google requires the user to create the testing device before using it. - -There is no QR Code to be used when the Smartphone APP wants to add the Matter Device. -Please enter the code manually: `34970112332` - -The devboard has a built-in LED that will be used as the Matter Light. -The default setting of the code uses pin 8 for the ESP32-C6, -Please change it in `main/matter_accessory_driver.h` or in the `sdkconfig.defaults` file. - -## LED Status and Factory Mode - -The WS2812b built-in LED will turn purple as soon as the device is flashed and runs for the first time. -The purple color indicates that the Matter Accessory has not been commissioned yet. -After using a Matter provider Smartphone APP to add a Matter device to your Home Application, it may turn orange to indicate that it has no WiFi connection. - -Once it connects to the WiFi network, the LED will turn white to indicate that Matter is working and the device is connected to the Matter Environment. -Please note that Matter over WiFi using an ESP32 device will connect to a 2.4GHz WiFi SSID, therefore the Commissioner APP Smartphone shall be connected to this SSID. - -The Matter and WiFi configuration will be stored in NVS to ensure that it will connect to the Matter Fabric and WiFi Network again once it is reset. - -The Matter Smartphone APP will control the light state (ON/OFF), temperature (Warm/Cold White), and brightness. - -## On Board Light toggle button - -The built-in BOOT button will toggle On/Off and replicate the new state to the Matter Environment, making it visible in the Matter Smartphone APP as well. - -## Returning to the Factory State - -Holding the BOOT button pressed for more than 10 seconds and then releasing it will erase all Matter and WiFi configuration, forcing it to reset to factory state. After that, the device needs to be commissioned again. Previous setups done in the Smartphone APP won't work again; therefore, the virtual device shall be removed from the APP. - -## Building the Application using WiFi and Matter - -This example has been tested with Arduino Core 3.2.0. It should work with newer versions too. - -There is a configuration file for these SoCs: esp32c3, esp32c6. -Those are the tested devices that have a WS2812 RGB LED and can run BLE, WiFi and Matter. - -In case it is necessary to change the Button Pin or the REG LED Pin, please use the `menuconfig` and change the Menu Option `Light Matter Accessory` - -## Using OpenThread with Matter - -This is possible with the ESP32-C6. -It is necessary to have a Thread Border Router in the Matter Environment. Check your Matter hardware provider. - +# Arduino ESP-Matter over WiFi (Thread) example using ESP32-C6 +This is an Arduino as IDF Project to build an ESP-Matter over WiFi or Thread RGB Light using ESP32-C6 and ESP-Matter Arduino API \ + +Example from [ESP32 Matter Library examples](https://github.com/espressif/arduino-esp32/tree/master/libraries/Matter/examples) + +The `main/idf_component.yml` file holds the ESP-Matter component.\ + +# Instructions: + +1- Compile with `pio run` + +2- Try to add the Matter RGB light to your Matter environment. + +# Thread variant: + +1- Rename `sdkconfig.defaults` to `sdkconfig.defaults.wifi` and `sdkconfig.defaults.thread` to `sdkconfig.defaults` + +2- Compile with `pio run` + +3- Try to add the Matter RGB light to your Matter environment. diff --git a/examples/espidf-arduino-matter-light/include/README b/examples/espidf-arduino-matter-light/include/README deleted file mode 100644 index 194dcd4..0000000 --- a/examples/espidf-arduino-matter-light/include/README +++ /dev/null @@ -1,39 +0,0 @@ - -This directory is intended for project header files. - -A header file is a file containing C declarations and macro definitions -to be shared between several project source files. You request the use of a -header file in your project source file (C, C++, etc) located in `src` folder -by including it, with the C preprocessing directive `#include'. - -```src/main.c - -#include "header.h" - -int main (void) -{ - ... -} -``` - -Including a header file produces the same results as copying the header file -into each source file that needs it. Such copying would be time-consuming -and error-prone. With a header file, the related declarations appear -in only one place. If they need to be changed, they can be changed in one -place, and programs that include the header file will automatically use the -new version when next recompiled. The header file eliminates the labor of -finding and changing all the copies as well as the risk that a failure to -find one copy will result in inconsistencies within a program. - -In C, the usual convention is to give header files names that end with `.h'. -It is most portable to use only letters, digits, dashes, and underscores in -header file names, and at most one dot. - -Read more about using header files in official GCC documentation: - -* Include Syntax -* Include Operation -* Once-Only Headers -* Computed Includes - -https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/examples/espidf-arduino-matter-light/lib/README b/examples/espidf-arduino-matter-light/lib/README deleted file mode 100644 index 2593a33..0000000 --- a/examples/espidf-arduino-matter-light/lib/README +++ /dev/null @@ -1,46 +0,0 @@ - -This directory is intended for project specific (private) libraries. -PlatformIO will compile them to static libraries and link into executable file. - -The source code of each library should be placed in an own separate directory -("lib/your_library_name/[here are source files]"). - -For example, see a structure of the following two libraries `Foo` and `Bar`: - -|--lib -| | -| |--Bar -| | |--docs -| | |--examples -| | |--src -| | |- Bar.c -| | |- Bar.h -| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html -| | -| |--Foo -| | |- Foo.c -| | |- Foo.h -| | -| |- README --> THIS FILE -| -|- platformio.ini -|--src - |- main.c - -and a contents of `src/main.c`: -``` -#include -#include - -int main (void) -{ - ... -} - -``` - -PlatformIO Library Dependency Finder will find automatically dependent -libraries scanning project source files. - -More information about PlatformIO Library Dependency Finder -- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/examples/espidf-arduino-matter-light/main/CMakeLists.txt b/examples/espidf-arduino-matter-light/main/CMakeLists.txt index dfe4b75..80e7d4e 100644 --- a/examples/espidf-arduino-matter-light/main/CMakeLists.txt +++ b/examples/espidf-arduino-matter-light/main/CMakeLists.txt @@ -1,9 +1,3 @@ -# This file was automatically generated for projects -# without default 'CMakeLists.txt' file. +idf_component_register(SRC_DIRS "." + INCLUDE_DIRS ".") -FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/main/*.*) - -idf_component_register(SRCS ${app_sources}) - -#set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17) -#target_compile_options(${COMPONENT_LIB} PRIVATE "-DCHIP_HAVE_CONFIG_H") \ No newline at end of file diff --git a/examples/espidf-arduino-matter-light/main/Kconfig.projbuild b/examples/espidf-arduino-matter-light/main/Kconfig.projbuild deleted file mode 100644 index 91ee5e8..0000000 --- a/examples/espidf-arduino-matter-light/main/Kconfig.projbuild +++ /dev/null @@ -1,42 +0,0 @@ -menu "Light Matter Accessory" - menu "On Board Light ON/OFF Button" - config BUTTON_PIN - int - prompt "Button 1 GPIO" - default 9 if IDF_TARGET_ESP32C3 || IDF_TARGET_ESP32C6 - default 0 - range -1 ENV_GPIO_IN_RANGE_MAX - help - The GPIO pin for button that will be used to turn on/off the Matter Light. It shall be connected to a push button. It can use the BOOT button of the development board. - endmenu - - menu "LEDs" - config WS2812_PIN - int - prompt "WS2812 RGB LED GPIO" - default 8 if IDF_TARGET_ESP32C3 || IDF_TARGET_ESP32C6 - default 48 - range -1 ENV_GPIO_OUT_RANGE_MAX - help - The GPIO pin for the Matter Light that will be driven by RMT. It shall be connected to one single WS2812 RGB LED. - endmenu - - config ENV_GPIO_RANGE_MIN - int - default 0 - - config ENV_GPIO_RANGE_MAX - int - default 19 if IDF_TARGET_ESP32C3 - default 30 if IDF_TARGET_ESP32C6 - default 48 - - config ENV_GPIO_IN_RANGE_MAX - int - default ENV_GPIO_RANGE_MAX - - config ENV_GPIO_OUT_RANGE_MAX - int - default ENV_GPIO_RANGE_MAX - -endmenu diff --git a/examples/espidf-arduino-matter-light/main/MatterEnhancedColorLight.cpp b/examples/espidf-arduino-matter-light/main/MatterEnhancedColorLight.cpp new file mode 100644 index 0000000..cff418b --- /dev/null +++ b/examples/espidf-arduino-matter-light/main/MatterEnhancedColorLight.cpp @@ -0,0 +1,186 @@ +// Copyright 2025 Espressif Systems (Shanghai) PTE LTD +// +// 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. + +// Matter Manager +#include +#include + +// List of Matter Endpoints for this Node +// Color Light Endpoint +MatterEnhancedColorLight EnhancedColorLight; + +// It will use HSV color to control all Matter Attribute Changes +HsvColor_t currentHSVColor = {0, 0, 0}; + +// it will keep last OnOff & HSV Color state stored, using Preferences +Preferences matterPref; +const char *onOffPrefKey = "OnOff"; +const char *hsvColorPrefKey = "HSV"; + +// set your board RGB LED pin here +#ifdef RGB_BUILTIN +const uint8_t ledPin = RGB_BUILTIN; +#else +const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN +#warning "Do not forget to set the RGB LED pin" +#endif + +// set your board USER BUTTON pin here +const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button. + +// Button control +uint32_t button_time_stamp = 0; // debouncing control +bool button_state = false; // false = released | true = pressed +const uint32_t debounceTime = 250; // button debouncing time (ms) +const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission + +// Set the RGB LED Light based on the current state of the Enhanced Color Light +bool setLightState(bool state, espHsvColor_t colorHSV, uint8_t brightness, uint16_t temperature_Mireds) { + + if (state) { +#ifdef RGB_BUILTIN + // currentHSVColor keeps final color result + espRgbColor_t rgbColor = espHsvColorToRgbColor(currentHSVColor); + // set the RGB LED + rgbLedWrite(ledPin, rgbColor.r, rgbColor.g, rgbColor.b); +#else + // No Color RGB LED, just use the HSV value (brightness) to control the LED + analogWrite(ledPin, colorHSV.v); +#endif + } else { +#ifndef RGB_BUILTIN + // after analogWrite(), it is necessary to set the GPIO to digital mode first + pinMode(ledPin, OUTPUT); +#endif + digitalWrite(ledPin, LOW); + } + // store last HSV Color and OnOff state for when the Light is restarted / power goes off + matterPref.putBool(onOffPrefKey, state); + matterPref.putUInt(hsvColorPrefKey, currentHSVColor.h << 16 | currentHSVColor.s << 8 | currentHSVColor.v); + // This callback must return the success state to Matter core + return true; +} + +void setup() { + // Initialize the USER BUTTON (Boot button) GPIO that will act as a toggle switch + pinMode(buttonPin, INPUT_PULLUP); + // Initialize the LED (light) GPIO and Matter End Point + pinMode(ledPin, OUTPUT); + + Serial.begin(115200); + + // Initialize Matter EndPoint + matterPref.begin("MatterPrefs", false); + // default OnOff state is ON if not stored before + bool lastOnOffState = matterPref.getBool(onOffPrefKey, true); + // default HSV color is (21, 216, 25) - Warm White Color at 10% intensity + uint32_t prefHsvColor = matterPref.getUInt(hsvColorPrefKey, 21 << 16 | 216 << 8 | 25); + currentHSVColor = {uint8_t(prefHsvColor >> 16), uint8_t(prefHsvColor >> 8), uint8_t(prefHsvColor)}; + EnhancedColorLight.begin(lastOnOffState, currentHSVColor); + // set the callback function to handle the Light state change + EnhancedColorLight.onChange(setLightState); + + // lambda functions are used to set the attribute change callbacks + EnhancedColorLight.onChangeOnOff([](bool state) { + Serial.printf("Light OnOff changed to %s\r\n", state ? "ON" : "OFF"); + return true; + }); + EnhancedColorLight.onChangeColorTemperature([](uint16_t colorTemperature) { + Serial.printf("Light Color Temperature changed to %d\r\n", colorTemperature); + // get correspondent Hue and Saturation of the color temperature + HsvColor_t hsvTemperature = espRgbColorToHsvColor(espCTToRgbColor(colorTemperature)); + // keep previous the brightness and just change the Hue and Saturation + currentHSVColor.h = hsvTemperature.h; + currentHSVColor.s = hsvTemperature.s; + return true; + }); + EnhancedColorLight.onChangeBrightness([](uint8_t brightness) { + Serial.printf("Light brightness changed to %d\r\n", brightness); + // change current brightness (HSV value) + currentHSVColor.v = brightness; + return true; + }); + EnhancedColorLight.onChangeColorHSV([](HsvColor_t hsvColor) { + Serial.printf("Light HSV Color changed to (%d,%d,%d)\r\n", hsvColor.h, hsvColor.s, hsvColor.v); + // keep the current brightness and just change Hue and Saturation + currentHSVColor.h = hsvColor.h; + currentHSVColor.s = hsvColor.s; + return true; + }); + + // Matter beginning - Last step, after all EndPoints are initialized + Matter.begin(); + // This may be a restart of a already commissioned Matter accessory + if (Matter.isDeviceCommissioned()) { + Serial.println("Matter Node is commissioned and connected to the network. Ready for use."); + Serial.printf( + "Initial state: %s | RGB Color: (%d,%d,%d) \r\n", EnhancedColorLight ? "ON" : "OFF", EnhancedColorLight.getColorRGB().r, + EnhancedColorLight.getColorRGB().g, EnhancedColorLight.getColorRGB().b + ); + // configure the Light based on initial on-off state and its color + EnhancedColorLight.updateAccessory(); + } +} + +void loop() { + // Check Matter Light Commissioning state, which may change during execution of loop() + if (!Matter.isDeviceCommissioned()) { + Serial.println(""); + Serial.println("Matter Node is not commissioned yet."); + Serial.println("Initiate the device discovery in your Matter environment."); + Serial.println("Commission it to your Matter hub with the manual pairing code or QR code"); + Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str()); + Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str()); + // waits for Matter Light Commissioning. + uint32_t timeCount = 0; + while (!Matter.isDeviceCommissioned()) { + delay(100); + if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec + Serial.println("Matter Node not commissioned yet. Waiting for commissioning."); + } + } + Serial.printf( + "Initial state: %s | RGB Color: (%d,%d,%d) \r\n", EnhancedColorLight ? "ON" : "OFF", EnhancedColorLight.getColorRGB().r, + EnhancedColorLight.getColorRGB().g, EnhancedColorLight.getColorRGB().b + ); + // configure the Light based on initial on-off state and its color + EnhancedColorLight.updateAccessory(); + Serial.println("Matter Node is commissioned and connected to the network. Ready for use."); + } + + // A button is also used to control the light + // Check if the button has been pressed + if (digitalRead(buttonPin) == LOW && !button_state) { + // deals with button debouncing + button_time_stamp = millis(); // record the time while the button is pressed. + button_state = true; // pressed. + } + + // Onboard User Button is used as a Light toggle switch or to decommission it + uint32_t time_diff = millis() - button_time_stamp; + if (button_state && time_diff > debounceTime && digitalRead(buttonPin) == HIGH) { + button_state = false; // released + // Toggle button is released - toggle the light + Serial.println("User button released. Toggling Light!"); + EnhancedColorLight.toggle(); // Matter Controller also can see the change + } + + // Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node + if (button_state && time_diff > decommissioningTimeout) { + Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again."); + EnhancedColorLight = false; // turn the light off + Matter.decommission(); + button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so + } +} diff --git a/examples/espidf-arduino-matter-light/main/builtinLED.cpp b/examples/espidf-arduino-matter-light/main/builtinLED.cpp deleted file mode 100644 index 8795dde..0000000 --- a/examples/espidf-arduino-matter-light/main/builtinLED.cpp +++ /dev/null @@ -1,237 +0,0 @@ -/* - This example code is in the Public Domain (or CC0 licensed, at your option.) - Unless required by applicable law or agreed to in writing, this - software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - CONDITIONS OF ANY KIND, either express or implied. - This will implement the onboard WS2812b LED as a LED indicator - It can be used to indicate some state or status of the device - The LED can be controlled using RGB, HSV or color temperature, brightness - - In this example, the LED Indicator class is used as the Matter light accessory -*/ - -#include "builtinLED.h" - -typedef struct { - uint16_t hue; - uint8_t saturation; -} HS_color_t; - -static const HS_color_t temperatureTable[] = { - {4, 100}, {8, 100}, {11, 100}, {14, 100}, {16, 100}, {18, 100}, {20, 100}, {22, 100}, {24, 100}, {25, 100}, {27, 100}, {28, 100}, {30, 100}, {31, 100}, - {31, 95}, {30, 89}, {30, 85}, {29, 80}, {29, 76}, {29, 73}, {29, 69}, {28, 66}, {28, 63}, {28, 60}, {28, 57}, {28, 54}, {28, 52}, {27, 49}, - {27, 47}, {27, 45}, {27, 43}, {27, 41}, {27, 39}, {27, 37}, {27, 35}, {27, 33}, {27, 31}, {27, 30}, {27, 28}, {27, 26}, {27, 25}, {27, 23}, - {27, 22}, {27, 21}, {27, 19}, {27, 18}, {27, 17}, {27, 15}, {28, 14}, {28, 13}, {28, 12}, {29, 10}, {29, 9}, {30, 8}, {31, 7}, {32, 6}, - {34, 5}, {36, 4}, {41, 3}, {49, 2}, {0, 0}, {294, 2}, {265, 3}, {251, 4}, {242, 5}, {237, 6}, {233, 7}, {231, 8}, {229, 9}, {228, 10}, - {227, 11}, {226, 11}, {226, 12}, {225, 13}, {225, 13}, {224, 14}, {224, 14}, {224, 15}, {224, 15}, {223, 16}, {223, 16}, {223, 17}, {223, 17}, {223, 17}, - {222, 18}, {222, 18}, {222, 19}, {222, 19}, {222, 19}, {222, 19}, {222, 20}, {222, 20}, {222, 20}, {222, 21}, {222, 21} -}; - -/* step brightness table: gamma = 2.3 */ -static const uint8_t gamma_table[MAX_PROGRESS] = { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, - 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, - 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, - 21, 22, 22, 23, 23, 24, 25, 25, 26, 26, 27, 28, 28, 29, 30, 30, 31, 32, 33, 33, 34, 35, 36, 36, 37, 38, 39, 40, 40, - 41, 42, 43, 44, 45, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, - 69, 70, 71, 72, 74, 75, 76, 77, 78, 79, 81, 82, 83, 84, 86, 87, 88, 89, 91, 92, 93, 95, 96, 97, 99, 100, 101, 103, 104, - 105, 107, 108, 110, 111, 112, 114, 115, 117, 118, 120, 121, 123, 124, 126, 128, 129, 131, 132, 134, 135, 137, 139, 140, 142, 144, 145, 147, 149, - 150, 152, 154, 156, 157, 159, 161, 163, 164, 166, 168, 170, 172, 174, 175, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195, 197, 199, 201, 203, - 205, 207, 209, 211, 213, 215, 217, 219, 221, 223, 226, 228, 230, 232, 234, 236, 239, 241, 243, 245, 248, 250, 252, 255, -}; - -BuiltInLED::BuiltInLED() { - pin_number = (uint8_t)-1; // no pin number - state = false; // LED is off - hsv_color.value = 0; // black color -} - -BuiltInLED::~BuiltInLED() { - end(); -} - -led_indicator_color_hsv_t BuiltInLED::rgb2hsv(led_indicator_color_rgb_t rgb) { - led_indicator_color_hsv_t hsv; - uint8_t minRGB, maxRGB; - uint8_t delta; - - minRGB = rgb.r < rgb.g ? (rgb.r < rgb.b ? rgb.r : rgb.b) : (rgb.g < rgb.b ? rgb.g : rgb.b); - maxRGB = rgb.r > rgb.g ? (rgb.r > rgb.b ? rgb.r : rgb.b) : (rgb.g > rgb.b ? rgb.g : rgb.b); - hsv.value = 0; - hsv.v = maxRGB; - delta = maxRGB - minRGB; - - if (delta == 0) { - hsv.h = 0; - hsv.s = 0; - } else { - hsv.s = delta * 255 / maxRGB; - - if (rgb.r == maxRGB) { - hsv.h = (60 * (rgb.g - rgb.b) / delta + 360) % 360; - } else if (rgb.g == maxRGB) { - hsv.h = (60 * (rgb.b - rgb.r) / delta + 120); - } else { - hsv.h = (60 * (rgb.r - rgb.g) / delta + 240); - } - } - return hsv; -} - -led_indicator_color_rgb_t BuiltInLED::hsv2rgb(led_indicator_color_hsv_t hsv) { - led_indicator_color_rgb_t rgb; - uint8_t rgb_max = hsv.v; - uint8_t rgb_min = rgb_max * (255 - hsv.s) / 255.0f; - - uint8_t i = hsv.h / 60; - uint8_t diff = hsv.h % 60; - - // RGB adjustment amount by hue - uint8_t rgb_adj = (rgb_max - rgb_min) * diff / 60; - rgb.value = 0; - switch (i) { - case 0: - rgb.r = rgb_max; - rgb.g = rgb_min + rgb_adj; - rgb.b = rgb_min; - break; - case 1: - rgb.r = rgb_max - rgb_adj; - rgb.g = rgb_max; - rgb.b = rgb_min; - break; - case 2: - rgb.r = rgb_min; - rgb.g = rgb_max; - rgb.b = rgb_min + rgb_adj; - break; - case 3: - rgb.r = rgb_min; - rgb.g = rgb_max - rgb_adj; - rgb.b = rgb_max; - break; - case 4: - rgb.r = rgb_min + rgb_adj; - rgb.g = rgb_min; - rgb.b = rgb_max; - break; - default: - rgb.r = rgb_max; - rgb.g = rgb_min; - rgb.b = rgb_max - rgb_adj; - break; - } - - // gamma correction - rgb.r = gamma_table[rgb.r]; - rgb.g = gamma_table[rgb.g]; - rgb.b = gamma_table[rgb.b]; - return rgb; -} - -void BuiltInLED::begin(uint8_t pin) { - if (pin < NUM_DIGITAL_PINS) { - pin_number = pin; - log_i("Initializing pin %d", pin); - } else { - log_e("Invalid pin (%d) number", pin); - } -} -void BuiltInLED::end() { - state = false; - write(); // turn off the LED - if (pin_number < NUM_DIGITAL_PINS) { - if (!rmtDeinit(pin_number)) { - log_e("Failed to deinitialize RMT"); - } - } -} - -void BuiltInLED::on() { - state = true; -} - -void BuiltInLED::off() { - state = false; -} - -void BuiltInLED::toggle() { - state = !state; -} - -bool BuiltInLED::getState() { - return state; -} - -bool BuiltInLED::write() { - led_indicator_color_rgb_t rgb_color = getRGB(); - log_d("Writing to pin %d with state = %s", pin_number, state ? "ON" : "OFF"); - log_d("HSV: %d, %d, %d", hsv_color.h, hsv_color.s, hsv_color.v); - log_d("RGB: %d, %d, %d", rgb_color.r, rgb_color.g, rgb_color.b); - if (pin_number < NUM_DIGITAL_PINS) { - if (state) { - rgbLedWrite(pin_number, rgb_color.r, rgb_color.g, rgb_color.b); - } else { - rgbLedWrite(pin_number, 0, 0, 0); - } - return true; - } else { - log_e("Invalid pin (%d) number", pin_number); - return false; - } -} - -void BuiltInLED::setBrightness(uint8_t brightness) { - hsv_color.v = brightness; -} - -uint8_t BuiltInLED::getBrightness() { - return hsv_color.v; -} - -void BuiltInLED::setHSV(led_indicator_color_hsv_t hsv) { - if (hsv.h > MAX_HUE) { - hsv.h = MAX_HUE; - } - hsv_color.value = hsv.value; -} - -led_indicator_color_hsv_t BuiltInLED::getHSV() { - return hsv_color; -} - -void BuiltInLED::setRGB(led_indicator_color_rgb_t rgb_color) { - hsv_color = rgb2hsv(rgb_color); -} - -led_indicator_color_rgb_t BuiltInLED::getRGB() { - return hsv2rgb(hsv_color); -} - -void BuiltInLED::setTemperature(uint32_t temperature) { - uint16_t hue; - uint8_t saturation; - - log_d("Requested Temperature: %ld", temperature); - //hsv_color.v = gamma_table[((temperature >> 25) & 0x7F)]; - temperature &= 0xFFFFFF; - if (temperature < 600) { - hue = 0; - saturation = 100; - } else { - if (temperature > 10000) { - hue = 222; - saturation = 21 + (temperature - 10000) * 41 / 990000; - } else { - temperature -= 600; - temperature /= 100; - hue = temperatureTable[temperature].hue; - saturation = temperatureTable[temperature].saturation; - } - } - saturation = (saturation * 255) / 100; - // brightness is not changed - hsv_color.h = hue; - hsv_color.s = saturation; - log_d("Calculated Temperature: %ld, Hue: %d, Saturation: %d, Brightness: %d", temperature, hue, saturation, hsv_color.v); -} diff --git a/examples/espidf-arduino-matter-light/main/builtinLED.h b/examples/espidf-arduino-matter-light/main/builtinLED.h deleted file mode 100644 index 1ca8c93..0000000 --- a/examples/espidf-arduino-matter-light/main/builtinLED.h +++ /dev/null @@ -1,74 +0,0 @@ -/* - This example code is in the Public Domain (or CC0 licensed, at your option.) - Unless required by applicable law or agreed to in writing, this - software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - CONDITIONS OF ANY KIND, either express or implied. - This will implement the onboard WS2812b LED as a LED indicator - It can be used to indicate some state or status of the device - The LED can be controlled using RGB, HSV or color temperature, brightness - - In this example, the BuiltInLED class is used as the Matter light accessory -*/ - -#pragma once - -#include - -#define MAX_HUE 360 -#define MAX_SATURATION 255 -#define MAX_BRIGHTNESS 255 -#define MAX_PROGRESS 256 - -typedef struct { - union { - struct { - uint32_t v : 8; /*!< Brightness/Value of the LED. 0-255 */ - uint32_t s : 8; /*!< Saturation of the LED. 0-255 */ - uint32_t h : 9; /*!< Hue of the LED. 0-360 */ - }; - uint32_t value; /*!< IHSV value of the LED. */ - }; -} led_indicator_color_hsv_t; - -typedef struct { - union { - struct { - uint32_t r : 8; /*!< Red component of the LED color. Range: 0-255. */ - uint32_t g : 8; /*!< Green component of the LED color. Range: 0-255. */ - uint32_t b : 8; /*!< Blue component of the LED color. Range: 0-255. */ - }; - uint32_t value; /*!< Combined RGB value of the LED color. */ - }; -} led_indicator_color_rgb_t; - -class BuiltInLED { -private: - uint8_t pin_number; - bool state; - led_indicator_color_hsv_t hsv_color; - -public: - BuiltInLED(); - ~BuiltInLED(); - - static led_indicator_color_hsv_t rgb2hsv(led_indicator_color_rgb_t rgb_value); - static led_indicator_color_rgb_t hsv2rgb(led_indicator_color_hsv_t hsv); - - void begin(uint8_t pin); - void end(); - - void on(); - void off(); - void toggle(); - bool getState(); - - bool write(); - - void setBrightness(uint8_t brightness); - uint8_t getBrightness(); - void setHSV(led_indicator_color_hsv_t hsv); - led_indicator_color_hsv_t getHSV(); - void setRGB(led_indicator_color_rgb_t color); - led_indicator_color_rgb_t getRGB(); - void setTemperature(uint32_t temperature); -}; diff --git a/examples/espidf-arduino-matter-light/main/idf_component.yml b/examples/espidf-arduino-matter-light/main/idf_component.yml index a0c5e72..91d37ce 100644 --- a/examples/espidf-arduino-matter-light/main/idf_component.yml +++ b/examples/espidf-arduino-matter-light/main/idf_component.yml @@ -1,7 +1,4 @@ -dependencies: - espressif/esp_matter: - version: ">=1.4.0" - espressif/cmake_utilities: - version: "0.*" - rules: - - if: "target in [esp32c2]" +dependencies: + espressif/esp_matter: + version: ">=1.4.0" + require: public diff --git a/examples/espidf-arduino-matter-light/main/matter_accessory_driver.cpp b/examples/espidf-arduino-matter-light/main/matter_accessory_driver.cpp deleted file mode 100644 index 523c38e..0000000 --- a/examples/espidf-arduino-matter-light/main/matter_accessory_driver.cpp +++ /dev/null @@ -1,89 +0,0 @@ -/* - This example code is in the Public Domain (or CC0 licensed, at your option.) - Unless required by applicable law or agreed to in writing, this - software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - CONDITIONS OF ANY KIND, either express or implied. -*/ -#include -#include -#include -#include "builtinLED.h" -#include "matter_accessory_driver.h" - -/* Do any conversions/remapping for the actual value here */ -esp_err_t light_accessory_set_power(void *led, uint8_t val) { - BuiltInLED *builtinLED = (BuiltInLED *)led; - esp_err_t err = ESP_OK; - if (val) { - builtinLED->on(); - } else { - builtinLED->off(); - } - if (!builtinLED->write()) { - err = ESP_FAIL; - } - log_i("LED set power: %d", val); - return err; -} - -esp_err_t light_accessory_set_brightness(void *led, uint8_t val) { - esp_err_t err = ESP_OK; - BuiltInLED *builtinLED = (BuiltInLED *)led; - int value = REMAP_TO_RANGE(val, MATTER_BRIGHTNESS, STANDARD_BRIGHTNESS); - - builtinLED->setBrightness(value); - if (!builtinLED->write()) { - err = ESP_FAIL; - } - log_i("LED set brightness: %d", value); - return err; -} - -esp_err_t light_accessory_set_hue(void *led, uint8_t val) { - esp_err_t err = ESP_OK; - BuiltInLED *builtinLED = (BuiltInLED *)led; - int value = REMAP_TO_RANGE(val, MATTER_HUE, STANDARD_HUE); - led_indicator_color_hsv_t hsv = builtinLED->getHSV(); - hsv.h = value; - builtinLED->setHSV(hsv); - if (!builtinLED->write()) { - err = ESP_FAIL; - } - log_i("LED set hue: %d", value); - return err; -} - -esp_err_t light_accessory_set_saturation(void *led, uint8_t val) { - esp_err_t err = ESP_OK; - BuiltInLED *builtinLED = (BuiltInLED *)led; - int value = REMAP_TO_RANGE(val, MATTER_SATURATION, STANDARD_SATURATION); - led_indicator_color_hsv_t hsv = builtinLED->getHSV(); - hsv.s = value; - builtinLED->setHSV(hsv); - if (!builtinLED->write()) { - err = ESP_FAIL; - } - log_i("LED set saturation: %d", value); - return err; -} - -esp_err_t light_accessory_set_temperature(void *led, uint16_t val) { - esp_err_t err = ESP_OK; - BuiltInLED *builtinLED = (BuiltInLED *)led; - uint32_t value = REMAP_TO_RANGE_INVERSE(val, STANDARD_TEMPERATURE_FACTOR); - builtinLED->setTemperature(value); - if (!builtinLED->write()) { - err = ESP_FAIL; - } - log_i("LED set temperature: %ld", value); - return err; -} - -app_driver_handle_t light_accessory_init() { - /* Initialize led */ - static BuiltInLED builtinLED; - - const uint8_t pin = WS2812_PIN; // set your board WS2812b pin here - builtinLED.begin(pin); - return (app_driver_handle_t)&builtinLED; -} diff --git a/examples/espidf-arduino-matter-light/main/matter_accessory_driver.h b/examples/espidf-arduino-matter-light/main/matter_accessory_driver.h deleted file mode 100644 index 3bf6655..0000000 --- a/examples/espidf-arduino-matter-light/main/matter_accessory_driver.h +++ /dev/null @@ -1,47 +0,0 @@ -#include -#include - -// set your board WS2812b pin here (e.g. 48 is the default pin for the ESP32-S3 devkit) -#ifndef CONFIG_WS2812_PIN -#define WS2812_PIN 48 // ESP32-S3 DevKitC built-in LED -#else -#define WS2812_PIN CONFIG_WS2812_PIN // From sdkconfig.defaults. -#endif - -#ifndef RGB_BUILTIN -#define RGB_BUILTIN WS2812_PIN -#endif - -// Set your board button pin here (e.g. 0 is the default pin for the ESP32-S3 devkit) -#ifndef CONFIG_BUTTON_PIN -#define BUTTON_PIN 0 // ESP32-S3 DevKitC built-in button -#else -#define BUTTON_PIN CONFIG_BUTTON_PIN // From sdkconfig.defaults. -#endif - -/** Standard max values (used for remapping attributes) */ -#define STANDARD_BRIGHTNESS 255 -#define STANDARD_HUE 360 -#define STANDARD_SATURATION 255 -#define STANDARD_TEMPERATURE_FACTOR 1000000 - -/** Matter max values (used for remapping attributes) */ -#define MATTER_BRIGHTNESS 254 -#define MATTER_HUE 254 -#define MATTER_SATURATION 254 -#define MATTER_TEMPERATURE_FACTOR 1000000 - -/** Default attribute values used during initialization */ -#define DEFAULT_POWER true -#define DEFAULT_BRIGHTNESS 64 -#define DEFAULT_HUE 128 -#define DEFAULT_SATURATION 254 - -typedef void *app_driver_handle_t; - -esp_err_t light_accessory_set_power(void *led, uint8_t val); -esp_err_t light_accessory_set_brightness(void *led, uint8_t val); -esp_err_t light_accessory_set_hue(void *led, uint8_t val); -esp_err_t light_accessory_set_saturation(void *led, uint8_t val); -esp_err_t light_accessory_set_temperature(void *led, uint16_t val); -app_driver_handle_t light_accessory_init(); diff --git a/examples/espidf-arduino-matter-light/main/matter_light.cpp b/examples/espidf-arduino-matter-light/main/matter_light.cpp deleted file mode 100644 index 6079ce4..0000000 --- a/examples/espidf-arduino-matter-light/main/matter_light.cpp +++ /dev/null @@ -1,384 +0,0 @@ -/* - This example code is in the Public Domain (or CC0 licensed, at your option.) - Unless required by applicable law or agreed to in writing, this - software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - CONDITIONS OF ANY KIND, either express or implied. -*/ -#include -#include "matter_accessory_driver.h" - -#include - -#include -#include -#include - -#include -#include - -#if CHIP_DEVICE_CONFIG_ENABLE_THREAD -#include -#include "esp_openthread_types.h" - -#define ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG() \ - { .radio_mode = RADIO_MODE_NATIVE, } - -#define ESP_OPENTHREAD_DEFAULT_HOST_CONFIG() \ - { .host_connection_mode = HOST_CONNECTION_MODE_NONE, } - -#define ESP_OPENTHREAD_DEFAULT_PORT_CONFIG() \ - { .storage_partition_name = "nvs", .netif_queue_size = 10, .task_queue_size = 10, } -#endif - -// set your board button pin here -const uint8_t button_gpio = BUTTON_PIN; // GPIO BOOT Button - -uint16_t light_endpoint_id = 0; - -using namespace esp_matter; -using namespace esp_matter::attribute; -using namespace esp_matter::endpoint; -using namespace chip::app::Clusters; - -constexpr auto k_timeout_seconds = 300; - -#if CONFIG_ENABLE_ENCRYPTED_OTA -extern const char decryption_key_start[] asm("_binary_esp_image_encryption_key_pem_start"); -extern const char decryption_key_end[] asm("_binary_esp_image_encryption_key_pem_end"); - -static const char *s_decryption_key = decryption_key_start; -static const uint16_t s_decryption_key_len = decryption_key_end - decryption_key_start; -#endif // CONFIG_ENABLE_ENCRYPTED_OTA - -bool isAccessoryCommissioned() { - return chip::Server::GetInstance().GetFabricTable().FabricCount() > 0; -} - -#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION -bool isWifiConnected() { - return chip::DeviceLayer::ConnectivityMgr().IsWiFiStationConnected(); -} -#endif - -#if CHIP_DEVICE_CONFIG_ENABLE_THREAD -bool isThreadConnected() { - return chip::DeviceLayer::ConnectivityMgr().IsThreadAttached(); -} -#endif - -static void app_event_cb(const ChipDeviceEvent *event, intptr_t arg) { - switch (event->Type) { - case chip::DeviceLayer::DeviceEventType::kInterfaceIpAddressChanged: - log_i( - "Interface %s Address changed", event->InterfaceIpAddressChanged.Type == chip::DeviceLayer::InterfaceIpChangeType::kIpV4_Assigned ? "IPv4" : "IPV6" - ); - break; - - case chip::DeviceLayer::DeviceEventType::kCommissioningComplete: log_i("Commissioning complete"); break; - - case chip::DeviceLayer::DeviceEventType::kFailSafeTimerExpired: log_i("Commissioning failed, fail safe timer expired"); break; - - case chip::DeviceLayer::DeviceEventType::kCommissioningSessionStarted: log_i("Commissioning session started"); break; - - case chip::DeviceLayer::DeviceEventType::kCommissioningSessionStopped: log_i("Commissioning session stopped"); break; - - case chip::DeviceLayer::DeviceEventType::kCommissioningWindowOpened: log_i("Commissioning window opened"); break; - - case chip::DeviceLayer::DeviceEventType::kCommissioningWindowClosed: log_i("Commissioning window closed"); break; - - case chip::DeviceLayer::DeviceEventType::kFabricRemoved: - { - log_i("Fabric removed successfully"); - if (chip::Server::GetInstance().GetFabricTable().FabricCount() == 0) { - chip::CommissioningWindowManager &commissionMgr = chip::Server::GetInstance().GetCommissioningWindowManager(); - constexpr auto kTimeoutSeconds = chip::System::Clock::Seconds16(k_timeout_seconds); - if (!commissionMgr.IsCommissioningWindowOpen()) { - /* After removing last fabric, this example does not remove the Wi-Fi credentials - * and still has IP connectivity so, only advertising on DNS-SD. - */ - CHIP_ERROR err = commissionMgr.OpenBasicCommissioningWindow(kTimeoutSeconds, chip::CommissioningWindowAdvertisement::kDnssdOnly); - if (err != CHIP_NO_ERROR) { - log_e("Failed to open commissioning window, err:%" CHIP_ERROR_FORMAT, err.Format()); - } - } - } - break; - } - - case chip::DeviceLayer::DeviceEventType::kFabricWillBeRemoved: log_i("Fabric will be removed"); break; - - case chip::DeviceLayer::DeviceEventType::kFabricUpdated: log_i("Fabric is updated"); break; - - case chip::DeviceLayer::DeviceEventType::kFabricCommitted: log_i("Fabric is committed"); break; - - case chip::DeviceLayer::DeviceEventType::kBLEDeinitialized: log_i("BLE deinitialized and memory reclaimed"); break; - - default: break; - } -} - -esp_err_t matter_light_attribute_update( - app_driver_handle_t driver_handle, uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val -) { - esp_err_t err = ESP_OK; - if (endpoint_id == light_endpoint_id) { - void *led = (void *)driver_handle; - if (cluster_id == OnOff::Id) { - if (attribute_id == OnOff::Attributes::OnOff::Id) { - err = light_accessory_set_power(led, val->val.b); - } - } else if (cluster_id == LevelControl::Id) { - if (attribute_id == LevelControl::Attributes::CurrentLevel::Id) { - err = light_accessory_set_brightness(led, val->val.u8); - } - } else if (cluster_id == ColorControl::Id) { - if (attribute_id == ColorControl::Attributes::CurrentHue::Id) { - err = light_accessory_set_hue(led, val->val.u8); - } else if (attribute_id == ColorControl::Attributes::CurrentSaturation::Id) { - err = light_accessory_set_saturation(led, val->val.u8); - } else if (attribute_id == ColorControl::Attributes::ColorTemperatureMireds::Id) { - err = light_accessory_set_temperature(led, val->val.u16); - } - } - } - return err; -} - -esp_err_t matter_light_set_defaults(uint16_t endpoint_id) { - esp_err_t err = ESP_OK; - - void *led = endpoint::get_priv_data(endpoint_id); - node_t *node = node::get(); - endpoint_t *endpoint = endpoint::get(node, endpoint_id); - cluster_t *cluster = NULL; - attribute_t *attribute = NULL; - esp_matter_attr_val_t val = esp_matter_invalid(NULL); - - /* Setting brightness */ - cluster = cluster::get(endpoint, LevelControl::Id); - attribute = attribute::get(cluster, LevelControl::Attributes::CurrentLevel::Id); - attribute::get_val(attribute, &val); - err |= light_accessory_set_brightness(led, val.val.u8); - - /* Setting color */ - cluster = cluster::get(endpoint, ColorControl::Id); - attribute = attribute::get(cluster, ColorControl::Attributes::ColorMode::Id); - attribute::get_val(attribute, &val); - if (val.val.u8 == (uint8_t)ColorControl::ColorMode::kCurrentHueAndCurrentSaturation) { - /* Setting hue */ - attribute = attribute::get(cluster, ColorControl::Attributes::CurrentHue::Id); - attribute::get_val(attribute, &val); - err |= light_accessory_set_hue(led, val.val.u8); - /* Setting saturation */ - attribute = attribute::get(cluster, ColorControl::Attributes::CurrentSaturation::Id); - attribute::get_val(attribute, &val); - err |= light_accessory_set_saturation(led, val.val.u8); - } else if (val.val.u8 == (uint8_t)ColorControl::ColorMode::kColorTemperature) { - /* Setting temperature */ - attribute = attribute::get(cluster, ColorControl::Attributes::ColorTemperatureMireds::Id); - attribute::get_val(attribute, &val); - err |= light_accessory_set_temperature(led, val.val.u16); - } else { - log_e("Color mode not supported"); - } - - /* Setting power */ - cluster = cluster::get(endpoint, OnOff::Id); - attribute = attribute::get(cluster, OnOff::Attributes::OnOff::Id); - attribute::get_val(attribute, &val); - err |= light_accessory_set_power(led, val.val.b); - - return err; -} - -void button_driver_init() { - /* Initialize button */ - pinMode(button_gpio, INPUT_PULLUP); -} - -// This callback is called for every attribute update. The callback implementation shall -// handle the desired attributes and return an appropriate error code. If the attribute -// is not of your interest, please do not return an error code and strictly return ESP_OK. -static esp_err_t app_attribute_update_cb( - attribute::callback_type_t type, uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val, void *priv_data -) { - esp_err_t err = ESP_OK; - - if (type == PRE_UPDATE) { - /* Driver update */ - app_driver_handle_t driver_handle = (app_driver_handle_t)priv_data; - err = matter_light_attribute_update(driver_handle, endpoint_id, cluster_id, attribute_id, val); - } - - return err; -} - -// This callback is invoked when clients interact with the Identify Cluster. -// In the callback implementation, an endpoint can identify itself. (e.g., by flashing an LED or light). -static esp_err_t app_identification_cb(identification::callback_type_t type, uint16_t endpoint_id, uint8_t effect_id, uint8_t effect_variant, void *priv_data) { - log_i("Identification callback: type: %u, effect: %u, variant: %u", type, effect_id, effect_variant); - return ESP_OK; -} - -void setup() { - esp_err_t err = ESP_OK; - - /* Initialize driver */ - app_driver_handle_t light_handle = light_accessory_init(); - button_driver_init(); - - /* Create a Matter node and add the mandatory Root Node device type on endpoint 0 */ - node::config_t node_config; - - // node handle can be used to add/modify other endpoints. - node_t *node = node::create(&node_config, app_attribute_update_cb, app_identification_cb); - if (node == nullptr) { - log_e("Failed to create Matter node"); - abort(); - } - - extended_color_light::config_t light_config; - light_config.on_off.on_off = DEFAULT_POWER; - light_config.on_off.lighting.start_up_on_off = nullptr; - light_config.level_control.current_level = DEFAULT_BRIGHTNESS; - light_config.level_control.lighting.start_up_current_level = DEFAULT_BRIGHTNESS; - light_config.color_control.color_mode = (uint8_t)ColorControl::ColorMode::kColorTemperature; - light_config.color_control.enhanced_color_mode = (uint8_t)ColorControl::ColorMode::kColorTemperature; - light_config.color_control.color_temperature.startup_color_temperature_mireds = nullptr; - - // endpoint handles can be used to add/modify clusters. - endpoint_t *endpoint = extended_color_light::create(node, &light_config, ENDPOINT_FLAG_NONE, light_handle); - if (endpoint == nullptr) { - log_e("Failed to create extended color light endpoint"); - abort(); - } - - light_endpoint_id = endpoint::get_id(endpoint); - log_i("Light created with endpoint_id %d", light_endpoint_id); - - /* Mark deferred persistence for some attributes that might be changed rapidly */ - cluster_t *level_control_cluster = cluster::get(endpoint, LevelControl::Id); - attribute_t *current_level_attribute = attribute::get(level_control_cluster, LevelControl::Attributes::CurrentLevel::Id); - attribute::set_deferred_persistence(current_level_attribute); - - cluster_t *color_control_cluster = cluster::get(endpoint, ColorControl::Id); - attribute_t *current_x_attribute = attribute::get(color_control_cluster, ColorControl::Attributes::CurrentX::Id); - attribute::set_deferred_persistence(current_x_attribute); - attribute_t *current_y_attribute = attribute::get(color_control_cluster, ColorControl::Attributes::CurrentY::Id); // codespell:ignore - attribute::set_deferred_persistence(current_y_attribute); - attribute_t *color_temp_attribute = attribute::get(color_control_cluster, ColorControl::Attributes::ColorTemperatureMireds::Id); - attribute::set_deferred_persistence(color_temp_attribute); - -#if CHIP_DEVICE_CONFIG_ENABLE_THREAD - /* Set OpenThread platform config */ - esp_openthread_platform_config_t config = { - .radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(), - .host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(), - .port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(), - }; - set_openthread_platform_config(&config); -#endif - - /* Matter start */ - err = esp_matter::start(app_event_cb); - if (err != ESP_OK) { - log_e("Failed to start Matter, err:%d", err); - abort(); - } - -#if CONFIG_ENABLE_ENCRYPTED_OTA - err = esp_matter_ota_requestor_encrypted_init(s_decryption_key, s_decryption_key_len); - if (err != ESP_OK) { - log_e("Failed to initialized the encrypted OTA, err: %d", err); - abort(); - } -#endif // CONFIG_ENABLE_ENCRYPTED_OTA - -#if CONFIG_ENABLE_CHIP_SHELL - esp_matter::console::diagnostics_register_commands(); - esp_matter::console::wifi_register_commands(); -#if CONFIG_OPENTHREAD_CLI - esp_matter::console::otcli_register_commands(); -#endif - esp_matter::console::init(); -#endif -} - -void loop() { - static uint32_t button_time_stamp = 0; - static bool button_state = false; - static bool started = false; - - if (!isAccessoryCommissioned()) { - log_w("Accessory not commissioned yet. Waiting for commissioning."); -#ifdef RGB_BUILTIN - rgbLedWrite(RGB_BUILTIN, 48, 0, 20); // Purple indicates accessory not commissioned -#endif - delay(5000); - return; - } - -#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION - if (!isWifiConnected()) { - log_w("Wi-Fi not connected yet. Waiting for connection."); -#ifdef RGB_BUILTIN - rgbLedWrite(RGB_BUILTIN, 48, 20, 0); // Orange indicates accessory not connected to Wi-Fi -#endif - delay(5000); - return; - } -#endif - -#if CHIP_DEVICE_CONFIG_ENABLE_THREAD - if (!isThreadConnected()) { - log_w("Thread not connected yet. Waiting for connection."); -#ifdef RGB_BUILTIN - rgbLedWrite(RGB_BUILTIN, 0, 20, 48); // Blue indicates accessory not connected to Trhead -#endif - delay(5000); - return; - } -#endif - - // Once all network connections are established, the accessory is ready for use - // Run it only once - if (!started) { - log_i("Accessory is commissioned and connected to Wi-Fi. Ready for use."); - started = true; - // Starting driver with default values - matter_light_set_defaults(light_endpoint_id); - } - - // Check if the button is pressed and toggle the light right away - if (digitalRead(button_gpio) == LOW && !button_state) { - // deals with button debounce - button_time_stamp = millis(); // record the time while the button is pressed. - button_state = true; // pressed. - - // Toggle button is pressed - toggle the light - log_i("Toggle button pressed"); - - endpoint_t *endpoint = endpoint::get(node::get(), light_endpoint_id); - cluster_t *cluster = cluster::get(endpoint, OnOff::Id); - attribute_t *attribute = attribute::get(cluster, OnOff::Attributes::OnOff::Id); - - esp_matter_attr_val_t val = esp_matter_invalid(NULL); - attribute::get_val(attribute, &val); - val.val.b = !val.val.b; - attribute::update(light_endpoint_id, OnOff::Id, OnOff::Attributes::OnOff::Id, &val); - } - - // Check if the button is released and handle the factory reset - uint32_t time_diff = millis() - button_time_stamp; - if (button_state && time_diff > 100 && digitalRead(button_gpio) == HIGH) { - button_state = false; // released. It can be pressed again after 100ms debounce. - - // Factory reset is triggered if the button is pressed for more than 10 seconds - if (time_diff > 10000) { - log_i("Factory reset triggered. Light will restored to factory settings."); - esp_matter::factory_reset(); - } - } - - delay(50); // WDT is happier with a delay -} diff --git a/examples/espidf-arduino-matter-light/partitions.csv b/examples/espidf-arduino-matter-light/partitions.csv index ffe5f24..a39be72 100644 --- a/examples/espidf-arduino-matter-light/partitions.csv +++ b/examples/espidf-arduino-matter-light/partitions.csv @@ -1,10 +1,10 @@ -# Name, Type, SubType, Offset, Size, Flags -# Note: Firmware partition offset needs to be 64K aligned, initial 36K (9 sectors) are reserved for bootloader and partition table -esp_secure_cert, 0x3F, ,0xd000, 0x2000, encrypted -nvs, data, nvs, 0x10000, 0xC000, -nvs_keys, data, nvs_keys,, 0x1000, encrypted -otadata, data, ota, , 0x2000 -phy_init, data, phy, , 0x1000, -ota_0, app, ota_0, 0x20000, 0x1E0000, -ota_1, app, ota_1, 0x200000, 0x1E0000, -fctry, data, nvs, 0x3E0000, 0x6000 +# Name, Type, SubType, Offset, Size, Flags +# Note: Firmware partition offset needs to be 64K aligned, initial 36K (9 sectors) are reserved for bootloader and partition table +esp_secure_cert, 0x3F, ,0xd000, 0x2000, encrypted +nvs, data, nvs, 0x10000, 0xC000, +nvs_keys, data, nvs_keys,, 0x1000, encrypted +otadata, data, ota, , 0x2000 +phy_init, data, phy, , 0x1000, +ota_0, app, ota_0, 0x20000, 0x1E0000, +ota_1, app, ota_1, 0x200000, 0x1E0000, +fctry, data, nvs, 0x3E0000, 0x6000 diff --git a/examples/espidf-arduino-matter-light/sdkconfig.defaults b/examples/espidf-arduino-matter-light/sdkconfig.defaults index 7982e41..cfe80f2 100644 --- a/examples/espidf-arduino-matter-light/sdkconfig.defaults +++ b/examples/espidf-arduino-matter-light/sdkconfig.defaults @@ -25,10 +25,10 @@ CONFIG_BT_ENABLED=y CONFIG_BT_NIMBLE_ENABLED=y #disable BT connection reattempt -CONFIG_BT_NIMBLE_ENABLE_CONN_REATTEMPT=n +# CONFIG_BT_NIMBLE_ENABLE_CONN_REATTEMPT is not set # NIMBLE -CONFIG_BT_NIMBLE_EXT_ADV=n +# CONFIG_BT_NIMBLE_EXT_ADV is not set CONFIG_BT_NIMBLE_HCI_EVT_BUF_SIZE=70 CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING=y @@ -41,22 +41,22 @@ CONFIG_PARTITION_TABLE_FILENAME="partitions.csv" CONFIG_PARTITION_TABLE_OFFSET=0xC000 # Disable chip shell -CONFIG_ENABLE_CHIP_SHELL=n +# CONFIG_ENABLE_CHIP_SHELL is not set # Enable OTA Requester -CONFIG_ENABLE_OTA_REQUESTOR=n +# CONFIG_ENABLE_OTA_REQUESTOR is not set #enable lwIP route hooks CONFIG_LWIP_HOOK_IP6_ROUTE_DEFAULT=y CONFIG_LWIP_HOOK_ND6_GET_GW_DEFAULT=y # disable softap by default -CONFIG_ESP_WIFI_SOFTAP_SUPPORT=n +# CONFIG_ESP_WIFI_SOFTAP_SUPPORT is not set CONFIG_ENABLE_WIFI_STATION=y -CONFIG_ENABLE_WIFI_AP=n +# CONFIG_ENABLE_WIFI_AP is not set # Disable DS Peripheral -CONFIG_ESP_SECURE_CERT_DS_PERIPHERAL=n +# CONFIG_ESP_SECURE_CERT_DS_PERIPHERAL is not set # Use compact attribute storage mode CONFIG_ESP_MATTER_NVS_USE_COMPACT_ATTR_STORAGE=y diff --git a/examples/espidf-arduino-matter-light/sdkconfig.defaults.esp32c6 b/examples/espidf-arduino-matter-light/sdkconfig.defaults.esp32c6 index 9fe5896..54ac2c7 100644 --- a/examples/espidf-arduino-matter-light/sdkconfig.defaults.esp32c6 +++ b/examples/espidf-arduino-matter-light/sdkconfig.defaults.esp32c6 @@ -4,9 +4,9 @@ CONFIG_IDF_TARGET="esp32c6" CONFIG_LIBSODIUM_USE_MBEDTLS_SHA=y # NIMBLE -CONFIG_BT_NIMBLE_EXT_ADV=n +# CONFIG_BT_NIMBLE_EXT_ADV is not set CONFIG_BT_NIMBLE_HCI_EVT_BUF_SIZE=70 -CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING=y +# CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING is not set # FreeRTOS should use legacy API CONFIG_FREERTOS_ENABLE_BACKWARD_COMPATIBILITY=y diff --git a/examples/espidf-arduino-matter-light/sdkconfig.defaults.c6_thread b/examples/espidf-arduino-matter-light/sdkconfig.defaults.thread similarity index 76% rename from examples/espidf-arduino-matter-light/sdkconfig.defaults.c6_thread rename to examples/espidf-arduino-matter-light/sdkconfig.defaults.thread index 502480f..b79ef84 100644 --- a/examples/espidf-arduino-matter-light/sdkconfig.defaults.c6_thread +++ b/examples/espidf-arduino-matter-light/sdkconfig.defaults.thread @@ -26,9 +26,9 @@ CONFIG_LIBSODIUM_USE_MBEDTLS_SHA=y # NIMBLE CONFIG_BT_ENABLED=y CONFIG_BT_NIMBLE_ENABLED=y -CONFIG_BT_NIMBLE_EXT_ADV=n +# CONFIG_BT_NIMBLE_EXT_ADV is not set CONFIG_BT_NIMBLE_HCI_EVT_BUF_SIZE=70 -CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING=n +# CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING is not set # FreeRTOS should use legacy API CONFIG_FREERTOS_ENABLE_BACKWARD_COMPATIBILITY=y @@ -37,12 +37,12 @@ CONFIG_FREERTOS_ENABLE_BACKWARD_COMPATIBILITY=y CONFIG_OPENTHREAD_ENABLED=y CONFIG_OPENTHREAD_SRP_CLIENT=y CONFIG_OPENTHREAD_DNS_CLIENT=y -CONFIG_OPENTHREAD_LOG_LEVEL_DYNAMIC=n +# CONFIG_OPENTHREAD_LOG_LEVEL_DYNAMIC is not set CONFIG_OPENTHREAD_LOG_LEVEL_NOTE=y -CONFIG_OPENTHREAD_CLI=n +# CONFIG_OPENTHREAD_CLI is not set # Disable lwip ipv6 autoconfig -CONFIG_LWIP_IPV6_AUTOCONFIG=n +# CONFIG_LWIP_IPV6_AUTOCONFIG is not set # Use a custom partition table CONFIG_PARTITION_TABLE_CUSTOM=y @@ -53,21 +53,22 @@ CONFIG_LWIP_IPV6_NUM_ADDRESSES=8 CONFIG_LWIP_MULTICAST_PING=y # MDNS platform -CONFIG_USE_MINIMAL_MDNS=n +# CONFIG_USE_MINIMAL_MDNS is not set CONFIG_ENABLE_EXTENDED_DISCOVERY=y # Enable OTA Requester -CONFIG_ENABLE_OTA_REQUESTOR=n +# CONFIG_ENABLE_OTA_REQUESTOR is not set # Disable STA and AP for ESP32C6 -CONFIG_ENABLE_WIFI_STATION=n -CONFIG_ENABLE_WIFI_AP=n +# CONFIG_ESP_WIFI_ENABLED is not set +# CONFIG_ENABLE_WIFI_STATION is not set +# CONFIG_ENABLE_WIFI_AP is not set # Enable chip shell -CONFIG_ENABLE_CHIP_SHELL=n +# CONFIG_ENABLE_CHIP_SHELL is not set # Disable persist subscriptions -CONFIG_ENABLE_PERSIST_SUBSCRIPTIONS=n +# CONFIG_ENABLE_PERSIST_SUBSCRIPTIONS is not set # MRP configs CONFIG_MRP_LOCAL_ACTIVE_RETRY_INTERVAL_FOR_THREAD=5000