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.

tactile_eeg_v1.1.ino 3.4KB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Arduino -> Adafruit ESP32 Feather auswählen
  2. #include <WiFi.h>
  3. #include <WiFiUDP.h>
  4. #include <stdio.h>
  5. #include <ctype.h>
  6. /* Firmware v1.1 04.04.2019
  7. * Christian Schuster
  8. * cchschuster@gmail.com
  9. */
  10. #define SSID "featherNetwork" // network SSID
  11. #define PASSWORD "featherNetwork" // network password
  12. #define PIN_LED 13
  13. #define PIN_Z 14
  14. #define PIN_V 32
  15. #define PIN_R 15
  16. #define PIN_L 33
  17. #define PIN_27 27
  18. #define PIN_12 12
  19. #define PIN_13 13
  20. #define PIN_A5 4
  21. #define PIN_A1 25
  22. #define PIN_A0 26
  23. #define SCRIPTMODE 1 // 0 = local filtering, 1 = laptop script filtering
  24. char packetBuffer[255]; // buffer to hold incoming packet
  25. unsigned int localPort = 8888;
  26. char sig_nr;
  27. int i_sig_nr;
  28. WiFiUDP udp;
  29. void setup()
  30. {
  31. setInitGPIO();
  32. // Initialize serial
  33. Serial.begin(115200);
  34. Serial.println();
  35. Serial.print("Setting up AP at SSID: ");
  36. Serial.print(SSID);
  37. bool result = WiFi.softAP(SSID, PASSWORD);
  38. //result = false; // for error testing
  39. if(result)
  40. {
  41. Serial.println();
  42. Serial.println("...AP successfully built.");
  43. Serial.print("...Feather IP: ");
  44. Serial.println(WiFi.softAPIP());
  45. Serial.print("...Feather MAC: ");
  46. Serial.println(WiFi.softAPmacAddress());
  47. Serial.print("...Local port: ");
  48. Serial.println(localPort);
  49. successWiFi();
  50. Serial.println("Listening to port...");
  51. } else {
  52. Serial.println("...Failed while setting up access point");
  53. errorWiFi();
  54. }
  55. udp.begin(localPort);
  56. }
  57. void loop()
  58. {
  59. int packetSize = udp.parsePacket();
  60. if(packetSize)
  61. {
  62. int len = udp.read(packetBuffer, 255);
  63. if(len)
  64. {
  65. packetBuffer[len] = 0;
  66. }
  67. if(SCRIPTMODE) // == 1
  68. {
  69. // laptop script filtering
  70. sig_nr = packetBuffer[0];
  71. Serial.println(sig_nr);
  72. vibrate(sig_nr);
  73. } else {
  74. // local filtering
  75. if(strstr(packetBuffer,"usCode "))
  76. {
  77. sig_nr = packetBuffer[13];
  78. Serial.println(sig_nr);
  79. vibrate(sig_nr);
  80. }
  81. }
  82. }
  83. }
  84. void setInitGPIO()
  85. {
  86. // Set all as OUTPUT
  87. pinMode(PIN_LED, OUTPUT);
  88. pinMode(PIN_Z, OUTPUT);
  89. pinMode(PIN_V, OUTPUT);
  90. pinMode(PIN_R, OUTPUT);
  91. pinMode(PIN_L, OUTPUT);
  92. // Bisher ungenutzte Pins
  93. pinMode(PIN_27, OUTPUT);
  94. pinMode(PIN_12, OUTPUT);
  95. pinMode(PIN_13, OUTPUT);
  96. pinMode(PIN_A5, OUTPUT);
  97. pinMode(PIN_A1, OUTPUT);
  98. pinMode(PIN_A0, OUTPUT);
  99. allLow();
  100. }
  101. void allLow()
  102. {
  103. // Set all LOW
  104. digitalWrite(PIN_LED, LOW);
  105. digitalWrite(PIN_Z, LOW);
  106. digitalWrite(PIN_V, LOW);
  107. digitalWrite(PIN_R, LOW);
  108. digitalWrite(PIN_L, LOW);
  109. // Bisher ungenutzte Pins
  110. digitalWrite(PIN_27, LOW);
  111. digitalWrite(PIN_12, LOW);
  112. digitalWrite(PIN_13, LOW);
  113. digitalWrite(PIN_A5, LOW);
  114. digitalWrite(PIN_A1, LOW);
  115. digitalWrite(PIN_A0, LOW);
  116. }
  117. void errorWiFi()
  118. {
  119. delay(2000);
  120. while(true)
  121. {
  122. digitalWrite(PIN_LED, HIGH);
  123. delay(100);
  124. digitalWrite(PIN_LED, LOW);
  125. delay(100);
  126. digitalWrite(PIN_LED, HIGH);
  127. delay(100);
  128. digitalWrite(PIN_LED, LOW);
  129. delay(1000);
  130. }
  131. }
  132. void successWiFi()
  133. {
  134. delay(2000);
  135. for(int i = 0; i < 5; i++)
  136. {
  137. digitalWrite(PIN_LED, HIGH);
  138. delay(1000);
  139. digitalWrite(PIN_LED, LOW);
  140. delay(200);
  141. }
  142. }
  143. void vibrate(const char signr)
  144. {
  145. switch(signr)
  146. {
  147. case '4': digitalWrite(PIN_Z, HIGH); break;
  148. case '3': digitalWrite(PIN_V, HIGH); break;
  149. case '2': digitalWrite(PIN_R, HIGH); break;
  150. case '1': digitalWrite(PIN_L, HIGH); break;
  151. case '0': allLow(); break;
  152. }
  153. }