Skip to content

Instantly share code, notes, and snippets.

@wi1k1n
Last active October 28, 2020 02:32
Show Gist options
  • Save wi1k1n/45fe79e37d57123797c827f5eab9ba2e to your computer and use it in GitHub Desktop.
Save wi1k1n/45fe79e37d57123797c827f5eab9ba2e to your computer and use it in GitHub Desktop.
EEPROM Explorer sketch. Easily check and modify the content of the EEPROM in Arduino
#include <EEPROM.h>
#define ARGBUFFERLENGTH 2
char buf[8];
int args[ARGBUFFERLENGTH];
uint8_t mode = '0'; // '?' - show, '>' - edit
void setup() {
Serial.begin(9600);
Serial.println(F("Use command '?15;' to explore value of 15th byte"));
Serial.println(F("and '?0 10;' to explore values of bytes from 0 to 10."));
Serial.println();
Serial.println(F("Use command '>3 248;' to change value of 3rd byte to value 248"));
Serial.println();
Serial.println();
}
void loop() {
// Print the commands in the following format (without quotes): "<query_symbol><arg1>[ arg2];"
// query_symbols:
// ? - show byte or range of bytes
// arg1 - idx of byte to show
// arg2 - idx of last byte to show (if 2 args r present, the range of bytes will be shown)
// > - change byte value
// arg1 - idx of byte to change
// arg2 - new value of byte
uint8_t argn = parseInput();
if (argn) {
Serial.print(F(" "));
Serial.print((char)mode);
Serial.print((int)args[0]);
if (argn == 2) {
Serial.print(F(" "));
Serial.print((int)args[1]);
}
Serial.println(";");
if (mode == '?') {
if (args[0] >= 0 && args[0] < EEPROM.length()) {
uint16_t i = args[0];
uint16_t iEnd = i + 1;
if (argn == 2 && args[1] >= 0 && args[1] <= EEPROM.length())
iEnd = args[1];
for (; i < iEnd; i++) {
uint8_t v;
EEPROM.get(i, v);
sprintf(buf, "%4d ", i);
Serial.print("i:");
Serial.print(buf);
Serial.print("B ");
printByteBin(v);
Serial.print(" D ");
Serial.print((int)v);
Serial.print(" H ");
Serial.println(v, HEX);
}
} else Serial.println(F("Invalid args!"));
} else if (mode == '>') {
if (argn == 2 && args[0] >= 0 && args[0] < EEPROM.length()) {
EEPROM.put(args[0], (uint8_t)args[1]);
uint8_t v;
EEPROM.get(args[0], v);
Serial.print("[");
Serial.print(args[0]);
Serial.print("] -> ");
Serial.println(v, DEC);
} else Serial.println(F("Invalid args!"));
}
}
}
void printByteBin(const uint8_t &b) {
for (int8_t i = 7; i >= 0; i--)
Serial.print((char)(bitRead(b, i) + 48));
}
// This part is taken (with modifications) from https://github.com/AlexGyver/GyverLibs/tree/master/GyverUART
uint8_t parseInput() {
if (!Serial.available()) return 0;
uint32_t timeoutTime = millis();
int value = 0;
byte index = 0;
boolean parseStart = false;
while (millis() - timeoutTime < 100) {
if (!Serial.available()) continue;
if (index > ARGBUFFERLENGTH) return 0;
timeoutTime = millis();
if (Serial.peek() == '?' || Serial.peek() == '>') {
mode = Serial.read();
parseStart = true;
continue;
}
if (!parseStart) {
Serial.read();
continue;
}
if (Serial.peek() == ' ') {
args[index] = value;
value = 0;
index++;
Serial.read();
continue;
}
if (Serial.peek() == ';') {
args[index] = value;
parseStart = false;
Serial.read();
return ++index;
}
value = value * 10 + (Serial.read() - '0');
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment