diff --git a/fw/Core/Src/main.c b/fw/Core/Src/main.c index c184977..2bcd962 100644 --- a/fw/Core/Src/main.c +++ b/fw/Core/Src/main.c @@ -902,16 +902,28 @@ void CO2_to_color(uint16_t co2, uint16_t co2_limit_yellow, uint16_t co2_limit_re { const uint16_t co2_limit_green = 400; /* fresh air, lowest possible value */ - uint32_t co2_full_span; + uint32_t co2_interval_length; + uint32_t co2_lower_limit; uint32_t intensity; + uint32_t intensity_offset; if (co2 < co2_limit_green) { rgbled_set_color(RGBLED_GREEN); } else if (co2 > co2_limit_red) { rgbled_set_color(RGBLED_RED); } else { - co2_full_span = co2_limit_red - co2_limit_green; - intensity = (co2 - co2_limit_green) * 255 / co2_full_span; + if (co2 < co2_limit_yellow) { + /* first interval: from green to yellow (co2_alert_limit1) */ + co2_interval_length = co2_limit_yellow - co2_limit_green; + co2_lower_limit = co2_limit_green; + intensity_offset = 0; + } else { + /* second interval: from yellow to red (co2_alert_limit2) */ + co2_interval_length = co2_limit_red - co2_limit_yellow; + co2_lower_limit = co2_limit_yellow; + intensity_offset = 127; + } + intensity = (co2 - co2_lower_limit) * 128 / co2_interval_length + intensity_offset; rgbled_set_color(intensity, 255 - intensity, 0); } }