You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 regels
1.4 KiB

  1. #pragma once
  2. #include "Debug.h"
  3. #include "Alerts.h"
  4. #include "Config.h"
  5. #include "Rtc.h"
  6. #define LOGGER_NAME "Alerts"
  7. namespace alerts {
  8. uint8_t getTriggered(PositionEntryMetadata &metadata) {
  9. config_t* config = &config::main::value;
  10. uint8_t active = 0;
  11. if (metadata.batteryLevel <= config->alertBatteryLevel1) bitSet(active, ALERT_BATTERY_LEVEL_1);
  12. if (metadata.batteryLevel <= config->alertBatteryLevel2) bitSet(active, ALERT_BATTERY_LEVEL_2);
  13. if (metadata.temperature == ALERT_SUSPICIOUS_RTC_TEMPERATURE) bitSet(active, ALERT_RTC_TEMPERATURE_FAILURE);
  14. if (!rtc::isAccurate()) bitSet(active, ALERT_RTC_CLOCK_FAILURE);
  15. return config->activeAlerts ^ active;
  16. }
  17. void add(uint8_t mask) {
  18. if (!mask) return; //save a write to eeprom if there is no change
  19. config_t* config = &config::main::value;
  20. config->activeAlerts |= mask;
  21. config::main::save();
  22. }
  23. void clear(PositionEntryMetadata &metadata) {
  24. config_t* config = &config::main::value;
  25. uint8_t clearMask = 0;
  26. if (metadata.batteryLevel >= config->alertBatteryLevelClear) clearMask |= _BV(ALERT_BATTERY_LEVEL_1) | _BV(ALERT_BATTERY_LEVEL_2);
  27. if (metadata.temperature != ALERT_SUSPICIOUS_RTC_TEMPERATURE) bitSet(clearMask, ALERT_RTC_TEMPERATURE_FAILURE);
  28. if (rtc::isAccurate()) bitSet(clearMask, ALERT_RTC_CLOCK_FAILURE);
  29. if (!config->activeAlerts || !clearMask) return; //save a write to eeprom if there is no change
  30. config->activeAlerts &= ~clearMask;
  31. config::main::save();
  32. }
  33. }