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

storage.cpp 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * storage.cpp
  3. *
  4. * EEPROM data history storage.
  5. *
  6. * ----------------------------------------------------------------------------
  7. * "THE BEER-WARE LICENSE" (Revision 42):
  8. * <xythobuz@xythobuz.de> & <ghost-ghost@web.de> wrote this file. As long as
  9. * you retain this notice you can do whatever you want with this stuff. If we
  10. * meet some day, and you think this stuff is worth it, you can buy us a beer
  11. * in return. Thomas Buck & Christian Högerle
  12. * ----------------------------------------------------------------------------
  13. */
  14. #include <Arduino.h>
  15. #include <EEPROM.h>
  16. #include "storage.h"
  17. //#define DEBUG
  18. void initMemory(void) {
  19. EEPROM.begin(EEPROM_SIZE);
  20. }
  21. void writeMemory(PersistentStorage &s) {
  22. unsigned char* r = (unsigned char*) &s;
  23. uint16_t a = 0, b = 0;
  24. for (int i = 0; i < sizeof(PersistentStorage) - 2; i++) {
  25. a = (a + r[i]) % 255;
  26. b = (b + a) % 255;
  27. }
  28. s.header.checksum = (b << 8) | a;
  29. #ifdef DEBUG
  30. Serial.print("write memory: ");
  31. Serial.println(s.header.checksum);
  32. #endif // DEBUG
  33. for (int i = 0; i < sizeof(PersistentStorage); i++) {
  34. EEPROM.write(i, r[i]);
  35. }
  36. EEPROM.commit();
  37. }
  38. PersistentStorage readMemory() {
  39. PersistentStorage s;
  40. unsigned char* r = (unsigned char*) &s;
  41. for (int i = 0; i < sizeof(PersistentStorage); i++) {
  42. r[i] = EEPROM.read(i);
  43. }
  44. uint16_t a = 0, b = 0;
  45. for (int i = 0; i < sizeof(PersistentStorage) - 2; i++) {
  46. a = (a + r[i]) % 255;
  47. b = (b + a) % 255;
  48. }
  49. if (s.header.checksum != ((b << 8) | a)) {
  50. #ifdef DEBUG
  51. Serial.print("read memory: checksum error: ");
  52. Serial.print(s.header.checksum);
  53. Serial.print(" != ");
  54. Serial.println((b << 8) | a);
  55. #endif // DEBUG
  56. s.header.count = 0;
  57. } else {
  58. #ifdef DEBUG
  59. Serial.println("read memory: checksum ok");
  60. #endif // DEBUG
  61. }
  62. return s;
  63. }