Skip to content

Instantly share code, notes, and snippets.

@combs
Created December 16, 2020 02:02
Show Gist options
  • Save combs/33902fbbbfe360b997c4fc759939d3fa to your computer and use it in GitHub Desktop.
Save combs/33902fbbbfe360b997c4fc759939d3fa to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stddef.h>
#include "Arduino.h"
#include "SparkFun_External_EEPROM.h"
#include <ArduinoJson.h>
struct EepromJsonReader {
uint16_t pos = 0;
ExternalEEPROM reader = ExternalEEPROM();
uint8_t address = 0x50;
EepromJsonReader(uint8_t addy=0x50) {
pos = 0;
address = addy;
Wire.begin();
reader.begin(addy);
Wire.setClock(1000000);
}
// Reads one byte, or returns -1
int read() {
Serial.println(pos);
char val = reader.read(pos);
pos++;
return val;
}
// Reads several bytes, returns the number of bytes read.
size_t readBytes(uint8_t* buffer, size_t length) {
reader.read(pos, buffer, length);
pos = pos + length;
return length;
}
};
class EepromJson {
private:
uint8_t address = 0x50;
public:
StaticJsonDocument<1000> json;
EepromJsonReader ejr;
EepromJson(uint8_t addy=0x50) {
address = addy;
}
void begin() {
ejr = EepromJsonReader(address);
DeserializationError error = deserializeJson(json, ejr);
}
};
EepromJson eeprom(0x50);
void setup() {
eeprom.begin();
}
void loop() {
Serial.println(eeprom.json["myKey"]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment