Kaynağa Gözat

New web control API with fade and selection

jediemil 5 yıl önce
ebeveyn
işleme
21d91771bb
1 değiştirilmiş dosya ile 248 ekleme ve 136 silme
  1. 248 136
      LedThing.ino/LedThing.ino.ino

+ 248 - 136
LedThing.ino/LedThing.ino.ino

@@ -7,6 +7,7 @@
 int NUMBER_OF_ANIMATIONS = 5;
 char ssid[] = "Pannkakshuset";
 char pass[] = "lavalampa";
+boolean doing_animation = true;
 
 int current_animation = 0;
 int loops_since_http = 0;
@@ -29,11 +30,11 @@ extern void animation_random1();
 void (*animation_table[])() = {animation_off, animation_rainbow, animation_running, animation_chasing, animation_random1};
 
 extern void setup_rainbow(); // forward declaration
-extern void setup_running(); // forward declaration
-extern void setup_chasing();
 extern void setup_random1();
+extern void normal_setup();
+extern void setup_chosen(int, int, CRGB, boolean, int, int);
 
-void (*setup_table[])() = {animation_off, setup_rainbow, setup_running, setup_chasing, setup_random1};
+void (*setup_table[])() = {animation_off, setup_rainbow, normal_setup, normal_setup, setup_random1};
 
 CRGB leds[NUM_LEDS];
 
@@ -66,7 +67,7 @@ void setup() {
     }
   }
   server.begin();
-  
+
   delay(3000);
   FastLED.addLeds<TM1804, DATA_PIN, BRG>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
   FastLED.setBrightness(255);
@@ -74,13 +75,22 @@ void setup() {
   FastLED.show();
   delay(1000);
   setup_table[current_animation]();
+  Serial.println("Started");
 }
 
 void loop() {
   loops_since_http += 1;
   long current_millis = millis();
   if (current_millis - last_animation_millis >= animation_step_time && loops_since_http < 100) {
-    animation_table[current_animation]();
+    if (doing_animation) {
+      animation_table[current_animation]();
+    } else {
+      if (animation_var_1 == 1) {
+        animation_fade_help();
+      } else {
+        delay(5);
+      }
+    }
     last_animation_millis = current_millis;
   } else {
     loops_since_http = 0;
@@ -91,35 +101,81 @@ void loop() {
   // put your main code here, to run repeatedly:
   //Serial.println("Hello");
   //delay(1000);
-//  leds[3] = CRGB::Blue;
-//  leds[0] = CRGB::Red;
-//  leds[1] = CRGB::Green;
-//  leds[49] = CRGB::White;
-//  FastLED.clear();
-//  FastLED.show();
-//  uint8_t delta = 10;
-//  for (uint8_t i = 0; i < 360; i++) {
-//    //leds[i] = CRGB(255-i*4, 255-i*4, 255-i*4);
-//    fill_rainbow(leds, NUM_LEDS, i, delta);
-//    FastLED.show();
-//    delay(10);
-//  }
-  
+  //  leds[3] = CRGB::Blue;
+  //  leds[0] = CRGB::Red;
+  //  leds[1] = CRGB::Green;
+  //  leds[49] = CRGB::White;
+  //  FastLED.clear();
+  //  FastLED.show();
+  //  uint8_t delta = 10;
+  //  for (uint8_t i = 0; i < 360; i++) {
+  //    //leds[i] = CRGB(255-i*4, 255-i*4, 255-i*4);
+  //    fill_rainbow(leds, NUM_LEDS, i, delta);
+  //    FastLED.show();
+  //    delay(10);
+  //  }
+
 }
 
 void process_HTTP() {
-   WiFiClient client = server.available();
+  WiFiClient client = server.available();
   if (client && millis() - latest_animation_change > 1000) {
     int anim = 0;
     Serial.println("new client");
     // an http request ends with a blank line
     boolean foundSlash = false;
+    boolean foundHashtag = false;
+    int leds1 = 0;
+    int leds2 = 0;
+    int newR = 0;
+    int newG = 0;
+    int newB = 0;
+    int doFade = 0; //Between 0 and 1
+    int cycles = 20;
+    int cycleLength = 100;
+    int currentLoop = 0;
+    String fullArguments = "";
     while (client.connected()) {
       if (client.available()) {
         char c = client.read();
         //Serial.write(c);
-        if (foundSlash) {
-          if (c == '(') {
+        if (foundHashtag) { //http://192.168.112.41/z0:49:0:255:0:0:20:1000;
+          if (c == ';') {
+            client.println("HTTP/1.1 200 OK");
+            client.println("Content-Type: text/plain");
+            client.println("Connection: close");  // the connection will be closed after completion of the response
+            client.println();
+            client.println("OK");
+            break;
+          } else if (c == ':') {
+            if (currentLoop == 0) {
+              leds1 = fullArguments.toInt();
+            } else if (currentLoop == 1) {
+              leds2 = fullArguments.toInt();
+            } else if (currentLoop == 2) {
+              newR = fullArguments.toInt();
+            } else if (currentLoop == 3) {
+              newG = fullArguments.toInt();
+            } else if (currentLoop == 4) {
+              newB = fullArguments.toInt();
+            } else if (currentLoop == 5) {
+              doFade = fullArguments.toInt() % 2;
+            } else if (currentLoop == 6) {
+              cycles = fullArguments.toInt();
+            } else if (currentLoop == 7) {
+              cycleLength = fullArguments.toInt();
+            }
+            fullArguments = "";
+            currentLoop++;
+          } else {
+            fullArguments = fullArguments + c;
+          }
+        } else if (foundSlash) {
+          if (c == 'z') {
+            foundHashtag = true;
+            foundSlash = false;
+            continue;
+          } else if (c == '(') {
             anim = current_animation - 1;
             if (anim < 0) {
               anim = NUMBER_OF_ANIMATIONS - 1;
@@ -130,8 +186,8 @@ void process_HTTP() {
             anim = c - 'A';
           }
           if (anim < 0) {
-              anim = -anim;
-            }
+            anim = -anim;
+          }
           anim %= NUMBER_OF_ANIMATIONS;
           client.println("HTTP/1.1 200 OK");
           client.println("Content-Type: text/plain");
@@ -140,6 +196,7 @@ void process_HTTP() {
           client.println("OK");
           break;
         }
+
         if (c == '/') {
           foundSlash = true;
         }
@@ -151,9 +208,21 @@ void process_HTTP() {
     // close the connection:
     client.stop();
     //Serial.println("client disconnected");
-    setup_table[anim]();
-    current_animation = anim;
-    latest_animation_change = millis();
+    if (foundSlash) {
+      doing_animation = true;
+      setup_table[anim]();
+      current_animation = anim;
+      latest_animation_change = millis();
+    } else {
+      doing_animation = false;
+//      Serial.println(leds1);
+//      Serial.println(leds2);
+//      Serial.println(newR);
+//      Serial.println(newG);
+//      Serial.println(newB);
+//      Serial.println(doFade);
+      setup_chosen(leds1, leds2, CRGB(newR, newG, newB), (boolean) doFade, cycles, cycleLength);
+    }
   }
 }
 
@@ -189,8 +258,8 @@ void animation_chasing() {
   leds[(animation_i + 10) % 50] = CRGB(100, 255, 200);
   leds[(animation_i + 20) % 50] = CRGB(100, 255, 200);
   leds[(50 - animation_i) - 1] = CRGB(0, 255, 255);
-//  leds[(50 - animation_i - 10)] = CRGB(0, 255, 255);
-//  leds[(50 - animation_i)] = CRGB(0, 255, 255);
+  //  leds[(50 - animation_i - 10)] = CRGB(0, 255, 255);
+  //  leds[(50 - animation_i)] = CRGB(0, 255, 255);
   FastLED.show();
   animation_i++;
   animation_i %= 50;
@@ -202,7 +271,7 @@ void animation_off() {
 }
 
 void animation_random1() {
-  for (int i = 0; i < NUM_LEDS/5; i++) {
+  for (int i = 0; i < NUM_LEDS / 5; i++) {
     int howManyCycles = animation_table_1[i][7];
     int currentStep = animation_table_1[i][6];
     uint8_t startRed = animation_table_1[i][0];
@@ -216,15 +285,15 @@ void animation_random1() {
     uint8_t b = (goalBlue - startBlue) * currentStep / howManyCycles + startBlue;
 
     for (int led = 0; led < 5; led++) {
-      leds[led + i*5] = CRGB(r, b, g);
+      leds[led + i * 5] = CRGB(r, b, g);
     }
 
     animation_table_1[i][6] += 1;
-    
+
     if (currentStep >= howManyCycles) {
-//      uint8_t newR = (uint8_t) random(0, 255);
-//      uint8_t newG = (uint8_t) random(0, 255);
-//      uint8_t newB = (uint8_t) random(0, 255);
+      //      uint8_t newR = (uint8_t) random(0, 255);
+      //      uint8_t newG = (uint8_t) random(0, 255);
+      //      uint8_t newB = (uint8_t) random(0, 255);
 
       uint8_t newH = (uint8_t) random(0, 255);
       uint8_t newS = (uint8_t) random(200, 255);
@@ -234,117 +303,145 @@ void animation_random1() {
       Serial.println(newColor.r);
       Serial.println(newColor.g);
       Serial.println(newColor.b);
-      
+
       animation_table_1[i][3] = newColor.r;
       animation_table_1[i][4] = newColor.g;
       animation_table_1[i][5] = newColor.b;
-      
+
       uint8_t steps = (uint8_t) random(10, 50);
       animation_table_1[i][0] = r;
       animation_table_1[i][1] = g;
       animation_table_1[i][2] = b;
       animation_table_1[i][7] = steps;
       animation_table_1[i][6] = 0;
-      
+
     }
-//    uint8_t r = leds[i*5].r + animation_table_1[i][0];
-//    uint8_t g = leds[i*5].g + animation_table_1[i][1];
-//    uint8_t b = leds[i*5].b + animation_table_1[i][2];
-//    if (i == 0) {
-//      Serial.println(r);
-//      Serial.println(g);
-//      Serial.println(b);
-//    }
-//    boolean rDone = false;
-//    boolean gDone = false;
-//    boolean bDone = false;
-//    if (animation_table_1[i][0] > 0 && r >= animation_table_1[i][3]) {
-//      r = animation_table_1[i][3];
-//      rDone = true;
-//    }
-//    if (animation_table_1[i][1] > 0 && g >= animation_table_1[i][4]) {
-//      g = animation_table_1[i][4];
-//      gDone = true;
-//    }
-//    if (animation_table_1[i][2] > 0 && b >= animation_table_1[i][5]) {
-//      b = animation_table_1[i][5];
-//      bDone = true;
-//    }
-//    if (animation_table_1[i][0] <= 0 && r <= animation_table_1[i][3]) {
-//      r = animation_table_1[i][3];
-//      rDone = true;
-//    } 
-//    if (animation_table_1[i][1] <= 0 && g <= animation_table_1[i][4]) {
-//      g = animation_table_1[i][4];
-//      gDone = true;
-//    } 
-//    if (animation_table_1[i][2] <= 0 && b <= animation_table_1[i][5]) {
-//      b = animation_table_1[i][5];
-//      bDone = true;
-//    }
-//    if (i == 0) {
-//    Serial.println(rDone);
-//    Serial.println(gDone);
-//    Serial.println(bDone);
-//    }
-//    
-//    for (int led = 0; led < 5; led++) {
-//      leds[led + i*5] = CRGB(r, b, g);
-//    }
-//
-//    if (rDone && gDone && bDone) {
-//      if (i == 0) {
-//        Serial.println("New color");
-//      } else {
-//        Serial.println("New color other i");
-//      }
-//      uint8_t newR = (uint8_t) random(0, 255);
-//      uint8_t newG = (uint8_t) random(0, 255);
-//      uint8_t newB = (uint8_t) random(0, 255);
-//      animation_table_1[i][3] = newR;
-//      animation_table_1[i][4] = newG;
-//      animation_table_1[i][5] = newB;
-//
-//      uint8_t steps = (uint8_t) random(10, 50);
-//      uint8_t newRStep = (uint8_t) (newR - r)/steps;
-//      uint8_t newGStep = (uint8_t) (newG - g)/steps;
-//      uint8_t newBStep = (uint8_t) (newB - b)/steps;
-//      if (newRStep < 1) {
-//        newRStep = 1;
-//      }
-//      if (newGStep < 1) {
-//        newGStep = 1;
-//      }
-//      if (newBStep < 1) {
-//        newBStep = 1;
-//      }
-//      animation_table_1[i][0] = newRStep;
-//      animation_table_1[i][1] = newGStep;
-//      animation_table_1[i][2] = newBStep;
-//      if (i == 0) {
-//      Serial.println(newR);
-//      Serial.println(newG);
-//      Serial.println(newB);
-//      }
-//    }
+    //    uint8_t r = leds[i*5].r + animation_table_1[i][0];
+    //    uint8_t g = leds[i*5].g + animation_table_1[i][1];
+    //    uint8_t b = leds[i*5].b + animation_table_1[i][2];
+    //    if (i == 0) {
+    //      Serial.println(r);
+    //      Serial.println(g);
+    //      Serial.println(b);
+    //    }
+    //    boolean rDone = false;
+    //    boolean gDone = false;
+    //    boolean bDone = false;
+    //    if (animation_table_1[i][0] > 0 && r >= animation_table_1[i][3]) {
+    //      r = animation_table_1[i][3];
+    //      rDone = true;
+    //    }
+    //    if (animation_table_1[i][1] > 0 && g >= animation_table_1[i][4]) {
+    //      g = animation_table_1[i][4];
+    //      gDone = true;
+    //    }
+    //    if (animation_table_1[i][2] > 0 && b >= animation_table_1[i][5]) {
+    //      b = animation_table_1[i][5];
+    //      bDone = true;
+    //    }
+    //    if (animation_table_1[i][0] <= 0 && r <= animation_table_1[i][3]) {
+    //      r = animation_table_1[i][3];
+    //      rDone = true;
+    //    }
+    //    if (animation_table_1[i][1] <= 0 && g <= animation_table_1[i][4]) {
+    //      g = animation_table_1[i][4];
+    //      gDone = true;
+    //    }
+    //    if (animation_table_1[i][2] <= 0 && b <= animation_table_1[i][5]) {
+    //      b = animation_table_1[i][5];
+    //      bDone = true;
+    //    }
+    //    if (i == 0) {
+    //    Serial.println(rDone);
+    //    Serial.println(gDone);
+    //    Serial.println(bDone);
+    //    }
+    //
+    //    for (int led = 0; led < 5; led++) {
+    //      leds[led + i*5] = CRGB(r, b, g);
+    //    }
+    //
+    //    if (rDone && gDone && bDone) {
+    //      if (i == 0) {
+    //        Serial.println("New color");
+    //      } else {
+    //        Serial.println("New color other i");
+    //      }
+    //      uint8_t newR = (uint8_t) random(0, 255);
+    //      uint8_t newG = (uint8_t) random(0, 255);
+    //      uint8_t newB = (uint8_t) random(0, 255);
+    //      animation_table_1[i][3] = newR;
+    //      animation_table_1[i][4] = newG;
+    //      animation_table_1[i][5] = newB;
+    //
+    //      uint8_t steps = (uint8_t) random(10, 50);
+    //      uint8_t newRStep = (uint8_t) (newR - r)/steps;
+    //      uint8_t newGStep = (uint8_t) (newG - g)/steps;
+    //      uint8_t newBStep = (uint8_t) (newB - b)/steps;
+    //      if (newRStep < 1) {
+    //        newRStep = 1;
+    //      }
+    //      if (newGStep < 1) {
+    //        newGStep = 1;
+    //      }
+    //      if (newBStep < 1) {
+    //        newBStep = 1;
+    //      }
+    //      animation_table_1[i][0] = newRStep;
+    //      animation_table_1[i][1] = newGStep;
+    //      animation_table_1[i][2] = newBStep;
+    //      if (i == 0) {
+    //      Serial.println(newR);
+    //      Serial.println(newG);
+    //      Serial.println(newB);
+    //      }
+    //    }
   }
-  for (int i = 0; i < NUM_LEDS/5; i++) {
-    uint8_t startRed = leds[i*5].r;
-    uint8_t startGreen = leds[i*5].g;
-    uint8_t startBlue = leds[i*5].b;
-    uint8_t goalRed = leds[((i+1)*5)%NUM_LEDS].r;
-    uint8_t goalGreen = leds[((i+1)*5)%NUM_LEDS].g;
-    uint8_t goalBlue = leds[((i+1)*5)%NUM_LEDS].b;
+  for (int i = 0; i < NUM_LEDS / 5; i++) {
+    uint8_t startRed = leds[i * 5].r;
+    uint8_t startGreen = leds[i * 5].g;
+    uint8_t startBlue = leds[i * 5].b;
+    uint8_t goalRed = leds[((i + 1) * 5) % NUM_LEDS].r;
+    uint8_t goalGreen = leds[((i + 1) * 5) % NUM_LEDS].g;
+    uint8_t goalBlue = leds[((i + 1) * 5) % NUM_LEDS].b;
     for (int currentStep = 1; currentStep < 5; currentStep++) {
       uint8_t r = (goalRed - startRed) * currentStep / 5 + startRed;
       uint8_t g = (goalGreen - startGreen) * currentStep / 5 + startGreen;
       uint8_t b = (goalBlue - startBlue) * currentStep / 5 + startBlue;
-      leds[i*5+currentStep] = CRGB(r, g, b);
+      leds[i * 5 + currentStep] = CRGB(r, g, b);
     }
   }
   FastLED.show();
 }
 
+void animation_fade_help() {
+  int leds1 = animation_table_1[0][0];
+  int leds2 = animation_table_1[0][1];
+  int finnishR = animation_table_1[0][2];
+  int finnishG = animation_table_1[0][3];
+  int finnishB = animation_table_1[0][4];
+  int cycles = animation_table_1[0][5];
+  int startR = animation_table_1[0][6];
+  int startG = animation_table_1[0][7];
+  int startB = animation_table_1[1][0];
+
+  uint8_t r = (finnishR - startR) * animation_i / cycles + startR;
+  uint8_t g = (finnishG - startG) * animation_i / cycles + startG;
+  uint8_t b = (finnishB - startB) * animation_i / cycles + startB;
+
+  for (int i = leds1; i < leds2; i++) {
+    leds[i] = CRGB(r, g, b);
+  }
+  animation_i++;
+  if (animation_i >= cycles) {
+    for (int i = leds1; i < leds2; i++) {
+      leds[i] = CRGB(finnishR, finnishG, finnishB);
+    }
+    animation_var_1 = 0;
+  }
+  FastLED.show();
+}
+
 void setup_rainbow() {
   normal_setup();
   animation_var_1 = 10;
@@ -357,22 +454,37 @@ void normal_setup() {
   animation_var_1 = 0;
 }
 
-void setup_running() {
-  normal_setup();
-}
-
-void setup_chasing() {
-  normal_setup();
-}
-
 void setup_random1() {
   for (int i = 0; i < NUM_LEDS; i++) {
     for (int i = 0; i < 8; i++) {
       animation_table_1[i][i] = 0;
     }
-      //= {0, 0, 0, 0, 0, 0}; //Step size R, Step size G, Step size B, fR, fG, fB
-      //Start R, Start G, Start B, fR, fG, fB, cycle, how many cycles
+    //= {0, 0, 0, 0, 0, 0}; //Step size R, Step size G, Step size B, fR, fG, fB
+    //Start R, Start G, Start B, fR, fG, fB, cycle, how many cycles
   }
   normal_setup();
   animation_step_time = 500;
 }
+
+void setup_chosen(int lamps1, int lamps2, CRGB newColor, boolean doFade = false, int cycles = 20, int cycleLength = 100) {
+  animation_step_time = cycleLength;
+  animation_var_1 = (int) doFade;
+  animation_i = 0;
+  doing_animation = false;
+  if (doFade) {
+    animation_table_1[0][0] = lamps1;
+    animation_table_1[0][1] = lamps2;
+    animation_table_1[0][2] = newColor.r;
+    animation_table_1[0][3] = newColor.g;
+    animation_table_1[0][4] = newColor.b;
+    animation_table_1[0][5] = cycles;
+    animation_table_1[0][6] = leds[lamps1].r;
+    animation_table_1[0][7] = leds[lamps1].g;
+    animation_table_1[1][0] = leds[lamps1].b;
+  } else {
+    for (int i = lamps1; i < lamps2; i++) {
+      leds[i] = newColor;
+    }
+    FastLED.show();
+  }
+}