Browse Source

pin change and data over socket

Vincent Molenaar 2 years ago
parent
commit
397f7eeb9a
3 changed files with 88 additions and 71 deletions
  1. 5 0
      .gitignore
  2. 70 45
      data/script.js
  3. 13 26
      src/main.cpp

+ 5 - 0
.gitignore

@@ -0,0 +1,5 @@
+.pio
+.vscode/.browse.c_cpp.db*
+.vscode/c_cpp_properties.json
+.vscode/launch.json
+.vscode/ipch

+ 70 - 45
data/script.js

@@ -1,5 +1,6 @@
 var rawContent;
 var data = [];
+var update = [];
 var loading = false;
 var websock;
 
@@ -18,15 +19,15 @@ function GetData() {
     return;
   loading = true;
   return fetch("/data")
-    .then(function(result) {
-      return result.text().then(function(text) {
+    .then(function (result) {
+      return result.text().then(function (text) {
         rawContent = text;
         data = ConvertData(rawContent);
         loading = false;
       });
     })
-    .catch(function() {
-        loading = false;
+    .catch(function () {
+      loading = false;
     });
 }
 
@@ -79,48 +80,48 @@ function SaveSettings() {
 }
 
 function LoadSettings(rawSettings) {
-    if(rawSettings == null){
-        var tmpsettings = JSON.parse(window.localStorage.getItem('settings'));
-        if (tmpsettings == null)
-            return;
-        settings = tmpsettings;
-    }else{
-        settings = rawSettings;
-    }
-
-    document.getElementById("modOffset").value = settings.modOffset;
-    document.getElementById("inputOffset").value = settings.inputOffset;
-    document.getElementById("updateInterval").value = settings.updateInterval;
-    document.getElementById("checkoptions-0").checked = settings.autoupdate;
-    document.getElementById("checkoptions-1").checked = settings.emulateMaster;
-    SetAutoUpdate();
+  if (rawSettings == null) {
+    var tmpsettings = JSON.parse(window.localStorage.getItem('settings'));
+    if (tmpsettings == null)
+      return;
+    settings = tmpsettings;
+  } else {
+    settings = rawSettings;
+  }
+
+  document.getElementById("modOffset").value = settings.modOffset;
+  document.getElementById("inputOffset").value = settings.inputOffset;
+  document.getElementById("updateInterval").value = settings.updateInterval;
+  document.getElementById("checkoptions-0").checked = settings.autoupdate;
+  document.getElementById("checkoptions-1").checked = settings.emulateMaster;
+  SetAutoUpdate();
 }
 
-function SaveSettingsOnDevice(){
-    SaveSettings();
-    var urlEncodedSettings = encodeURI(JSON.stringify(settings));
-    var url = "/config?key=settings&value="+urlEncodedSettings;
+function SaveSettingsOnDevice() {
+  SaveSettings();
+  var urlEncodedSettings = encodeURI(JSON.stringify(settings));
+  var url = "/config?key=settings&value=" + urlEncodedSettings;
 
-    fetch(url)
-    .then(function(result) {
-      result.text().then(function(text) {
+  fetch(url)
+    .then(function (result) {
+      result.text().then(function (text) {
         alert("Saved")
       });
     })
-    .catch(function() {
+    .catch(function () {
     });
 }
 
-function LoadSettingsFromDevice(){
-    fetch("/config?key=settings")
-    .then(function(result) {
-      return result.text().then(function(text) {
+function LoadSettingsFromDevice() {
+  fetch("/config?key=settings")
+    .then(function (result) {
+      return result.text().then(function (text) {
         var settingsraw = JSON.parse(decodeURI(text));
         LoadSettings(settingsraw);
       });
     })
-    .catch(function() {
-        loading = false;
+    .catch(function () {
+      loading = false;
     });
 }
 
@@ -133,7 +134,7 @@ function SetAutoUpdate() {
         return;
       DoUpdate();
     }
-  }, settings.updateInterval*1000);
+  }, settings.updateInterval * 1000);
 }
 
 function DoUpdate() {
@@ -145,18 +146,42 @@ function DoUpdate() {
 }
 
 function StartSocket() {
-    websock = new WebSocket('ws://' + window.location.hostname + ':81/');
-    websock.onopen = function(evt) { console.log('websock open'); };
-    websock.onclose = function(evt) { console.log('websock close'); };
-    websock.onerror = function(evt) { console.log(evt); };
-    websock.onmessage = function(evt) {
-      console.log(evt);
-        switch(evt.data){
-            case "update":
-                DoUpdate();
-            break;
+  websock = new WebSocket('ws://' + window.location.hostname + ':81/');
+  websock.onopen = function (evt) { console.log('websock open'); };
+  websock.onclose = function (evt) { console.log('websock close'); };
+  websock.onerror = function (evt) { console.log(evt); };
+  websock.onmessage = function (evt) {
+    console.log(evt);
+    switch (evt.data) {
+      case "update":
+        DoUpdate();
+        break;
+      default:
+        var data = evt.data;
+        update = data.split(";");
+        UpdateData(update);
+        break;
+    }
+  };
+}
+
+function UpdateData(update) {
+  update.forEach(u => {
+    if (u != '') {
+      var tmp = u.split('|');
+      var module = tmp[0];
+      if (tmp[1] != null) {
+        tmp = tmp[1].split(':');
+        var input = tmp[0];
+
+        if (tmp[1] != null) {
+          var value = tmp[1];
+          data.find(d => d.module == module && d.input == input).data = (value == "1");
         }
-    };
+      }
+    }
+  });
+  RenderData(data);
 }
 
 LoadSettings();

+ 13 - 26
src/main.cpp

@@ -16,6 +16,8 @@ D6 = 6 - Load
 D2 = 2 - Data
 D4 = 4 - Clock
 
+Switched D4 to D1 
+create wire link on PCB
 */
 
 ESP8266WebServer httpServer(80);
@@ -41,6 +43,8 @@ bool interuptsSet = false;
 unsigned long lastClockChange;
 unsigned long loadStartedAt;
 
+String changes = "";
+
 void ICACHE_RAM_ATTR s88Clock()
 {
   delayMicroseconds(16); //Delay makes reading output signal more reliable.
@@ -49,7 +53,8 @@ void ICACHE_RAM_ATTR s88Clock()
 
   if (val != oldVal)
   {
-    somthingChanged = true;
+    changes += ";"+String(moduleCounter)+"|"+String(clockCounter)+":"+String(val); 
+    //somthingChanged = true;
   }
 
   bitWrite(data[moduleCounter], clockCounter, val);
@@ -172,12 +177,10 @@ void setup()
 {
   pinMode(dataPin, INPUT);
   setEmulation(false);
-  Serial.begin(115200);
   WiFi.softAP(SSID, PASSWORD);
   // put your setup code here, to run once:
   if (!LittleFS.begin())
   {
-    Serial.println("An Error has occurred while mounting LittleFS");
     return;
   }
 
@@ -193,6 +196,8 @@ void setup()
   webSocket.begin();
 
   //setEmulation(true);
+
+  setupInterupts();
 }
 
 void loop()
@@ -200,42 +205,24 @@ void loop()
   if (resetTriggered)
   {
     resetTriggered = false;
-    if (DEBUGOUTPUT)
-      Serial.println("RESET");
   }
 
   if (loadTriggered)
   {
     loadTriggered = false;
-    if (DEBUGOUTPUT)
-      Serial.println("LOAD");
   }
 
   if (somthingChanged)
   {
     somthingChanged = false;
-    if (DEBUGOUTPUT)
-      Serial.println("UPDATE");
     webSocket.broadcastTXT("update");
   }
 
-  if (DEBUGOUTPUT)
-  {
-    Serial.print("Modcounter");
-    Serial.println(moduleCounter);
-
-    Serial.print("Data: ");
-    for (int i = 0; i < MAXMODULECOUNT; i++)
-    {
-      Serial.println(data[i]);
-    }
+  if(changes != ""){
+    webSocket.broadcastTXT(changes);
+    changes = "";
   }
-  httpServer.handleClient();
-  webSocket.loop();
-  //runEmulation(); //not working
 
-  if(millis() > 10000 && !interuptsSet)
-  {
-    setupInterupts();
-  }
+  httpServer.handleClient();
+  webSocket.loop(); 
 }