PageRenderTime 586ms CodeModel.GetById 252ms app.highlight 127ms RepoModel.GetById 205ms app.codeStats 0ms

/Controller/test_esplora/EEPROMHelper.ino

https://github.com/karlqui/QuadraCopter
Arduino Sketch | 109 lines | 99 code | 10 blank | 0 comment | 0 complexity | 90b229f1eb20ed66349831020e7655df MD5 | raw file
  1#include "EEPROM.h"
  2
  3void saveEEPROM(String paraphrase, int data){ //1024 bytes of EEPROM on the esplora -- Empty possitions have the value of 255
  4
  5  int i=0;
  6  String tempPara = "";
  7  bool paraExists = false; //if the paraphrase already exists
  8  byte b;
  9  b = EEPROM.read(i);
 10  while(b!=255)//Read until empty positions -- Loop through every paraphrase in the mem to check if it already exists
 11  {
 12
 13    while((char)b!=':'){//paraphrase
 14      tempPara += (char)b;
 15      i++;
 16      b = EEPROM.read(i);
 17    }
 18    if(tempPara==paraphrase){
 19      paraExists = true; //paraphrase found, stop looking
 20      break;
 21    }
 22    tempPara = "";
 23    while((char)b!='|'){//loop through data
 24      i++;
 25      b = EEPROM.read(i);
 26    }
 27    i++;
 28    b = EEPROM.read(i);
 29  }
 30  if(paraExists){//@TODO replace data on index i
 31    i++;
 32    byte high = highByte(data);
 33    EEPROM.write(i, high);
 34    byte low = lowByte(data);
 35    i++;
 36    EEPROM.write(i, low);
 37  }
 38  else{ //@TODO add new paraphrase and data on index i
 39  
 40    for(int j=0;j<paraphrase.length(); j++){
 41      EEPROM.write(i, paraphrase[j]);
 42      i++;
 43    }
 44    EEPROM.write(i, ':');
 45    i++;
 46    EEPROM.write(i, highByte(data));
 47    i++;
 48    EEPROM.write(i, lowByte(data));
 49    i++;
 50    EEPROM.write(i, '|');
 51  }
 52}
 53
 54int readEEPROM(String paraphrase){ //read int value from EEPROM
 55    int i=0;
 56  String tempPara = "";
 57  bool paraExists = false; //if the paraphrase already exists
 58  byte b;
 59  b = EEPROM.read(i);
 60  while(b!=255)//Read until empty positions -- Loop through every paraphrase in the mem to check if it already exists
 61  {
 62
 63    while((char)b!=':'){//paraphrase
 64      tempPara += (char)b;
 65      i++;
 66      b = EEPROM.read(i);
 67    }
 68    if(tempPara==paraphrase){
 69      paraExists = true; //paraphrase found, stop looking
 70      break;
 71    }
 72    tempPara = "";
 73    while((char)b!='|'){//loop through data
 74      i++;
 75      b = EEPROM.read(i);
 76    }
 77    i++;
 78    b = EEPROM.read(i);
 79  }
 80  if(paraExists){//@TODO replace data on index i
 81    i++;
 82    byte high = EEPROM.read(i);
 83    i++;
 84    byte low = EEPROM.read(i);
 85  
 86    return word(high, low);
 87  }
 88  else{ //@TODO add new paraphrase and data on index i
 89    return -1;
 90  }
 91}
 92
 93void cleanEEPROM(){
 94  for(int i=0;i<1024;i++){
 95    EEPROM.write(i, 255);
 96  }
 97}
 98
 99void printEEPROM(){
100  Serial.println("printing!");
101  int i = 0;
102  byte b = EEPROM.read(i);
103  while(b!=255){
104    Serial.println((char)b);
105    i++;
106    b = EEPROM.read(i);
107    
108  }
109}