Skip to content

Commit 695df24

Browse files
committed
feat(matter): api simplification with begin
1 parent d3f8703 commit 695df24

File tree

6 files changed

+20
-25
lines changed

6 files changed

+20
-25
lines changed

libraries/Matter/examples/MatterCommissionTest/MatterCommissionTest.ino

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,11 @@ void setup() {
3939
Serial.println(WiFi.localIP());
4040
delay(500);
4141

42-
// Initialize Matter Node
43-
Matter.begin();
44-
// Initialize Matter EndPoint
42+
// Initialize at least one Matter EndPoint
4543
OnOffLight.begin();
46-
// Matter start - Last step, after all EndPoints are initialized
47-
48-
Matter.start();
44+
45+
// Matter begining - Last step, after all EndPoints are initialized
46+
Matter.begin();
4947
}
5048

5149
void loop() {

libraries/Matter/examples/MatterComposedLights/MatterComposedLights.ino

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ void setup() {
5757
Serial.println(WiFi.localIP());
5858
delay(500);
5959

60-
// Initialize Matter Node
61-
Matter.begin();
62-
6360
// Initialize all 3 Matter EndPoints
6461
OnOffLight1.begin();
6562
OnOffLight2.begin();
@@ -68,8 +65,8 @@ void setup() {
6865
OnOffLight2.onChangeOnOff(setLightOnOff2);
6966
OnOffLight3.onChangeOnOff(setLightOnOff3);
7067

71-
// Matter start - Last step, after all EndPoints are initialized
72-
Matter.start();
68+
// Matter begining - Last step, after all EndPoints are initialized
69+
Matter.begin();
7370
}
7471

7572
void loop() {

libraries/Matter/examples/MatterOnOffLight/MatterOnOffLight.ino

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,12 @@ void setup() {
5959
Serial.println(WiFi.localIP());
6060
delay(500);
6161

62-
// Initialize Matter Node
63-
Matter.begin();
64-
6562
// Initialize Matter EndPoint
6663
OnOffLight.begin(true);
6764
OnOffLight.onChangeOnOff(setLightOnOff);
6865

69-
// Matter start - Last step, after all EndPoints are initialized
70-
Matter.start();
66+
// Matter begining - Last step, after all EndPoints are initialized
67+
Matter.begin();
7168
}
7269

7370
uint32_t lastMillis = millis();

libraries/Matter/src/Matter.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,8 @@ static void app_event_cb(const ChipDeviceEvent *event, intptr_t arg) {
9191
}
9292
}
9393

94-
void ArduinoMatter::begin() {
94+
void ArduinoMatter::_init() {
9595
if (_matter_has_started) {
96-
log_w("Matter has already started.");
9796
return;
9897
}
9998

@@ -102,15 +101,15 @@ void ArduinoMatter::begin() {
102101
deviceNode = node::create(&node_config, app_attribute_update_cb, app_identification_cb);
103102
if (deviceNode == nullptr) {
104103
log_e("Failed to create Matter node");
105-
abort();
104+
return;
106105
}
107106

108107
_matter_has_started = true;
109108
}
110109

111-
void ArduinoMatter::start() {
110+
void ArduinoMatter::begin() {
112111
if (!_matter_has_started) {
113-
log_w("No Matter Node has been created. Execute Matter.begin() first.");
112+
log_w("No Matter endpoint has been created. Please create an endpoint first.");
114113
return;
115114
}
116115

libraries/Matter/src/Matter.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ using namespace esp_matter;
99

1010
class ArduinoMatter {
1111
public:
12-
static void begin();
1312
static inline String getManualPairingCode() {
1413
// return the pairing code for manual pairing
1514
return String("34970112332");
@@ -18,7 +17,7 @@ class ArduinoMatter {
1817
// return the URL for the QR code for onboarding
1918
return String("https://project-chip.github.io/connectedhomeip/qrcode.html?data=MT:Y.K9042C00KA0648G00");
2019
}
21-
static void start();
20+
static void begin();
2221
static bool isDeviceCommissioned();
2322
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION
2423
static bool isWiFiConnected();
@@ -28,6 +27,11 @@ class ArduinoMatter {
2827
#endif
2928
static bool isDeviceConnected();
3029
static void decommission();
30+
31+
// list of Matter EndPoints Friend Classes
32+
friend class MatterOnOffLight;
33+
protected:
34+
static void _init();
3135
};
3236

3337
extern ArduinoMatter Matter;

libraries/Matter/src/MatterOnOffLight.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ MatterOnOffLight::~MatterOnOffLight() {
4040
}
4141

4242
bool MatterOnOffLight::begin(bool initialState) {
43-
43+
ArduinoMatter::_init();
4444
on_off_light::config_t light_config;
4545
light_config.on_off.on_off = initialState;
4646
state = initialState;
@@ -50,7 +50,7 @@ bool MatterOnOffLight::begin(bool initialState) {
5050
endpoint_t *endpoint = on_off_light::create(node::get(), &light_config, ENDPOINT_FLAG_NONE, (void *) this);
5151
if (endpoint == nullptr) {
5252
log_e("Failed to create on-off light endpoint");
53-
abort();
53+
return false;
5454
}
5555

5656
setEndPointId(endpoint::get_id(endpoint));

0 commit comments

Comments
 (0)