Skip to content

Commit 8cbc395

Browse files
ci(pre-commit): Apply automatic fixes
1 parent 9c38368 commit 8cbc395

File tree

2 files changed

+153
-153
lines changed

2 files changed

+153
-153
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,152 +1,152 @@
1-
// Copyright 2024 Espressif Systems (Shanghai) PTE LTD
2-
//
3-
// Licensed under the Apache License, Version 2.0 (the "License");
4-
// you may not use this file except in compliance with the License.
5-
// You may obtain a copy of the License at
6-
7-
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
9-
// Unless required by applicable law or agreed to in writing, software
10-
// distributed under the License is distributed on an "AS IS" BASIS,
11-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
// See the License for the specific language governing permissions and
13-
// limitations under the License.
14-
15-
/*
16-
* This example is an example code that will create a Matter Device which can be
17-
* commissioned and controlled from a Matter Environment APP.
18-
* Additionally the ESP32 will send debug messages indicating the Matter activity.
19-
* Turning DEBUG Level ON may be useful to following Matter Accessory and Controller messages.
20-
*
21-
* The example will create a Matter Contact Sensor Device.
22-
* The Contact Sensor state can be toggled by pressing the onboard button.
23-
* The Contact Sensor state will be indicated by the onboard LED.
24-
* The Contact Sensor state will be simulated to change every 20 seconds.
25-
*
26-
* The onboard button can be kept pressed for 5 seconds to decommission the Matter Node.
27-
* The example will also show the manual commissioning code and QR code to be used in the Matter environment.
28-
*
29-
*/
30-
31-
// Matter Manager
32-
#include <Matter.h>
33-
#include <WiFi.h>
34-
35-
// List of Matter Endpoints for this Node
36-
// Matter Contact Sensor Endpoint
37-
MatterContactSensor ContactSensor;
38-
39-
// LED will be used to indicate the Contact Sensor state
40-
// set your board RGB LED pin here
41-
#ifdef RGB_BUILTIN
42-
const uint8_t ledPin = RGB_BUILTIN;
43-
#else
44-
const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN
45-
#warning "Do not forget to set the RGB LED pin"
46-
#endif
47-
48-
// set your board USER BUTTON pin here - decommissioning and Manual Contact Sensor toggle button
49-
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
50-
51-
// WiFi is manually set and started
52-
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
53-
const char *password = "your-password"; // Change this to your WiFi password
54-
55-
// Button control
56-
uint32_t button_time_stamp = 0; // debouncing control
57-
bool button_state = false; // false = released | true = pressed
58-
const uint32_t debouceTime = 250; // button debouncing time (ms)
59-
const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission
60-
61-
void setup() {
62-
// Initialize the USER BUTTON (Boot button) that will be used to decommission the Matter Node
63-
// The button will also be used to manually toggle the Contact Sensor state
64-
pinMode(buttonPin, INPUT_PULLUP);
65-
// Initialize the LED (light) GPIO and Matter End Point
66-
pinMode(ledPin, OUTPUT);
67-
68-
Serial.begin(115200);
69-
70-
// Manually connect to WiFi
71-
WiFi.begin(ssid, password);
72-
// Wait for connection
73-
while (WiFi.status() != WL_CONNECTED) {
74-
delay(500);
75-
Serial.print(".");
76-
}
77-
Serial.println();
78-
79-
// set initial contact sensor state as false (default)
80-
ContactSensor.begin();
81-
digitalWrite(ledPin, LOW); // LED OFF
82-
83-
// Matter beginning - Last step, after all EndPoints are initialized
84-
Matter.begin();
85-
86-
// Check Matter Accessory Commissioning state, which may change during execution of loop()
87-
if (!Matter.isDeviceCommissioned()) {
88-
Serial.println("");
89-
Serial.println("Matter Node is not commissioned yet.");
90-
Serial.println("Initiate the device discovery in your Matter environment.");
91-
Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
92-
Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str());
93-
Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str());
94-
// waits for Matter Contact Sensor Commissioning.
95-
uint32_t timeCount = 0;
96-
while (!Matter.isDeviceCommissioned()) {
97-
delay(100);
98-
if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec
99-
Serial.println("Matter Node not commissioned yet. Waiting for commissioning.");
100-
}
101-
}
102-
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
103-
}
104-
}
105-
106-
bool simulatedHWContactSensor() {
107-
// Simulated Contact Sensor
108-
static bool contactState = false;
109-
static uint32_t lastTime = 0;
110-
111-
// Simulate a Contact Sensor state change every 20 seconds
112-
if (millis() - lastTime > 20000) {
113-
contactState = !contactState;
114-
lastTime = millis();
115-
}
116-
return contactState;
117-
}
118-
119-
void loop() {
120-
// Check if the button has been pressed
121-
if (digitalRead(buttonPin) == LOW && !button_state) {
122-
// deals with button debouncing
123-
button_time_stamp = millis(); // record the time while the button is pressed.
124-
button_state = true; // pressed.
125-
}
126-
127-
uint32_t time_diff = millis() - button_time_stamp;
128-
if (button_state && time_diff > debouceTime && digitalRead(buttonPin) == HIGH) {
129-
button_state = false; // released
130-
// button is released - toggle Contact State (Open/Closed)
131-
ContactSensor.setContact(!ContactSensor.getContact()); // same as ContactSensor = !ContactSensor;
132-
Serial.printf("User button released. Setting the Contact Sensor to %s.\r\n", ContactSensor ? "Closed" : "Open");
133-
// LED will indicate the Contact Sensor state
134-
if (ContactSensor) {
135-
digitalWrite(ledPin, HIGH); // LED ON
136-
} else {
137-
digitalWrite(ledPin, LOW); // LED OFF
138-
}
139-
}
140-
141-
// Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node
142-
if (button_state && time_diff > decommissioningTimeout) {
143-
Serial.println("Decommissioning the Generic Switch Matter Accessory. It shall be commissioned again.");
144-
Matter.decommission();
145-
button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so
146-
}
147-
148-
// Simulated Contact Sensor
149-
ContactSensor.setContact(simulatedHWContactSensor());
150-
151-
delay(50);
152-
}
1+
// Copyright 2024 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/*
16+
* This example is an example code that will create a Matter Device which can be
17+
* commissioned and controlled from a Matter Environment APP.
18+
* Additionally the ESP32 will send debug messages indicating the Matter activity.
19+
* Turning DEBUG Level ON may be useful to following Matter Accessory and Controller messages.
20+
*
21+
* The example will create a Matter Contact Sensor Device.
22+
* The Contact Sensor state can be toggled by pressing the onboard button.
23+
* The Contact Sensor state will be indicated by the onboard LED.
24+
* The Contact Sensor state will be simulated to change every 20 seconds.
25+
*
26+
* The onboard button can be kept pressed for 5 seconds to decommission the Matter Node.
27+
* The example will also show the manual commissioning code and QR code to be used in the Matter environment.
28+
*
29+
*/
30+
31+
// Matter Manager
32+
#include <Matter.h>
33+
#include <WiFi.h>
34+
35+
// List of Matter Endpoints for this Node
36+
// Matter Contact Sensor Endpoint
37+
MatterContactSensor ContactSensor;
38+
39+
// LED will be used to indicate the Contact Sensor state
40+
// set your board RGB LED pin here
41+
#ifdef RGB_BUILTIN
42+
const uint8_t ledPin = RGB_BUILTIN;
43+
#else
44+
const uint8_t ledPin = 2; // Set your pin here if your board has not defined LED_BUILTIN
45+
#warning "Do not forget to set the RGB LED pin"
46+
#endif
47+
48+
// set your board USER BUTTON pin here - decommissioning and Manual Contact Sensor toggle button
49+
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
50+
51+
// WiFi is manually set and started
52+
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
53+
const char *password = "your-password"; // Change this to your WiFi password
54+
55+
// Button control
56+
uint32_t button_time_stamp = 0; // debouncing control
57+
bool button_state = false; // false = released | true = pressed
58+
const uint32_t debouceTime = 250; // button debouncing time (ms)
59+
const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission
60+
61+
void setup() {
62+
// Initialize the USER BUTTON (Boot button) that will be used to decommission the Matter Node
63+
// The button will also be used to manually toggle the Contact Sensor state
64+
pinMode(buttonPin, INPUT_PULLUP);
65+
// Initialize the LED (light) GPIO and Matter End Point
66+
pinMode(ledPin, OUTPUT);
67+
68+
Serial.begin(115200);
69+
70+
// Manually connect to WiFi
71+
WiFi.begin(ssid, password);
72+
// Wait for connection
73+
while (WiFi.status() != WL_CONNECTED) {
74+
delay(500);
75+
Serial.print(".");
76+
}
77+
Serial.println();
78+
79+
// set initial contact sensor state as false (default)
80+
ContactSensor.begin();
81+
digitalWrite(ledPin, LOW); // LED OFF
82+
83+
// Matter beginning - Last step, after all EndPoints are initialized
84+
Matter.begin();
85+
86+
// Check Matter Accessory Commissioning state, which may change during execution of loop()
87+
if (!Matter.isDeviceCommissioned()) {
88+
Serial.println("");
89+
Serial.println("Matter Node is not commissioned yet.");
90+
Serial.println("Initiate the device discovery in your Matter environment.");
91+
Serial.println("Commission it to your Matter hub with the manual pairing code or QR code");
92+
Serial.printf("Manual pairing code: %s\r\n", Matter.getManualPairingCode().c_str());
93+
Serial.printf("QR code URL: %s\r\n", Matter.getOnboardingQRCodeUrl().c_str());
94+
// waits for Matter Contact Sensor Commissioning.
95+
uint32_t timeCount = 0;
96+
while (!Matter.isDeviceCommissioned()) {
97+
delay(100);
98+
if ((timeCount++ % 50) == 0) { // 50*100ms = 5 sec
99+
Serial.println("Matter Node not commissioned yet. Waiting for commissioning.");
100+
}
101+
}
102+
Serial.println("Matter Node is commissioned and connected to Wi-Fi. Ready for use.");
103+
}
104+
}
105+
106+
bool simulatedHWContactSensor() {
107+
// Simulated Contact Sensor
108+
static bool contactState = false;
109+
static uint32_t lastTime = 0;
110+
111+
// Simulate a Contact Sensor state change every 20 seconds
112+
if (millis() - lastTime > 20000) {
113+
contactState = !contactState;
114+
lastTime = millis();
115+
}
116+
return contactState;
117+
}
118+
119+
void loop() {
120+
// Check if the button has been pressed
121+
if (digitalRead(buttonPin) == LOW && !button_state) {
122+
// deals with button debouncing
123+
button_time_stamp = millis(); // record the time while the button is pressed.
124+
button_state = true; // pressed.
125+
}
126+
127+
uint32_t time_diff = millis() - button_time_stamp;
128+
if (button_state && time_diff > debouceTime && digitalRead(buttonPin) == HIGH) {
129+
button_state = false; // released
130+
// button is released - toggle Contact State (Open/Closed)
131+
ContactSensor.setContact(!ContactSensor.getContact()); // same as ContactSensor = !ContactSensor;
132+
Serial.printf("User button released. Setting the Contact Sensor to %s.\r\n", ContactSensor ? "Closed" : "Open");
133+
// LED will indicate the Contact Sensor state
134+
if (ContactSensor) {
135+
digitalWrite(ledPin, HIGH); // LED ON
136+
} else {
137+
digitalWrite(ledPin, LOW); // LED OFF
138+
}
139+
}
140+
141+
// Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node
142+
if (button_state && time_diff > decommissioningTimeout) {
143+
Serial.println("Decommissioning the Generic Switch Matter Accessory. It shall be commissioned again.");
144+
Matter.decommission();
145+
button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so
146+
}
147+
148+
// Simulated Contact Sensor
149+
ContactSensor.setContact(simulatedHWContactSensor());
150+
151+
delay(50);
152+
}

Diff for: libraries/Matter/src/MatterEndpoints/MatterContactSensor.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ bool MatterContactSensor::setContact(bool _contactState) {
7575
}
7676

7777
esp_matter_attr_val_t contactVal = esp_matter_invalid(NULL);
78-
78+
7979
if (!getAttributeVal(BooleanState::Id, BooleanState::Attributes::StateValue::Id, &contactVal)) {
8080
log_e("Failed to get Contact Sensor Attribute.");
8181
return false;

0 commit comments

Comments
 (0)