PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Controller/test_esplora/EEPROMHelper.ino

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