Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

126 rindas
3.5 KiB

  1. #include "Positions.h"
  2. #include "SdPositionsBackup.h"
  3. #include "Debug.h"
  4. #include "Config.h"
  5. #include "Gps.h"
  6. #include "Network.h"
  7. #define LOGGER_NAME "Positions"
  8. #define ENTRY_RESERVED_SIZE 128
  9. #define ENTRIES_ADDR ENTRY_RESERVED_SIZE
  10. namespace positions {
  11. backup::PositionsBackup **_backups;
  12. size_t _backupLength;
  13. namespace details {
  14. uint16_t maxEntryIndex = (E24_MAX_ADDRESS(hardware::i2c::eeprom.getSize()) - ENTRIES_ADDR) / ENTRY_RESERVED_SIZE;
  15. uint16_t getEntryAddress(uint16_t index) {
  16. if (index > maxEntryIndex) return -1;
  17. return ENTRIES_ADDR + (ENTRY_RESERVED_SIZE * index);
  18. }
  19. }
  20. void setup() {
  21. //TODO : enable/disable based on config
  22. _backupLength = 1;
  23. _backups = new backup::PositionsBackup*[_backupLength];
  24. _backups[0] = new backup::sd::SdPositionsBackup();
  25. _backups[0]->setup();
  26. }
  27. bool acquire(PositionEntryMetadata &metadata) {
  28. VERBOSE("acquire");
  29. timestamp_t before;
  30. gps::powerOn();
  31. before = rtc::getTime();
  32. SIM808_GPS_STATUS gpsStatus = gps::acquireCurrentPosition(GPS_DEFAULT_TOTAL_TIMEOUT_MS);
  33. SIM808ChargingStatus battery = hardware::sim808::device.getChargingState();
  34. gps::powerOff();
  35. if (gpsStatus < SIM808_GPS_STATUS::FIX) return false;
  36. uint16_t timeToFix = rtc::getTime() - before;
  37. tmElements_t time;
  38. gps::getTime(time);
  39. rtc::setTime(time);
  40. metadata = {
  41. battery.level,
  42. battery.voltage,
  43. rtc::getTemperature(),
  44. timeToFix,
  45. gpsStatus
  46. };
  47. return true;
  48. }
  49. void appendLast(const PositionEntryMetadata &metadata) {
  50. VERBOSE("appendLast");
  51. uint16_t entryAddress;
  52. PositionEntry entry = { metadata };
  53. strlcpy(entry.position, gps::lastPosition, POSITION_SIZE);
  54. hardware::i2c::powerOn();
  55. Config_t config = config::main::get();
  56. config.lastEntry++;
  57. if (config.lastEntry > details::maxEntryIndex) config.lastEntry = 0;
  58. if (config.lastEntry == config.firstEntry) config.firstEntry++;
  59. if (config.firstEntry > details::maxEntryIndex) config.firstEntry = 0;
  60. entryAddress = details::getEntryAddress(config.lastEntry);
  61. hardware::i2c::eeprom.writeBlock(entryAddress, entry);
  62. 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);
  63. config::main::set(config);
  64. hardware::i2c::powerOff();
  65. }
  66. bool get(uint16_t index, PositionEntry &entry) {
  67. uint16_t entryAddress = details::getEntryAddress(index);
  68. if (entryAddress == -1) return false;
  69. VERBOSE_FORMAT("get", "Reading entry n°%d @ %X", index, entryAddress);
  70. hardware::i2c::powerOn();
  71. hardware::i2c::eeprom.readBlock(entryAddress, entry);
  72. hardware::i2c::powerOff();
  73. VERBOSE_FORMAT("get", "Read from EEPROM @ %X : [%d%% @ %dmV] [%f°C] [%d, %s]", entryAddress, entry.metadata.batteryLevel, entry.metadata.batteryVoltage, entry.metadata.temperature, entry.metadata.timeToFix, entry.metadata.status, entry.position);
  74. return true;
  75. }
  76. bool moveNext(uint16_t &index) {
  77. if (index == config::main::get().lastEntry) return false;
  78. if (index == details::maxEntryIndex) index = 0; //could use a modulo but easier to understand that way
  79. else index++;
  80. return true;
  81. }
  82. uint16_t count(uint16_t fromIndex) {
  83. Config_t config = config::main::get();
  84. if (config.lastEntry < config.firstEntry) { config.lastEntry += details::maxEntryIndex; }
  85. return config.lastEntry - fromIndex;
  86. }
  87. void doBackup() {
  88. for (int i = 0; i < _backupLength; i++) {
  89. _backups[i]->backup();
  90. }
  91. }
  92. }