//*****************************************************
bool newCommand = false;
String command;
const int maxEntries = 4;
bool memFull = 0;
uint8_t numberOfEntries;
char record[maxEntries] = {'a','b','c','d'};
uint8_t recordIndex;
uint8_t recordOrder[2 * maxEntries];

void setup() {
  Serial.begin(115200);
  Serial.println("Starting");
  numberOfEntries = 0;
  recordIndex = 1;
  initOrder();
  printOrder();
  printRecords();
  while(1);
}

//********************************************************
void loop() {
  



  serialEvent();  // must be called to handle serial1 commands
  if (newCommand) {
    Serial.print("New Command: ");Serial.println(command);
    newCommand = 0;
    executeCommand();
    command = "";
  }
  delay(50);
}


//**************************************************
void executeCommand(){
  char c = command.charAt(0);
  saveRecord(recordIndex);
}

void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // if the incoming character is a newline, set a flag so the main loop can
    // do something about it:
    if (inChar == '\n') {
      newCommand = true;
      break;
    }
    command += inChar;
  }
}

void initOrder(){
  int order = 0;
  for(int i = recordIndex; i < maxEntries ; i++){
    recordOrder[i] = order;
    order++;
  }
  for(int i = 0; i < recordIndex; i++){
    recordOrder[i] = order;
    order++;
  }

  for(int i = 0; i < maxEntries; i++){
    recordOrder[i + maxEntries] = recordOrder[i];
  }
}

void printOrder(){
  Serial.println("Record Order");
  for(int i = 0; i< 2* maxEntries; i++){
    Serial.print(recordOrder[i]);
    if(i < ( 2 * maxEntries - 1)) Serial.print("-");
  }
  Serial.println();
}

void printRecords(){
  Serial.println("Record Data");
  for(int i = 0; i < maxEntries; i++){

    Serial.print(record[recordOrder[i]]);
    if(i < (maxEntries - 1)) Serial.print("-");
  }
  Serial.println();
}
void saveRecord(int index){


}