Skip to content

Commit 132e752

Browse files
committed
feat(matter): organize examples, keywords and fix init error
1 parent 657d118 commit 132e752

File tree

7 files changed

+292
-129
lines changed

7 files changed

+292
-129
lines changed

docs/en/matter/ep_temperature_controlled_cabinet.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,15 +279,15 @@ Example
279279
Temperature Controlled Cabinet
280280
******************************
281281

282-
The example demonstrates the temperature_number mode with dynamic temperature updates. The temperature setpoint automatically cycles between the minimum and maximum limits every 1 second using the configured step value, allowing Matter controllers to observe real-time changes.
282+
The example demonstrates the temperature_number mode with dynamic temperature updates. The temperature setpoint automatically cycles between the minimum and maximum limits every 1 second using the configured step value, allowing Matter controllers to observe real-time changes. The example also monitors and logs when the initial setpoint is reached or overpassed in each direction.
283283

284284
.. literalinclude:: ../../../libraries/Matter/examples/MatterTemperatureControlledCabinet/MatterTemperatureControlledCabinet.ino
285285
:language: arduino
286286

287287
Temperature Controlled Cabinet (Level Mode)
288288
********************************************
289289

290-
A separate example demonstrates the temperature_level mode with dynamic level updates. The temperature level automatically cycles through all supported levels every 1 second, allowing Matter controllers to observe real-time changes.
290+
A separate example demonstrates the temperature_level mode with dynamic level updates. The temperature level automatically cycles through all supported levels every 1 second in both directions (increasing and decreasing), allowing Matter controllers to observe real-time changes. The example also monitors and logs when the initial level is reached or overpassed in each direction.
291291

292292
See ``MatterTemperatureControlledCabinetLevels`` example for the temperature level mode implementation.
293293

libraries/Matter/examples/MatterTemperatureControlledCabinet/MatterTemperatureControlledCabinet.ino

Lines changed: 121 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
55
// You may obtain a copy of the License at
6-
6+
//
77
// http://www.apache.org/licenses/LICENSE-2.0
88
//
99
// Unless required by applicable law or agreed to in writing, software
@@ -53,6 +53,119 @@ uint32_t button_time_stamp = 0; // debouncing control
5353
bool button_state = false; // false = released | true = pressed
5454
const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission
5555

56+
// Temperature control state
57+
struct TemperatureControlState {
58+
bool initialized;
59+
bool increasing;
60+
double currentSetpoint;
61+
double initialSetpoint;
62+
bool setpointReachedIncreasing;
63+
bool setpointReachedDecreasing;
64+
};
65+
66+
static TemperatureControlState tempState = {
67+
.initialized = false,
68+
.increasing = true,
69+
.currentSetpoint = 0.0,
70+
.initialSetpoint = 0.0,
71+
.setpointReachedIncreasing = false,
72+
.setpointReachedDecreasing = false
73+
};
74+
75+
// Initialize temperature control state
76+
void initTemperatureControl() {
77+
if (!tempState.initialized) {
78+
tempState.currentSetpoint = TemperatureCabinet.getTemperatureSetpoint();
79+
tempState.initialSetpoint = tempState.currentSetpoint;
80+
tempState.initialized = true;
81+
}
82+
}
83+
84+
// Check and log when initial setpoint is reached/overpassed
85+
void checkSetpointReached(double newSetpoint, bool isIncreasing, bool directionChanged) {
86+
if (directionChanged) {
87+
// Reset flags when direction changes
88+
tempState.setpointReachedIncreasing = false;
89+
tempState.setpointReachedDecreasing = false;
90+
return;
91+
}
92+
93+
if (isIncreasing && !tempState.setpointReachedIncreasing && newSetpoint >= tempState.initialSetpoint) {
94+
Serial.printf("*** Temperature setpoint %.02f°C reached/overpassed while increasing ***\r\n", tempState.initialSetpoint);
95+
tempState.setpointReachedIncreasing = true;
96+
} else if (!isIncreasing && !tempState.setpointReachedDecreasing && newSetpoint <= tempState.initialSetpoint) {
97+
Serial.printf("*** Temperature setpoint %.02f°C reached/overpassed while decreasing ***\r\n", tempState.initialSetpoint);
98+
tempState.setpointReachedDecreasing = true;
99+
}
100+
}
101+
102+
// Update temperature setpoint with cycling logic
103+
void updateTemperatureSetpoint() {
104+
double minTemp = TemperatureCabinet.getMinTemperature();
105+
double maxTemp = TemperatureCabinet.getMaxTemperature();
106+
double step = TemperatureCabinet.getStep();
107+
108+
// Calculate next setpoint based on direction and step
109+
bool directionChanged = false;
110+
111+
if (tempState.increasing) {
112+
tempState.currentSetpoint += step;
113+
if (tempState.currentSetpoint >= maxTemp) {
114+
tempState.currentSetpoint = maxTemp;
115+
tempState.increasing = false; // Reverse direction
116+
directionChanged = true;
117+
}
118+
} else {
119+
tempState.currentSetpoint -= step;
120+
if (tempState.currentSetpoint <= minTemp) {
121+
tempState.currentSetpoint = minTemp;
122+
tempState.increasing = true; // Reverse direction
123+
directionChanged = true;
124+
}
125+
}
126+
127+
// Check if setpoint has been reached or overpassed
128+
checkSetpointReached(tempState.currentSetpoint, tempState.increasing, directionChanged);
129+
130+
// Update the temperature setpoint
131+
if (TemperatureCabinet.setTemperatureSetpoint(tempState.currentSetpoint)) {
132+
Serial.printf("Temperature setpoint updated to: %.02f°C (Range: %.02f°C to %.02f°C)\r\n",
133+
tempState.currentSetpoint, minTemp, maxTemp);
134+
} else {
135+
Serial.printf("Failed to update temperature setpoint to: %.02f°C\r\n", tempState.currentSetpoint);
136+
}
137+
}
138+
139+
// Print current temperature status
140+
void printTemperatureStatus() {
141+
Serial.printf("Current Temperature Setpoint: %.02f°C (Range: %.02f°C to %.02f°C)\r\n",
142+
TemperatureCabinet.getTemperatureSetpoint(),
143+
TemperatureCabinet.getMinTemperature(),
144+
TemperatureCabinet.getMaxTemperature());
145+
}
146+
147+
// Handle button press for decommissioning
148+
void handleButtonPress() {
149+
// Check if the button has been pressed
150+
if (digitalRead(buttonPin) == LOW && !button_state) {
151+
// deals with button debouncing
152+
button_time_stamp = millis(); // record the time while the button is pressed.
153+
button_state = true; // pressed.
154+
}
155+
156+
if (digitalRead(buttonPin) == HIGH && button_state) {
157+
button_state = false; // released
158+
}
159+
160+
// Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node
161+
uint32_t time_diff = millis() - button_time_stamp;
162+
if (button_state && time_diff > decommissioningTimeout) {
163+
Serial.println("Decommissioning Temperature Controlled Cabinet Matter Accessory. It shall be commissioned again.");
164+
Matter.decommission();
165+
button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so
166+
}
167+
}
168+
56169
void setup() {
57170
// Initialize the USER BUTTON (Boot button) that will be used to decommission the Matter Node
58171
pinMode(buttonPin, INPUT_PULLUP);
@@ -111,77 +224,24 @@ void setup() {
111224
void loop() {
112225
static uint32_t timeCounter = 0;
113226
static uint32_t lastUpdateTime = 0;
114-
static bool initialized = false;
115-
static bool increasing = true; // Direction of temperature change
116-
static double currentSetpoint = 0.0; // Will be initialized from actual setpoint
117-
118-
// Initialize currentSetpoint from actual setpoint on first run
119-
if (!initialized) {
120-
currentSetpoint = TemperatureCabinet.getTemperatureSetpoint();
121-
initialized = true;
122-
}
227+
228+
// Initialize temperature control state on first run
229+
initTemperatureControl();
123230

124231
// Update temperature setpoint dynamically every 1 second
125232
uint32_t currentTime = millis();
126233
if (currentTime - lastUpdateTime >= 1000) { // 1 second interval
127234
lastUpdateTime = currentTime;
128-
129-
double minTemp = TemperatureCabinet.getMinTemperature();
130-
double maxTemp = TemperatureCabinet.getMaxTemperature();
131-
double step = TemperatureCabinet.getStep();
132-
133-
// Calculate next setpoint based on direction and step
134-
if (increasing) {
135-
currentSetpoint += step;
136-
if (currentSetpoint >= maxTemp) {
137-
currentSetpoint = maxTemp;
138-
increasing = false; // Reverse direction
139-
}
140-
} else {
141-
currentSetpoint -= step;
142-
if (currentSetpoint <= minTemp) {
143-
currentSetpoint = minTemp;
144-
increasing = true; // Reverse direction
145-
}
146-
}
147-
148-
// Update the temperature setpoint
149-
if (TemperatureCabinet.setTemperatureSetpoint(currentSetpoint)) {
150-
Serial.printf("Temperature setpoint updated to: %.02f°C (Range: %.02f°C to %.02f°C)\r\n",
151-
currentSetpoint, minTemp, maxTemp);
152-
} else {
153-
Serial.printf("Failed to update temperature setpoint to: %.02f°C\r\n", currentSetpoint);
154-
}
235+
updateTemperatureSetpoint();
155236
}
156237

157238
// Print the current temperature setpoint every 5s
158239
if (!(timeCounter++ % 10)) { // delaying for 500ms x 10 = 5s
159-
// Print the current temperature setpoint value
160-
Serial.printf("Current Temperature Setpoint: %.02f°C (Range: %.02f°C to %.02f°C)\r\n",
161-
TemperatureCabinet.getTemperatureSetpoint(),
162-
TemperatureCabinet.getMinTemperature(),
163-
TemperatureCabinet.getMaxTemperature());
240+
printTemperatureStatus();
164241
}
165242

166-
// Check if the button has been pressed
167-
if (digitalRead(buttonPin) == LOW && !button_state) {
168-
// deals with button debouncing
169-
button_time_stamp = millis(); // record the time while the button is pressed.
170-
button_state = true; // pressed.
171-
}
172-
173-
if (digitalRead(buttonPin) == HIGH && button_state) {
174-
button_state = false; // released
175-
}
176-
177-
// Onboard User Button is kept pressed for longer than 5 seconds in order to decommission matter node
178-
uint32_t time_diff = millis() - button_time_stamp;
179-
if (button_state && time_diff > decommissioningTimeout) {
180-
Serial.println("Decommissioning Temperature Controlled Cabinet Matter Accessory. It shall be commissioned again.");
181-
Matter.decommission();
182-
button_time_stamp = millis(); // avoid running decommissining again, reboot takes a second or so
183-
}
243+
// Handle button press for decommissioning
244+
handleButtonPress();
184245

185246
delay(500);
186247
}
187-

libraries/Matter/examples/MatterTemperatureControlledCabinet/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,14 @@ Temperature Controlled Cabinet Configuration:
115115
Max Temperature: 10.00°C
116116
Step: 0.50°C
117117
Temperature setpoint updated to: 4.50°C (Range: -10.00°C to 10.00°C)
118+
*** Temperature setpoint 4.00°C reached/overpassed while increasing ***
118119
Temperature setpoint updated to: 5.00°C (Range: -10.00°C to 10.00°C)
119120
Temperature setpoint updated to: 5.50°C (Range: -10.00°C to 10.00°C)
120121
...
121122
Current Temperature Setpoint: 6.00°C (Range: -10.00°C to 10.00°C)
123+
...
124+
*** Temperature setpoint 4.00°C reached/overpassed while decreasing ***
125+
Temperature setpoint updated to: 3.50°C (Range: -10.00°C to 10.00°C)
122126
```
123127

124128
## Using the Device
@@ -167,11 +171,20 @@ Use a Matter-compatible hub (like an Apple HomePod, Google Nest Hub, or Amazon E
167171
The MatterTemperatureControlledCabinet example consists of the following main components:
168172

169173
1. **`setup()`**: Initializes hardware (button), configures Wi-Fi (if needed), sets up the Matter Temperature Controlled Cabinet endpoint with initial temperature configuration, and waits for Matter commissioning.
174+
170175
2. **`loop()`**:
171176
- **Dynamic Temperature Updates**: Automatically changes the temperature setpoint every 1 second, cycling between the minimum and maximum temperature limits using the configured step value. This demonstrates the temperature control functionality and allows Matter controllers to observe real-time changes.
177+
- **Setpoint Reached Detection**: Monitors when the initial setpoint is reached or overpassed in each direction and prints a notification message once per direction.
172178
- Periodically prints the current temperature setpoint (every 5 seconds)
173179
- Handles button input for factory reset
174180

181+
3. **Helper Functions**:
182+
- `initTemperatureControl()`: Initializes the temperature control state from the current setpoint
183+
- `checkSetpointReached()`: Checks and logs when the initial setpoint is reached/overpassed
184+
- `updateTemperatureSetpoint()`: Updates the temperature setpoint with cycling logic and boundary detection
185+
- `printTemperatureStatus()`: Prints the current temperature status
186+
- `handleButtonPress()`: Handles button press detection and factory reset functionality
187+
175188
## API Usage
176189

177190
The example demonstrates the following API methods:

0 commit comments

Comments
 (0)