Skip to content

Commit 193916d

Browse files
committed
feat(matter): commentaries and code improvement. update() and callbacks added.
1 parent fb3a5ea commit 193916d

File tree

5 files changed

+55
-22
lines changed

5 files changed

+55
-22
lines changed

libraries/Matter/src/MatterEndPoint.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ class MatterEndPoint {
2828
void setEndPointId(uint16_t ep) {
2929
endpoint_id = ep;
3030
}
31-
31+
// this function is called by Matter internal event processor. It could be overwritten by the application, if necessary.
3232
virtual bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val) = 0;
33-
3433
protected:
3534
uint16_t endpoint_id = 0;
3635
};

libraries/Matter/src/MatterEndpoints/MatterDimmableLight.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,33 @@ bool MatterDimmableLight::attributeChangeCB(uint16_t endpoint_id, uint32_t clust
3333
log_d("Dimmable Attr update callback: endpoint: %u, cluster: %u, attribute: %u, val: %u", endpoint_id, cluster_id, attribute_id, val->val.u32);
3434

3535
if (endpoint_id == getEndPointId()) {
36+
bool ret = false;
3637
switch(cluster_id) {
3738
case OnOff::Id:
3839
if (attribute_id == OnOff::Attributes::OnOff::Id) {
40+
log_d("DimmableLight On/Off State changed to %d", val->val.b);
3941
if (_onChangeOnOffCB != NULL) {
40-
ret = _onChangeOnOffCB(val->val.b);
41-
log_d("DimmableLight On/Off State changed to %d", val->val.b);
42-
if (ret == true) {
43-
onOffState = val->val.b;
44-
}
42+
ret |= _onChangeOnOffCB(val->val.b);
43+
}
44+
if (_onChangeCB != NULL) {
45+
ret |= _onChangeCB(val->val.b, brightnessLevel);
46+
}
47+
if (ret == true) {
48+
onOffState = val->val.b;
4549
}
4650
}
4751
break;
4852
case LevelControl::Id:
4953
if (attribute_id == LevelControl::Attributes::CurrentLevel::Id) {
54+
log_d("DimmableLight Brightness changed to %d", val->val.u8);
5055
if (_onChangeBrightnessCB != NULL) {
51-
ret = _onChangeBrightnessCB(val->val.u8);
52-
log_d("DimmableLight Brightness changed to %d", val->val.u8);
53-
if (ret == true) {
54-
brightnessLevel = val->val.u8;
55-
}
56+
ret |= _onChangeBrightnessCB(val->val.u8);
57+
}
58+
if (_onChangeCB != NULL) {
59+
ret |= _onChangeCB(onOffState, val->val.u8);
60+
}
61+
if (ret == true) {
62+
brightnessLevel = val->val.u8;
5663
}
5764
}
5865
break;
@@ -123,6 +130,12 @@ bool MatterDimmableLight::setOnOff(bool newState) {
123130
return true;
124131
}
125132

133+
void MatterDimmableLight::updateAccessory() {
134+
if (_onChangeCB != NULL) {
135+
_onChangeCB(onOffState, brightnessLevel);
136+
}
137+
}
138+
126139
bool MatterDimmableLight::getOnOff() {
127140
return onOffState;
128141
}

libraries/Matter/src/MatterEndpoints/MatterDimmableLight.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ class MatterDimmableLight : public MatterEndPoint {
3737
bool setBrightness(uint8_t newBrightness); // returns true if successful
3838
uint8_t getBrightness(); // returns current brightness
3939

40+
// used to update the state of the light using the current Matter Light internal state
41+
// It is necessary to set a user callback function using onChange() to handle the physical light state
42+
void updateAccessory();
43+
4044
operator bool(); // returns current on/off light state
4145
void operator=(bool state); // turns light on or off
4246
// this function is called by Matter internal event processor. It could be overwritten by the application, if necessary.
@@ -52,11 +56,18 @@ class MatterDimmableLight : public MatterEndPoint {
5256
_onChangeBrightnessCB = onChangeCB;
5357
}
5458

59+
// User Callback for whenever any parameter is changed by the Matter Controller
60+
using EndPointCB = std::function<bool(bool, uint8_t)>;
61+
void onChange(EndPointCB onChangeCB) {
62+
_onChangeCB = onChangeCB;
63+
}
64+
5565
protected:
5666
bool started = false;
5767
bool onOffState = false; // default initial state is off, but it can be changed by begin(bool)
5868
uint8_t brightnessLevel = 0; // default initial brightness is 0, but it can be changed by begin(bool, uint8_t)
5969
EndPointOnOffCB _onChangeOnOffCB = NULL;
6070
EndPointBrightnessCB _onChangeBrightnessCB = NULL;
71+
EndPointCB _onChangeCB = NULL;
6172
};
6273
#endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */

libraries/Matter/src/MatterEndpoints/MatterOnOffLight.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ bool MatterOnOffLight::attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_
3939
ret = _onChangeCB(val->val.b);
4040
log_d("OnOffLight state changed to %d", val->val.b);
4141
if (ret == true) {
42-
state = val->val.b;
42+
onOffState = val->val.b;
4343
}
4444
}
4545
}
@@ -60,7 +60,7 @@ bool MatterOnOffLight::begin(bool initialState) {
6060

6161
light_config.on_off.on_off = initialState;
6262
light_config.on_off.lighting.start_up_on_off = nullptr;
63-
state = initialState;
63+
onOffState = initialState;
6464

6565
// endpoint handles can be used to add/modify clusters.
6666
endpoint_t *endpoint = on_off_light::create(node::get(), &light_config, ENDPOINT_FLAG_NONE, (void *)this);
@@ -79,18 +79,24 @@ void MatterOnOffLight::end() {
7979
started = false;
8080
}
8181

82+
void MatterOnOffLight::updateAccessory() {
83+
if (_onChangeCB != NULL) {
84+
_onChangeCB(onOffState);
85+
}
86+
}
87+
8288
bool MatterOnOffLight::setOnOff(bool newState) {
8389
if (!started) {
8490
log_e("Matter On-Off Light device has not begun.");
8591
return false;
8692
}
8793

8894
// avoid processing the a "no-change"
89-
if (state == newState) {
95+
if (onOffState == newState) {
9096
return true;
9197
}
9298

93-
state = newState;
99+
onOffState = newState;
94100

95101
endpoint_t *endpoint = endpoint::get(node::get(), endpoint_id);
96102
cluster_t *cluster = cluster::get(endpoint, OnOff::Id);
@@ -99,19 +105,19 @@ bool MatterOnOffLight::setOnOff(bool newState) {
99105
esp_matter_attr_val_t val = esp_matter_invalid(NULL);
100106
attribute::get_val(attribute, &val);
101107

102-
if (val.val.b != state) {
103-
val.val.b = state;
108+
if (val.val.b != onOffState) {
109+
val.val.b = onOffState;
104110
attribute::update(endpoint_id, OnOff::Id, OnOff::Attributes::OnOff::Id, &val);
105111
}
106112
return true;
107113
}
108114

109115
bool MatterOnOffLight::getOnOff() {
110-
return state;
116+
return onOffState;
111117
}
112118

113119
bool MatterOnOffLight::toggle() {
114-
return setOnOff(!state);
120+
return setOnOff(!onOffState);
115121
}
116122

117123
MatterOnOffLight::operator bool() {

libraries/Matter/src/MatterEndpoints/MatterOnOffLight.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,23 @@ class MatterOnOffLight : public MatterEndPoint {
3030
bool getOnOff(); // returns current light state
3131
bool toggle(); // returns true if successful
3232

33+
// used to update the state of the light using the current Matter Light internal state
34+
// It is necessary to set a user callback function using onChange() to handle the physical light state
35+
void updateAccessory();
36+
3337
operator bool(); // returns current light state
3438
void operator=(bool state); // turns light on or off
3539
// this function is called by Matter internal event processor. It could be overwritten by the application, if necessary.
3640
bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val);
3741
// User Callback for whenever the Light state is changed by the Matter Controller
3842
using EndPointCB = std::function<bool(bool)>;
39-
void onChangeOnOff(EndPointCB onChangeCB) {
43+
void onChange(EndPointCB onChangeCB) {
4044
_onChangeCB = onChangeCB;
4145
}
4246

4347
protected:
4448
bool started = false;
45-
bool state = false; // default initial state is off, but it can be changed by begin(bool)
49+
bool onOffState = false; // default initial state is off, but it can be changed by begin(bool)
4650
EndPointCB _onChangeCB = NULL;
4751
};
4852
#endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */

0 commit comments

Comments
 (0)