25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

155 satır
4.3 KiB

  1. #include "Config.h"
  2. #include "Debug.h"
  3. #include "Positions.h"
  4. #include "Gps.h"
  5. #if BACKUP_ENABLE_SDCARD || BACKUP_ENABLE_NETWORK
  6. #define BACKUPS_ENABLED BACKUP_ENABLE_SDCARD + BACKUP_ENABLE_NETWORK
  7. #endif
  8. #if BACKUP_ENABLE_SDCARD
  9. #include "SdPositionsBackup.h"
  10. #endif
  11. #if BACKUP_ENABLE_NETWORK
  12. #include "NetworkPositionsBackup.h"
  13. #endif
  14. #define LOGGER_NAME "Positions"
  15. #define ENTRY_RESERVED_SIZE 128
  16. #define ENTRIES_ADDR CONFIG_RESERVED_SIZE
  17. namespace positions {
  18. #ifdef BACKUPS_ENABLED
  19. backup::PositionsBackup **_backups;
  20. #endif
  21. namespace details {
  22. uint16_t maxEntryIndex = (E24_MAX_ADDRESS(hardware::i2c::eeprom.getSize()) - ENTRIES_ADDR) / ENTRY_RESERVED_SIZE;
  23. uint16_t getEntryAddress(uint16_t index) {
  24. if (index > maxEntryIndex) return -1;
  25. return ENTRIES_ADDR + (ENTRY_RESERVED_SIZE * index);
  26. }
  27. }
  28. void setup() {
  29. #ifdef BACKUPS_ENABLED
  30. uint8_t backupIdx = 0;
  31. _backups = new backup::PositionsBackup*[BACKUPS_ENABLED];
  32. #if BACKUP_ENABLE_SDCARD
  33. _backups[backupIdx] = new backup::sd::SdPositionsBackup();
  34. _backups[backupIdx]->setup();
  35. backupIdx++;
  36. #endif
  37. #if BACKUP_ENABLE_NETWORK
  38. _backups[backupIdx] = new backup::net::NetworkPositionsBackup();
  39. _backups[backupIdx]->setup();
  40. backupIdx++;
  41. #endif
  42. #endif
  43. }
  44. bool acquire(PositionEntryMetadata &metadata) {
  45. VERBOSE("acquire");
  46. timestamp_t before;
  47. gps::powerOn();
  48. before = rtc::getTime();
  49. SIM808_GPS_STATUS gpsStatus = gps::acquireCurrentPosition(GPS_DEFAULT_TOTAL_TIMEOUT_MS);
  50. SIM808ChargingStatus battery = hardware::sim808::device.getChargingState();
  51. gps::powerOff();
  52. if (gpsStatus < SIM808_GPS_STATUS::FIX) return false;
  53. uint16_t timeToFix = rtc::getTime() - before;
  54. tmElements_t time;
  55. gps::getTime(time);
  56. rtc::setTime(time);
  57. metadata = {
  58. battery.level,
  59. battery.voltage,
  60. rtc::getTemperature(),
  61. timeToFix,
  62. gpsStatus
  63. };
  64. return true;
  65. }
  66. void appendLast(const PositionEntryMetadata &metadata) {
  67. VERBOSE("appendLast");
  68. uint16_t entryIndex;
  69. uint16_t entryAddress;
  70. PositionEntry entry = { metadata };
  71. strlcpy(entry.position, gps::lastPosition, POSITION_SIZE);
  72. config_t* config = &config::main::value;
  73. entryIndex = config->lastEntry + 1;
  74. entryAddress = details::getEntryAddress(entryIndex);
  75. hardware::i2c::powerOn();
  76. hardware::i2c::eeprom.writeBlock(entryAddress, entry);
  77. VERBOSE_FORMAT("appendLast", "Written to EEPROM @ %X : [%d%% @ %dmV] [%f°C] [TTF : %d, Status : %d, Position : %s]", entryAddress, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, entry.metadata.timeToFix, entry.metadata.status, entry.position);
  78. config->lastEntry++;
  79. if (config->lastEntry > details::maxEntryIndex) config->lastEntry = 0;
  80. if (config->lastEntry == config->firstEntry) config->firstEntry++;
  81. if (config->firstEntry > details::maxEntryIndex) config->firstEntry = 0;
  82. config::main::save();
  83. hardware::i2c::powerOff();
  84. }
  85. bool get(uint16_t index, PositionEntry &entry) {
  86. uint16_t entryAddress = details::getEntryAddress(index);
  87. if (entryAddress == -1) return false;
  88. VERBOSE_FORMAT("get", "Reading entry n°%d @ %X", index, entryAddress);
  89. hardware::i2c::powerOn();
  90. hardware::i2c::eeprom.readBlock(entryAddress, entry);
  91. hardware::i2c::powerOff();
  92. VERBOSE_FORMAT("get", "Read from EEPROM @ %X : [%d%% @ %dmV] [%f°C] [TTF : %d, Status : %d, Position : %s]", entryAddress, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, entry.metadata.timeToFix, entry.metadata.status, entry.position);
  93. return true;
  94. }
  95. bool moveNext(uint16_t &index) {
  96. if (index == config::main::value.lastEntry) return false;
  97. if (index == details::maxEntryIndex) index = 0; //could use a modulo but easier to understand that way
  98. else index++;
  99. return true;
  100. }
  101. uint16_t count(uint16_t fromIndex) {
  102. config_t *config = &config::main::value;
  103. if (config->lastEntry < config->firstEntry) { config->lastEntry += details::maxEntryIndex; }
  104. return config->lastEntry - fromIndex;
  105. }
  106. void doBackup() {
  107. #ifdef BACKUPS_ENABLED
  108. debug::displayFreeRam();
  109. VERBOSE_FORMAT("doBackup", "%d backups enabled", BACKUPS_ENABLED);
  110. //_backups[0]->backup(); //disabled for first real running test
  111. debug::displayFreeRam();
  112. /*for (int i = 0; i < BACKUPS_ENABLED; i++) {
  113. VERBOSE_FORMAT("doBackup", "calling backup %d", i);
  114. delay(1000);
  115. _backups[i]->backup();
  116. }*/
  117. #endif
  118. }
  119. }