Skip to content

Commit 5fcfa07

Browse files
committed
Changed to explicit safety checks instead of try/catch.
1 parent 20d7862 commit 5fcfa07

File tree

2 files changed

+26
-31
lines changed

2 files changed

+26
-31
lines changed

src/display/plugins/ShotHistoryPlugin.cpp

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,22 @@ void ShotHistoryPlugin::setup(Controller *c, PluginManager *pm) {
1515
pm->on("controller:volumetric-measurement:estimation:change",
1616
[this](Event const &event) { currentEstimatedWeight = event.getFloat("value"); });
1717
pm->on("controller:volumetric-measurement:bluetooth:change", [this](Event const &event) {
18-
try {
19-
const float weight = event.getFloat("value");
20-
const unsigned long now = millis();
21-
if (lastVolumeSample != 0) {
22-
const unsigned long timeDiff = now - lastVolumeSample;
23-
const float volumeDiff = weight - currentBluetoothWeight;
24-
currentBluetoothFlow = volumeDiff / static_cast<float>(timeDiff) * 1000.0f;
25-
}
26-
lastVolumeSample = now;
27-
currentBluetoothWeight = weight;
28-
} catch (...) {
29-
// If there's any exception processing weight data, ignore this update
30-
// This prevents crashes if BLE data is corrupted or connection is unstable
18+
// Explicit checks instead of try/catch for embedded systems
19+
const float weight = event.getFloat("value");
20+
21+
// Validate weight data before processing
22+
if (isnan(weight) || weight < 0) {
23+
return; // Skip invalid weight data
24+
}
25+
26+
const unsigned long now = millis();
27+
if (lastVolumeSample != 0) {
28+
const unsigned long timeDiff = now - lastVolumeSample;
29+
const float volumeDiff = weight - currentBluetoothWeight;
30+
currentBluetoothFlow = volumeDiff / static_cast<float>(timeDiff) * 1000.0f;
3131
}
32+
lastVolumeSample = now;
33+
currentBluetoothWeight = weight;
3234
});
3335
pm->on("boiler:currentTemperature:change", [this](Event const &event) { currentTemperature = event.getFloat("value"); });
3436
xTaskCreatePinnedToCore(loopTask, "ShotHistoryPlugin::loop", configMINIMAL_STACK_SIZE * 3, this, 1, &taskHandle, 0);
@@ -71,14 +73,10 @@ void ShotHistoryPlugin::record() {
7173
if (extendedRecording) {
7274
const unsigned long now = millis();
7375

74-
// Add safety check to prevent crashes if BLE connection is unstable
75-
bool canProcessWeight = true;
76-
try {
77-
if (!controller || !controller->isVolumetricAvailable()) {
78-
canProcessWeight = false;
79-
}
80-
} catch (...) {
81-
canProcessWeight = false;
76+
// Explicit safety checks instead of try/catch for embedded systems
77+
bool canProcessWeight = (controller != nullptr);
78+
if (canProcessWeight) {
79+
canProcessWeight = controller->isVolumetricAvailable();
8280
}
8381

8482
if (!canProcessWeight) {
@@ -152,17 +150,14 @@ void ShotHistoryPlugin::endRecording() {
152150
recording = false;
153151

154152
// Check if we have active bluetooth weight data (regardless of volumetric mode)
155-
// Add safety check to ensure we have a valid controller and BLE connection
156153
bool hasActiveWeightData = false;
157-
try {
158-
hasActiveWeightData = controller &&
159-
controller->isVolumetricAvailable() &&
160-
currentBluetoothWeight > 0;
161-
} catch (...) {
162-
// If there's any exception checking BLE status, assume no weight data
163-
hasActiveWeightData = false;
164-
}
165154

155+
if (controller != nullptr) {
156+
if (controller->isVolumetricAvailable() && currentBluetoothWeight > 0) {
157+
hasActiveWeightData = true;
158+
}
159+
}
160+
166161
if (hasActiveWeightData) {
167162
// Start extended recording for any shot with active weight data
168163
extendedRecording = true;

src/display/plugins/ShotHistoryPlugin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <display/core/utils.h>
88

99
constexpr size_t SHOT_HISTORY_INTERVAL = 100;
10-
constexpr size_t MAX_HISTORY_ENTRIES = 3;
10+
constexpr size_t MAX_HISTORY_ENTRIES = 6;
1111
constexpr unsigned long EXTENDED_RECORDING_DURATION = 3000; // 3 seconds
1212
constexpr unsigned long WEIGHT_STABILIZATION_TIME = 1000; // 1 second
1313
constexpr float WEIGHT_STABILIZATION_THRESHOLD = 0.1f; // 0.1g threshold

0 commit comments

Comments
 (0)