Jelajahi Sumber

Clusters will set up and 1 animation is working

Emil 3 tahun lalu
induk
melakukan
882838fc93
6 mengubah file dengan 114 tambahan dan 90 penghapusan
  1. 0 30
      src/Animations.cpp
  2. 0 27
      src/Animations.h
  3. 74 10
      src/LightCluster.cpp
  4. 14 14
      src/LightCluster.h
  5. 23 8
      src/main.cpp
  6. 3 1
      src/main.h

+ 0 - 30
src/Animations.cpp

@@ -1,30 +0,0 @@
-//
-// Created by emilr on 2022-04-13.
-//
-
-#include "Animations.h"
-#include "main.h"
-
-int * Animations::runAnimationByNumber(int animation) {
-
-};
-int * Animations::setupAnimationByNumber(int animation) {
-
-};
-
-
-Animations::Animations(int *setupLights) {
-
-}
-
-
-
-void Animations::rainbowFill() {
-    for (int light = 0; light < numLights; light++) {
-        lights[light] = leds.ColorHSV(((light * 360 / 10) + animationI) * 360, 255, 255);
-    }
-}
-
-void Animations::setupRainbowFill() {
-    animationDelayMS = 100;
-}

+ 0 - 27
src/Animations.h

@@ -1,27 +0,0 @@
-//
-// Created by emilr on 2022-04-13.
-//
-
-#ifndef UNTITLED_ANIMATIONS_H
-#define UNTITLED_ANIMATIONS_H
-
-
-class Animations {
-public:
-    int * runAnimationByNumber(int animation);
-    int * setupAnimationByNumber(int animation);
-
-    int animationDelayMS;
-
-    Animations(int setupLights[]);
-private:
-    void rainbowFill();
-    void setupRainbowFill();
-
-    int animationI;
-    int numLights;
-    int lights[];
-};
-
-
-#endif //UNTITLED_ANIMATIONS_H

+ 74 - 10
src/LightCluster.cpp

@@ -3,31 +3,95 @@
 //
 
 #include "LightCluster.h"
-#include "Animations.h"
+#include "main.h"
+
+int animationI;
+int maxAnimationI;
+int delayTimeMS;
+int animationSetting1;
+int animationSetting2;
+int numLeds;
+
+extern void rainbow(light lights[]);
+
+extern void setupRainbow();
+void (*animationTable[])(light lights[]) = {rainbow};
+void (*setup_table[])() = {setupRainbow};
 
 void LightCluster::runAnimation() {
+    if (shouldRun()) {
+        //Serial.println("Animation begin");
+        animationTable[animationNumber](lights);
+        lastRun = millis();
 
+        for (int i = 0; i < numLights; i++) {
+//            Serial.print("lights mapped = ");
+//            Serial.println(lights[i].mapped);
+//            Serial.print("color = ");
+//            Serial.println(lights[i].color, HEX);
+            leds.setPixelColor(lights[i].mapped, lights[i].color);
+        }
+        //Serial.println("Animation has run");
+        //return true;
+    }
+    //return false;
 }
 
 void LightCluster::runSetup() {
-
+    setup_table[animationNumber]();
 }
 
-void LightCluster::changeAnimation() {
-
+void LightCluster::changeAnimation(int newAnimationNumber) {
+    animationNumber = newAnimationNumber;
+    runSetup();
+    runAnimation();
 }
 
-LightCluster::LightCluster(int *lights, int animation) {
-    for (int i = 0; i < sizeof(lights); i++) {
-        mappedLightTable[i] = lights[i];
-    }
+LightCluster::LightCluster(light *incomingLights, int size, int animation) {
+//    for (int i = 0; i < sizeof(lights); i++) {
+//        light data;
+//        data.mapped = lights[i];
+//        this->lights[i] = data;
+//    }
     animationNumber = animation;
-    numLights = sizeof(lights);
+    numLights = size;
+    numLeds = size;
+    lights = incomingLights;
+
 
-    LocalAnimation = Animations(mappedLightTable);
     runSetup();
+    runAnimation();
 }
 
 bool LightCluster::shouldRun() {
+    //Serial.println("ShouldRun started");
+    if (millis() - lastRun >= delayTimeMS) {
+        //Serial.println("ShouldRun true");
+        return true;
+    }
+    //Serial.println("ShouldRun false");
     return false;
 }
+
+void rainbow(light lights[]) {
+    for (int light = 0; light < numLeds; light++) {
+        lights[light].color = leds.ColorHSV(((light * animationSetting1 + animationI*65536/maxAnimationI) % 65536), 255, 255);
+//        Serial.print("Set ");
+//        Serial.print(light);
+//        Serial.print(" to ");
+//        Serial.println(leds.ColorHSV(((light * animationSetting1 + animationI) * 360), 255, 255));
+//        Serial.println(light * animationSetting1);
+//        Serial.println((light * animationSetting1 + animationI) * 360);
+    }
+    //Serial.print("AnimationI = ");
+    //Serial.println(animationI);
+    animationI += 1;
+    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 setupRainbow() {
+    animationI = 0;
+    delayTimeMS = 100;
+    maxAnimationI = 256;
+    animationSetting1 = 65536/(numLeds+1);
+}

+ 14 - 14
src/LightCluster.h

@@ -1,33 +1,33 @@
 //
 // Created by emilr on 2022-04-13.
 //
-
-#include "Animations.h"
+#include "stdint.h"
 
 #ifndef UNTITLED_LIGHTCLUSTER_H
 #define UNTITLED_LIGHTCLUSTER_H
 
+struct light {
+    int mapped;
+    uint32_t color;
+};
 
 class LightCluster {
+private:
+    long lastRun;
+
+    void runSetup();
+    bool shouldRun();
+
 public:
     int numLights;
     int animationNumber;
 
     void runAnimation();
-    void changeAnimation();
-    bool shouldRun();
-
-    LightCluster(int *lights, int animation);
-
-
+    void changeAnimation(int newAnimationNumber);
 
-private:
-    long lastRun;
-    Animations LocalAnimation;
-
-    void runSetup();
+    LightCluster(light *incomingLights, int size, int animation);
 
-    int mappedLightTable[];
+    light * lights;
 };
 
 

+ 23 - 8
src/main.cpp

@@ -1,13 +1,12 @@
-#include <Arduino.h>
 #include "main.h"
 #include "LightCluster.h"
 
-#define LED_PIN    4
-#define NUM_LEDS 10
-
 Adafruit_NeoPixel leds(NUM_LEDS, LED_PIN, NEO_WRGB + NEO_KHZ800);
 
-LightCluster clusters[] = {};
+//LightCluster clusters[] = {};
+light lampsInCluster1[7] = {{0,0},{1,0},{2,0},{3,0},{4,0},{5,0},{6,0}};
+
+LightCluster myCluster(lampsInCluster1, 7, 0);
 
 void setup() {
     // write your initialization code here
@@ -15,11 +14,17 @@ void setup() {
     delay(1000);
     leds.begin();
     leds.clear();
+
+    Serial.println("Started");
+
+    //int lampsInCluster1[] = {0,1,2,3,4,5,6,7,8,9};
+    //clusters[0] = LightCluster(lampsInCluster1, 0);
+    Serial.println("Cluster made");
+
 }
 
 void loop() {
-
-    leds.clear();
+    /*leds.clear();
     delay(1000);
     for (int i = 0; i < 360; i++) {
         for (int light = 0; light < NUM_LEDS; light++) {
@@ -50,5 +55,15 @@ void loop() {
         }
         leds.show();
         delay(1000);
-    }
+    }*/
+//    Serial.println("In loop");
+//    delay(1000);
+//    Serial.println("In loop2");
+    myCluster.runAnimation();
+    //if (hasRun) {
+      //  Serial.println("Running show");
+      //  leds.show();
+   // }
+   leds.show();
+   delay(1);
 }

+ 3 - 1
src/main.h

@@ -2,7 +2,9 @@
 // Created by emilr on 2022-04-13.
 //
 #include "../.pio/libdeps/esp32doit-devkit-v1/Adafruit NeoPixel/Adafruit_NeoPixel.h"
-
+#include <Arduino.h>
+#define LED_PIN    4
+#define NUM_LEDS 10
 
 extern Adafruit_NeoPixel leds;