From 09eba3f455c63a79ba97c916ee0e977431309302 Mon Sep 17 00:00:00 2001 From: Chirag Atal Date: Wed, 3 Aug 2022 16:43:51 +0530 Subject: [PATCH] app_driver: Restructuring setting the driver to default value led_driver: Removing the get() APIs --- device_hal/led_driver/gpio/led_driver.c | 25 -------- device_hal/led_driver/hollow_led/led_driver.c | 29 --------- device_hal/led_driver/include/led_driver.h | 6 -- device_hal/led_driver/vled/led_driver.c | 27 +------- device_hal/led_driver/ws2812/led_driver.c | 27 +------- examples/light/main/app_driver.cpp | 63 ++++++++++++------- examples/light/main/app_main.cpp | 13 ++-- examples/light/main/app_priv.h | 12 ++-- examples/light_switch/main/app_driver.cpp | 27 -------- examples/light_switch/main/app_priv.h | 9 --- examples/zap_light/main/app_driver.cpp | 58 +++++++++-------- examples/zap_light/main/app_main.cpp | 7 ++- examples/zap_light/main/app_priv.h | 12 ++-- 13 files changed, 101 insertions(+), 214 deletions(-) diff --git a/device_hal/led_driver/gpio/led_driver.c b/device_hal/led_driver/gpio/led_driver.c index e67454c07..d7604157c 100644 --- a/device_hal/led_driver/gpio/led_driver.c +++ b/device_hal/led_driver/gpio/led_driver.c @@ -105,28 +105,3 @@ esp_err_t led_driver_set_temperature(led_driver_handle_t handle, uint32_t temper { return ESP_ERR_NOT_SUPPORTED; } - -bool led_driver_get_power(led_driver_handle_t handle) -{ - return current_power; -} - -uint8_t led_driver_get_brightness(led_driver_handle_t handle) -{ - return current_brightness; -} - -uint16_t led_driver_get_hue(led_driver_handle_t handle) -{ - return 0; -} - -uint8_t led_driver_get_saturation(led_driver_handle_t handle) -{ - return 0; -} - -uint32_t led_driver_get_temperature(led_driver_handle_t handle) -{ - return 0; -} diff --git a/device_hal/led_driver/hollow_led/led_driver.c b/device_hal/led_driver/hollow_led/led_driver.c index c0a5445d3..ecca5f466 100644 --- a/device_hal/led_driver/hollow_led/led_driver.c +++ b/device_hal/led_driver/hollow_led/led_driver.c @@ -16,10 +16,6 @@ #include static const char *TAG = "led_driver_hollow"; -static bool current_power = false; -static uint8_t current_brightness = 0; -static HS_color_t current_HS = {0, 0}; -static uint32_t current_temperature = 6600; led_driver_handle_t led_driver_init(led_driver_config_t *config) { @@ -68,28 +64,3 @@ esp_err_t led_driver_set_temperature(led_driver_handle_t handle, uint32_t temper return ESP_OK; } - -bool led_driver_get_power(led_driver_handle_t handle) -{ - return current_power; -} - -uint8_t led_driver_get_brightness(led_driver_handle_t handle) -{ - return current_brightness; -} - -uint16_t led_driver_get_hue(led_driver_handle_t handle) -{ - return current_HS.hue; -} - -uint8_t led_driver_get_saturation(led_driver_handle_t handle) -{ - return current_HS.saturation; -} - -uint32_t led_driver_get_temperature(led_driver_handle_t handle) -{ - return current_temperature; -} diff --git a/device_hal/led_driver/include/led_driver.h b/device_hal/led_driver/include/led_driver.h index 1d5d624a8..97981ebf3 100644 --- a/device_hal/led_driver/include/led_driver.h +++ b/device_hal/led_driver/include/led_driver.h @@ -34,12 +34,6 @@ esp_err_t led_driver_set_hue(led_driver_handle_t handle, uint16_t hue); esp_err_t led_driver_set_saturation(led_driver_handle_t handle, uint8_t saturation); esp_err_t led_driver_set_temperature(led_driver_handle_t handle, uint32_t temperature); -bool led_driver_get_power(led_driver_handle_t handle); -uint8_t led_driver_get_brightness(led_driver_handle_t handle); -uint16_t led_driver_get_hue(led_driver_handle_t handle); -uint8_t led_driver_get_saturation(led_driver_handle_t handle); -uint32_t led_driver_get_temperature(led_driver_handle_t handle); - #ifdef __cplusplus } #endif diff --git a/device_hal/led_driver/vled/led_driver.c b/device_hal/led_driver/vled/led_driver.c index 839828f53..e311b3048 100644 --- a/device_hal/led_driver/vled/led_driver.c +++ b/device_hal/led_driver/vled/led_driver.c @@ -29,7 +29,7 @@ static const char *TAG = "led_driver_vled"; static bool current_power = false; static uint8_t current_brightness = 0; static HS_color_t current_HS = {0, 0}; -static uint32_t current_temperature = 6600; +static uint32_t current_temperature = 0; static RGB_color_t mRGB; static uint16_t DisplayHeight; static uint16_t DisplayWidth; @@ -196,28 +196,3 @@ esp_err_t led_driver_set_temperature(led_driver_handle_t handle, uint32_t temper hsv_to_rgb(current_HS, brightness, &mRGB); return led_driver_set_RGB(handle); } - -bool led_driver_get_power(led_driver_handle_t handle) -{ - return current_power; -} - -uint8_t led_driver_get_brightness(led_driver_handle_t handle) -{ - return current_brightness; -} - -uint16_t led_driver_get_hue(led_driver_handle_t handle) -{ - return current_HS.hue; -} - -uint8_t led_driver_get_saturation(led_driver_handle_t handle) -{ - return current_HS.saturation; -} - -uint32_t led_driver_get_temperature(led_driver_handle_t handle) -{ - return current_temperature; -} diff --git a/device_hal/led_driver/ws2812/led_driver.c b/device_hal/led_driver/ws2812/led_driver.c index 0e61166fc..3eb160f39 100644 --- a/device_hal/led_driver/ws2812/led_driver.c +++ b/device_hal/led_driver/ws2812/led_driver.c @@ -20,7 +20,7 @@ static const char *TAG = "led_driver_ws2812"; static bool current_power = false; static uint8_t current_brightness = 0; -static uint32_t current_temp = 6600; +static uint32_t current_temp = 0; static HS_color_t current_HS = {0, 0}; static RGB_color_t mRGB; @@ -113,28 +113,3 @@ esp_err_t led_driver_set_temperature(led_driver_handle_t handle, uint32_t temper hsv_to_rgb(current_HS, brightness, &mRGB); return led_driver_set_RGB(handle); } - -bool led_driver_get_power(led_driver_handle_t handle) -{ - return current_power; -} - -uint8_t led_driver_get_brightness(led_driver_handle_t handle) -{ - return current_brightness; -} - -uint16_t led_driver_get_hue(led_driver_handle_t handle) -{ - return current_HS.hue; -} - -uint8_t led_driver_get_saturation(led_driver_handle_t handle) -{ - return current_HS.saturation; -} - -uint32_t led_driver_get_temperature(led_driver_handle_t handle) -{ - return current_temp; -} diff --git a/examples/light/main/app_driver.cpp b/examples/light/main/app_driver.cpp index d1c35cb20..f477872cc 100644 --- a/examples/light/main/app_driver.cpp +++ b/examples/light/main/app_driver.cpp @@ -97,30 +97,51 @@ esp_err_t app_driver_attribute_update(uint16_t endpoint_id, uint32_t cluster_id, return err; } -esp_err_t app_driver_attribute_set_defaults() +esp_err_t app_driver_light_set_defaults(uint16_t endpoint_id) { - /* Get the default value (current value) from esp_matter and update the app_driver */ esp_err_t err = ESP_OK; + void *priv_data = endpoint::get_priv_data(endpoint_id); + led_driver_handle_t handle = (led_driver_handle_t)priv_data; node_t *node = node::get(); - endpoint_t *endpoint = endpoint::get_first(node); - while (endpoint) { - uint16_t endpoint_id = endpoint::get_id(endpoint); - cluster_t *cluster = cluster::get_first(endpoint); - while (cluster) { - uint32_t cluster_id = cluster::get_id(cluster); - attribute_t *attribute = attribute::get_first(cluster); - while (attribute) { - uint32_t attribute_id = attribute::get_id(attribute); - esp_matter_attr_val_t val = esp_matter_invalid(NULL); - err |= attribute::get_val(attribute, &val); - void *priv_data = endpoint::get_priv_data(endpoint_id); - err |= app_driver_attribute_update(endpoint_id, cluster_id, attribute_id, &val, priv_data); - attribute = attribute::get_next(attribute); - } - cluster = cluster::get_next(cluster); - } - endpoint = endpoint::get_next(endpoint); + 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 |= app_driver_light_set_brightness(handle, &val); + + /* 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 == EMBER_ZCL_COLOR_MODE_CURRENT_HUE_AND_CURRENT_SATURATION) { + /* Setting hue */ + attribute = attribute::get(cluster, ColorControl::Attributes::CurrentHue::Id); + attribute::get_val(attribute, &val); + err |= app_driver_light_set_hue(handle, &val); + /* Setting saturation */ + attribute = attribute::get(cluster, ColorControl::Attributes::CurrentSaturation::Id); + attribute::get_val(attribute, &val); + err |= app_driver_light_set_saturation(handle, &val); + } else if (val.val.u8 == EMBER_ZCL_COLOR_MODE_COLOR_TEMPERATURE) { + /* Setting temperature */ + attribute = attribute::get(cluster, ColorControl::Attributes::ColorTemperature::Id); + attribute::get_val(attribute, &val); + err |= app_driver_light_set_temperature(handle, &val); + } else { + ESP_LOGE(TAG, "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 |= app_driver_light_set_power(handle, &val); + return err; } @@ -132,7 +153,7 @@ void *app_driver_light_init() return (void *)handle; } -void *app_driver_switch_init() +void *app_driver_button_init() { /* Initialize button */ button_config_t config = button_driver_get_config(); diff --git a/examples/light/main/app_main.cpp b/examples/light/main/app_main.cpp index 1d111f06b..3eb010b26 100644 --- a/examples/light/main/app_main.cpp +++ b/examples/light/main/app_main.cpp @@ -75,8 +75,8 @@ extern "C" void app_main() /* Initialize driver */ void *light_handle = app_driver_light_init(); - void *switch_handle = app_driver_switch_init(); - app_reset_button_register(switch_handle); + void *button_handle = app_driver_button_init(); + app_reset_button_register(button_handle); /* Create a Matter node */ node::config_t node_config; @@ -84,7 +84,10 @@ extern "C" void app_main() color_temperature_light::config_t light_config; light_config.on_off.on_off = DEFAULT_POWER; + light_config.on_off.lighting.start_up_on_off = DEFAULT_POWER; 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 = EMBER_ZCL_COLOR_MODE_COLOR_TEMPERATURE; endpoint_t *endpoint = color_temperature_light::create(node, &light_config, ENDPOINT_FLAG_NONE, light_handle); /* These node and endpoint handles can be used to create/add other endpoints and clusters. */ @@ -102,9 +105,6 @@ extern "C" void app_main() hue_saturation_config.current_saturation = DEFAULT_SATURATION; cluster::color_control::feature::hue_saturation::add(cluster, &hue_saturation_config); - /* Initialize driver defaults */ - app_driver_attribute_set_defaults(); - /* Matter start */ err = esp_matter::start(app_event_cb); if (err != ESP_OK) { @@ -112,6 +112,9 @@ extern "C" void app_main() } app_qrcode_print(); + /* Starting driver with default values */ + app_driver_light_set_defaults(light_endpoint_id); + #if CONFIG_ENABLE_CHIP_SHELL esp_matter_console_diagnostics_register_commands(); esp_matter_console_init(); diff --git a/examples/light/main/app_priv.h b/examples/light/main/app_priv.h index 6da8a0f5c..79e46c2fe 100644 --- a/examples/light/main/app_priv.h +++ b/examples/light/main/app_priv.h @@ -38,14 +38,14 @@ */ void *app_driver_light_init(); -/** Initialize the switch driver +/** Initialize the button driver * - * This initializes the switch driver associated with the selected board. + * This initializes the button driver associated with the selected board. * * @return Handle on success. * @return NULL in case of failure. */ -void *app_driver_switch_init(); +void *app_driver_button_init(); /** Driver Update * @@ -63,11 +63,13 @@ void *app_driver_switch_init(); esp_err_t app_driver_attribute_update(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val, void *priv_data); -/** Set driver defaults +/** Set defaults for light driver * * Set the attribute drivers to their default values from the created data model. * + * @param[in] endpoint_id Endpoint ID of the driver. + * * @return ESP_OK on success. * @return error in case of failure. */ -esp_err_t app_driver_attribute_set_defaults(); +esp_err_t app_driver_light_set_defaults(uint16_t endpoint_id); diff --git a/examples/light_switch/main/app_driver.cpp b/examples/light_switch/main/app_driver.cpp index cf2c8b3cc..6d6371ccb 100644 --- a/examples/light_switch/main/app_driver.cpp +++ b/examples/light_switch/main/app_driver.cpp @@ -136,33 +136,6 @@ esp_err_t app_driver_attribute_update(uint16_t endpoint_id, uint32_t cluster_id, return ESP_OK; } -esp_err_t app_driver_attribute_set_defaults() -{ - /* Get the default value (current value) from esp_matter and update the app_driver */ - esp_err_t err = ESP_OK; - node_t *node = node::get(); - endpoint_t *endpoint = endpoint::get_first(node); - while (endpoint) { - uint16_t endpoint_id = endpoint::get_id(endpoint); - cluster_t *cluster = cluster::get_first(endpoint); - while (cluster) { - uint32_t cluster_id = cluster::get_id(cluster); - attribute_t *attribute = attribute::get_first(cluster); - while (attribute) { - uint32_t attribute_id = attribute::get_id(attribute); - esp_matter_attr_val_t val = esp_matter_invalid(NULL); - err |= attribute::get_val(attribute, &val); - void *priv_data = endpoint::get_priv_data(endpoint_id); - err |= app_driver_attribute_update(endpoint_id, cluster_id, attribute_id, &val, priv_data); - attribute = attribute::get_next(attribute); - } - cluster = cluster::get_next(cluster); - } - endpoint = endpoint::get_next(endpoint); - } - return err; -} - void *app_driver_switch_init() { /* Initialize button */ diff --git a/examples/light_switch/main/app_priv.h b/examples/light_switch/main/app_priv.h index b2db3a4f5..321aee886 100644 --- a/examples/light_switch/main/app_priv.h +++ b/examples/light_switch/main/app_priv.h @@ -35,12 +35,3 @@ void *app_driver_switch_init(); */ esp_err_t app_driver_attribute_update(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val, void *priv_data); - -/** Set driver defaults - * - * Set the attribute drivers to their default values from the created data model. - * - * @return ESP_OK on success. - * @return error in case of failure. - */ -esp_err_t app_driver_attribute_set_defaults(); diff --git a/examples/zap_light/main/app_driver.cpp b/examples/zap_light/main/app_driver.cpp index 6570cb836..cab4e61e5 100644 --- a/examples/zap_light/main/app_driver.cpp +++ b/examples/zap_light/main/app_driver.cpp @@ -95,53 +95,57 @@ esp_err_t app_driver_attribute_update(uint16_t endpoint_id, uint32_t cluster_id, return err; } -esp_err_t app_driver_attribute_set_defaults() +esp_err_t app_driver_light_set_defaults(uint16_t endpoint_id) { /* When using static endpoints, i.e. using the data model from zap-generated, this needs to be done after esp_matter::start() */ /* Get the default value (current value) from matter submodule and update the app_driver */ esp_err_t err = ESP_OK; + led_driver_handle_t handle = (led_driver_handle_t)light_handle; uint8_t value; uint16_t value_u16; - uint16_t endpoint_id = 0; uint32_t cluster_id = 0; uint32_t attribute_id = 0; esp_matter_attr_val_t val = esp_matter_invalid(NULL); - endpoint_id = light_endpoint_id; - cluster_id = OnOff::Id; - attribute_id = OnOff::Attributes::OnOff::Id; - attribute::get_val_raw(endpoint_id, cluster_id, attribute_id, &value, sizeof(uint8_t)); - val = esp_matter_bool(value); - err |= app_driver_attribute_update(endpoint_id, cluster_id, attribute_id, &val, light_handle); - - endpoint_id = light_endpoint_id; + /* Setting brightness */ cluster_id = LevelControl::Id; attribute_id = LevelControl::Attributes::CurrentLevel::Id; attribute::get_val_raw(endpoint_id, cluster_id, attribute_id, &value, sizeof(uint8_t)); val = esp_matter_uint8(value); - err |= app_driver_attribute_update(endpoint_id, cluster_id, attribute_id, &val, light_handle); + err |= app_driver_light_set_brightness(handle, &val); - endpoint_id = light_endpoint_id; + /* Setting color */ cluster_id = ColorControl::Id; attribute_id = ColorControl::Attributes::CurrentHue::Id; attribute::get_val_raw(endpoint_id, cluster_id, attribute_id, &value, sizeof(uint8_t)); - val = esp_matter_uint8(value); - err |= app_driver_attribute_update(endpoint_id, cluster_id, attribute_id, &val, light_handle); + if (value == EMBER_ZCL_COLOR_MODE_CURRENT_HUE_AND_CURRENT_SATURATION) { + /* Setting hue */ + attribute_id = ColorControl::Attributes::CurrentHue::Id; + attribute::get_val_raw(endpoint_id, cluster_id, attribute_id, &value, sizeof(uint8_t)); + val = esp_matter_uint8(value); + err |= app_driver_light_set_hue(handle, &val); + /* Setting saturation */ + attribute_id = ColorControl::Attributes::CurrentSaturation::Id; + attribute::get_val_raw(endpoint_id, cluster_id, attribute_id, &value, sizeof(uint8_t)); + val = esp_matter_uint8(value); + err |= app_driver_light_set_saturation(handle, &val); + } else if (value == EMBER_ZCL_COLOR_MODE_COLOR_TEMPERATURE) { + /* Setting temperature */ + attribute_id = ColorControl::Attributes::ColorTemperature::Id; + attribute::get_val_raw(endpoint_id, cluster_id, attribute_id, (uint8_t *)&value_u16, sizeof(uint16_t)); + val = esp_matter_uint16(value_u16); + err |= app_driver_light_set_temperature(handle, &val); + } else { + ESP_LOGE(TAG, "Color mode not supported"); + } - endpoint_id = light_endpoint_id; - cluster_id = ColorControl::Id; - attribute_id = ColorControl::Attributes::CurrentSaturation::Id; + /* Setting power */ + cluster_id = OnOff::Id; + attribute_id = OnOff::Attributes::OnOff::Id; attribute::get_val_raw(endpoint_id, cluster_id, attribute_id, &value, sizeof(uint8_t)); - val = esp_matter_uint8(value); - err |= app_driver_attribute_update(endpoint_id, cluster_id, attribute_id, &val, light_handle); - - endpoint_id = light_endpoint_id; - cluster_id = ColorControl::Id; - attribute_id = ColorControl::Attributes::ColorTemperature::Id; - attribute::get_val_raw(endpoint_id, cluster_id, attribute_id, (uint8_t *)&value_u16, sizeof(uint16_t)); - val = esp_matter_uint16(value_u16); - err |= app_driver_attribute_update(endpoint_id, cluster_id, attribute_id, &val, light_handle); + val = esp_matter_bool(value); + err |= app_driver_light_set_power(handle, &val); return err; } @@ -154,7 +158,7 @@ void *app_driver_light_init() return (void *)handle; } -void *app_driver_switch_init() +void *app_driver_button_init() { /* Initialize button */ button_config_t config = button_driver_get_config(); diff --git a/examples/zap_light/main/app_main.cpp b/examples/zap_light/main/app_main.cpp index caf93c28c..3b16fe981 100644 --- a/examples/zap_light/main/app_main.cpp +++ b/examples/zap_light/main/app_main.cpp @@ -66,8 +66,8 @@ extern "C" void app_main() /* Initialize driver */ light_handle = app_driver_light_init(); - void *switch_handle = app_driver_switch_init(); - app_reset_button_register(switch_handle); + void *button_handle = app_driver_button_init(); + app_reset_button_register(button_handle); /* Initialize matter callback */ attribute::set_callback(app_attribute_update_cb); @@ -80,7 +80,8 @@ extern "C" void app_main() } app_qrcode_print(); - app_driver_attribute_set_defaults(); + /* Starting driver with default values */ + app_driver_light_set_defaults(light_endpoint_id); #if CONFIG_ENABLE_CHIP_SHELL esp_matter_console_diagnostics_register_commands(); diff --git a/examples/zap_light/main/app_priv.h b/examples/zap_light/main/app_priv.h index 60786dd22..85e938747 100644 --- a/examples/zap_light/main/app_priv.h +++ b/examples/zap_light/main/app_priv.h @@ -32,14 +32,14 @@ */ void *app_driver_light_init(); -/** Initialize the switch driver +/** Initialize the button driver * - * This initializes the switch driver associated with the selected board. + * This initializes the button driver associated with the selected board. * * @return Handle on success. * @return NULL in case of failure. */ -void *app_driver_switch_init(); +void *app_driver_button_init(); /** Driver Update * @@ -57,13 +57,15 @@ void *app_driver_switch_init(); esp_err_t app_driver_attribute_update(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val, void *priv_data); -/** Set driver defaults +/** Set defaults for light driver * * Set the attribute drivers to their default values. * This API should be called after esp_matter::start() because, for zap data model, the attribute defaults are a part * of zap-generated and they are initialized in esp_matter::start(). * + * @param[in] endpoint_id Endpoint ID of the driver. + * * @return ESP_OK on success. * @return error in case of failure. */ -esp_err_t app_driver_attribute_set_defaults(); +esp_err_t app_driver_light_set_defaults(uint16_t endpoint_id);