Skip to content

Commit 79f372a

Browse files
committed
fix(mlx90632): skip re-reading if recently read
1 parent 96fa8d2 commit 79f372a

File tree

1 file changed

+64
-63
lines changed

1 file changed

+64
-63
lines changed

src/components/i2c/drivers/WipperSnapper_I2C_Driver_MLX90632D.h

Lines changed: 64 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -79,93 +79,93 @@ class WipperSnapper_I2C_Driver_MLX90632D : public WipperSnapper_I2C_Driver {
7979
bool ConfigureAndPrintSensorInfo(bool extendedInsteadOfMedicalRange = false) {
8080
// Reset the device
8181
if (!_mlx90632->reset()) {
82-
WS_PRINTER.println(F("Device reset failed"));
82+
WS_DEBUG_PRINTLN(F("Device reset failed"));
8383
while (1) {
8484
delay(10);
8585
}
8686
}
87-
WS_PRINTER.println(F("Device reset: SUCCESS"));
87+
WS_DEBUG_PRINTLN(F("Device reset: SUCCESS"));
8888

8989
uint64_t productID = _mlx90632->getProductID();
90-
WS_PRINTER.print(F("Product ID: 0x"));
91-
WS_PRINTER.print((uint32_t)(productID >> 32), HEX);
92-
WS_PRINTER.println((uint32_t)(productID & 0xFFFFFFFF), HEX);
90+
WS_DEBUG_PRINT(F("Product ID: 0x"));
91+
WS_DEBUG_PRINT((uint32_t)(productID >> 32), HEX);
92+
WS_DEBUG_PRINTLN((uint32_t)(productID & 0xFFFFFFFF), HEX);
9393

9494
uint16_t productCode = _mlx90632->getProductCode();
95-
WS_PRINTER.print(F("Product Code: 0x"));
96-
WS_PRINTER.println(productCode, HEX);
95+
WS_DEBUG_PRINT(F("Product Code: 0x"));
96+
WS_DEBUG_PRINTLN(productCode, HEX);
9797

9898
uint16_t eepromVersion = _mlx90632->getEEPROMVersion();
99-
WS_PRINTER.print(F("EEPROM Version: 0x"));
100-
WS_PRINTER.println(eepromVersion, HEX);
99+
WS_DEBUG_PRINT(F("EEPROM Version: 0x"));
100+
WS_DEBUG_PRINTLN(eepromVersion, HEX);
101101

102102
// Decode product code bits
103103
uint8_t fov = (productCode >> 8) & 0x3;
104104
uint8_t package = (productCode >> 5) & 0x7;
105105
uint8_t accuracy = productCode & 0x1F;
106106

107-
WS_PRINTER.print(F("FOV: "));
108-
WS_PRINTER.println(fov == 0 ? F("50°") : F("Unknown"));
107+
WS_DEBUG_PRINT(F("FOV: "));
108+
WS_DEBUG_PRINTLN(fov == 0 ? F("50°") : F("Unknown"));
109109

110-
WS_PRINTER.print(F("Package: "));
111-
WS_PRINTER.println(package == 1 ? F("SFN 3x3") : F("Unknown"));
110+
WS_DEBUG_PRINT(F("Package: "));
111+
WS_DEBUG_PRINTLN(package == 1 ? F("SFN 3x3") : F("Unknown"));
112112

113-
WS_PRINTER.print(F("Accuracy: "));
113+
WS_DEBUG_PRINT(F("Accuracy: "));
114114
if (accuracy == 1) {
115-
WS_PRINTER.println(F("Medical"));
115+
WS_DEBUG_PRINTLN(F("Medical"));
116116
} else if (accuracy == 2) {
117-
WS_PRINTER.println(F("Standard"));
117+
WS_DEBUG_PRINTLN(F("Standard"));
118118
} else {
119-
WS_PRINTER.println(F("Unknown"));
119+
WS_DEBUG_PRINTLN(F("Unknown"));
120120
}
121121

122122
// Set and get mode - choose one:
123-
WS_PRINTER.println(F("\n--- Mode Settings ---"));
123+
WS_DEBUG_PRINTLN(F("\n--- Mode Settings ---"));
124124
if (!_mlx90632->setMode(MLX90632_MODE_CONTINUOUS)) {
125125
// if (!_mlx90632->setMode(MLX90632_MODE_STEP)) { // Uncomment
126126
// for step mode testing if
127127
// (!_mlx90632->setMode(MLX90632_MODE_SLEEPING_STEP)) { // Uncomment for
128128
// sleeping step mode testing
129-
WS_PRINTER.println(F("Failed to set mode"));
129+
WS_DEBUG_PRINTLN(F("Failed to set mode"));
130130
while (1) {
131131
delay(10);
132132
}
133133
}
134134

135135
// TODO: use Step mode?
136136
mlx90632_mode_t currentMode = _mlx90632->getMode();
137-
WS_PRINTER.print(F("Current mode: "));
137+
WS_DEBUG_PRINT(F("Current mode: "));
138138
switch (currentMode) {
139139
case MLX90632_MODE_HALT:
140-
WS_PRINTER.println(F("Halt"));
140+
WS_DEBUG_PRINTLN(F("Halt"));
141141
break;
142142
case MLX90632_MODE_SLEEPING_STEP:
143-
WS_PRINTER.println(F("Sleeping Step"));
143+
WS_DEBUG_PRINTLN(F("Sleeping Step"));
144144
break;
145145
case MLX90632_MODE_STEP:
146-
WS_PRINTER.println(F("Step"));
146+
WS_DEBUG_PRINTLN(F("Step"));
147147
break;
148148
case MLX90632_MODE_CONTINUOUS:
149-
WS_PRINTER.println(F("Continuous"));
149+
WS_DEBUG_PRINTLN(F("Continuous"));
150150
break;
151151
default:
152-
WS_PRINTER.println(F("Unknown"));
152+
WS_DEBUG_PRINTLN(F("Unknown"));
153153
}
154154

155155
// set accuracy mode based on medical if detected
156156
if (accuracy == 1) {
157157
// Set and get measurement select (medical)
158-
WS_PRINTER.println(F("\n--- Measurement Select Settings ---"));
158+
WS_DEBUG_PRINTLN(F("\n--- Measurement Select Settings ---"));
159159
if (!extendedInsteadOfMedicalRange &&
160160
!_mlx90632->setMeasurementSelect(MLX90632_MEAS_MEDICAL)) {
161-
WS_PRINTER.println(F("Failed to set measurement select to Medical"));
161+
WS_DEBUG_PRINTLN(F("Failed to set measurement select to Medical"));
162162
while (1) {
163163
delay(10);
164164
}
165165
} else if (extendedInsteadOfMedicalRange &&
166166
!_mlx90632->setMeasurementSelect(
167167
MLX90632_MEAS_EXTENDED_RANGE)) {
168-
WS_PRINTER.println(
168+
WS_DEBUG_PRINTLN(
169169
F("Failed to set measurement select to Extended Range"));
170170
while (1) {
171171
delay(10);
@@ -174,63 +174,63 @@ class WipperSnapper_I2C_Driver_MLX90632D : public WipperSnapper_I2C_Driver {
174174

175175
mlx90632_meas_select_t currentMeasSelect =
176176
_mlx90632->getMeasurementSelect();
177-
WS_PRINTER.print(F("Current measurement select: "));
177+
WS_DEBUG_PRINT(F("Current measurement select: "));
178178
switch (currentMeasSelect) {
179179
case MLX90632_MEAS_MEDICAL:
180-
WS_PRINTER.println(F("Medical"));
180+
WS_DEBUG_PRINTLN(F("Medical"));
181181
break;
182182
case MLX90632_MEAS_EXTENDED_RANGE:
183-
WS_PRINTER.println(F("Extended Range"));
183+
WS_DEBUG_PRINTLN(F("Extended Range"));
184184
break;
185185
default:
186-
WS_PRINTER.println(F("Unknown"));
186+
WS_DEBUG_PRINTLN(F("Unknown"));
187187
}
188188
}
189189

190190
// Set and get refresh rate (default to 2Hz)
191-
WS_PRINTER.println(F("\n--- Refresh Rate Settings ---"));
191+
WS_DEBUG_PRINTLN(F("\n--- Refresh Rate Settings ---"));
192192
if (!_mlx90632->setRefreshRate(MLX90632_REFRESH_2HZ)) {
193-
WS_PRINTER.println(F("Failed to set refresh rate to 2Hz"));
193+
WS_DEBUG_PRINTLN(F("Failed to set refresh rate to 2Hz"));
194194
while (1) {
195195
delay(10);
196196
}
197197
}
198198

199199
mlx90632_refresh_rate_t currentRefreshRate = _mlx90632->getRefreshRate();
200-
WS_PRINTER.print(F("Current refresh rate: "));
200+
WS_DEBUG_PRINT(F("Current refresh rate: "));
201201
switch (currentRefreshRate) {
202202
case MLX90632_REFRESH_0_5HZ:
203-
WS_PRINTER.println(F("0.5 Hz"));
203+
WS_DEBUG_PRINTLN(F("0.5 Hz"));
204204
break;
205205
case MLX90632_REFRESH_1HZ:
206-
WS_PRINTER.println(F("1 Hz"));
206+
WS_DEBUG_PRINTLN(F("1 Hz"));
207207
break;
208208
case MLX90632_REFRESH_2HZ:
209-
WS_PRINTER.println(F("2 Hz"));
209+
WS_DEBUG_PRINTLN(F("2 Hz"));
210210
break;
211211
case MLX90632_REFRESH_4HZ:
212-
WS_PRINTER.println(F("4 Hz"));
212+
WS_DEBUG_PRINTLN(F("4 Hz"));
213213
break;
214214
case MLX90632_REFRESH_8HZ:
215-
WS_PRINTER.println(F("8 Hz"));
215+
WS_DEBUG_PRINTLN(F("8 Hz"));
216216
break;
217217
case MLX90632_REFRESH_16HZ:
218-
WS_PRINTER.println(F("16 Hz"));
218+
WS_DEBUG_PRINTLN(F("16 Hz"));
219219
break;
220220
case MLX90632_REFRESH_32HZ:
221-
WS_PRINTER.println(F("32 Hz"));
221+
WS_DEBUG_PRINTLN(F("32 Hz"));
222222
break;
223223
case MLX90632_REFRESH_64HZ:
224-
WS_PRINTER.println(F("64 Hz"));
224+
WS_DEBUG_PRINTLN(F("64 Hz"));
225225
break;
226226
default:
227-
WS_PRINTER.println(F("Unknown"));
227+
WS_DEBUG_PRINTLN(F("Unknown"));
228228
}
229229

230230
// Clear new data flag before starting continuous measurements
231-
WS_PRINTER.println(F("\\n--- Starting Continuous Measurements ---"));
231+
WS_DEBUG_PRINTLN(F("\\n--- Starting Continuous Measurements ---"));
232232
if (!_mlx90632->resetNewData()) {
233-
WS_PRINTER.println(F("Failed to reset new data flag"));
233+
WS_DEBUG_PRINTLN(F("Failed to reset new data flag"));
234234
while (1) {
235235
delay(10);
236236
}
@@ -256,36 +256,39 @@ class WipperSnapper_I2C_Driver_MLX90632D : public WipperSnapper_I2C_Driver {
256256
/*******************************************************************************/
257257
bool ReadSensorData() {
258258
bool result = false;
259+
if (HasBeenReadInLast200ms()) {
260+
WS_DEBUG_PRINTLN(F("Sensor was read recently, using cached data"));
261+
return true;
262+
}
259263

260264
// Only check new data flag - much more efficient for continuous mode
261265
if (_mlx90632->isNewData()) {
262-
WS_PRINTER.print(F("New Data Available - Cycle Position: "));
263-
WS_PRINTER.println(_mlx90632->readCyclePosition());
266+
WS_DEBUG_PRINT(F("New Data Available - Cycle Position: "));
267+
WS_DEBUG_PRINTLN(_mlx90632->readCyclePosition());
264268

265269
// Read ambient temperature
266270
_deviceTemp = _mlx90632->getAmbientTemperature();
267-
WS_PRINTER.print(F("Ambient Temperature: "));
268-
WS_PRINTER.print(_deviceTemp, 4);
269-
WS_PRINTER.println(F(" °C"));
271+
WS_DEBUG_PRINT(F("Ambient Temperature: "));
272+
WS_DEBUG_PRINT(_deviceTemp, 4);
273+
WS_DEBUG_PRINTLN(F(" °C"));
270274

271275
// Read object temperature
272276
_objectTemp = _mlx90632->getObjectTemperature();
273-
WS_PRINTER.print(F("Object Temperature: "));
277+
WS_DEBUG_PRINT(F("Object Temperature: "));
274278
if (isnan(_objectTemp)) {
275-
WS_PRINTER.println(F("NaN (invalid cycle position)"));
279+
WS_DEBUG_PRINTLN(F("NaN (invalid cycle position)"));
276280
} else {
277-
WS_PRINTER.print(_objectTemp, 4);
278-
WS_PRINTER.println(F(" °C"));
281+
WS_DEBUG_PRINT(_objectTemp, 4);
282+
WS_DEBUG_PRINTLN(F(" °C"));
279283
}
280284
result = true;
285+
_lastRead = millis();
281286
// Reset new data flag after reading
282287
if (!_mlx90632->resetNewData()) {
283-
WS_PRINTER.println(F("Failed to reset new data flag"));
288+
WS_DEBUG_PRINTLN(F("Failed to reset new data flag"));
284289
}
285-
286-
WS_PRINTER.println(); // Add blank line between readings
287290
} else {
288-
WS_PRINTER.println(F("No new data available, skipping read"));
291+
WS_DEBUG_PRINTLN(F("No new data available, skipping read"));
289292
}
290293

291294
// Check if we need to trigger a new measurement for step modes
@@ -294,11 +297,9 @@ class WipperSnapper_I2C_Driver_MLX90632D : public WipperSnapper_I2C_Driver {
294297
currentMode == MLX90632_MODE_SLEEPING_STEP) {
295298
// Trigger single measurement (SOC bit) for step modes
296299
if (!_mlx90632->startSingleMeasurement()) {
297-
WS_PRINTER.println(F("Failed to start single measurement"));
300+
WS_DEBUG_PRINTLN(F("Failed to start single measurement"));
298301
}
299302
}
300-
301-
_lastRead = millis();
302303
return result;
303304
}
304305

@@ -315,7 +316,7 @@ class WipperSnapper_I2C_Driver_MLX90632D : public WipperSnapper_I2C_Driver {
315316
if (ReadSensorData() && _deviceTemp != NAN) {
316317
// TODO: check max/min or error values in datasheet
317318
// if (_deviceTemp < -40 || _deviceTemp > 125) {
318-
// WS_PRINTER.println(F("Invalid ambient temperature"));
319+
// WS_DEBUG_PRINTLN(F("Invalid ambient temperature"));
319320
// return false;
320321
// }
321322
// if the sensor was read recently, return the cached temperature

0 commit comments

Comments
 (0)