From ac38f7b003aae1cdee7a0e5921f9b1ac98e9dde6 Mon Sep 17 00:00:00 2001 From: Bertrand Lemasle Date: Sun, 5 Aug 2018 21:31:47 +1200 Subject: [PATCH] Reduced conditions for allerts triggering and clearing (-46 bytes) --- GpsTracker/Alerts.cpp | 39 ++++++++++----------------------------- 1 file changed, 10 insertions(+), 29 deletions(-) diff --git a/GpsTracker/Alerts.cpp b/GpsTracker/Alerts.cpp index 9a39699..33bab8c 100644 --- a/GpsTracker/Alerts.cpp +++ b/GpsTracker/Alerts.cpp @@ -11,25 +11,14 @@ namespace alerts { uint8_t getTriggered(PositionEntryMetadata &metadata) { config_t* config = &config::main::value; - uint8_t result = 0; + uint8_t active = 0; - if (metadata.batteryLevel <= config->alertBatteryLevel1 && !bitRead(config->activeAlerts, ALERT_BATTERY_LEVEL_1)) { - bitSet(result, ALERT_BATTERY_LEVEL_1); - } + if (metadata.batteryLevel <= config->alertBatteryLevel1) bitSet(active, ALERT_BATTERY_LEVEL_1); + if (metadata.batteryLevel <= config->alertBatteryLevel2) bitSet(active, ALERT_BATTERY_LEVEL_2); + if (metadata.temperature == ALERT_SUSPICIOUS_RTC_TEMPERATURE) bitSet(active, ALERT_RTC_TEMPERATURE_FAILURE); + if (!rtc::isAccurate()) bitSet(active, ALERT_RTC_CLOCK_FAILURE); - if (metadata.batteryLevel <= config->alertBatteryLevel2 && !bitRead(config->activeAlerts, ALERT_BATTERY_LEVEL_2)) { - bitSet(result, ALERT_BATTERY_LEVEL_2); - } - - if (metadata.temperature == ALERT_SUSPICIOUS_RTC_TEMPERATURE && !bitRead(config->activeAlerts, ALERT_RTC_TEMPERATURE_FAILURE)) { - bitSet(result, ALERT_RTC_TEMPERATURE_FAILURE); - } - - if (!rtc::isAccurate() && !bitRead(config->activeAlerts, ALERT_RTC_CLOCK_FAILURE)) { - bitSet(result, ALERT_RTC_CLOCK_FAILURE); - } - - return result; + return config->activeAlerts ^ active; } void add(uint8_t mask) { @@ -44,19 +33,11 @@ namespace alerts { config_t* config = &config::main::value; uint8_t clearMask = 0; - if ((config->activeAlerts & (_BV(ALERT_BATTERY_LEVEL_1) | _BV(ALERT_BATTERY_LEVEL_2))) && metadata.batteryLevel >= config->alertBatteryLevelClear) { - clearMask |= _BV(ALERT_BATTERY_LEVEL_1) | _BV(ALERT_BATTERY_LEVEL_2); - } - - if (bitRead(config->activeAlerts, ALERT_RTC_TEMPERATURE_FAILURE) && metadata.temperature != ALERT_SUSPICIOUS_RTC_TEMPERATURE) { - bitSet(clearMask, ALERT_RTC_TEMPERATURE_FAILURE); - } - - if (bitRead(config->activeAlerts, ALERT_RTC_CLOCK_FAILURE) && rtc::isAccurate()) { - bitSet(clearMask, ALERT_RTC_CLOCK_FAILURE); - } + if (metadata.batteryLevel >= config->alertBatteryLevelClear) clearMask |= _BV(ALERT_BATTERY_LEVEL_1) | _BV(ALERT_BATTERY_LEVEL_2); + if (metadata.temperature != ALERT_SUSPICIOUS_RTC_TEMPERATURE) bitSet(clearMask, ALERT_RTC_TEMPERATURE_FAILURE); + if (rtc::isAccurate()) bitSet(clearMask, ALERT_RTC_CLOCK_FAILURE); - if (!clearMask) return; //save a write to eeprom if there is no change + if (!config->activeAlerts || !clearMask) return; //save a write to eeprom if there is no change config->activeAlerts &= ~clearMask; config::main::save(); }