Przeglądaj źródła

Web server changes animation and new functional animation registration.

jediemil 5 lat temu
rodzic
commit
e5919d3129
1 zmienionych plików z 113 dodań i 6 usunięć
  1. 113 6
      LedThing.ino/LedThing.ino.ino

+ 113 - 6
LedThing.ino/LedThing.ino.ino

@@ -7,6 +7,26 @@
 char ssid[] = "Pannkakshuset";
 char pass[] = "lavalampa";
 
+int current_animation = 0;
+int loops_since_http = 0;
+
+int animation_step_time = 0;
+int animation_i = 0;
+int animation_var_1 = 0;
+
+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
+
+void (*animation_table[])() = {animation_rainbow, animation_running};
+
+extern void setup_rainbow(); // forward declaration
+extern void setup_running(); // forward declaration
+
+void (*setup_table[])() = {setup_rainbow, setup_running};
+
 CRGB leds[NUM_LEDS];
 
 int status = WL_IDLE_STATUS;
@@ -38,9 +58,21 @@ void setup() {
   FastLED.clear();
   FastLED.show();
   delay(1000);
+  setup_table[current_animation]();
 }
 
 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]();
+    last_animation_millis = current_millis;
+  } else {
+    loops_since_http = 0;
+    //Serial.println("Waiting for animation");
+    process_HTTP();
+    delay(1);
+  }
   // put your main code here, to run repeatedly:
   //Serial.println("Hello");
   //delay(1000);
@@ -48,13 +80,88 @@ void loop() {
 //  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();
+  if (client) {
+    int anim = 0;
+    Serial.println("new client");
+    // an http request ends with a blank line
+    boolean foundSlash = false;
+    while (client.connected()) {
+      if (client.available()) {
+        char c = client.read();
+        //Serial.write(c);
+        if (foundSlash) {
+          anim = c - 'A';
+          client.println("HTTP/1.1 200 OK");
+          break;
+        }
+        if (c == '/') {
+          foundSlash = true;
+        }
+      }
+    }
+    // give the web browser time to receive the data
+    delay(1);
+
+    // close the connection:
+    client.stop();
+    Serial.println("client disconnected");
+    setup_table[anim]();
+  }
+}
+
+void clear_strip() {
   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 animation_rainbow() {
+  fill_rainbow(leds, NUM_LEDS, animation_i, animation_var_1);
+  FastLED.show();
+  animation_i++;
+  animation_i %= 260;
+  //Serial.println(animation_i);
+  //Serial.println("Animation 1 step");
+}
+
+void animation_running() {
+  //Serial.println("Animation 2 step");
+  leds[animation_i] = CRGB(100, 255, 200);
+  FastLED.show();
+  animation_i++;
+  if (animation_i >= 49) {
+    animation_i = 0;
+    clear_strip();
   }
 }
+
+void setup_rainbow() {
+  Serial.println("Animation 1 setup");
+  clear_strip();
+  animation_step_time = 10;
+  animation_i = 0;
+  animation_var_1 = 10;
+  current_animation = 0;
+}
+
+void setup_running() {
+  Serial.println("Animation 2 setup");
+  clear_strip();
+  animation_step_time = 100;
+  animation_i = 0;
+  animation_var_1 = 0;
+  current_animation = 1;
+}