|
1 | | -#include <sdkconfig.h> |
2 | | -#ifdef CONFIG_ESP_MATTER_ENABLE_DATA_MODEL |
3 | | - |
4 | | -#include <Matter.h> |
5 | | -#include <app/server/Server.h> |
6 | | -#include <MatterOnOffLight.h> |
7 | | - |
8 | | -using namespace esp_matter; |
9 | | -using namespace esp_matter::endpoint; |
10 | | -using namespace chip::app::Clusters; |
11 | | - |
12 | | -bool MatterOnOffLight::attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val) { |
13 | | - bool ret = true; |
14 | | - if (!started) { |
15 | | - log_w("Matter On-Off Light device has not begun."); |
16 | | - return false; |
17 | | - } |
18 | | - |
19 | | - if (endpoint_id == getEndPointId()) { |
20 | | - if (cluster_id == OnOff::Id) { |
21 | | - if (attribute_id == OnOff::Attributes::OnOff::Id) { |
22 | | - if (_onChangeCB != NULL) { |
23 | | - ret = _onChangeCB(val->val.b); |
24 | | - log_d("OnOffLight state changed to %d", val->val.b); |
25 | | - if (ret == true) { |
26 | | - state = val->val.b; |
27 | | - } |
28 | | - } |
29 | | - } |
30 | | - } |
31 | | - } |
32 | | - return ret; |
33 | | -} |
34 | | - |
35 | | -MatterOnOffLight::MatterOnOffLight() { |
36 | | -} |
37 | | - |
38 | | -MatterOnOffLight::~MatterOnOffLight() { |
39 | | - end(); |
40 | | -} |
41 | | - |
42 | | -bool MatterOnOffLight::begin(bool initialState) { |
43 | | - ArduinoMatter::_init(); |
44 | | - on_off_light::config_t light_config; |
45 | | - light_config.on_off.on_off = initialState; |
46 | | - state = initialState; |
47 | | - light_config.on_off.lighting.start_up_on_off = nullptr; |
48 | | - |
49 | | - // endpoint handles can be used to add/modify clusters. |
50 | | - endpoint_t *endpoint = on_off_light::create(node::get(), &light_config, ENDPOINT_FLAG_NONE, (void *) this); |
51 | | - if (endpoint == nullptr) { |
52 | | - log_e("Failed to create on-off light endpoint"); |
53 | | - return false; |
54 | | - } |
55 | | - |
56 | | - setEndPointId(endpoint::get_id(endpoint)); |
57 | | - log_i("On-Off Light created with endpoint_id %d", getEndPointId()); |
58 | | - started = true; |
59 | | - return true; |
60 | | -} |
61 | | - |
62 | | -void MatterOnOffLight::end() { |
63 | | - started = false; |
64 | | -} |
65 | | - |
66 | | -bool MatterOnOffLight::setOnOff(bool newState) { |
67 | | - if (!started) { |
68 | | - log_w("Matter On-Off Light device has not begun."); |
69 | | - return false; |
70 | | - } |
71 | | - |
72 | | - // avoid processing the a "no-change" |
73 | | - if (state == newState) { |
74 | | - return true; |
75 | | - } |
76 | | - |
77 | | - state = newState; |
78 | | - |
79 | | - endpoint_t *endpoint = endpoint::get(node::get(), endpoint_id); |
80 | | - cluster_t *cluster = cluster::get(endpoint, OnOff::Id); |
81 | | - attribute_t *attribute = attribute::get(cluster, OnOff::Attributes::OnOff::Id); |
82 | | - |
83 | | - esp_matter_attr_val_t val = esp_matter_invalid(NULL); |
84 | | - attribute::get_val(attribute, &val); |
85 | | - |
86 | | - if (val.val.b != state) { |
87 | | - val.val.b = state; |
88 | | - attribute::update(endpoint_id, OnOff::Id, OnOff::Attributes::OnOff::Id, &val); |
89 | | - } |
90 | | - return true; |
91 | | -} |
92 | | - |
93 | | -bool MatterOnOffLight::getOnOff() { |
94 | | - return state; |
95 | | -} |
96 | | - |
97 | | -bool MatterOnOffLight::toggle() { |
98 | | - return setOnOff(!state); |
99 | | -} |
100 | | - |
101 | | -MatterOnOffLight::operator bool() { |
102 | | - return getOnOff(); |
103 | | -} |
104 | | - |
105 | | -void MatterOnOffLight::operator=(bool newState) { |
106 | | - setOnOff(newState); |
107 | | -} |
108 | | -#endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ |
| 1 | +#include <sdkconfig.h> |
| 2 | +#ifdef CONFIG_ESP_MATTER_ENABLE_DATA_MODEL |
| 3 | + |
| 4 | +#include <Matter.h> |
| 5 | +#include <app/server/Server.h> |
| 6 | +#include <MatterOnOffLight.h> |
| 7 | + |
| 8 | +using namespace esp_matter; |
| 9 | +using namespace esp_matter::endpoint; |
| 10 | +using namespace chip::app::Clusters; |
| 11 | + |
| 12 | +bool MatterOnOffLight::attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val) { |
| 13 | + bool ret = true; |
| 14 | + if (!started) { |
| 15 | + log_w("Matter On-Off Light device has not begun."); |
| 16 | + return false; |
| 17 | + } |
| 18 | + |
| 19 | + if (endpoint_id == getEndPointId()) { |
| 20 | + if (cluster_id == OnOff::Id) { |
| 21 | + if (attribute_id == OnOff::Attributes::OnOff::Id) { |
| 22 | + if (_onChangeCB != NULL) { |
| 23 | + ret = _onChangeCB(val->val.b); |
| 24 | + log_d("OnOffLight state changed to %d", val->val.b); |
| 25 | + if (ret == true) { |
| 26 | + state = val->val.b; |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + return ret; |
| 33 | +} |
| 34 | + |
| 35 | +MatterOnOffLight::MatterOnOffLight() {} |
| 36 | + |
| 37 | +MatterOnOffLight::~MatterOnOffLight() { |
| 38 | + end(); |
| 39 | +} |
| 40 | + |
| 41 | +bool MatterOnOffLight::begin(bool initialState) { |
| 42 | + ArduinoMatter::_init(); |
| 43 | + on_off_light::config_t light_config; |
| 44 | + light_config.on_off.on_off = initialState; |
| 45 | + state = initialState; |
| 46 | + light_config.on_off.lighting.start_up_on_off = nullptr; |
| 47 | + |
| 48 | + // endpoint handles can be used to add/modify clusters. |
| 49 | + endpoint_t *endpoint = on_off_light::create(node::get(), &light_config, ENDPOINT_FLAG_NONE, (void *)this); |
| 50 | + if (endpoint == nullptr) { |
| 51 | + log_e("Failed to create on-off light endpoint"); |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + setEndPointId(endpoint::get_id(endpoint)); |
| 56 | + log_i("On-Off Light created with endpoint_id %d", getEndPointId()); |
| 57 | + started = true; |
| 58 | + return true; |
| 59 | +} |
| 60 | + |
| 61 | +void MatterOnOffLight::end() { |
| 62 | + started = false; |
| 63 | +} |
| 64 | + |
| 65 | +bool MatterOnOffLight::setOnOff(bool newState) { |
| 66 | + if (!started) { |
| 67 | + log_w("Matter On-Off Light device has not begun."); |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + // avoid processing the a "no-change" |
| 72 | + if (state == newState) { |
| 73 | + return true; |
| 74 | + } |
| 75 | + |
| 76 | + state = newState; |
| 77 | + |
| 78 | + endpoint_t *endpoint = endpoint::get(node::get(), endpoint_id); |
| 79 | + cluster_t *cluster = cluster::get(endpoint, OnOff::Id); |
| 80 | + attribute_t *attribute = attribute::get(cluster, OnOff::Attributes::OnOff::Id); |
| 81 | + |
| 82 | + esp_matter_attr_val_t val = esp_matter_invalid(NULL); |
| 83 | + attribute::get_val(attribute, &val); |
| 84 | + |
| 85 | + if (val.val.b != state) { |
| 86 | + val.val.b = state; |
| 87 | + attribute::update(endpoint_id, OnOff::Id, OnOff::Attributes::OnOff::Id, &val); |
| 88 | + } |
| 89 | + return true; |
| 90 | +} |
| 91 | + |
| 92 | +bool MatterOnOffLight::getOnOff() { |
| 93 | + return state; |
| 94 | +} |
| 95 | + |
| 96 | +bool MatterOnOffLight::toggle() { |
| 97 | + return setOnOff(!state); |
| 98 | +} |
| 99 | + |
| 100 | +MatterOnOffLight::operator bool() { |
| 101 | + return getOnOff(); |
| 102 | +} |
| 103 | + |
| 104 | +void MatterOnOffLight::operator=(bool newState) { |
| 105 | + setOnOff(newState); |
| 106 | +} |
| 107 | +#endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */ |
0 commit comments