소스 검색

Added random colors animation and settings can be chnaged with http. Nothing untested.

Emil 3 년 전
부모
커밋
2faba4b3a1
6개의 변경된 파일157개의 추가작업 그리고 10개의 파일을 삭제
  1. 108 1
      src/Animations.cpp
  2. 11 5
      src/Animations.h
  3. 1 0
      src/LightCluster.cpp
  4. 2 2
      src/WiFiPass.h
  5. 22 2
      src/main.cpp
  6. 13 0
      src/main.h

+ 108 - 1
src/Animations.cpp

@@ -41,6 +41,18 @@ void Animations::runSetup(int animationID) {
     //}
 }
 
+
+void Animations::off() {
+    for (int light = 0; light < numLeds; light++) {
+        lights[light].color = 0;
+    }
+}
+
+void Animations::setup_off() {
+    delayTimeMS = 10000;
+}
+
+
 void Animations::rainbow() {
     //Serial.println("Inside animation function");
     //delay(100);
@@ -59,10 +71,105 @@ void Animations::rainbow() {
     animationI %= maxAnimationI; //Don't go all way to 255 because 0*65536/256 = 0 and 256*65536/256 = 65536. It will result in the same color for 2 cycles.
 }
 
-void Animations::setupRainbow() {
+void Animations::setup_rainbow() {
     animationI = 0;
     delayTimeMS = 100;
     maxAnimationI = 256;
     animationSetting1 = 65536/(numLeds+1);
+
+}
+
+
+void Animations::randomColors() {
+    for (int i = 0; i < numLeds; i++) {
+        light *lamp = &lights[i];
+
+        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;
+        //Serial.println("After main calc");
+
+        lamp->color = w << 24 | r << 16 | g << 8 | b;
+        /*Serial.print("R = ");
+        Serial.print(r);
+        Serial.print(", G = ");
+        Serial.print(g);
+        Serial.print(", B = ");
+        Serial.print(b);
+        Serial.print(", W = ");
+        Serial.println(w);
+        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(30, 300); //Between 3 and 30 seconds. TODO add animationSetting3 and 4 fpr this.
+
+            lamp->startR = r;
+            lamp->startG = g;
+            lamp->startB = b;
+            lamp->startW = w;
+        }
+    }
 }
 
+void Animations::setup_randomColors() {
+    delayTimeMS = 100;
+    animationSetting1 = 0x00000000;
+    animationSetting2 = 0xFFFFFFFF;
+    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;
+        //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->animI = 0;
+        lamp->endTick = random(30, 300); //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;
+        /*Serial.println("New target: ");
+        Serial.println(lamp->targetR);
+        Serial.println(lamp->targetG);
+        Serial.println(lamp->targetB);
+        Serial.println(lamp->targetW);
+        Serial.println("");*/
+    }
+}

+ 11 - 5
src/Animations.h

@@ -12,8 +12,8 @@ public:
     int animationI;
     int maxAnimationI;
     int delayTimeMS;
-    int animationSetting1;
-    int animationSetting2;
+    long animationSetting1;
+    long animationSetting2;
     int numLeds;
 
     explicit Animations(struct light *lights);
@@ -23,15 +23,21 @@ public:
     //void createLookup();
 
     typedef void (Animations::*method_function)();
-    method_function animationPointer[1] = {&Animations::rainbow};
-    method_function setupPointer[1] = {&Animations::setupRainbow};
+    method_function animationPointer[3] = {&Animations::off, &Animations::rainbow, &Animations::randomColors};
+    method_function setupPointer[3] = {&Animations::setup_off, &Animations::setup_rainbow, &Animations::setup_randomColors};
 
     struct light *lights;
 
 
 private:
     void rainbow();
-    void setupRainbow();
+    void setup_rainbow();
+
+    void off();
+    void setup_off();
+
+    void randomColors();
+    void setup_randomColors();
 };
 
 

+ 1 - 0
src/LightCluster.cpp

@@ -38,6 +38,7 @@ void LightCluster::runSetup() {
 
 void LightCluster::changeAnimation(int newAnimationNumber) {
     animationNumber = newAnimationNumber;
+    lastRun = 0;
     runSetup();
     runAnimation();
 }

+ 2 - 2
src/WiFiPass.h

@@ -5,7 +5,7 @@
 #ifndef UNTITLED_WIFIPASS_H
 #define UNTITLED_WIFIPASS_H
 
-const char* ssid = "REPLACE_WITH_YOUR_SSID";
-const char* password = "REPLACE_WITH_YOUR_PASSWORD";
+const char* ssid = "Tallium 2.4";
+const char* password = "vgjsdsmhds";
 
 #endif //UNTITLED_WIFIPASS_H

+ 22 - 2
src/main.cpp

@@ -78,7 +78,7 @@ void setupServer() {
     server.serveStatic("/", SD, "/");
 
     server.on("/api/set_config", HTTP_POST, [](AsyncWebServerRequest *request) {
-        StaticJsonDocument<200> doc;
+        StaticJsonDocument<400> doc;
         DeserializationError error = deserializeJson(doc, request->header("data"));
         Serial.print(request->header("data"));
         if (error) {
@@ -125,10 +125,30 @@ void setupServer() {
         }
 
         int cluster = doc["targetCluster"];
-        int value = doc["newValue"];
+        long value = doc["newValue"];
         if (doc["setting"] == "delayTime") {
             clusters[cluster]->animationObject->delayTimeMS = value;
+        } else if (doc["setting"] == "animationSetting1") {
+            clusters[cluster]->animationObject->animationSetting1 = value;
+        } else if (doc["setting"] == "animationSetting2") {
+            clusters[cluster]->animationObject->animationSetting2 = value;
+        }
+        request->send(200, "application/json", "{}");
+    });
+
+    server.on("/api/change_animation", HTTP_POST, [](AsyncWebServerRequest *request) {
+        StaticJsonDocument<200> doc;
+        DeserializationError error = deserializeJson(doc, request->getHeader("data")->value().c_str());
+        Serial.println(request->getHeader("data")->value().c_str());
+        if (error) {
+            Serial.print(F("deserializeJson() failed: "));
+            Serial.println(error.f_str());
+            return;
         }
+
+        int cluster = doc["targetCluster"];
+        int value = doc["animation"];
+        clusters[cluster]->changeAnimation(value);
         request->send(200, "application/json", "{}");
     });
 }

+ 13 - 0
src/main.h

@@ -15,6 +15,19 @@ extern Adafruit_NeoPixel leds;
 struct light {
     int mapped;
     uint32_t color;
+
+    uint8_t targetR;
+    uint8_t targetG;
+    uint8_t targetB;
+    uint8_t targetW;
+
+    uint8_t startR;
+    uint8_t startG;
+    uint8_t startB;
+    uint8_t startW;
+
+    uint16_t animI;
+    uint16_t endTick;
 };
 
 #endif //UNTITLED_MAIN_H