ESP8266 SHT21 sensor
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

storage.cpp 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <Arduino.h>
  2. #include <EEPROM.h>
  3. #include "storage.h"
  4. //#define DEBUG
  5. void initMemory(void) {
  6. EEPROM.begin(EEPROM_SIZE);
  7. }
  8. void writeMemory(PersistentStorage &s) {
  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. #ifdef DEBUG
  17. Serial.print("write memory: ");
  18. Serial.println(s.header.checksum);
  19. #endif // DEBUG
  20. for (int i = 0; i < sizeof(PersistentStorage); i++) {
  21. EEPROM.write(i, r[i]);
  22. }
  23. EEPROM.commit();
  24. }
  25. PersistentStorage readMemory() {
  26. PersistentStorage s;
  27. unsigned char* r = (unsigned char*) &s;
  28. for (int i = 0; i < sizeof(PersistentStorage); i++) {
  29. r[i] = EEPROM.read(i);
  30. }
  31. uint16_t a = 0, b = 0;
  32. for (int i = 0; i < sizeof(PersistentStorage) - 2; i++) {
  33. a = (a + r[i]) % 255;
  34. b = (b + a) % 255;
  35. }
  36. if (s.header.checksum != ((b << 8) | a)) {
  37. #ifdef DEBUG
  38. Serial.print("read memory: checksum error: ");
  39. Serial.print(s.header.checksum);
  40. Serial.print(" != ");
  41. Serial.println((b << 8) | a);
  42. #endif // DEBUG
  43. s.header.count = 0;
  44. } else {
  45. #ifdef DEBUG
  46. Serial.println("read memory: checksum ok");
  47. #endif // DEBUG
  48. }
  49. return s;
  50. }