mirror of
https://github.com/daniviga/bite.git
synced 2024-11-22 21:16:12 +01:00
EEPROM experiments
This commit is contained in:
parent
9e39d4c7d2
commit
64def2a70e
64
arduino/eeprom_get/eeprom_get.ino
Normal file
64
arduino/eeprom_get/eeprom_get.ino
Normal file
|
@ -0,0 +1,64 @@
|
|||
/***
|
||||
eeprom_get example.
|
||||
|
||||
This shows how to use the EEPROM.get() method.
|
||||
|
||||
To pre-set the EEPROM data, run the example sketch eeprom_put.
|
||||
This sketch will run without it, however, the values shown
|
||||
will be shown from what ever is already on the EEPROM.
|
||||
|
||||
This may cause the serial object to print out a large string
|
||||
of garbage if there is no null character inside one of the strings
|
||||
loaded.
|
||||
|
||||
Written by Christopher Andrews 2015
|
||||
Released under MIT licence.
|
||||
***/
|
||||
|
||||
#include <EEPROM.h>
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
while (!Serial) {
|
||||
; // wait for serial port to connect. Needed for native USB port only
|
||||
}
|
||||
readConfig(); //Run the next test.
|
||||
}
|
||||
|
||||
struct config {
|
||||
byte mac[6];
|
||||
byte remoteAddr[4];
|
||||
char serial[4];
|
||||
int remotePort;
|
||||
char name[128];
|
||||
};
|
||||
|
||||
void printAddr(byte addr[], int size, Stream *stream) {
|
||||
for (int thisByte = 0; thisByte < size; thisByte++) {
|
||||
// print the value of each byte of the IP address:
|
||||
stream->print(addr[thisByte], HEX);
|
||||
if (thisByte < size - 1) {
|
||||
stream->print(".");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void readConfig() {
|
||||
int eeAddress = 0; //Move address to the next byte after float 'f'.
|
||||
|
||||
config customVar; //Variable to store custom object read from EEPROM.
|
||||
EEPROM.get(eeAddress, customVar);
|
||||
|
||||
Serial.println("Read custom object from EEPROM: ");
|
||||
printAddr(customVar.mac, sizeof(customVar.mac), &Serial);
|
||||
Serial.println();
|
||||
printAddr(customVar.remoteAddr, sizeof(customVar.remoteAddr), &Serial);
|
||||
Serial.println();
|
||||
Serial.println(customVar.remotePort);
|
||||
Serial.println(customVar.serial);
|
||||
Serial.println(customVar.name);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
/* Empty loop */
|
||||
}
|
53
arduino/eeprom_put/eeprom_put.ino
Normal file
53
arduino/eeprom_put/eeprom_put.ino
Normal file
|
@ -0,0 +1,53 @@
|
|||
/***
|
||||
eeprom_put example.
|
||||
|
||||
This shows how to use the EEPROM.put() method.
|
||||
Also, this sketch will pre-set the EEPROM data for the
|
||||
example sketch eeprom_get.
|
||||
|
||||
Note, unlike the single byte version EEPROM.write(),
|
||||
the put method will use update semantics. As in a byte
|
||||
will only be written to the EEPROM if the data is actually
|
||||
different.
|
||||
|
||||
Written by Christopher Andrews 2015
|
||||
Released under MIT licence.
|
||||
***/
|
||||
|
||||
#include <EEPROM.h>
|
||||
|
||||
struct config {
|
||||
byte mac[6];
|
||||
byte remoteAddr[4];
|
||||
char serial[4];
|
||||
int remotePort;
|
||||
char name[128];
|
||||
};
|
||||
|
||||
void setup() {
|
||||
|
||||
Serial.begin(9600);
|
||||
while (!Serial) {
|
||||
; // wait for serial port to connect. Needed for native USB port only
|
||||
}
|
||||
|
||||
int eeAddress = 0; //Location we want the data to be put.
|
||||
|
||||
/** Put is designed for use with custom structures also. **/
|
||||
|
||||
//Data to store.
|
||||
config customVar = {
|
||||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED },
|
||||
{ 192, 168, 10, 123 },
|
||||
"abcd",
|
||||
80,
|
||||
"sensor.server.domain"
|
||||
};
|
||||
|
||||
EEPROM.put(eeAddress, customVar);
|
||||
Serial.print("Written custom data type! \n\nView the example sketch eeprom_get to see how you can retrieve the values!");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
/* Empty loop */
|
||||
}
|
Loading…
Reference in New Issue
Block a user