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.

106 rindas
2.9 KiB

  1. #include "Positions.h"
  2. #include "Debug.h"
  3. #include "Config.h"
  4. #include "Gps.h"
  5. #include "Network.h"
  6. #define LOGGER_NAME "Positions"
  7. #define ENTRY_RESERVED_SIZE 128
  8. #define ENTRIES_ADDR ENTRY_RESERVED_SIZE
  9. namespace positions {
  10. uint16_t _maxEntryIndex = (E24_MAX_ADDRESS(hardware::i2c::eeprom.getSize()) - ENTRIES_ADDR) / ENTRY_RESERVED_SIZE;
  11. uint16_t getEntryAddress(uint16_t index) {
  12. if (index > _maxEntryIndex) return -1;
  13. return ENTRIES_ADDR + (ENTRY_RESERVED_SIZE * index);
  14. }
  15. bool acquire(PositionEntryMetadata &metadata) {
  16. VERBOSE("acquire");
  17. timestamp_t before;
  18. gps::powerOn();
  19. before = rtc::getTime();
  20. SIM808_GPS_STATUS gpsStatus = gps::acquireCurrentPosition(GPS_DEFAULT_TOTAL_TIMEOUT_MS);
  21. SIM808ChargingStatus battery = hardware::sim808::device.getChargingState();
  22. gps::powerOff();
  23. if (gpsStatus < SIM808_GPS_STATUS::FIX) return false;
  24. uint16_t timeToFix = rtc::getTime() - before;
  25. tmElements_t time;
  26. gps::getTime(time);
  27. rtc::setTime(time);
  28. metadata = {
  29. battery.level,
  30. battery.voltage,
  31. rtc::getTemperature(),
  32. timeToFix,
  33. gpsStatus
  34. };
  35. return true;
  36. }
  37. void appendLast(const PositionEntryMetadata &metadata) {
  38. VERBOSE("appendLast");
  39. uint16_t entryAddress;
  40. PositionEntry entry = { metadata };
  41. strlcpy(entry.position, gps::lastPosition, POSITION_SIZE);
  42. hardware::i2c::powerOn();
  43. Config config = config::get();
  44. config.lastEntry++;
  45. if (config.lastEntry > _maxEntryIndex) config.lastEntry = 0;
  46. if (config.lastEntry == config.firstEntry) config.firstEntry++;
  47. if (config.firstEntry > _maxEntryIndex) config.firstEntry = 0;
  48. entryAddress = getEntryAddress(config.lastEntry);
  49. hardware::i2c::eeprom.writeBlock(entryAddress, entry);
  50. 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);
  51. config::set(config);
  52. hardware::i2c::powerOff();
  53. }
  54. bool get(uint16_t index, PositionEntry &entry) {
  55. uint16_t entryAddress = getEntryAddress(index);
  56. if (entryAddress == -1) return false;
  57. VERBOSE_FORMAT("get", "Reading entry n°%d @ %X", index, entryAddress);
  58. hardware::i2c::powerOn();
  59. hardware::i2c::eeprom.readBlock(entryAddress, entry);
  60. hardware::i2c::powerOff();
  61. 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);
  62. return true;
  63. }
  64. bool moveNext(uint16_t &index) {
  65. if (index == config::get().lastEntry) return false;
  66. if (index == _maxEntryIndex) index = 0; //could use a modulo but easier to understand that way
  67. else index++;
  68. return true;
  69. }
  70. bool needsToSend() {
  71. return false;
  72. }
  73. void send() {
  74. }
  75. }