Bläddra i källkod

Added a fourth alert (RTC clock halt) and saving active alerts to eeprom to survive resets

tags/v1.2.0
Bertrand Lemasle 6 år sedan
förälder
incheckning
95b43e3f44
5 ändrade filer med 45 tillägg och 22 borttagningar
  1. +27
    -10
      GpsTracker/Alerts.cpp
  2. +5
    -6
      GpsTracker/Alerts.h
  3. +4
    -2
      GpsTracker/Config.cpp
  4. +2
    -1
      GpsTracker/Config.h
  5. +7
    -3
      GpsTracker/Core.cpp

+ 27
- 10
GpsTracker/Alerts.cpp Visa fil

@@ -2,42 +2,59 @@


#include "Alerts.h" #include "Alerts.h"
#include "Config.h" #include "Config.h"
#include "Rtc.h"


namespace alerts { namespace alerts {


uint8_t _alerts = 0;

uint8_t getTriggered(PositionEntryMetadata &metadata) { uint8_t getTriggered(PositionEntryMetadata &metadata) {
config_t* config = &config::main::value; config_t* config = &config::main::value;
uint8_t result = 0; uint8_t result = 0;


if (metadata.temperature == ALERT_SUSPICIOUS_RTC_TEMPERATURE && !bitRead(_alerts, ALERT_RTC_FAILURE)) {
bitSet(result, ALERT_RTC_FAILURE);
if (!rtc::isAccurate() && !bitRead(config->activeAlerts, ALERT_RTC_CLOCK_FAILURE)) {
bitSet(result, ALERT_RTC_CLOCK_FAILURE);
}

if (metadata.temperature == ALERT_SUSPICIOUS_RTC_TEMPERATURE && !bitRead(config->activeAlerts, ALERT_RTC_TEMPERATURE_FAILURE)) {
bitSet(result, ALERT_RTC_TEMPERATURE_FAILURE);
} }


if (metadata.batteryLevel <= config->alertBatteryLevel1 && !bitRead(_alerts, ALERT_BATTERY_LEVEL_1)) {
if (metadata.batteryLevel <= config->alertBatteryLevel1 && !bitRead(config->activeAlerts, ALERT_BATTERY_LEVEL_1)) {
bitSet(result, ALERT_BATTERY_LEVEL_1); bitSet(result, ALERT_BATTERY_LEVEL_1);
} }


if (metadata.batteryLevel <= config->alertBatteryLevel2 && !bitRead(_alerts, ALERT_BATTERY_LEVEL_2)) {
if (metadata.batteryLevel <= config->alertBatteryLevel2 && !bitRead(config->activeAlerts, ALERT_BATTERY_LEVEL_2)) {
bitSet(result, ALERT_BATTERY_LEVEL_2); bitSet(result, ALERT_BATTERY_LEVEL_2);
} }


return result; return result;
} }


void add(uint8_t mask) {
if (!mask) return; //save a write to eeprom if there is no change

config_t* config = &config::main::value;
config->activeAlerts |= mask;
config::main::save();
}

void clear(PositionEntryMetadata &metadata) { void clear(PositionEntryMetadata &metadata) {
config_t* config = &config::main::value; config_t* config = &config::main::value;
uint8_t clearMask = 0; uint8_t clearMask = 0;


if (bitRead(_alerts, ALERT_RTC_FAILURE) && metadata.temperature != ALERT_SUSPICIOUS_RTC_TEMPERATURE) {
bitSet(clearMask, ALERT_RTC_FAILURE);
if (bitRead(config->activeAlerts, ALERT_RTC_CLOCK_FAILURE) && rtc::isAccurate()) {
bitSet(clearMask, ALERT_RTC_CLOCK_FAILURE);
}

if (bitRead(config->activeAlerts, ALERT_RTC_TEMPERATURE_FAILURE) && metadata.temperature != ALERT_SUSPICIOUS_RTC_TEMPERATURE) {
bitSet(clearMask, ALERT_RTC_TEMPERATURE_FAILURE);
} }


if (_alerts & (_BV(ALERT_BATTERY_LEVEL_1) | _BV(ALERT_BATTERY_LEVEL_2) && metadata.temperature >= config->alertBatteryLevelClear)) {
if (config->activeAlerts & (_BV(ALERT_BATTERY_LEVEL_1) | _BV(ALERT_BATTERY_LEVEL_2) && metadata.temperature >= config->alertBatteryLevelClear)) {
clearMask |= _BV(ALERT_BATTERY_LEVEL_1) | _BV(ALERT_BATTERY_LEVEL_2); clearMask |= _BV(ALERT_BATTERY_LEVEL_1) | _BV(ALERT_BATTERY_LEVEL_2);
} }


_alerts &= ~clearMask;
if (!clearMask) return; //save a write to eeprom if there is no change
config->activeAlerts &= ~clearMask;
config::main::save();
} }
} }

+ 5
- 6
GpsTracker/Alerts.h Visa fil

@@ -2,15 +2,14 @@


#include "Positions.h" #include "Positions.h"


#define ALERT_BATTERY_LEVEL_1 1
#define ALERT_BATTERY_LEVEL_2 2
#define ALERT_RTC_FAILURE 3
#define ALERT_BATTERY_LEVEL_1 1
#define ALERT_BATTERY_LEVEL_2 2
#define ALERT_RTC_TEMPERATURE_FAILURE 3
#define ALERT_RTC_CLOCK_FAILURE 4


namespace alerts { namespace alerts {


extern uint8_t _alerts;

uint8_t getTriggered(PositionEntryMetadata &metadata); uint8_t getTriggered(PositionEntryMetadata &metadata);
inline void add(uint8_t mask) { _alerts |= mask; } //TODO : save to EEPROM to survive reset
void add(uint8_t mask);
void clear(PositionEntryMetadata &metadata); void clear(PositionEntryMetadata &metadata);
} }

+ 4
- 2
GpsTracker/Config.cpp Visa fil

@@ -19,7 +19,7 @@ namespace config {
if (CONFIG_SEED != value.seed) reset(); //todo : reset network if seed for network is not right if (CONFIG_SEED != value.seed) reset(); //todo : reset network if seed for network is not right
hardware::i2c::powerOff(); hardware::i2c::powerOff();


NOTICE_FORMAT("read", "%d, %s, %d, %d, %d, %d, %d, %s", value.seed, value.version, value.firstEntry, value.lastEntry, value.alertBatteryLevel1, value.alertBatteryLevel2, value.alertBatteryLevelClear, value.contactPhone);
NOTICE_FORMAT("read", "%d, %s, %d, %d, %d, %d, %d, %d, %s", value.seed, value.version, value.firstEntry, value.lastEntry, value.alertBatteryLevel1, value.alertBatteryLevel2, value.alertBatteryLevelClear, value.activeAlerts, value.contactPhone);
#if BACKUP_ENABLE_NETWORK #if BACKUP_ENABLE_NETWORK
NOTICE_FORMAT("read", "%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); NOTICE_FORMAT("read", "%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url);
//networkConfig_t c = { //networkConfig_t c = {
@@ -33,12 +33,13 @@ namespace config {
value.alertBatteryLevel1 = CONFIG_DEFAULT_BATTERY_ALERT_LEVEL1; value.alertBatteryLevel1 = CONFIG_DEFAULT_BATTERY_ALERT_LEVEL1;
value.alertBatteryLevel2 = CONFIG_DEFAULT_BATTERY_ALERT_LEVEL2; value.alertBatteryLevel2 = CONFIG_DEFAULT_BATTERY_ALERT_LEVEL2;
value.alertBatteryLevelClear = CONFIG_DEFAULT_BATTERY_ALERT_CLEAR; value.alertBatteryLevelClear = CONFIG_DEFAULT_BATTERY_ALERT_CLEAR;
value.activeAlerts = 0;
strcpy(value.contactPhone, CONFIG_DEFAULT_CONTACT_PHONE); strcpy(value.contactPhone, CONFIG_DEFAULT_CONTACT_PHONE);
#endif #endif
} }


void write() { void write() {
NOTICE_FORMAT("write", "%d, %s, %d, %d, %d, %d, %d, %s", value.seed, value.version, value.firstEntry, value.lastEntry, value.alertBatteryLevel1, value.alertBatteryLevel2, value.alertBatteryLevelClear, value.contactPhone);
NOTICE_FORMAT("write", "%d, %s, %d, %d, %d, %d, %d, %d, %s", value.seed, value.version, value.firstEntry, value.lastEntry, value.activeAlerts, value.alertBatteryLevel1, value.alertBatteryLevel2, value.alertBatteryLevelClear, value.activeAlerts, value.contactPhone);
#if BACKUP_ENABLE_NETWORK #if BACKUP_ENABLE_NETWORK
NOTICE_FORMAT("write", "%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url); NOTICE_FORMAT("write", "%d, %d, %s, %s", value.network.saveThreshold, value.network.lastSavedEntry, value.network.apn, value.network.url);
#endif #endif
@@ -75,6 +76,7 @@ namespace config {
CONFIG_DEFAULT_BATTERY_ALERT_LEVEL1, CONFIG_DEFAULT_BATTERY_ALERT_LEVEL1,
CONFIG_DEFAULT_BATTERY_ALERT_LEVEL2, CONFIG_DEFAULT_BATTERY_ALERT_LEVEL2,
CONFIG_DEFAULT_BATTERY_ALERT_CLEAR, CONFIG_DEFAULT_BATTERY_ALERT_CLEAR,
0,
CONFIG_DEFAULT_CONTACT_PHONE CONFIG_DEFAULT_CONTACT_PHONE
#endif #endif
}; };


+ 2
- 1
GpsTracker/Config.h Visa fil

@@ -69,8 +69,9 @@ struct config_t {
uint8_t alertBatteryLevel1; //sizeof = 1 uint8_t alertBatteryLevel1; //sizeof = 1
uint8_t alertBatteryLevel2; //sizeof = 1 uint8_t alertBatteryLevel2; //sizeof = 1
uint8_t alertBatteryLevelClear; //sizeof = 1 uint8_t alertBatteryLevelClear; //sizeof = 1
uint8_t activeAlerts; //sizeof = 1
char contactPhone[15]; //sizeof = 15 char contactPhone[15]; //sizeof = 15
}; //sizeof = 28 + 73 = 101
}; //sizeof = 29 + 73 = 102


namespace config { namespace config {




+ 7
- 3
GpsTracker/Core.cpp Visa fil

@@ -23,7 +23,7 @@ namespace core {


if (acquired) { if (acquired) {
positions::appendLast(metadata); positions::appendLast(metadata);
forceBackup = updateSleepTime(); forceBackup = updateSleepTime();
gps::preserveCurrentCoordinates(); gps::preserveCurrentCoordinates();
} }
@@ -51,8 +51,12 @@ namespace core {
if (bitRead(triggered, ALERT_BATTERY_LEVEL_1) || bitRead(triggered, ALERT_BATTERY_LEVEL_2)) { if (bitRead(triggered, ALERT_BATTERY_LEVEL_1) || bitRead(triggered, ALERT_BATTERY_LEVEL_2)) {
sprintf_P(buffer + strlen(buffer), PSTR(" - Battery at %d%%.\n"), metadata.batteryLevel); sprintf_P(buffer + strlen(buffer), PSTR(" - Battery at %d%%.\n"), metadata.batteryLevel);
} }
if (bitRead(triggered, ALERT_RTC_FAILURE)) {

if (bitRead(triggered, ALERT_RTC_CLOCK_FAILURE)) {
sprintf_P(buffer + strlen(buffer), PSTR(" - RTC was stopped. Bakup battery failure ?\n"));
}

if (bitRead(triggered, ALERT_RTC_TEMPERATURE_FAILURE)) {
sprintf_P(buffer + strlen(buffer), PSTR(" - Temperature is %dC. Backup battery failure ?\n"), static_cast<uint16_t>(metadata.temperature * 100)); sprintf_P(buffer + strlen(buffer), PSTR(" - Temperature is %dC. Backup battery failure ?\n"), static_cast<uint16_t>(metadata.temperature * 100));
} }




Laddar…
Avbryt
Spara