Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

77 řádky
2.1 KiB

  1. #include "Positions.h"
  2. #include "Debug.h"
  3. #include "Config.h"
  4. #include "Gps.h"
  5. #include "Network.h"
  6. #include "Storage.h"
  7. #define LOGGER_NAME "Positions"
  8. #define ENTRY_RESERVED_SIZE 128
  9. #define ENTRIES_ADDR ENTRY_RESERVED_SIZE
  10. namespace positions {
  11. uint16_t _maxEntryIndex = (E24_MAX_ADDRESS(hardware::i2c::eeprom.getSize()) - ENTRIES_ADDR) / ENTRY_RESERVED_SIZE;
  12. uint16_t getEntryAddress(uint16_t index) {
  13. if (index > _maxEntryIndex) return -1;
  14. return ENTRIES_ADDR + (ENTRY_RESERVED_SIZE * index);
  15. }
  16. void appendLast(const SIM808ChargingStatus battery, const SIM808_GPS_STATUS gpsStatus) {
  17. VERBOSE("appendLast");
  18. uint16_t entryAddress;
  19. PositionEntry entry = { battery, gpsStatus };
  20. strlcpy(entry.position, gps::lastPosition, POSITION_SIZE);
  21. storage::powerOn();
  22. Config config = config::get();
  23. config.lastEntry++;
  24. if (config.lastEntry > _maxEntryIndex) config.lastEntry = 0;
  25. if (config.lastEntry == config.firstEntry) config.firstEntry++;
  26. if (config.firstEntry > _maxEntryIndex) config.firstEntry = 0;
  27. entryAddress = getEntryAddress(config.lastEntry);
  28. hardware::i2c::eeprom.writeBlock(entryAddress, entry);
  29. VERBOSE_FORMAT("appendLast", "Written to EEPROM @ %X : [%d%% @ %dmV] [%d, %s]", entryAddress, battery.level, battery.voltage, gpsStatus, entry.position);
  30. config::set(config);
  31. storage::powerOff();
  32. }
  33. bool get(uint16_t index, PositionEntry &entry) {
  34. uint16_t entryAddress = getEntryAddress(index);
  35. if (entryAddress == -1) return false;
  36. VERBOSE_FORMAT("get", "Reading entry n°%d @ %X", index, entryAddress);
  37. storage::powerOn();
  38. hardware::i2c::eeprom.readBlock(entryAddress, entry);
  39. storage::powerOff();
  40. VERBOSE_FORMAT("get", "Read from EEPROM @ %X : [%d%% @ %dmV] [%d, %s]", entryAddress, entry.battery.level, entry.battery.voltage, entry.status, entry.position);
  41. return true;
  42. }
  43. bool moveNext(uint16_t &index) {
  44. if (index == config::get().lastEntry) return false;
  45. if (index == _maxEntryIndex) index = 0; //could use a modulo but easier to understand that way
  46. else index++;
  47. return true;
  48. }
  49. bool needsToSend() {
  50. return false;
  51. }
  52. void send() {
  53. }
  54. }