PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

67 lines
1.8KB

  1. /***
  2. eeprom_get example.
  3. This shows how to use the EEPROM.get() method.
  4. To pre-set the EEPROM data, run the example sketch eeprom_put.
  5. This sketch will run without it, however, the values shown
  6. will be shown from what ever is already on the EEPROM.
  7. This may cause the serial object to print out a large string
  8. of garbage if there is no null character inside one of the strings
  9. loaded.
  10. Written by Christopher Andrews 2015
  11. Released under MIT licence.
  12. ***/
  13. #include <EEPROM.h>
  14. void setup(){
  15. float f = 0.00f; //Variable to store data read from EEPROM.
  16. unsigned int eeAddress = 0; //EEPROM address to start reading from
  17. Serial.begin( 9600 );
  18. while (!Serial) {
  19. ; // wait for serial port to connect. Needed for Leonardo only
  20. }
  21. Serial.print( "Read float from EEPROM: " );
  22. //Get the float data from the EEPROM at position 'eeAddress'
  23. EEPROM.get( eeAddress, f );
  24. Serial.println( f, 3 ); //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float.
  25. /***
  26. As get also returns a reference to 'f', you can use it inline.
  27. E.g: Serial.print( EEPROM.get( eeAddress, f ) );
  28. ***/
  29. /***
  30. Get can be used with custom structures too.
  31. I have separated this into an extra function.
  32. ***/
  33. secondTest(); //Run the next test.
  34. }
  35. struct MyObject{
  36. float field1;
  37. byte field2;
  38. char name[10];
  39. };
  40. void secondTest(){
  41. int eeAddress = sizeof(float); //Move address to the next byte after float 'f'.
  42. MyObject customVar; //Variable to store custom object read from EEPROM.
  43. EEPROM.get( eeAddress, customVar );
  44. Serial.println( "Read custom object from EEPROM: " );
  45. Serial.println( customVar.field1 );
  46. Serial.println( customVar.field2 );
  47. Serial.println( customVar.name );
  48. }
  49. void loop(){ /* Empty loop */ }