|
| 1 | +// Copyright 2024 Espressif Systems (Shanghai) PTE LTD |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | + |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#pragma once |
| 16 | +#include <sdkconfig.h> |
| 17 | +#ifdef CONFIG_ESP_MATTER_ENABLE_DATA_MODEL |
| 18 | + |
| 19 | +#include <Matter.h> |
| 20 | +#include <MatterEndPoint.h> |
| 21 | + |
| 22 | +class MatterEnhancedColorLight : public MatterEndPoint { |
| 23 | +public: |
| 24 | + static const uint8_t MAX_BRIGHTNESS = 255; |
| 25 | + static const uint16_t MAX_COLOR_TEMPERATURE = 500; |
| 26 | + static const uint16_t MIN_COLOR_TEMPERATURE = 100; |
| 27 | + |
| 28 | + MatterEnhancedColorLight(); |
| 29 | + ~MatterEnhancedColorLight(); |
| 30 | + // default initial state is off, brigthness = 25 (10%), HSV(21, 216, 25), color temperature is 454 (Warm White) |
| 31 | + virtual bool begin(bool initialState = false, espHsvColor_t colorHSV = { 21, 216, 25}, uint8_t newBrightness = 25, uint16_t colorTemperature = 454); |
| 32 | + // this will just stop processing Light Matter events |
| 33 | + void end(); |
| 34 | + |
| 35 | + bool setOnOff(bool newState); // returns true if successful |
| 36 | + bool getOnOff(); // returns current light state |
| 37 | + bool toggle(); // returns true if successful |
| 38 | + |
| 39 | + bool setColorTemperature(uint16_t newTemperature); // returns true if successful |
| 40 | + uint16_t getColorTemperature(); // returns current temperature |
| 41 | + |
| 42 | + bool setBrightness(uint8_t newBrightness); // returns true if successful |
| 43 | + uint8_t getBrightness(); // returns current brightness |
| 44 | + |
| 45 | + bool setColorRGB(espRgbColor_t rgbColor); // returns true if successful |
| 46 | + espRgbColor_t getColorRGB(); // returns current RGB Color |
| 47 | + bool setColorHSV(espHsvColor_t hsvColor); // returns true if successful |
| 48 | + espHsvColor_t getColorHSV(); // returns current HSV Color |
| 49 | + |
| 50 | + |
| 51 | + // used to update the state of the light using the current Matter Light internal state |
| 52 | + // It is necessary to set a user callback function using onChange() to handle the physical light state |
| 53 | + void updateAccessory(); |
| 54 | + |
| 55 | + operator bool(); // returns current on/off light state |
| 56 | + void operator=(bool state); // turns light on or off |
| 57 | + |
| 58 | + // this function is called by Matter internal event processor. It could be overwritten by the application, if necessary. |
| 59 | + bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val); |
| 60 | + |
| 61 | + // User Callback for whenever the Light On/Off state is changed by the Matter Controller |
| 62 | + using EndPointOnOffCB = std::function<bool(bool)>; |
| 63 | + void onChangeOnOff(EndPointOnOffCB onChangeCB) { |
| 64 | + _onChangeOnOffCB = onChangeCB; |
| 65 | + } |
| 66 | + |
| 67 | + // User Callback for whenever the Light brightness value [0..255] is changed by the Matter Controller |
| 68 | + using EndPointBrightnessCB = std::function<bool(uint8_t)>; |
| 69 | + void onChangeBrightness(EndPointBrightnessCB onChangeCB) { |
| 70 | + _onChangeBrightnessCB = onChangeCB; |
| 71 | + } |
| 72 | + |
| 73 | + // User Callback for whenever the HSV Color value is changed by the Matter Controller |
| 74 | + using EndPointRGBColorCB = std::function<bool(espHsvColor_t)>; |
| 75 | + void onChangeColorHSV(EndPointRGBColorCB onChangeCB) { |
| 76 | + _onChangeColorCB = onChangeCB; |
| 77 | + } |
| 78 | + |
| 79 | + // User Callbqck for whenever the Light temperature value is changed by the Matter Controller |
| 80 | + using EndPointTemperatureCB = std::function<bool(uint16_t)>; |
| 81 | + void onChangeColorTemperature(EndPointTemperatureCB onChangeCB) { |
| 82 | + _onChangeTemperatureCB = onChangeCB; |
| 83 | + } |
| 84 | + |
| 85 | + // User Callback for whenever any parameter is changed by the Matter Controller |
| 86 | + using EndPointCB = std::function<bool(bool, espHsvColor_t, uint8_t, uint16_t)>; |
| 87 | + void onChange(EndPointCB onChangeCB) { |
| 88 | + _onChangeCB = onChangeCB; |
| 89 | + } |
| 90 | + |
| 91 | +protected: |
| 92 | + bool started = false; |
| 93 | + bool onOffState = false; // default initial state is off, but it can be changed by begin(bool) |
| 94 | + uint8_t brightnessLevel = 0; // default initial brightness is 0, but it can be changed by begin(bool, uint8_t) |
| 95 | + espHsvColor_t colorHSV = { 0 }; // default initial color HSV is black, but it can be changed by begin(bool, uint8_t, espHsvColor_t) |
| 96 | + uint16_t colorTemperatureLevel = 0; // default initial color temperature is 0, but it can be changed by begin(bool, uint8_t, espHsvColor_t, uint16_t) |
| 97 | + EndPointOnOffCB _onChangeOnOffCB = NULL; |
| 98 | + EndPointBrightnessCB _onChangeBrightnessCB = NULL; |
| 99 | + EndPointRGBColorCB _onChangeColorCB = NULL; |
| 100 | + EndPointTemperatureCB _onChangeTemperatureCB = NULL; |
| 101 | + EndPointCB _onChangeCB = NULL; |
| 102 | +}; |
| 103 | +#endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ |
0 commit comments