ソースを参照

Done some stuff. Error in JSON.stringify on web side

Emil 3 年 前
コミット
8905e41c2c
6 ファイル変更146 行追加86 行削除
  1. 54 52
      src/Animations.cpp
  2. 1 1
      src/Animations.h
  3. 2 3
      src/LightCluster.cpp
  4. 1 1
      src/LightCluster.h
  5. 85 29
      src/main.cpp
  6. 3 0
      src/main.h

+ 54 - 52
src/Animations.cpp

@@ -4,7 +4,7 @@
 
 #include "Animations.h"
 
-Animations::Animations(struct light *lights) {
+Animations::Animations(light *lights) {
     //Serial.println("Inside animations constructor");
     //delay(100);
     this->lights = lights;
@@ -57,7 +57,8 @@ void Animations::rainbow() {
     //Serial.println("Inside animation function");
     //delay(100);
     for (int light = 0; light < numLeds; light++) {
-        lights[light].color = leds.ColorHSV(((light * animationSetting1 + animationI*65536/maxAnimationI) % 65536), 255, 255);
+        lights[light].color = leds.ColorHSV(((light * animationSetting1 + animationI * 65536 / maxAnimationI) % 65536),
+                                            255, 255);
 //        Serial.print("Set ");
 //        Serial.print(light);
 //        Serial.print(" to ");
@@ -75,27 +76,27 @@ void Animations::setup_rainbow() {
     animationI = 0;
     delayTimeMS = 100;
     maxAnimationI = 256;
-    animationSetting1 = 65536/(numLeds+1);
+    animationSetting1 = 65536 / (numLeds + 1);
 
 }
 
 
 void Animations::randomColors() {
     for (int i = 0; i < numLeds; i++) {
-        light *lamp = &lights[i];
+        light lamp = lights[i];
 
-        lamp->animI++;
+        lamp.animI++;
         /*Serial.println("Inside animation, doing main calc");
         Serial.print("endTick = ");
         Serial.println(lamp->endTick);
         delay(100);*/
-        uint32_t r = (lamp->targetR - lamp->startR) * lamp->animI/lamp->endTick + lamp->startR;
-        uint32_t g = (lamp->targetG - lamp->startG) * lamp->animI/lamp->endTick + lamp->startG;
-        uint32_t b = (lamp->targetB - lamp->startB) * lamp->animI/lamp->endTick + lamp->startB;
-        uint32_t w = (lamp->targetW - lamp->startW) * lamp->animI/lamp->endTick + lamp->startW;
+        uint32_t r = (lamp.targetR - lamp.startR) * lamp.animI / lamp.endTick + lamp.startR;
+        uint32_t g = (lamp.targetG - lamp.startG) * lamp.animI / lamp.endTick + lamp.startG;
+        uint32_t b = (lamp.targetB - lamp.startB) * lamp.animI / lamp.endTick + lamp.startB;
+        uint32_t w = (lamp.targetW - lamp.startW) * lamp.animI / lamp.endTick + lamp.startW;
         //Serial.println("After main calc");
 
-        lamp->color = w << 24 | r << 16 | g << 8 | b;
+        lamp.color = w << 24 | r << 16 | g << 8 | b;
         /*Serial.print("R = ");
         Serial.print(r);
         Serial.print(", G = ");
@@ -107,28 +108,28 @@ void Animations::randomColors() {
         Serial.print("Set color to");
         Serial.println(lamp->color);*/
 
-        if (lamp->animI == lamp->endTick) {
-            uint8_t minR = (animationSetting1 >> 24) &0xff;
-            uint8_t maxR = (animationSetting2 >> 24) &0xff;
-            uint8_t minG = (animationSetting1 >> 16) &0xff;
-            uint8_t maxG = (animationSetting2 >> 16) &0xff;
-            uint8_t minB = (animationSetting1 >> 8) &0xff;
-            uint8_t maxB = (animationSetting2 >> 8) &0xff;
-            uint8_t minW = animationSetting1 &0xff;
-            uint8_t maxW = animationSetting2 &0xff;
-
-            lamp->targetR = random(minR, maxR);
-            lamp->targetG = random(minG, maxG);
-            lamp->targetB = random(minB, maxB);
-            lamp->targetW = random(minW, maxW);
-
-            lamp->animI = 0;
-            lamp->endTick = random(animationSetting3, animationSetting4);
-
-            lamp->startR = r;
-            lamp->startG = g;
-            lamp->startB = b;
-            lamp->startW = w;
+        if (lamp.animI == lamp.endTick) {
+            uint8_t minR = (animationSetting1 >> 24) & 0xff;
+            uint8_t maxR = (animationSetting2 >> 24) & 0xff;
+            uint8_t minG = (animationSetting1 >> 16) & 0xff;
+            uint8_t maxG = (animationSetting2 >> 16) & 0xff;
+            uint8_t minB = (animationSetting1 >> 8) & 0xff;
+            uint8_t maxB = (animationSetting2 >> 8) & 0xff;
+            uint8_t minW = animationSetting1 & 0xff;
+            uint8_t maxW = animationSetting2 & 0xff;
+
+            lamp.targetR = random(minR, maxR);
+            lamp.targetG = random(minG, maxG);
+            lamp.targetB = random(minB, maxB);
+            lamp.targetW = random(minW, maxW);
+
+            lamp.animI = 0;
+            lamp.endTick = random(animationSetting3, animationSetting4);
+
+            lamp.startR = r;
+            lamp.startG = g;
+            lamp.startB = b;
+            lamp.startW = w;
         }
     }
 }
@@ -141,32 +142,33 @@ void Animations::setup_randomColors() {
     animationSetting4 = 300;
     for (int i = 0; i < numLeds; i++) {
         //Serial.println("In loop");
-        light *lamp = &lights[i];
-
-        uint8_t minR = (animationSetting1 >> 24) &0xff;
-        uint8_t maxR = (animationSetting2 >> 24) &0xff;
-        uint8_t minG = (animationSetting1 >> 16) &0xff;
-        uint8_t maxG = (animationSetting2 >> 16) &0xff;
-        uint8_t minB = (animationSetting1 >> 8) &0xff;
-        uint8_t maxB = (animationSetting2 >> 8) &0xff;
-        uint8_t minW = animationSetting1 &0xff;
-        uint8_t maxW = animationSetting2 &0xff;
+        light lamp = lights[i];
+
+        uint8_t minR = (animationSetting1 >> 24) & 0xff;
+        uint8_t maxR = (animationSetting2 >> 24) & 0xff;
+        uint8_t minG = (animationSetting1 >> 16) & 0xff;
+        uint8_t maxG = (animationSetting2 >> 16) & 0xff;
+        uint8_t minB = (animationSetting1 >> 8) & 0xff;
+        uint8_t maxB = (animationSetting2 >> 8) & 0xff;
+        uint8_t minW = animationSetting1 & 0xff;
+        uint8_t maxW = animationSetting2 & 0xff;
         //Serial.println("Grabbed max and min");
 
-        lamp->targetR = random(minR, maxR);
-        lamp->targetG = random(minG, maxG);
-        lamp->targetB = random(minB, maxB);
-        lamp->targetW = random(minW, maxW);
+        lamp.targetR = random(minR, maxR);
+        lamp.targetG = random(minG, maxG);
+        lamp.targetB = random(minB, maxB);
+        lamp.targetW = random(minW, maxW);
 
-        lamp->animI = 0;
-        lamp->endTick = random(animationSetting3, animationSetting4); //Between 3 and 30 seconds. TODO add animationSetting3 and 4 for this.
+        lamp.animI = 0;
+        lamp.endTick = random(animationSetting3,
+                               animationSetting4); //Between 3 and 30 seconds. TODO add animationSetting3 and 4 for this.
         //Serial.print("EndTick = ");
         //Serial.println(lamp->endTick);
 
-        lamp->startR = (lamp->color >> 16) &0xff;
-        lamp->startG = (lamp->color >> 8) &0xff;
-        lamp->startB = lamp->color &0xff;
-        lamp->startW = (lamp->color >> 24) &0xff;
+        lamp.startR = (lamp.color >> 16) & 0xff;
+        lamp.startG = (lamp.color >> 8) & 0xff;
+        lamp.startB = lamp.color & 0xff;
+        lamp.startW = (lamp.color >> 24) & 0xff;
         /*Serial.println("New target: ");
         Serial.println(lamp->targetR);
         Serial.println(lamp->targetG);

+ 1 - 1
src/Animations.h

@@ -18,7 +18,7 @@ public:
     long animationSetting4;
     int numLeds;
 
-    explicit Animations(struct light *lights);
+    explicit Animations(light *lights);
     void runAnimation(int animationId);
     void runSetup(int animationId);
 

+ 2 - 3
src/LightCluster.cpp

@@ -43,7 +43,7 @@ void LightCluster::changeAnimation(int newAnimationNumber) {
     runAnimation();
 }
 
-LightCluster::LightCluster(struct light *incomingLights, int size, int animation, Animations *animationObject)
+LightCluster::LightCluster(light *incomingLights, int size, int animation, Animations *animationObject)
         : animationObject(animationObject) {
 //    for (int i = 0; i < sizeof(lights); i++) {
 //        light data;
@@ -68,9 +68,8 @@ LightCluster::LightCluster(struct light *incomingLights, int size, int animation
 }
 
 LightCluster::~LightCluster() {
-    delete[] animationObject->lights;
     delete[] lights;
-    delete[] animationObject;
+    delete animationObject;
 }
 
 bool LightCluster::shouldRun() {

+ 1 - 1
src/LightCluster.h

@@ -23,7 +23,7 @@ public:
     bool runAnimation();
     void changeAnimation(int newAnimationNumber);
 
-    LightCluster(struct light *incomingLights, int size, int animation, Animations *animationObject);
+    LightCluster(light *incomingLights, int size, int animation, Animations *animationObject);
     ~LightCluster();
 
     light *lights;

+ 85 - 29
src/main.cpp

@@ -6,6 +6,7 @@
 #include <../.pio/libdeps/esp32doit-devkit-v1/AsyncTCP/src/AsyncTCP.h>
 #include "../.pio/libdeps/esp32doit-devkit-v1/ESP Async WebServer/src/ESPAsyncWebServer.h"
 #include "../.pio/libdeps/esp32doit-devkit-v1/ESP Async WebServer/src/AsyncJson.h"
+#include "../.pio/libdeps/esp32doit-devkit-v1/ArduinoJson/src/ArduinoJson/Deserialization/DeserializationError.hpp"
 #include <SPI.h>
 #include <SD.h>
 #include <../.pio/libdeps/esp32doit-devkit-v1/IRremote/src/IRremote.hpp>
@@ -106,9 +107,6 @@ void setupServer() {
         response->addHeader("Access-Control-Allow-Origin","*");
         response->addHeader("Access-Control-Allow-Headers", "Content-Type, data");
         response->addHeader("Access-Control-Allow-Methods", "POST, GET");
-        /*response->addHeader("access-control-allow-credentials", "true");
-        response->addHeader("access-control-allow-origin", "*");
-        response->addHeader("access-control-expose-headers", "");*/
         request->send(response);
     });
 
@@ -130,21 +128,29 @@ void setupServer() {
         int numNewClusters = doc["numClusters"];
         for (int i = 0; i < numNewClusters; i++) {
             int newNumLights = doc["clusters"][i]["numLights"];
-            static light *newLights;
-            newLights = (light*) malloc(newNumLights * sizeof(light));
+            auto newLights = new light[newNumLights];
 
             for (int lamp = 0; lamp < newNumLights; lamp++) {
-                newLights[lamp] = {doc["clusters"][i]["lights"][lamp], 0};
+                int mappedLight = doc["clusters"][i]["lights"][lamp];
+                auto lampa = new light();
+                lampa->mapped = mappedLight;
+                lampa->color = 0;
+                newLights[lamp] = *lampa;
             }
 
-            static auto newAnimationObject = new Animations(newLights);
-            static auto newLightCluster = new LightCluster(newLights, newNumLights, doc["clusters"][i]["animation"], newAnimationObject);
+            auto newAnimationObject = new Animations(newLights);
+            auto newLightCluster = new LightCluster(newLights, newNumLights, doc["clusters"][i]["animation"], newAnimationObject);
             clusters[i] = newLightCluster;
             numClusters = numNewClusters;
 
             updateOccupiedLamps();
 
-            request->send(200, "application/json", "{}");
+            AsyncResponseStream *response = request->beginResponseStream("text/plain");
+            response->addHeader("Access-Control-Allow-Origin","*");
+            response->addHeader("Access-Control-Allow-Headers", "Content-Type, data");
+            response->addHeader("Access-Control-Allow-Methods", "POST, GET");
+            response->setCode(200);
+            request->send(response);
         }
     });
 
@@ -173,7 +179,13 @@ void setupServer() {
         } else if (doc["setting"] == "maxAnimationI") {
             clusters[cluster]->animationObject->maxAnimationI = value;
         }
-        request->send(200, "application/json", "{}");
+
+        AsyncResponseStream *response = request->beginResponseStream("text/plain");
+        response->addHeader("Access-Control-Allow-Origin","*");
+        response->addHeader("Access-Control-Allow-Headers", "Content-Type, data");
+        response->addHeader("Access-Control-Allow-Methods", "POST, GET");
+        response->setCode(200);
+        request->send(response);
     });
 
     server.on("/api/change_animation", HTTP_POST, [](AsyncWebServerRequest *request) {
@@ -189,7 +201,13 @@ void setupServer() {
         int cluster = doc["targetCluster"];
         int value = doc["animation"];
         clusters[cluster]->changeAnimation(value);
-        request->send(200, "application/json", "{}");
+
+        AsyncResponseStream *response = request->beginResponseStream("text/plain");
+        response->addHeader("Access-Control-Allow-Origin","*");
+        response->addHeader("Access-Control-Allow-Headers", "Content-Type, data");
+        response->addHeader("Access-Control-Allow-Methods", "POST, GET");
+        response->setCode(200);
+        request->send(response);
     });
 
     server.on("/api/create_cluster", HTTP_POST, [](AsyncWebServerRequest *request) {
@@ -203,24 +221,38 @@ void setupServer() {
         }
 
         int numLights = doc["numLights"];
-        static light *newLights;
-        newLights = (light*) malloc(numLights * sizeof(light));
 
+        bool containsOccupiedLamp = false;
         for (int i = 0; i < numLights; i++) {
             int mappedLight = doc["lights"][i];
-            if (occupiedLamps[mappedLight] < 0) {
-                newLights[i] = {mappedLight, 0};
-            }
+            containsOccupiedLamp = containsOccupiedLamp || occupiedLamps[i] < 0;
         }
 
-        static auto newAnimationObject = new Animations(newLights);
-        static auto newLightCluster = new LightCluster(newLights, numLights, 0, newAnimationObject);
-        clusters[numClusters] = newLightCluster;
-        numClusters++;
+        if (!containsOccupiedLamp) {
+            auto newLights = new light[numLights];
+
+            for (int i = 0; i < numLights; i++) {
+                int mappedLight = doc["lights"][i];
+                auto lamp = new light();
+                lamp->mapped = mappedLight;
+                lamp->color = 0;
+                newLights[i] = *lamp;
+            }
+
+            auto newAnimationObject = new Animations(newLights);
+            auto newLightCluster = new LightCluster(newLights, numLights, 0, newAnimationObject);
+            clusters[numClusters] = newLightCluster;
+            numClusters++;
+        }
 
         updateOccupiedLamps();
 
-        request->send(200, "application/json", "{}");
+        AsyncResponseStream *response = request->beginResponseStream("text/plain");
+        response->addHeader("Access-Control-Allow-Origin","*");
+        response->addHeader("Access-Control-Allow-Headers", "Content-Type, data");
+        response->addHeader("Access-Control-Allow-Methods", "POST, GET");
+        response->setCode(200);
+        request->send(response);
     });
 
     server.on("/api/delete_cluster", HTTP_POST, [](AsyncWebServerRequest *request) {
@@ -233,11 +265,11 @@ void setupServer() {
             return;
         }
 
-        int targetCluster = doc["cluster"];
 
+        int targetCluster = doc["cluster"];
         for (int i = 0; i < numClusters; i++) {
             if (i == targetCluster) {
-                delete[]clusters[i];
+                delete clusters[i];
             }
             if (i > targetCluster) {
                 clusters[i-1] = clusters[i];
@@ -247,7 +279,12 @@ void setupServer() {
 
         updateOccupiedLamps();
 
-        request->send(200, "application/json", "{}");
+        AsyncResponseStream *response = request->beginResponseStream("text/plain");
+        response->addHeader("Access-Control-Allow-Origin","*");
+        response->addHeader("Access-Control-Allow-Headers", "Content-Type, data");
+        response->addHeader("Access-Control-Allow-Methods", "POST, GET");
+        response->setCode(200);
+        request->send(response);
     });
 
     server.on("/api/sync", HTTP_POST, [](AsyncWebServerRequest *request) {
@@ -263,7 +300,13 @@ void setupServer() {
         for (int i = 0; i < numClusters; i++) {
             clusters[i]->animationObject->animationI = 0;
         }
-        request->send(200, "application/json", "{}");
+
+        AsyncResponseStream *response = request->beginResponseStream("text/plain");
+        response->addHeader("Access-Control-Allow-Origin","*");
+        response->addHeader("Access-Control-Allow-Headers", "Content-Type, data");
+        response->addHeader("Access-Control-Allow-Methods", "POST, GET");
+        response->setCode(200);
+        request->send(response);
     });
 }
 
@@ -271,13 +314,17 @@ void setup() {
     // write your initialization code here
     Serial.begin(115200);
 
-    static light allLights[NUM_LEDS];
+    auto allLights = new light[NUM_LEDS];
+
     for (int i = 0; i < NUM_LEDS; i++) {
-        allLights[i] = {i, 0};
+        auto lamp = new light();
+        lamp->mapped = i;
+        lamp->color = 0;
+        allLights[i] = *lamp;
     }
 
-    static auto startAnimationObject = new Animations(allLights);
-    static auto startCluster = new LightCluster(allLights, NUM_LEDS, 0, startAnimationObject);
+    auto startAnimationObject = new Animations(allLights);
+    auto startCluster = new LightCluster(allLights, NUM_LEDS, 0, startAnimationObject);
 
     clusters[0] = startCluster;
     numClusters = 1;
@@ -373,5 +420,14 @@ void loop() {
     if (hasRun) {
         leds.show();
     }
+
+    if (numClusters > 0) {
+        Serial.println("");
+        for (int i = 0; i < clusters[0]->numLights; i++) {
+            Serial.print("Mapped light: ");
+            Serial.println(clusters[0]->lights[i].mapped);
+        }
+        delay(1000);
+    }
     delay(1);
 }

+ 3 - 0
src/main.h

@@ -30,4 +30,7 @@ struct light {
     uint16_t endTick;
 };
 
+typedef light* lightPtr;
+
+
 #endif //UNTITLED_MAIN_H