fix(matter): fixes identify and double begin() call
This commit is contained in:
parent
ba8d04eff2
commit
6f79e03449
16 changed files with 134 additions and 42 deletions
|
|
@ -51,6 +51,11 @@ uint32_t button_time_stamp = 0; // debouncing control
|
|||
bool button_state = false; // false = released | true = pressed
|
||||
const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission
|
||||
|
||||
// Identify Flag and blink time - Blink the LED
|
||||
const uint8_t identifyLedPin = ledPin; // uses the same LED as the Light - change if needed
|
||||
volatile bool identifyFlag = false; // Flag to start the Blink when in Identify state
|
||||
bool identifyBlink = false; // Blink state when in Identify state
|
||||
|
||||
// Matter Protocol Endpoint (On/OFF Light) Callback
|
||||
bool onOffLightCallback(bool state) {
|
||||
digitalWrite(ledPin, state ? HIGH : LOW);
|
||||
|
|
@ -58,14 +63,19 @@ bool onOffLightCallback(bool state) {
|
|||
return true;
|
||||
}
|
||||
|
||||
bool onIdentifyLightCallback(bool identifyIsActive, uint8_t counter) {
|
||||
log_i("Identify Cluster is %s, counter: %d", identifyIsActive ? "Active" : "Inactive", counter);
|
||||
// Identification shall be done by Blink in Red or just the GPIO when no LED_BUILTIN is not defined
|
||||
bool onIdentifyLightCallback(bool identifyIsActive) {
|
||||
log_i("Identify Cluster is %s", identifyIsActive ? "Active" : "Inactive");
|
||||
if (identifyIsActive) {
|
||||
// Start Blinking the light
|
||||
OnOffLight.toggle();
|
||||
// Start Blinking the light in loop()
|
||||
identifyFlag = true;
|
||||
identifyBlink = !OnOffLight; // Start with the inverted light state
|
||||
} else {
|
||||
// Stop Blinking and restore the light to the its last state
|
||||
OnOffLight.updateAccessory();
|
||||
identifyFlag = false;
|
||||
// force returning to the original state by toggling the light twice
|
||||
OnOffLight.toggle();
|
||||
OnOffLight.toggle();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
@ -76,12 +86,16 @@ void setup() {
|
|||
// Initialize the LED GPIO
|
||||
pinMode(ledPin, OUTPUT);
|
||||
|
||||
Serial.begin(115200);
|
||||
|
||||
// Manually connect to WiFi
|
||||
WiFi.begin(ssid, password);
|
||||
// Wait for connection
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
// Initialize at least one Matter EndPoint
|
||||
OnOffLight.begin();
|
||||
|
|
@ -95,16 +109,38 @@ void setup() {
|
|||
// Matter beginning - Last step, after all EndPoints are initialized
|
||||
Matter.begin();
|
||||
|
||||
// Check Matter Accessory Commissioning state, which may change during execution of loop()
|
||||
if (!Matter.isDeviceCommissioned()) {
|
||||
log_i("Matter Node is not commissioned yet.");
|
||||
log_i("Initiate the device discovery in your Matter environment.");
|
||||
log_i("Commission it to your Matter hub with the manual pairing code or QR code");
|
||||
log_i("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str());
|
||||
log_i("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str());
|
||||
Serial.println("");
|
||||
Serial.println("Matter Node is not commissioned yet.");
|
||||
Serial.println("Initiate the device discovery in your Matter environment.");
|
||||
Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
|
||||
Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str());
|
||||
Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str());
|
||||
// waits for Matter Occupancy Sensor Commissioning.
|
||||
uint32_t timeCount = 0;
|
||||
while (!Matter.isDeviceCommissioned()) {
|
||||
delay(100);
|
||||
if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec
|
||||
Serial.println("Matter Node not commissioned yet. Waiting for commissioning.");
|
||||
}
|
||||
}
|
||||
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// check if the Ligth is in identify state and blink it every 500ms (delay loop time)
|
||||
if (identifyFlag) {
|
||||
#ifdef LED_BUILTIN
|
||||
uint8_t brightness = 32 * identifyBlink;
|
||||
rgbLedWrite(identifyLedPin, brightness, 0, 0);
|
||||
#else
|
||||
digitalWrite(identifyLedPin, identifyBlink ? HIGH : LOW);
|
||||
#endif
|
||||
identifyBlink = !identifyBlink;
|
||||
}
|
||||
|
||||
// Check if the button has been pressed
|
||||
if (digitalRead(buttonPin) == LOW && !button_state) {
|
||||
// deals with button debouncing
|
||||
|
|
@ -124,5 +160,5 @@ void loop() {
|
|||
button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so
|
||||
}
|
||||
|
||||
delay(500);
|
||||
delay(500); // works as a debounce for the button and also for the LED blink
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,11 +30,6 @@ static bool _matter_has_started = false;
|
|||
static node::config_t node_config;
|
||||
static node_t *deviceNode = NULL;
|
||||
|
||||
typedef void *app_driver_handle_t;
|
||||
esp_err_t matter_light_attribute_update(
|
||||
app_driver_handle_t driver_handle, uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val
|
||||
);
|
||||
|
||||
// This callback is called for every attribute update. The callback implementation shall
|
||||
// handle the desired attributes and return an appropriate error code. If the attribute
|
||||
// is not of your interest, please do not return an error code and strictly return ESP_OK.
|
||||
|
|
@ -72,22 +67,19 @@ static esp_err_t app_identification_cb(identification::callback_type_t type, uin
|
|||
esp_err_t err = ESP_OK;
|
||||
MatterEndPoint *ep = (MatterEndPoint *)priv_data; // endpoint pointer to base class
|
||||
// Identify the endpoint sending a counter to the application
|
||||
static uint8_t counter = 0;
|
||||
bool identifyIsActive = false;
|
||||
|
||||
if (type == identification::callback_type_t::START) {
|
||||
log_v("Identification callback: START");
|
||||
counter = 0;
|
||||
identifyIsActive = true;
|
||||
} else if (type == identification::callback_type_t::EFFECT) {
|
||||
log_v("Identification callback: EFFECT");
|
||||
counter++;
|
||||
} else if (type == identification::callback_type_t::STOP) {
|
||||
identifyIsActive = false;
|
||||
log_v("Identification callback: STOP");
|
||||
}
|
||||
if (ep != NULL) {
|
||||
err = ep->endpointIdentifyCB(endpoint_id, identifyIsActive, counter) ? ESP_OK : ESP_FAIL;
|
||||
err = ep->endpointIdentifyCB(endpoint_id, identifyIsActive) ? ESP_OK : ESP_FAIL;
|
||||
}
|
||||
|
||||
return err;
|
||||
|
|
|
|||
|
|
@ -103,14 +103,14 @@ public:
|
|||
virtual bool attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val) = 0;
|
||||
|
||||
// This callback is invoked when clients interact with the Identify Cluster of an specific endpoint.
|
||||
bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled, uint8_t identifyCounter) {
|
||||
bool endpointIdentifyCB(uint16_t endpoint_id, bool identifyIsEnabled) {
|
||||
if (_onEndPointIdentifyCB) {
|
||||
return _onEndPointIdentifyCB(identifyIsEnabled, identifyCounter);
|
||||
return _onEndPointIdentifyCB(identifyIsEnabled);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// User callaback for the Identify Cluster functionality
|
||||
using EndPointIdentifyCB = std::function<bool(bool, uint8_t)>;
|
||||
using EndPointIdentifyCB = std::function<bool(bool)>;
|
||||
void onIdentify(EndPointIdentifyCB onEndPointIdentifyCB) {
|
||||
_onEndPointIdentifyCB = onEndPointIdentifyCB;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -162,8 +162,13 @@ MatterColorLight::~MatterColorLight() {
|
|||
|
||||
bool MatterColorLight::begin(bool initialState, espHsvColor_t _colorHSV) {
|
||||
ArduinoMatter::_init();
|
||||
rgb_color_light::config_t light_config;
|
||||
|
||||
if (getEndPointId() != 0) {
|
||||
log_e("Matter RGB Color Light with Endpoint Id %d device has already been created.", getEndPointId());
|
||||
return false;
|
||||
}
|
||||
|
||||
rgb_color_light::config_t light_config;
|
||||
light_config.on_off.on_off = initialState;
|
||||
light_config.on_off.lighting.start_up_on_off = nullptr;
|
||||
onOffState = initialState;
|
||||
|
|
|
|||
|
|
@ -26,17 +26,17 @@ using namespace chip::app::Clusters;
|
|||
bool MatterColorTemperatureLight::attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, esp_matter_attr_val_t *val) {
|
||||
bool ret = true;
|
||||
if (!started) {
|
||||
log_e("Matter CW_WW Light device has not begun.");
|
||||
log_e("Matter Temperature Light device has not begun.");
|
||||
return false;
|
||||
}
|
||||
|
||||
log_d("CW_WW Attr update callback: endpoint: %u, cluster: %u, attribute: %u, val: %u", endpoint_id, cluster_id, attribute_id, val->val.u32);
|
||||
log_d("Temperature Attr update callback: endpoint: %u, cluster: %u, attribute: %u, val: %u", endpoint_id, cluster_id, attribute_id, val->val.u32);
|
||||
|
||||
if (endpoint_id == getEndPointId()) {
|
||||
switch (cluster_id) {
|
||||
case OnOff::Id:
|
||||
if (attribute_id == OnOff::Attributes::OnOff::Id) {
|
||||
log_d("CW_WW Light On/Off State changed to %d", val->val.b);
|
||||
log_d("Temperature Light On/Off State changed to %d", val->val.b);
|
||||
if (_onChangeOnOffCB != NULL) {
|
||||
ret &= _onChangeOnOffCB(val->val.b);
|
||||
}
|
||||
|
|
@ -50,7 +50,7 @@ bool MatterColorTemperatureLight::attributeChangeCB(uint16_t endpoint_id, uint32
|
|||
break;
|
||||
case LevelControl::Id:
|
||||
if (attribute_id == LevelControl::Attributes::CurrentLevel::Id) {
|
||||
log_d("CW_WW Light Brightness changed to %d", val->val.u8);
|
||||
log_d("Temperature Light Brightness changed to %d", val->val.u8);
|
||||
if (_onChangeBrightnessCB != NULL) {
|
||||
ret &= _onChangeBrightnessCB(val->val.u8);
|
||||
}
|
||||
|
|
@ -64,7 +64,7 @@ bool MatterColorTemperatureLight::attributeChangeCB(uint16_t endpoint_id, uint32
|
|||
break;
|
||||
case ColorControl::Id:
|
||||
if (attribute_id == ColorControl::Attributes::ColorTemperatureMireds::Id) {
|
||||
log_d("CW_WW Light Temperature changed to %d", val->val.u16);
|
||||
log_d("Temperature Light Temperature changed to %d", val->val.u16);
|
||||
if (_onChangeTemperatureCB != NULL) {
|
||||
ret &= _onChangeTemperatureCB(val->val.u16);
|
||||
}
|
||||
|
|
@ -89,8 +89,13 @@ MatterColorTemperatureLight::~MatterColorTemperatureLight() {
|
|||
|
||||
bool MatterColorTemperatureLight::begin(bool initialState, uint8_t brightness, uint16_t ColorTemperature) {
|
||||
ArduinoMatter::_init();
|
||||
color_temperature_light::config_t light_config;
|
||||
|
||||
if (getEndPointId() != 0) {
|
||||
log_e("Matter Temperature Light with Endpoint Id %d device has already been created.", getEndPointId());
|
||||
return false;
|
||||
}
|
||||
|
||||
color_temperature_light::config_t light_config;
|
||||
light_config.on_off.on_off = initialState;
|
||||
light_config.on_off.lighting.start_up_on_off = nullptr;
|
||||
onOffState = initialState;
|
||||
|
|
@ -108,12 +113,12 @@ bool MatterColorTemperatureLight::begin(bool initialState, uint8_t brightness, u
|
|||
// endpoint handles can be used to add/modify clusters.
|
||||
endpoint_t *endpoint = color_temperature_light::create(node::get(), &light_config, ENDPOINT_FLAG_NONE, (void *)this);
|
||||
if (endpoint == nullptr) {
|
||||
log_e("Failed to create CW_WW light endpoint");
|
||||
log_e("Failed to create Temperature Light endpoint");
|
||||
return false;
|
||||
}
|
||||
|
||||
setEndPointId(endpoint::get_id(endpoint));
|
||||
log_i("CW_WW Light created with endpoint_id %d", getEndPointId());
|
||||
log_i("Temperature Light created with endpoint_id %d", getEndPointId());
|
||||
|
||||
/* Mark deferred persistence for some attributes that might be changed rapidly */
|
||||
cluster_t *level_control_cluster = cluster::get(endpoint, LevelControl::Id);
|
||||
|
|
@ -134,7 +139,7 @@ void MatterColorTemperatureLight::end() {
|
|||
|
||||
bool MatterColorTemperatureLight::setOnOff(bool newState) {
|
||||
if (!started) {
|
||||
log_e("Matter CW_WW Light device has not begun.");
|
||||
log_e("Matter Temperature Light device has not begun.");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -175,7 +180,7 @@ bool MatterColorTemperatureLight::toggle() {
|
|||
|
||||
bool MatterColorTemperatureLight::setBrightness(uint8_t newBrightness) {
|
||||
if (!started) {
|
||||
log_w("Matter CW_WW Light device has not begun.");
|
||||
log_w("Matter Temperature Light device has not begun.");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -206,7 +211,7 @@ uint8_t MatterColorTemperatureLight::getBrightness() {
|
|||
|
||||
bool MatterColorTemperatureLight::setColorTemperature(uint16_t newTemperature) {
|
||||
if (!started) {
|
||||
log_w("Matter CW_WW Light device has not begun.");
|
||||
log_w("Matter Temperature Light device has not begun.");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -43,6 +43,11 @@ MatterContactSensor::~MatterContactSensor() {
|
|||
bool MatterContactSensor::begin(bool _contactState) {
|
||||
ArduinoMatter::_init();
|
||||
|
||||
if (getEndPointId() != 0) {
|
||||
log_e("Matter Contact Sensor with Endpoint Id %d device has already been created.", getEndPointId());
|
||||
return false;
|
||||
}
|
||||
|
||||
contact_sensor::config_t contact_sensor_config;
|
||||
contact_sensor_config.boolean_state.state_value = _contactState;
|
||||
|
||||
|
|
|
|||
|
|
@ -75,8 +75,12 @@ MatterDimmableLight::~MatterDimmableLight() {
|
|||
|
||||
bool MatterDimmableLight::begin(bool initialState, uint8_t brightness) {
|
||||
ArduinoMatter::_init();
|
||||
dimmable_light::config_t light_config;
|
||||
if (getEndPointId() != 0) {
|
||||
log_e("Matter Dimmable Light with Endpoint Id %d device has already been created.", getEndPointId());
|
||||
return false;
|
||||
}
|
||||
|
||||
dimmable_light::config_t light_config;
|
||||
light_config.on_off.on_off = initialState;
|
||||
light_config.on_off.lighting.start_up_on_off = nullptr;
|
||||
onOffState = initialState;
|
||||
|
|
|
|||
|
|
@ -178,8 +178,13 @@ MatterEnhancedColorLight::~MatterEnhancedColorLight() {
|
|||
|
||||
bool MatterEnhancedColorLight::begin(bool initialState, espHsvColor_t _colorHSV, uint8_t brightness, uint16_t ColorTemperature) {
|
||||
ArduinoMatter::_init();
|
||||
enhanced_color_light::config_t light_config;
|
||||
|
||||
if (getEndPointId() != 0) {
|
||||
log_e("Matter Enhanced ColorLight with Endpoint Id %d device has already been created.", getEndPointId());
|
||||
return false;
|
||||
}
|
||||
|
||||
enhanced_color_light::config_t light_config;
|
||||
light_config.on_off.on_off = initialState;
|
||||
light_config.on_off.lighting.start_up_on_off = nullptr;
|
||||
onOffState = initialState;
|
||||
|
|
|
|||
|
|
@ -85,7 +85,12 @@ bool MatterFan::attributeChangeCB(uint16_t endpoint_id, uint32_t cluster_id, uin
|
|||
bool MatterFan::begin(uint8_t percent, FanMode_t fanMode, FanModeSequence_t fanModeSeq) {
|
||||
ArduinoMatter::_init();
|
||||
|
||||
// endpoint handles can be used to add/modify clusters.
|
||||
if (getEndPointId() != 0) {
|
||||
log_e("Matter Fan with Endpoint Id %d device has already been created.", getEndPointId());
|
||||
return false;
|
||||
}
|
||||
|
||||
// endpoint handles can be used to add/modify clusters.
|
||||
fan::config_t fan_config;
|
||||
fan_config.fan_control.fan_mode = fanMode;
|
||||
fan_config.fan_control.percent_current = percent;
|
||||
|
|
|
|||
|
|
@ -42,12 +42,17 @@ bool MatterGenericSwitch::attributeChangeCB(uint16_t endpoint_id, uint32_t clust
|
|||
|
||||
bool MatterGenericSwitch::begin() {
|
||||
ArduinoMatter::_init();
|
||||
generic_switch::config_t switch_config;
|
||||
|
||||
if (getEndPointId() != 0) {
|
||||
log_e("Matter Generic Switch with Endpoint Id %d device has already been created.", getEndPointId());
|
||||
return false;
|
||||
}
|
||||
|
||||
generic_switch::config_t switch_config;
|
||||
// endpoint handles can be used to add/modify clusters.
|
||||
endpoint_t *endpoint = generic_switch::create(node::get(), &switch_config, ENDPOINT_FLAG_NONE, (void *)this);
|
||||
if (endpoint == nullptr) {
|
||||
log_e("Failed to create Generic switch endpoint");
|
||||
log_e("Failed to create Generic Switch endpoint");
|
||||
return false;
|
||||
}
|
||||
// Add group cluster to the switch endpoint
|
||||
|
|
|
|||
|
|
@ -43,6 +43,11 @@ MatterHumiditySensor::~MatterHumiditySensor() {
|
|||
bool MatterHumiditySensor::begin(uint16_t _rawHumidity) {
|
||||
ArduinoMatter::_init();
|
||||
|
||||
if (getEndPointId() != 0) {
|
||||
log_e("Matter Humidity Sensor with Endpoint Id %d device has already been created.", getEndPointId());
|
||||
return false;
|
||||
}
|
||||
|
||||
// is it a valid percentage value?
|
||||
if (_rawHumidity > 10000) {
|
||||
log_e("Humidity Sensor Percentage value out of range [0..100].");
|
||||
|
|
|
|||
|
|
@ -52,6 +52,11 @@ MatterOccupancySensor::~MatterOccupancySensor() {
|
|||
bool MatterOccupancySensor::begin(bool _occupancyState, OccupancySensorType_t _occupancySensorType) {
|
||||
ArduinoMatter::_init();
|
||||
|
||||
if (getEndPointId() != 0) {
|
||||
log_e("Matter Occupancy Sensor with Endpoint Id %d device has already been created.", getEndPointId());
|
||||
return false;
|
||||
}
|
||||
|
||||
occupancy_sensor::config_t occupancy_sensor_config;
|
||||
occupancy_sensor_config.occupancy_sensing.occupancy = _occupancyState;
|
||||
occupancy_sensor_config.occupancy_sensing.occupancy_sensor_type = _occupancySensorType;
|
||||
|
|
|
|||
|
|
@ -59,8 +59,13 @@ MatterOnOffLight::~MatterOnOffLight() {
|
|||
|
||||
bool MatterOnOffLight::begin(bool initialState) {
|
||||
ArduinoMatter::_init();
|
||||
on_off_light::config_t light_config;
|
||||
|
||||
if (getEndPointId() != 0) {
|
||||
log_e("Matter On-Off Light with Endpoint Id %d device has already been created.", getEndPointId());
|
||||
return false;
|
||||
}
|
||||
|
||||
on_off_light::config_t light_config;
|
||||
light_config.on_off.on_off = initialState;
|
||||
light_config.on_off.lighting.start_up_on_off = nullptr;
|
||||
onOffState = initialState;
|
||||
|
|
|
|||
|
|
@ -59,8 +59,13 @@ MatterOnOffPlugin::~MatterOnOffPlugin() {
|
|||
|
||||
bool MatterOnOffPlugin::begin(bool initialState) {
|
||||
ArduinoMatter::_init();
|
||||
on_off_plugin_unit::config_t plugin_config;
|
||||
|
||||
if (getEndPointId() != 0) {
|
||||
log_e("Matter On-Off Plugin with Endpoint Id %d device has already been created.", getEndPointId());
|
||||
return false;
|
||||
}
|
||||
|
||||
on_off_plugin_unit::config_t plugin_config;
|
||||
plugin_config.on_off.on_off = initialState;
|
||||
plugin_config.on_off.lighting.start_up_on_off = nullptr;
|
||||
|
||||
|
|
|
|||
|
|
@ -42,6 +42,11 @@ MatterPressureSensor::~MatterPressureSensor() {
|
|||
bool MatterPressureSensor::begin(int16_t _rawPressure) {
|
||||
ArduinoMatter::_init();
|
||||
|
||||
if (getEndPointId() != 0) {
|
||||
log_e("Matter Pressure Sensor with Endpoint Id %d device has already been created.", getEndPointId());
|
||||
return false;
|
||||
}
|
||||
|
||||
pressure_sensor::config_t pressure_sensor_config;
|
||||
pressure_sensor_config.pressure_measurement.pressure_measured_value = _rawPressure;
|
||||
pressure_sensor_config.pressure_measurement.pressure_min_measured_value = nullptr;
|
||||
|
|
|
|||
|
|
@ -42,6 +42,11 @@ MatterTemperatureSensor::~MatterTemperatureSensor() {
|
|||
bool MatterTemperatureSensor::begin(int16_t _rawTemperature) {
|
||||
ArduinoMatter::_init();
|
||||
|
||||
if (getEndPointId() != 0) {
|
||||
log_e("Temperature Sensor with Endpoint Id %d device has already been created.", getEndPointId());
|
||||
return false;
|
||||
}
|
||||
|
||||
temperature_sensor::config_t temperature_sensor_config;
|
||||
temperature_sensor_config.temperature_measurement.measured_value = _rawTemperature;
|
||||
temperature_sensor_config.temperature_measurement.min_measured_value = nullptr;
|
||||
|
|
|
|||
Loading…
Reference in a new issue