Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

eepromReadWrite.ino 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. Copyright (c) 2014 Arduino. All right reserved.
  3. This library is free software; you can redistribute it and / or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110 - 1301 USA
  14. */
  15. #include "extEEPROM.h"
  16. extEEPROM myEEPROM(kbits_256, 1, 64, 0x57);
  17. void setup(void)
  18. {
  19. SerialUSB.begin(115200);
  20. while (!SerialUSB) {
  21. ;
  22. }
  23. byte i2cStat = myEEPROM.begin(myEEPROM.twiClock100kHz);
  24. if ( i2cStat != 0 ) {
  25. SerialUSB.println(F("I2C Problem"));
  26. }
  27. SerialUSB.println(
  28. F("EEPROM Memory commands: read:(a)(l)(r) , write:(a)(d)(w), next read data (n)"));
  29. SerialUSB.println(F("- Commands TO PRESS:"));
  30. SerialUSB.println(F("\t a : memory address to read / write"));
  31. SerialUSB.println(F("\t d : data to write"));
  32. SerialUSB.println(F("\t l : data to write"));
  33. SerialUSB.println(F("\t r : read command"));
  34. SerialUSB.println(F("\t w : write command"));
  35. }
  36. unsigned long address = 0;
  37. const unsigned int maxDataSize = 1024; //0x8000; // 32 k bytes (32768 = 0x8000) = 256 kbits
  38. byte data[maxDataSize] = {'p', 'i', 'p', 'p', 'o'};
  39. unsigned int dataSize = 5;
  40. void eprom_read_write(bool write)
  41. {
  42. byte i2cStat = 0;
  43. if (write) {
  44. i2cStat = myEEPROM.write(address, data, dataSize);
  45. } else {
  46. memset(data, 0, maxDataSize);
  47. i2cStat = myEEPROM.read(address, data, dataSize);
  48. }
  49. if ( i2cStat != 0 ) {
  50. //there was a problem
  51. SerialUSB.print(F("I2C Problem: "));
  52. if ( i2cStat == EEPROM_ADDR_ERR) {
  53. SerialUSB.println(F("Wrong address"));
  54. } else {
  55. SerialUSB.print(F("I2C error: "));
  56. SerialUSB.print(i2cStat);
  57. SerialUSB.println(F(""));
  58. }
  59. }
  60. }
  61. void parse(char inChar)
  62. {
  63. const char addr_len = 5;
  64. char addr_char[addr_len] = "";
  65. const char data_len = 3;
  66. char data_char[data_len] = "";
  67. char size_char[data_len] = "";
  68. char inc = 0, i = 0, j = 0;
  69. switch (inChar) {
  70. case 'a':
  71. SerialUSB.print(F("Insert Address as 4 Hex chars (without '0x'): "));
  72. while (i < 4) {
  73. while (SerialUSB.available() <= 0)
  74. ;
  75. inc = SerialUSB.read();
  76. if (inc == 'q') {
  77. return;
  78. }
  79. addr_char[i] = inc;
  80. ++i;
  81. }
  82. address = (unsigned long)strtol(addr_char, NULL, 16);
  83. SerialUSB.println(address);
  84. break;
  85. case 'd':
  86. SerialUSB.print(F("Insert Hex data sequence (without '0x'), return to enter: "));
  87. memset(data, 0, maxDataSize);
  88. while (true) {
  89. while (SerialUSB.available() <= 0)
  90. ;
  91. inc = SerialUSB.read();
  92. if (inc == 'q') {
  93. return;
  94. }
  95. if (inc == '\r' || inc == '\n') {
  96. break;
  97. }
  98. if (inc >= 'a' && inc <= 'f') {
  99. data[j] += inc - 'a' + 10;
  100. } else if (inc >= 'A' && inc <= 'F') {
  101. data[j] += inc - 'A' + 10;
  102. } else if (inc >= '0' && inc <= '9') {
  103. data[j] += inc - '0';
  104. } else {
  105. return;
  106. }
  107. if (i % 2) {
  108. j++;
  109. } else {
  110. data[j] = data[j] << 4;
  111. }
  112. i++;
  113. }
  114. dataSize = j;
  115. SerialUSB.println(dataSize);
  116. SerialUSB.println(F(""));
  117. break;
  118. case 'l':
  119. SerialUSB.print(F("Insert data len as 2 Hex chars (without '0x'): "));
  120. while (i < 2) {
  121. while (SerialUSB.available() <= 0)
  122. ;
  123. inc = SerialUSB.read();
  124. if (inc == 'q') {
  125. return;
  126. }
  127. size_char[i] = inc;
  128. ++i;
  129. if (inc == '\n') {
  130. return;
  131. }
  132. }
  133. dataSize = (unsigned int)strtol(size_char, NULL, 16);
  134. SerialUSB.println(dataSize);
  135. break;
  136. case 'n':
  137. address += dataSize;
  138. /* FALLTHROUGH */
  139. case 'r':
  140. SerialUSB.print(F("reading address: "));
  141. SerialUSB.println(address, HEX);
  142. eprom_read_write(false);
  143. for (i = 0; i < dataSize ; ++i) {
  144. SerialUSB.print(data[i], HEX);
  145. SerialUSB.print(F(" "));
  146. }
  147. SerialUSB.println();
  148. break;
  149. case 'w':
  150. SerialUSB.print(F("writing at address: "));
  151. SerialUSB.print(address, HEX);
  152. SerialUSB.print(F(", len: "));
  153. SerialUSB.println(address, dataSize);
  154. for (i = 0; i < dataSize ; ++i) {
  155. SerialUSB.print(data[i], HEX);
  156. SerialUSB.print(F(" "));
  157. }
  158. eprom_read_write(true);
  159. SerialUSB.println();
  160. break;
  161. case 'T':
  162. SerialUSB.println(F("Memory test: writing and verifying the whole memory"));
  163. break;
  164. default:
  165. break;
  166. }
  167. }
  168. void loop(void)
  169. {
  170. if (SerialUSB.available() > 0) {
  171. char inChar = SerialUSB.read();
  172. SerialUSB.print(inChar);
  173. parse(inChar);
  174. }
  175. delay(10);
  176. }