Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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