Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(matter): commentaries and code improvement. update() and callbac…
…ks added.
  • Loading branch information
SuGlider committed Nov 4, 2024
commit 193916d9542822d2adcdb851b6fd7e580fe08ca1
3 changes: 1 addition & 2 deletions libraries/Matter/src/MatterEndPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ class MatterEndPoint {
void setEndPointId(uint16_t ep) {
endpoint_id = ep;
}

// this function is called by Matter internal event processor. It could be overwritten by the application, if necessary.
virtual bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val) = 0;

protected:
uint16_t endpoint_id = 0;
};
Expand Down
33 changes: 23 additions & 10 deletions libraries/Matter/src/MatterEndpoints/MatterDimmableLight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,33 @@ bool MatterDimmableLight::attributeChangeCB(uint16_t endpoint_id, uint32_t clust
log_d("Dimmable Attr update callback: endpoint: %u, cluster: %u, attribute: %u, val: %u", endpoint_id, cluster_id, attribute_id, val->val.u32);

if (endpoint_id == getEndPointId()) {
bool ret = false;
switch(cluster_id) {
case OnOff::Id:
if (attribute_id == OnOff::Attributes::OnOff::Id) {
log_d("DimmableLight On/Off State changed to %d", val->val.b);
if (_onChangeOnOffCB != NULL) {
ret = _onChangeOnOffCB(val->val.b);
log_d("DimmableLight On/Off State changed to %d", val->val.b);
if (ret == true) {
onOffState = val->val.b;
}
ret |= _onChangeOnOffCB(val->val.b);
}
if (_onChangeCB != NULL) {
ret |= _onChangeCB(val->val.b, brightnessLevel);
}
if (ret == true) {
onOffState = val->val.b;
}
}
break;
case LevelControl::Id:
if (attribute_id == LevelControl::Attributes::CurrentLevel::Id) {
log_d("DimmableLight Brightness changed to %d", val->val.u8);
if (_onChangeBrightnessCB != NULL) {
ret = _onChangeBrightnessCB(val->val.u8);
log_d("DimmableLight Brightness changed to %d", val->val.u8);
if (ret == true) {
brightnessLevel = val->val.u8;
}
ret |= _onChangeBrightnessCB(val->val.u8);
}
if (_onChangeCB != NULL) {
ret |= _onChangeCB(onOffState, val->val.u8);
}
if (ret == true) {
brightnessLevel = val->val.u8;
}
}
break;
Expand Down Expand Up @@ -123,6 +130,12 @@ bool MatterDimmableLight::setOnOff(bool newState) {
return true;
}

void MatterDimmableLight::updateAccessory() {
if (_onChangeCB != NULL) {
_onChangeCB(onOffState, brightnessLevel);
}
}

bool MatterDimmableLight::getOnOff() {
return onOffState;
}
Expand Down
11 changes: 11 additions & 0 deletions libraries/Matter/src/MatterEndpoints/MatterDimmableLight.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class MatterDimmableLight : public MatterEndPoint {
bool setBrightness(uint8_t newBrightness); // returns true if successful
uint8_t getBrightness(); // returns current brightness

// used to update the state of the light using the current Matter Light internal state
// It is necessary to set a user callback function using onChange() to handle the physical light state
void updateAccessory();

operator bool(); // returns current on/off light state
void operator=(bool state); // turns light on or off
// this function is called by Matter internal event processor. It could be overwritten by the application, if necessary.
Expand All @@ -52,11 +56,18 @@ class MatterDimmableLight : public MatterEndPoint {
_onChangeBrightnessCB = onChangeCB;
}

// User Callback for whenever any parameter is changed by the Matter Controller
using EndPointCB = std::function<bool(bool, uint8_t)>;
void onChange(EndPointCB onChangeCB) {
_onChangeCB = onChangeCB;
}

protected:
bool started = false;
bool onOffState = false; // default initial state is off, but it can be changed by begin(bool)
uint8_t brightnessLevel = 0; // default initial brightness is 0, but it can be changed by begin(bool, uint8_t)
EndPointOnOffCB _onChangeOnOffCB = NULL;
EndPointBrightnessCB _onChangeBrightnessCB = NULL;
EndPointCB _onChangeCB = NULL;
};
#endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */
22 changes: 14 additions & 8 deletions libraries/Matter/src/MatterEndpoints/MatterOnOffLight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ bool MatterOnOffLight::attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_
ret = _onChangeCB(val->val.b);
log_d("OnOffLight state changed to %d", val->val.b);
if (ret == true) {
state = val->val.b;
onOffState = val->val.b;
}
}
}
Expand All @@ -60,7 +60,7 @@ bool MatterOnOffLight::begin(bool initialState) {

light_config.on_off.on_off = initialState;
light_config.on_off.lighting.start_up_on_off = nullptr;
state = initialState;
onOffState = initialState;

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

void MatterOnOffLight::updateAccessory() {
if (_onChangeCB != NULL) {
_onChangeCB(onOffState);
}
}

bool MatterOnOffLight::setOnOff(bool newState) {
if (!started) {
log_e("Matter On-Off Light device has not begun.");
return false;
}

// avoid processing the a "no-change"
if (state == newState) {
if (onOffState == newState) {
return true;
}

state = newState;
onOffState = newState;

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

if (val.val.b != state) {
val.val.b = state;
if (val.val.b != onOffState) {
val.val.b = onOffState;
attribute::update(endpoint_id, OnOff::Id, OnOff::Attributes::OnOff::Id, &val);
}
return true;
}

bool MatterOnOffLight::getOnOff() {
return state;
return onOffState;
}

bool MatterOnOffLight::toggle() {
return setOnOff(!state);
return setOnOff(!onOffState);
}

MatterOnOffLight::operator bool() {
Expand Down
8 changes: 6 additions & 2 deletions libraries/Matter/src/MatterEndpoints/MatterOnOffLight.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,23 @@ class MatterOnOffLight : public MatterEndPoint {
bool getOnOff(); // returns current light state
bool toggle(); // returns true if successful

// used to update the state of the light using the current Matter Light internal state
// It is necessary to set a user callback function using onChange() to handle the physical light state
void updateAccessory();

operator bool(); // returns current light state
void operator=(bool state); // turns light on or off
// this function is called by Matter internal event processor. It could be overwritten by the application, if necessary.
bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val);
// User Callback for whenever the Light state is changed by the Matter Controller
using EndPointCB = std::function<bool(bool)>;
void onChangeOnOff(EndPointCB onChangeCB) {
void onChange(EndPointCB onChangeCB) {
_onChangeCB = onChangeCB;
}

protected:
bool started = false;
bool state = false; // default initial state is off, but it can be changed by begin(bool)
bool onOffState = false; // default initial state is off, but it can be changed by begin(bool)
EndPointCB _onChangeCB = NULL;
};
#endif /* CONFIG_ESP_MATTER_ENABLE_DATA_MODEL */