EEPROM fuckery #99

Closed
opened 2019-06-04 16:52:00 +01:00 by Squatnet · 0 comments
Squatnet commented 2019-06-04 16:52:00 +01:00 (Migrated from github.com)
#define DEBUG   //Comment this line to disable Debug output
#ifdef DEBUG    // Debug is on
#define DBEGIN(...)    Serial.begin(__VA_ARGS__)
#define DPRINT(...)    Serial.print(__VA_ARGS__)     //Sends our arguments to DPRINT()
#define DPRINTLN(...)  Serial.println(__VA_ARGS__)   //Sends our arguments to DPRINTLN()
#define DFLUSH(...)    Serial.flush()

#else // Debug is off
#define DBEGIN(...)
#define DPRINT(...)     //Nothing Happens
#define DPRINTLN(...)   //Nothing Happens
#define DFLUSH(...)

#endif // end macro
#include <EEPROM.h>
#define DEBUG_LED 13
bool debugLED = true;
struct devStruct{
  String dType;
  String dName;
  int dNumPanels;
};
devStruct devInfo;
String string;
void(* resetFunc) (void) = 0; // Software reset hack
void flashLed(int times){
  for(int i=0; i<times; i++){
    delay(500);
      if(debugLED){
        debugLED = false;
        digitalWrite(DEBUG_LED,HIGH);
      }
      else{
        debugLED = true;
        digitalWrite(DEBUG_LED,LOW);
    }
  }
}
void eepromPut(String dtype, String dname, int numPanels){
  devStruct thisDev = {
    dtype,
    dname,
    numPanels
  };
  EEPROM.put(0,thisDev);
}
void eepromGet(){
  int readByte = EEPROM.read(0);
  if(readByte == 255 or readByte == 0){
    DPRINTLN("EEPROM EMPTY");
  }
  else{
    EEPROM.get(0,devInfo);
    DPRINTLN("Got Data from EEPROM");
    DPRINTLN(devInfo.dType);
    DPRINTLN(devInfo.dName);
    DPRINTLN(devInfo.dNumPanels);
  }
}
void eepromClear(){
  for (int i = 0 ; i < EEPROM.length() ; i++) {
    EEPROM.write(i, 0);
  }
  
  DPRINTLN("EEPROM CLEARED!");
}
void setup() {
  DBEGIN(115200);
  DPRINTLN("DONE");
  eepromGet();
}
void parser() {
  DPRINTLN("PARSER");
  byte randTrigger=0;//initializes a randPattern trigger (didn't need to be global);
  while (string.length() >= 1) { // While there message left to read.
    DPRINT("Message is ");
    DPRINT(string);
    DPRINT(" With length ");
    DPRINTLN(string.length());
    if (!string.endsWith(","))string.concat(","); // STOP CRASHING;
    String subs = string.substring(0, string.indexOf(',')); // get everything until the first comma.
    DPRINT("SubStr - ");
    DPRINT(subs);
    string.remove(0, string.indexOf(',') + 1); // remove everything up to and including the first comma
    DPRINT(" String - ");
    DPRINTLN(string);
    if (subs.startsWith("Rst"))resetFunc();
    if (subs.startsWith("Clr")) {
      DPRINTLN("Clear EEPROM");
      eepromClear();
      string = "";
    }
    if (subs.startsWith("Set")){
      String dTyp = string.substring(0, string.indexOf(",")); // get until first comma
      string.remove(0, string.indexOf(",") + 1); // remove it
      String dNam = string.substring(0, string.indexOf(",")); // get until first comma
      string.remove(0, string.indexOf(",") + 1); // remove it
      int dNumPan = string.substring(0, string.indexOf(",")).toInt(); // get until first comma
      string.remove(0, string.indexOf(",") + 1); // remove it
      eepromPut(dTyp,dNam,dNumPan);
      DPRINTLN("Saved EEPROM");
      delay(200);
      resetFunc();
    }
  }
};
void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available()){
    string = "";
    while (Serial.available()) {
      char c = Serial.read();
      string.concat(c);
      
    }
    if (string != "") {
      parser();
      string = "";
    }
  }
}
``` #define DEBUG //Comment this line to disable Debug output #ifdef DEBUG // Debug is on #define DBEGIN(...) Serial.begin(__VA_ARGS__) #define DPRINT(...) Serial.print(__VA_ARGS__) //Sends our arguments to DPRINT() #define DPRINTLN(...) Serial.println(__VA_ARGS__) //Sends our arguments to DPRINTLN() #define DFLUSH(...) Serial.flush() #else // Debug is off #define DBEGIN(...) #define DPRINT(...) //Nothing Happens #define DPRINTLN(...) //Nothing Happens #define DFLUSH(...) #endif // end macro #include <EEPROM.h> #define DEBUG_LED 13 bool debugLED = true; struct devStruct{ String dType; String dName; int dNumPanels; }; devStruct devInfo; String string; void(* resetFunc) (void) = 0; // Software reset hack void flashLed(int times){ for(int i=0; i<times; i++){ delay(500); if(debugLED){ debugLED = false; digitalWrite(DEBUG_LED,HIGH); } else{ debugLED = true; digitalWrite(DEBUG_LED,LOW); } } } void eepromPut(String dtype, String dname, int numPanels){ devStruct thisDev = { dtype, dname, numPanels }; EEPROM.put(0,thisDev); } void eepromGet(){ int readByte = EEPROM.read(0); if(readByte == 255 or readByte == 0){ DPRINTLN("EEPROM EMPTY"); } else{ EEPROM.get(0,devInfo); DPRINTLN("Got Data from EEPROM"); DPRINTLN(devInfo.dType); DPRINTLN(devInfo.dName); DPRINTLN(devInfo.dNumPanels); } } void eepromClear(){ for (int i = 0 ; i < EEPROM.length() ; i++) { EEPROM.write(i, 0); } DPRINTLN("EEPROM CLEARED!"); } void setup() { DBEGIN(115200); DPRINTLN("DONE"); eepromGet(); } void parser() { DPRINTLN("PARSER"); byte randTrigger=0;//initializes a randPattern trigger (didn't need to be global); while (string.length() >= 1) { // While there message left to read. DPRINT("Message is "); DPRINT(string); DPRINT(" With length "); DPRINTLN(string.length()); if (!string.endsWith(","))string.concat(","); // STOP CRASHING; String subs = string.substring(0, string.indexOf(',')); // get everything until the first comma. DPRINT("SubStr - "); DPRINT(subs); string.remove(0, string.indexOf(',') + 1); // remove everything up to and including the first comma DPRINT(" String - "); DPRINTLN(string); if (subs.startsWith("Rst"))resetFunc(); if (subs.startsWith("Clr")) { DPRINTLN("Clear EEPROM"); eepromClear(); string = ""; } if (subs.startsWith("Set")){ String dTyp = string.substring(0, string.indexOf(",")); // get until first comma string.remove(0, string.indexOf(",") + 1); // remove it String dNam = string.substring(0, string.indexOf(",")); // get until first comma string.remove(0, string.indexOf(",") + 1); // remove it int dNumPan = string.substring(0, string.indexOf(",")).toInt(); // get until first comma string.remove(0, string.indexOf(",") + 1); // remove it eepromPut(dTyp,dNam,dNumPan); DPRINTLN("Saved EEPROM"); delay(200); resetFunc(); } } }; void loop() { // put your main code here, to run repeatedly: if (Serial.available()){ string = ""; while (Serial.available()) { char c = Serial.read(); string.concat(c); } if (string != "") { parser(); string = ""; } } } ```
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
squatnet/ArduinoStuff#99
No description provided.