Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

86 rader
2.3 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 lastEntryIndex;
  19. uint16_t firstEntryIndex;
  20. uint16_t entryAddress;
  21. PositionEntry entry = { battery, gpsStatus };
  22. strlcpy(entry.position, gps::lastPosition, POSITION_SIZE);
  23. storage::powerOn();
  24. config::read();
  25. firstEntryIndex = config::value.firstEntry;
  26. lastEntryIndex = config::value.lastEntry;
  27. lastEntryIndex++;
  28. if (lastEntryIndex > _maxEntryIndex) lastEntryIndex = 0;
  29. if (lastEntryIndex == firstEntryIndex) firstEntryIndex++;
  30. if (firstEntryIndex > _maxEntryIndex) firstEntryIndex = 0;
  31. entryAddress = getEntryAddress(lastEntryIndex);
  32. hardware::i2c::eeprom.writeBlock(entryAddress, entry);
  33. VERBOSE_FORMAT("appendLast", "Written to EEPROM @ %X : [%d%% @ %dmV] [%d, %s]", entryAddress, battery.level, battery.voltage, gpsStatus, entry.position);
  34. config::value.firstEntry = firstEntryIndex;
  35. config::value.lastEntry = lastEntryIndex;
  36. config::write();
  37. storage::powerOff();
  38. }
  39. bool get(uint16_t index, PositionEntry &entry) {
  40. uint16_t entryAddress = getEntryAddress(index);
  41. if (entryAddress == -1) return false;
  42. VERBOSE_FORMAT("get", "Reading entry n°%d @ %X", index, entryAddress);
  43. storage::powerOn();
  44. hardware::i2c::eeprom.readBlock(entryAddress, entry);
  45. storage::powerOff();
  46. VERBOSE_FORMAT("get", "Read from EEPROM @ %X : [%d%% @ %dmV] [%d, %s]", entryAddress, entry.battery.level, entry.battery.voltage, entry.status, entry.position);
  47. return true;
  48. }
  49. bool moveNext(uint16_t &index) {
  50. config::read();
  51. if (index == config::value.lastEntry) return false;
  52. if (index == _maxEntryIndex) index = 0; //could use a modulo but easier to understand that way
  53. else index++;
  54. return true;
  55. }
  56. bool needsToSend() {
  57. return false;
  58. }
  59. void send() {
  60. }
  61. }