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.

52 lines
1.6 KiB

  1. /*
  2. MicroMDAEPiano
  3. MicroMDAEPiano is a port of the MDA-EPiano sound engine
  4. (https://sourceforge.net/projects/mda-vst/) for the Teensy-3.5/3.6 with audio shield.
  5. (c)2019 H. Wirtz <wirtz@parasitstudio.de>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software Foundation,
  16. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. // Idea from: https://playground.arduino.cc/Code/EEPROMWriteAnything/
  19. #include <EEPROM.h>
  20. #include <Arduino.h> // for type definitions
  21. uint32_t crc32(uint8_t* calc_start, uint16_t calc_bytes);
  22. template <class T> int EEPROM_writeAnything(int ee, const T& value)
  23. {
  24. uint8_t* p = (uint8_t*)(const void*)&value;
  25. uint16_t i;
  26. uint32_t checksum = crc32(p + 4, sizeof(value) - 4);
  27. *p = checksum;
  28. for (i = 0; i < sizeof(value); i++)
  29. EEPROM.update(ee++, *p++);
  30. return i;
  31. }
  32. template <class T> int EEPROM_readAnything(int ee, T& value)
  33. {
  34. uint8_t* p = (uint8_t*)(void*)&value;
  35. unsigned int i;
  36. for (i = 0; i < sizeof(value); i++)
  37. *p++ = EEPROM.read(ee++);
  38. return i;
  39. }