main.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include <Arduino.h>
  2. #include "LittleFS.h"
  3. #include <ESP8266WiFi.h>
  4. #include <ESP8266WebServer.h>
  5. #include <WebSocketsServer.h>
  6. const char *SSID = "S88Sniffer";
  7. const char *PASSWORD = "12345678";
  8. const int MAXMODULECOUNT = 16; // 16 * 8 inputs
  9. const bool DEBUGOUTPUT = false;
  10. /*
  11. PINOUT PCB
  12. D7 = 7 - Reset
  13. D6 = 6 - Load
  14. D2 = 2 - Data
  15. D4 = 4 - Clock
  16. Switched D4 to D1
  17. create wire link on PCB
  18. */
  19. ESP8266WebServer httpServer(80);
  20. WebSocketsServer webSocket = WebSocketsServer(81);
  21. int clockPin = D1;
  22. int dataPin = D2;
  23. int loadPin = D6;
  24. int resetPin = D7;
  25. int clockCounter = 0;
  26. long loopCounter = 0;
  27. long moduleCounter = 0;
  28. unsigned int data[MAXMODULECOUNT];
  29. bool loadSensors = false;
  30. bool resetTriggered = false;
  31. bool loadTriggered = false;
  32. bool somthingChanged = false;
  33. bool interuptsSet = false;
  34. unsigned long lastClockChange;
  35. unsigned long loadStartedAt;
  36. String changes = "";
  37. void ICACHE_RAM_ATTR s88Clock()
  38. {
  39. delayMicroseconds(16); //Delay makes reading output signal more reliable.
  40. int val = digitalRead(dataPin);
  41. int oldVal = bitRead(data[moduleCounter], clockCounter);
  42. if (val != oldVal)
  43. {
  44. changes += ";"+String(moduleCounter)+"|"+String(clockCounter)+":"+String(val);
  45. //somthingChanged = true;
  46. }
  47. bitWrite(data[moduleCounter], clockCounter, val);
  48. if ((clockCounter + 1) == 8)
  49. {
  50. moduleCounter++;
  51. }
  52. clockCounter = (clockCounter + 1) % 8;
  53. }
  54. void ICACHE_RAM_ATTR s88Load()
  55. {
  56. clockCounter = 0;
  57. moduleCounter = 0;
  58. loopCounter++;
  59. loadTriggered = true;
  60. }
  61. void ICACHE_RAM_ATTR s88Reset()
  62. {
  63. resetTriggered = true;
  64. }
  65. void handleData()
  66. {
  67. String resp = "";
  68. for (int i = 0; i < MAXMODULECOUNT; i++)
  69. {
  70. //resp += (data[i]);
  71. for (int j = 0; j < 8; j++)
  72. {
  73. resp += ((String(i) + "." + String(j) + ":" + String(bitRead(data[i], j))) + "~");
  74. }
  75. }
  76. httpServer.send(200, "text/html", resp);
  77. }
  78. void handleSocketTest()
  79. {
  80. webSocket.broadcastTXT("update");
  81. httpServer.send(200, "text/html", "OK");
  82. }
  83. void handleConfigs()
  84. {
  85. if (httpServer.arg("key") != "")
  86. {
  87. String key = httpServer.arg("key");
  88. if (httpServer.arg("value") != "")
  89. {
  90. File f = LittleFS.open(String("/config-" + key), "w");
  91. String value = httpServer.arg("value");
  92. f.write(value.c_str());
  93. httpServer.send(200, "text/html", "OK");
  94. f.close();
  95. }
  96. else
  97. {
  98. File f = LittleFS.open(String("/config-" + key), "r");
  99. httpServer.send(200, "text/html", f.readString());
  100. f.close();
  101. }
  102. }
  103. else
  104. {
  105. httpServer.send(400, "text/html", "must give a key");
  106. }
  107. }
  108. void setEmulation(bool emulationActive)
  109. {
  110. if (emulationActive)
  111. {
  112. pinMode(clockPin, OUTPUT);
  113. pinMode(loadPin, OUTPUT);
  114. pinMode(resetPin, OUTPUT);
  115. }
  116. else
  117. {
  118. pinMode(clockPin, INPUT);
  119. pinMode(loadPin, INPUT);
  120. pinMode(resetPin, INPUT);
  121. }
  122. }
  123. void runEmulation()
  124. {
  125. if(loadStartedAt+500 > micros()){
  126. digitalWrite(loadPin,HIGH);
  127. return;
  128. }
  129. if(loadStartedAt+1000 > micros()){
  130. digitalWrite(loadPin,LOW);
  131. return;
  132. }
  133. if (lastClockChange + 300 < micros())
  134. {
  135. lastClockChange = micros();
  136. digitalWrite(clockPin, !digitalRead(clockPin));
  137. }
  138. if(moduleCounter == MAXMODULECOUNT){
  139. loadStartedAt = micros();
  140. s88Load();
  141. }
  142. }
  143. void setupInterupts(){
  144. interuptsSet = true;
  145. attachInterrupt(digitalPinToInterrupt(clockPin), s88Clock, RISING);
  146. attachInterrupt(digitalPinToInterrupt(loadPin), s88Load, RISING);
  147. attachInterrupt(digitalPinToInterrupt(resetPin), s88Reset, RISING);
  148. }
  149. void setup()
  150. {
  151. pinMode(dataPin, INPUT);
  152. setEmulation(false);
  153. WiFi.softAP(SSID, PASSWORD);
  154. // put your setup code here, to run once:
  155. if (!LittleFS.begin())
  156. {
  157. return;
  158. }
  159. httpServer.on("/data", handleData);
  160. httpServer.on("/test", handleSocketTest);
  161. httpServer.on("/config", handleConfigs);
  162. httpServer.serveStatic("/index.html", LittleFS, "/index.html", "max-age=43200");
  163. httpServer.serveStatic("/", LittleFS, "/index.html", "max-age=43200");
  164. httpServer.serveStatic("/script.js", LittleFS, "/script.js", "max-age=43200");
  165. httpServer.serveStatic("/reset.css", LittleFS, "/reset.css", "max-age=43200");
  166. httpServer.serveStatic("/style.css", LittleFS, "/style.css", "max-age=43200");
  167. httpServer.begin();
  168. webSocket.begin();
  169. //setEmulation(true);
  170. setupInterupts();
  171. }
  172. void loop()
  173. {
  174. if (resetTriggered)
  175. {
  176. resetTriggered = false;
  177. }
  178. if (loadTriggered)
  179. {
  180. loadTriggered = false;
  181. }
  182. if (somthingChanged)
  183. {
  184. somthingChanged = false;
  185. webSocket.broadcastTXT("update");
  186. }
  187. if(changes != ""){
  188. webSocket.broadcastTXT(changes);
  189. changes = "";
  190. }
  191. httpServer.handleClient();
  192. webSocket.loop();
  193. }