Skip to content

Commit 9a0f1bd

Browse files
committed
feat(matter): better user button experience and api renaming
1 parent e6cecba commit 9a0f1bd

File tree

3 files changed

+30
-28
lines changed

3 files changed

+30
-28
lines changed

Diff for: libraries/Matter/examples/MatterHumiditySensor/MatterHumiditySensor.ino

+24-22
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@
2828
MatterHumiditySensor SimulatedHumiditySensor;
2929

3030
// set your board USER BUTTON pin here - decommissioning button
31-
#ifdef BOOT_PIN
3231
const uint8_t buttonPin = BOOT_PIN; // Set your pin here. Using BOOT Button.
33-
#else
34-
const uint8_t buttonPin = 0; // Set your button pin here.
35-
#warning "Do not forget to set the USER BUTTON pin"
36-
#endif
3732

3833
// WiFi is manually set and started
3934
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
4035
const char *password = "your-password"; // Change this to your WiFi password
4136

37+
// Button control - decommision the Matter Node
38+
uint32_t button_time_stamp = 0; // debouncing control
39+
bool button_state = false; // false = released | true = pressed
40+
const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission
41+
4242
// Simulate a humidity sensor - add your preferred humidity sensor library code here
4343
float getSimulatedHumidity() {
4444
// The Endpoint implementation keeps an uint16_t as internal value information,
@@ -96,13 +96,17 @@ void setup() {
9696
}
9797
}
9898

99-
// Button control - decommision the Matter Node
100-
uint32_t button_time_stamp = 0; // debouncing control
101-
bool button_state = false; // false = released | true = pressed
102-
const uint32_t decommissioningTimeout = 10000; // keep the button pressed for 10s to decommission
103-
10499
void loop() {
105-
Serial.printf("Current Humidity is %.02f%%\r\n", SimulatedHumiditySensor.getHumidityPercent());
100+
static uint32_t timeCounter = 0;
101+
102+
// Print the current humidity value every 5s
103+
if (!(timeCounter++ % 10)) { // delaying for 500ms x 10 = 5s
104+
// Print the current humidity value
105+
Serial.printf("Current Humidity is %.02f%%\r\n", SimulatedHumiditySensor.getHumidity());
106+
// Update Humidity from the (Simulated) Hardware Sensor
107+
// Matter APP shall display the updated humidity percent
108+
SimulatedHumiditySensor.setHumidity(getSimulatedHumidity());
109+
}
106110

107111
// Check if the button has been pressed
108112
if (digitalRead(buttonPin) == LOW && !button_state) {
@@ -111,19 +115,17 @@ void loop() {
111115
button_state = true; // pressed.
112116
}
113117

114-
// Onboard User Button is used to decommission matter node
115-
uint32_t time_diff = millis() - button_time_stamp;
116-
if (button_state && time_diff > decommissioningTimeout && digitalRead(buttonPin) == HIGH) {
118+
if (digitalRead(buttonPin) == HIGH && button_state) {
117119
button_state = false; // released
120+
}
121+
122+
// Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node
123+
uint32_t time_diff = millis() - button_time_stamp;
124+
if (button_state && time_diff > decommissioningTimeout) {
118125
// Factory reset is triggered if the button is pressed longer than 10 seconds
119-
if (time_diff > decommissioningTimeout) {
120-
Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again.");
121-
Matter.decommission();
122-
}
126+
Serial.println("Decommissioning the Light Matter Accessory. It shall be commissioned again.");
127+
Matter.decommission();
123128
}
124129

125-
// update the humidity sensor value every 5 seconds
126-
delay(5000);
127-
// Matter APP shall display the updated humidity percent
128-
SimulatedHumiditySensor.setHumidityPercent(getSimulatedHumidity());
130+
delay(500);
129131
}

Diff for: libraries/Matter/keywords.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ onChangeMode KEYWORD2
6666
onChangeSpeedPercent KEYWORD2
6767
setTemperature KEYWORD2
6868
getTemperature KEYWORD2
69-
setHumidityPercent KEYWORD2
70-
getHumidityPercent KEYWORD2
69+
setHumidity KEYWORD2
70+
getHumidity KEYWORD2
7171

7272
#######################################
7373
# Constants (LITERAL1)

Diff for: libraries/Matter/src/MatterEndpoints/MatterHumiditySensor.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,24 @@ class MatterHumiditySensor : public MatterEndPoint {
3535
void end();
3636

3737
// set the humidity percent with 1/100th of a percent precision
38-
bool setHumidityPercent(double humidityPercent) {
38+
bool setHumidity(double humidityPercent) {
3939
if (humidityPercent < 0.0 || humidityPercent > 100.0) {
4040
log_e("Humidity Sensor Percentage value out of range [0..100].");
4141
return false;
4242
}
4343
return setRawHumidity(static_cast<uint16_t>(humidityPercent * 100.0f));
4444
}
4545
// returns the reported float humidity percent with 1/100th of precision
46-
double getHumidityPercent() {
46+
double getHumidity() {
4747
return (double)rawHumidity / 100.0;
4848
}
4949
// double conversion operator
5050
void operator=(double humidityPercent) {
51-
setHumidityPercent(humidityPercent);
51+
setHumidity(humidityPercent);
5252
}
5353
// double conversion operator
5454
operator double() {
55-
return (double) getHumidityPercent();
55+
return (double) getHumidity();
5656
}
5757

5858
// this function is called by Matter internal event processor. It could be overwritten by the application, if necessary.

0 commit comments

Comments
 (0)