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.

61 line
1.9 KiB

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