From dc7169596de210521651a303520b44cc7da04439 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michael=20K=C3=A4ppler?= <michael_kaeppler@web.de>
Date: Thu, 29 Apr 2021 13:54:31 +0200
Subject: [PATCH] Cast `co2` to uint32_t to avoid overflow

---
 ampel-firmware/co2_sensor.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/ampel-firmware/co2_sensor.cpp b/ampel-firmware/co2_sensor.cpp
index 3abe0aa..823c21e 100644
--- a/ampel-firmware/co2_sensor.cpp
+++ b/ampel-firmware/co2_sensor.cpp
@@ -8,7 +8,7 @@ namespace config {
   uint16_t co2_calibration_level = ATMOSPHERIC_CO2_CONCENTRATION; // [ppm]
   int8_t max_deviation_during_calibration = 30; // [ppm]
   int8_t enough_stable_measurements = 60;
-  const uint8_t max_deviation_during_accl = 20; // [%]
+  const uint8_t max_deviation_during_bootup = 20; // [%]
 #ifdef TEMPERATURE_OFFSET
   // Residual heat from CO2 sensor seems to be high enough to change the temperature reading. How much should it be offset?
   // NOTE: Sign isn't relevant. The returned temperature will always be shifted down.
@@ -135,7 +135,7 @@ namespace sensor {
     last_co2 = co2;
     // We assume the sensor has acclimated to the environment if measurements
     // change less than a specified percentage of the current value.
-    return (co2 > 0 && (100 * delta / config::max_deviation_during_accl) < co2);
+    return (co2 > 0 && delta < ((uint32_t)co2 * config::max_deviation_during_bootup / 100));
   }
 
   bool countStableMeasurements() {
-- 
GitLab