Переглянути джерело

New animation and arrow http request

jediemil 5 роки тому
батько
коміт
aa957fc8b5
1 змінених файлів з 52 додано та 6 видалено
  1. 52 6
      LedThing.ino/LedThing.ino.ino

+ 52 - 6
LedThing.ino/LedThing.ino.ino

@@ -4,6 +4,7 @@
 
 #define NUM_LEDS 50
 #define DATA_PIN 9
+int NUMBER_OF_ANIMATIONS = 3;
 char ssid[] = "Pannkakshuset";
 char pass[] = "lavalampa";
 
@@ -19,13 +20,15 @@ long last_animation_millis = millis();
 //typedef enum {STATE_A = 0, STATE_B = 1} State_type;
 extern void animation_rainbow(); // forward declaration
 extern void animation_running(); // forward declaration
+extern void animation_chasing();
 
-void (*animation_table[])() = {animation_rainbow, animation_running};
+void (*animation_table[])() = {animation_rainbow, animation_running, animation_chasing};
 
 extern void setup_rainbow(); // forward declaration
 extern void setup_running(); // forward declaration
+extern void setup_chasing();
 
-void (*setup_table[])() = {setup_rainbow, setup_running};
+void (*setup_table[])() = {setup_rainbow, setup_running, setup_chasing};
 
 CRGB leds[NUM_LEDS];
 
@@ -41,6 +44,7 @@ void setup() {
     ; // wait for serial port to connect. Needed for native USB port only
   }
   //pinMode(DATA_PIN, OUTPUT);
+  boolean first_check = true;
   while (status != WL_CONNECTED) {
     Serial.print("Attempting to connect to SSID: ");
     Serial.println(ssid);
@@ -48,7 +52,12 @@ void setup() {
     status = WiFi.begin(ssid, pass);
 
     // wait 10 seconds for connection:
-    delay(10000);
+    if (first_check) {
+      delay(1000);
+      first_check = false;
+    } else {
+      delay(10000);
+    }
   }
   server.begin();
   
@@ -104,8 +113,25 @@ void process_HTTP() {
         char c = client.read();
         //Serial.write(c);
         if (foundSlash) {
-          anim = c - 'A';
+          if (c == '(') {
+            anim = current_animation - 1;
+            if (anim < 0) {
+              anim = NUMBER_OF_ANIMATIONS - 1;
+            }
+          } else if (c == ')') {
+            anim = current_animation + 1;
+          } else {
+            anim = c - 'A';
+          }
+          if (anim < 0) {
+              anim = -anim;
+            }
+          anim %= NUMBER_OF_ANIMATIONS;
           client.println("HTTP/1.1 200 OK");
+          client.println("Content-Type: application/json");
+          client.println("Connection: close");  // the connection will be closed after completion of the response
+          client.println();
+          client.println("{}");
           break;
         }
         if (c == '/') {
@@ -118,7 +144,7 @@ void process_HTTP() {
 
     // close the connection:
     client.stop();
-    Serial.println("client disconnected");
+    //Serial.println("client disconnected");
     setup_table[anim]();
   }
 }
@@ -148,10 +174,21 @@ void animation_running() {
   }
 }
 
+void animation_chasing() {
+  //Serial.println("Animation 2 step");
+  clear_strip();
+  leds[animation_i] = CRGB(100, 255, 200);
+  leds[(animation_i + 10) % 50] = CRGB(100, 255, 200);
+  leds[(animation_i + 20) % 50] = CRGB(100, 255, 200);
+  FastLED.show();
+  animation_i++;
+  animation_i %= 50;
+}
+
 void setup_rainbow() {
   Serial.println("Animation 1 setup");
   clear_strip();
-  animation_step_time = 10;
+  animation_step_time = 100;
   animation_i = 0;
   animation_var_1 = 10;
   current_animation = 0;
@@ -165,3 +202,12 @@ void setup_running() {
   animation_var_1 = 0;
   current_animation = 1;
 }
+
+void setup_chasing() {
+  Serial.println("Animation 3 setup");
+  clear_strip();
+  animation_step_time = 100;
+  animation_i = 0;
+  animation_var_1 = 0;
+  current_animation = 2;
+}