Files
esp-matter/examples/rainmaker_light/main/app_driver.cpp
T
Chirag Atal 743f5cd90e light: Change color_dimmable_light to color_temperature_light
Added support for temperature in app_driver.
Adding hue and saturation after the color_temperature_light endpoint has been created.
Similar changes in other light examples.
2022-05-28 07:43:20 +08:00

142 lines
4.9 KiB
C++

/*
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 <esp_log.h>
#include <stdlib.h>
#include <string.h>
#include <device.h>
#include <esp_matter.h>
#include <led_driver.h>
#include <app_reset.h>
#include <app_priv.h>
using namespace esp_matter;
static const char *TAG = "app_driver";
extern uint16_t light_endpoint_id;
/* Do any conversions/remapping for the actual value here */
static esp_err_t app_driver_light_set_power(esp_matter_attr_val_t *val)
{
return led_driver_set_power(val->val.b);
}
static esp_err_t app_driver_light_set_brightness(esp_matter_attr_val_t *val)
{
int value = REMAP_TO_RANGE(val->val.u8, MATTER_BRIGHTNESS, STANDARD_BRIGHTNESS);
return led_driver_set_brightness(value);
}
static esp_err_t app_driver_light_set_hue(esp_matter_attr_val_t *val)
{
int value = REMAP_TO_RANGE(val->val.u8, MATTER_HUE, STANDARD_HUE);
return led_driver_set_hue(value);
}
static esp_err_t app_driver_light_set_saturation(esp_matter_attr_val_t *val)
{
int value = REMAP_TO_RANGE(val->val.u8, MATTER_SATURATION, STANDARD_SATURATION);
return led_driver_set_saturation(value);
}
static esp_err_t app_driver_light_set_temperature(esp_matter_attr_val_t *val)
{
uint32_t value = REMAP_TO_RANGE_INVERSE(val->val.u16, STANDARD_TEMPERATURE_FACTOR);
return led_driver_set_temperature(value);
}
static void app_driver_button_toggle_cb(void *arg)
{
ESP_LOGI(TAG, "Toggle button pressed");
uint16_t endpoint_id = light_endpoint_id;
uint32_t cluster_id = OnOff::Id;
uint32_t attribute_id = OnOff::Attributes::OnOff::Id;
node_t *node = node::get();
endpoint_t *endpoint = endpoint::get(node, endpoint_id);
cluster_t *cluster = cluster::get(endpoint, cluster_id);
attribute_t *attribute = attribute::get(cluster, attribute_id);
esp_matter_attr_val_t val = esp_matter_invalid(NULL);
attribute::get_val(attribute, &val);
val.val.b = !val.val.b;
attribute::update(endpoint_id, cluster_id, attribute_id, &val);
}
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)
{
esp_err_t err = ESP_OK;
if (endpoint_id == light_endpoint_id) {
if (cluster_id == OnOff::Id) {
if (attribute_id == OnOff::Attributes::OnOff::Id) {
err = app_driver_light_set_power(val);
}
} else if (cluster_id == LevelControl::Id) {
if (attribute_id == LevelControl::Attributes::CurrentLevel::Id) {
err = app_driver_light_set_brightness(val);
}
} else if (cluster_id == ColorControl::Id) {
if (attribute_id == ColorControl::Attributes::CurrentHue::Id) {
err = app_driver_light_set_hue(val);
} else if (attribute_id == ColorControl::Attributes::CurrentSaturation::Id) {
err = app_driver_light_set_saturation(val);
} else if (attribute_id == ColorControl::Attributes::ColorTemperature::Id) {
err = app_driver_light_set_temperature(val);
}
}
}
return err;
}
static 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);
err |= app_driver_attribute_update(endpoint_id, cluster_id, attribute_id, &val);
attribute = attribute::get_next(attribute);
}
cluster = cluster::get_next(cluster);
}
endpoint = endpoint::get_next(endpoint);
}
return err;
}
esp_err_t app_driver_init()
{
ESP_LOGI(TAG, "Initialising driver");
/* Initialize button */
button_config_t button_config = button_driver_get_config();
button_handle_t handle = iot_button_create(&button_config);
iot_button_register_cb(handle, BUTTON_PRESS_DOWN, app_driver_button_toggle_cb);
app_reset_button_register(handle);
/* Initialize led */
led_driver_config_t led_config = led_driver_get_config();
led_driver_init(&led_config);
app_driver_attribute_set_defaults();
return ESP_OK;
}