|
3 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | 4 | // you may not use this file except in compliance with the License. |
5 | 5 | // You may obtain a copy of the License at |
6 | | - |
| 6 | +// |
7 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | 8 | // |
9 | 9 | // Unless required by applicable law or agreed to in writing, software |
@@ -53,6 +53,119 @@ uint32_t button_time_stamp = 0; // debouncing control |
53 | 53 | bool button_state = false; // false = released | true = pressed |
54 | 54 | const uint32_t decommissioningTimeout = 5000; // keep the button pressed for 5s, or longer, to decommission |
55 | 55 |
|
| 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 | + |
56 | 169 | void setup() { |
57 | 170 | // Initialize the USER BUTTON (Boot button) that will be used to decommission the Matter Node |
58 | 171 | pinMode(buttonPin, INPUT_PULLUP); |
@@ -111,77 +224,24 @@ void setup() { |
111 | 224 | void loop() { |
112 | 225 | static uint32_t timeCounter = 0; |
113 | 226 | 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(); |
123 | 230 |
|
124 | 231 | // Update temperature setpoint dynamically every 1 second |
125 | 232 | uint32_t currentTime = millis(); |
126 | 233 | if (currentTime - lastUpdateTime >= 1000) { // 1 second interval |
127 | 234 | 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(); |
155 | 236 | } |
156 | 237 |
|
157 | 238 | // Print the current temperature setpoint every 5s |
158 | 239 | 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(); |
164 | 241 | } |
165 | 242 |
|
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(); |
184 | 245 |
|
185 | 246 | delay(500); |
186 | 247 | } |
187 | | - |
|
0 commit comments