您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

77 行
1.7 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 getEntryAddress(uint16_t &index) {
  12. uint16_t address = ENTRIES_ADDR + (ENTRY_RESERVED_SIZE * index);
  13. if (address + ENTRY_RESERVED_SIZE > E24_MAX_ADDRESS(hardware::i2c::eeprom.getSize())) {
  14. address = ENTRIES_ADDR;
  15. index = 0;
  16. }
  17. return address;
  18. }
  19. void setEntriesIndexes(uint16_t lastEntry) {
  20. uint16_t firstEntry = config::value.firstEntry;
  21. while (config::value.lastEntry < config::value.firstEntry) {
  22. config::value.firstEntry++;
  23. getEntryAddress(firstEntry);
  24. }
  25. config::value.lastEntry = lastEntry;
  26. config::value.firstEntry = firstEntry;
  27. }
  28. void appendLast(const SIM808ChargingStatus battery, const SIM808_GPS_STATUS gpsStatus) {
  29. VERBOSE("appendLast");
  30. uint16_t entryIndex;
  31. uint16_t entryAddress;
  32. PositionEntry entry = { battery, gpsStatus };
  33. strncpy(entry.position, gps::lastPosition, POSITION_SIZE);
  34. storage::powerOn();
  35. config::read();
  36. entryIndex = config::value.lastEntry + 1;
  37. entryAddress = getEntryAddress(entryIndex);
  38. bool success = hardware::i2c::eeprom.writeBlock(entryAddress, entry);
  39. if (success) {
  40. VERBOSE_MSG("appendLast", "written to EEPROM");
  41. setEntriesIndexes(entryIndex);
  42. config::write();
  43. }
  44. storage::powerOff();
  45. }
  46. void get(uint16_t &index, PositionEntry &entry) {
  47. uint16_t entryAddress = getEntryAddress(index);
  48. storage::powerOn();
  49. hardware::i2c::eeprom.readBlock(entryAddress, entry);
  50. storage::powerOff();
  51. }
  52. bool needsToSend() {
  53. return false;
  54. }
  55. void send() {
  56. }
  57. }