ESP8266 SHT21 sensor
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.

storage.cpp 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include <Arduino.h>
  2. #include <EEPROM.h>
  3. #include "storage.h"
  4. void initMemory(void) {
  5. EEPROM.begin(EEPROM_SIZE);
  6. }
  7. void writeMemory(PersistentStorage &s) {
  8. Serial.println("write Memory");
  9. unsigned char* r = (unsigned char*) &s;
  10. uint16_t a = 0, b = 0;
  11. for (int i = 0; i < sizeof(PersistentStorage) - 2; i++) {
  12. a = (a + r[i]) % 255;
  13. b = (b + a) % 255;
  14. }
  15. s.header.checksum = (b << 8) | a;
  16. for (int i = 0; i < sizeof(PersistentStorage); i++) {
  17. EEPROM.write(i, r[i]);
  18. }
  19. EEPROM.commit();
  20. }
  21. PersistentStorage readMemory() {
  22. PersistentStorage s;
  23. unsigned char* r = (unsigned char*) &s;
  24. for (int i = 0; i < sizeof(PersistentStorage); i++) {
  25. r[i] = EEPROM.read(i);
  26. }
  27. uint16_t a = 0, b = 0;
  28. for (int i = 0; i < sizeof(PersistentStorage) - 2; i++) {
  29. a = (a + r[i]) % 255;
  30. b = (b + a) % 255;
  31. }
  32. if (s.header.checksum != ((b << 8) | a)) {
  33. Serial.print("Checksum error ");
  34. Serial.print(s.header.checksum);
  35. Serial.print(" ");
  36. Serial.println((b << 8) | a);
  37. s.header.count = 0;
  38. } else {
  39. Serial.println("Checksum ok");
  40. }
  41. return s;
  42. }