You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 rivejä
1.1 KiB

  1. #include "Config.h"
  2. #include "Debug.h"
  3. #include "Hardware.h"
  4. #define LOGGER_NAME "Config"
  5. namespace config {
  6. namespace main {
  7. Config_t value;
  8. namespace details {
  9. void read() {
  10. VERBOSE("read");
  11. hardware::i2c::powerOn();
  12. hardware::i2c::eeprom.readBlock(CONFIG_ADDR, value);
  13. if (CONFIG_SEED != value.seed) reset();
  14. hardware::i2c::powerOff();
  15. }
  16. void write() {
  17. VERBOSE_FORMAT("write", "%d, %s, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry);
  18. hardware::i2c::powerOn();
  19. int written = hardware::i2c::eeprom.writeBlock(CONFIG_ADDR, value);
  20. hardware::i2c::powerOff();
  21. }
  22. }
  23. Config_t get() {
  24. if (value.seed == 0) details::read();
  25. VERBOSE_FORMAT("get", "%d, %s, %s, %d, %d", value.seed, value.version, value.firstEntry, value.lastEntry);
  26. return value;
  27. }
  28. void set(const Config_t config) {
  29. value = config;
  30. details::write();
  31. }
  32. void reset() {
  33. VERBOSE("reset");
  34. Config_t config = {
  35. CONFIG_SEED,
  36. VERSION,
  37. 0xFFFF,
  38. 0xFFFF,
  39. };
  40. value = config;
  41. details::write();
  42. }
  43. }
  44. }