ソースを参照

Not working -> probably crashing.
Also the functions listed in animationPonter and setupPointer might be shared for every Animations object and not only the local object.

Emil 3 年 前
コミット
3fd590628c
6 ファイル変更130 行追加56 行削除
  1. 62 0
      src/Animations.cpp
  2. 38 0
      src/Animations.h
  3. 15 45
      src/LightCluster.cpp
  4. 4 6
      src/LightCluster.h
  5. 4 3
      src/main.cpp
  6. 7 2
      src/main.h

+ 62 - 0
src/Animations.cpp

@@ -0,0 +1,62 @@
+//
+// Created by emilr on 2022-05-07.
+//
+
+#include "Animations.h"
+
+Animations::Animations(struct light *lights) {
+    Serial.println("Inside animations constructor");
+    delay(100);
+    this->lights = lights;
+    Serial.println("Animations constructed");
+    delay(100);
+}
+
+/*void Animations::createLookup() {
+    Serial.println("Creating tables");
+    delay(100);
+    //animationPointer[1] = {this->rainbow};
+    //setupPointer[1] = {this->setupRainbow};
+}*/
+
+void Animations::runAnimation(int animationID) {
+    method_function func = animationPointer[animationID];
+    (this->*func)();
+
+    //if (animationID == 0) {
+    //    rainbow();
+    //}
+}
+
+void Animations::runSetup(int animationID) {
+    Serial.println("Inside setup");
+    method_function func = setupPointer[animationID];
+    (this->*func)();
+    //if (animationID == 0) {
+    //    setupRainbow();
+    //}
+}
+
+void Animations::rainbow() {
+    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 Animations::setupRainbow() {
+    animationI = 0;
+    delayTimeMS = 100;
+    maxAnimationI = 256;
+    animationSetting1 = 65536/(numLeds+1);
+}
+

+ 38 - 0
src/Animations.h

@@ -0,0 +1,38 @@
+//
+// Created by emilr on 2022-05-07.
+//
+
+#include "main.h"
+
+//#ifndef UNTITLED_ANIMATIONS_H
+//#define UNTITLED_ANIMATIONS_H
+
+class Animations {
+public:
+    int animationI;
+    int maxAnimationI;
+    int delayTimeMS;
+    int animationSetting1;
+    int animationSetting2;
+    int numLeds;
+
+    explicit Animations(struct light *lights);
+    void runAnimation(int animationId);
+    void runSetup(int animationId);
+
+    //void createLookup();
+
+    typedef void (Animations::*method_function)();
+    method_function animationPointer[1] = {&Animations::rainbow};
+    method_function setupPointer[1] = {&Animations::setupRainbow};
+
+    struct light *lights;
+
+
+private:
+    void rainbow();
+    void setupRainbow();
+};
+
+
+//#endif //UNTITLED_ANIMATIONS_H

+ 15 - 45
src/LightCluster.cpp

@@ -5,23 +5,10 @@
 #include "LightCluster.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);
+        animationObject.runAnimation(animationNumber);
         lastRun = millis();
 
         for (int i = 0; i < numLights; i++) {
@@ -29,7 +16,7 @@ void LightCluster::runAnimation() {
 //            Serial.println(lights[i].mapped);
 //            Serial.print("color = ");
 //            Serial.println(lights[i].color, HEX);
-            leds.setPixelColor(lights[i].mapped, lights[i].color);
+            leds.setPixelColor(animationObject.lights[i].mapped, animationObject.lights[i].color);
         }
         //Serial.println("Animation has run");
         //return true;
@@ -38,7 +25,9 @@ void LightCluster::runAnimation() {
 }
 
 void LightCluster::runSetup() {
-    setup_table[animationNumber]();
+    Serial.println("Before setup");
+    animationObject.runSetup(animationNumber);
+    Serial.println("After setup");
 }
 
 void LightCluster::changeAnimation(int newAnimationNumber) {
@@ -47,51 +36,32 @@ void LightCluster::changeAnimation(int newAnimationNumber) {
     runAnimation();
 }
 
-LightCluster::LightCluster(struct light *incomingLights, int size, int animation) {
+LightCluster::LightCluster(struct light *incomingLights, int size, int animation, Animations animationObject)
+        : animationObject(animationObject) {
 //    for (int i = 0; i < sizeof(lights); i++) {
 //        light data;
 //        data.mapped = lights[i];
 //        this->lights[i] = data;
 //    }
+    Serial.println("Inside light cluster constructor");
     animationNumber = animation;
     numLights = size;
-    numLeds = size;
-    lights = incomingLights;
     lastRun = 0;
-
+    Serial.println("About to set up animation object");
+    this->animationObject = animationObject;
+    Serial.println("Creating lookup");
+    //this->animationObject.createLookup();
+    Serial.println("Construct done, running setup and animation");
     runSetup();
     runAnimation();
 }
 
 bool LightCluster::shouldRun() {
     //Serial.println("ShouldRun started");
-    if (millis() - lastRun >= delayTimeMS) {
+    if (millis() - lastRun >= animationObject.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);
-}
+}

+ 4 - 6
src/LightCluster.h

@@ -2,14 +2,12 @@
 // Created by emilr on 2022-04-13.
 //
 #include "stdint.h"
+#include "Animations.h"
+#include "main.h"
 
 #ifndef UNTITLED_LIGHTCLUSTER_H
 #define UNTITLED_LIGHTCLUSTER_H
 
-struct light {
-    int mapped;
-    uint32_t color;
-};
 
 class LightCluster {
 private:
@@ -25,9 +23,9 @@ public:
     void runAnimation();
     void changeAnimation(int newAnimationNumber);
 
-    LightCluster(struct light *incomingLights, int size, int animation);
+    LightCluster(struct light *incomingLights, int size, int animation, Animations animationObject);
 
-    struct light *lights;
+    Animations animationObject;
 };
 
 

+ 4 - 3
src/main.cpp

@@ -20,7 +20,7 @@ Adafruit_NeoPixel leds(NUM_LEDS, LED_PIN, NEO_WRGB + NEO_KHZ800);
 //LightCluster clusters[] = {};
 light lampsInCluster1[7] = {{0,0},{1,0},{3,0},{4,0},{5,0},{6,0},{7,0}};
 
-LightCluster myCluster(lampsInCluster1, 7, 0);
+LightCluster myCluster(lampsInCluster1, 7, 0, Animations(lampsInCluster1));
 
 void connectWiFi() {
     WiFi.mode(WIFI_STA);
@@ -75,7 +75,7 @@ void setup() {
     // write your initialization code here
     Serial.begin(115000);
 
-    connectSDCard();
+    /*connectSDCard();
     Serial.println("\nSD card connected. Connecting WiFi\n");
     delay(250);
 
@@ -89,9 +89,10 @@ void setup() {
     Serial.println("\nServer started. Connecting to IR Receiver\n");
     delay(250);
 
-    IrReceiver.begin(IR_RECEIVE_PIN, false);
+    IrReceiver.begin(IR_RECEIVE_PIN, false);*/
     Serial.println("\nIR Receiver connected. Starting LEDS\n");
 
+    //LightCluster myCluster(lampsInCluster1, 7, 0, Animations(lampsInCluster1));
 
     delay(1000);
     leds.begin();

+ 7 - 2
src/main.h

@@ -7,9 +7,14 @@
 #define LED_PIN    4
 #define NUM_LEDS 10
 
-extern Adafruit_NeoPixel leds;
-
 #ifndef UNTITLED_MAIN_H
 #define UNTITLED_MAIN_H
 
+extern Adafruit_NeoPixel leds;
+
+struct light {
+    int mapped;
+    uint32_t color;
+};
+
 #endif //UNTITLED_MAIN_H