【问题标题】:How to use a class in another class in Arduino C++ (FastLED)?如何在 Arduino C++ (FastLED) 的另一个类中使用一个类?
【发布时间】:2020-07-07 12:17:19
【问题描述】:

我正在为我的房间做一个 ledstip 项目。我正在使用arduino来做到这一点。对于这个项目,我想使用 C++,所以我可以使用 OOP。在我的 ledstrips 工作后,我想创建一个 cluster 类,使用 strip 类来控制 LED 灯条的特定部分。我不能让它工作。编译器没有给出错误,使用函数Desk.rgb(0,100,0);后我没有看到任何变化。

这是我的 .h 文件

#include <FastLED.h>
template<class T>
class Cluster {
  public:
    T Strip;
    int first;
    int last;

    Cluster(T Strip, int first, int last) {
      this->Strip = Strip;
      this->first = first;
      this->last = last;
    }
    
    void rgb(int r, int g, int b){
      Strip.rgb( r,  g,  b, first, last);
    }
};

template<byte pin, int AmountOfLeds>
class Strip {
  public:
    CRGB leds[AmountOfLeds];

    void setup() {
      FastLED.addLeds<WS2812, pin, GRB>(leds, AmountOfLeds);
      rgb(0, 0, 0);
    }
    //hole strip
    void rgb(int r, int g, int b) {
      for (int i = 0; i <= AmountOfLeds - 1; i++) {
        this->leds[i] = CRGB(r, g, b);
      }
      FastLED.show();
    }
    //single led
    void rgb(int i, int r, int g, int b) {
      this->leds[i] = CRGB(r, g, b);
      FastLED.show();
    }
    //range
    void rgb(int r, int g, int b, int f, int l) {
      for (int i = f; i <= l; i++) {
        this->leds[i] = CRGB(r, g, b);
      }
      FastLED.show();
    }

    void hsv(int h, int s, int v) {
      for (int i = 0; i <= AmountOfLeds; i++) {
        this->leds[i] = CHSV(h, s, v);
      }
      FastLED.show();
    }
    void hsv(int i, int h, int s, int v) {
      this->leds[i] = CHSV(h, s, v);
      FastLED.show();
    }
    void hsv(int h, int s, int v, int f, int l) {
      for (int i = f; i <= l; i++) {
        this->leds[i] = CHSV(h, s, v);
      }
      FastLED.show();
    }

    void hsvWhiteBalance(int S, int V) {         //S is yellowness, V is brightness
      hsv(15, S, V);
    }

    void rainbow(float V) {
      for (int i = 0; i <= AmountOfLeds; i++) {
        leds[i] = CHSV( float(i) * (255 / float(AmountOfLeds)), 255, V);
      }
      FastLED.show();
    }
    void rainbow(float p, float V) {
      for (int i = 0; i <= AmountOfLeds; i++) {
        leds[i] = CHSV( float(i) * (255.0 / float(AmountOfLeds) * p), 255, V);
      }
      FastLED.show();
    }
};

这是我的 .ino 文件:

#include "LedClasses.h"
Strip<5, 190> DTVC;

Cluster<Strip<5, 190>> Desk(DTVC, 1, 42);

void setup() {
  Serial.begin(9600);   

  DTVC.setup();

  DTVC.hsvWhiteBalance(153, 50);
  Desk.rgb(0,100,0);
  //DTVC.rgb(Desk, 0, 100, 0);
}

提前致谢。

【问题讨论】:

    标签: c++ class arduino arduino-c++


    【解决方案1】:

    不起作用,因为在 Cluster 构造函数中,您通过复制获取 Strip 类。然后,在您的示例中,您有 2 个 Stripe 实例:一个在全局上下文中,一个在 Cluster 中。您在全局上下文中的实例上调用Stripe::setup(它调用FastLED.addLeds)(在FastLED 库中注册Stripe::ledspublic 字段的地址),但稍后您在位于您的内部的实例上调用rgb ClusterStripe::leds 字段不同。

    要快速修复它(虽然不是很干净),您可以重新设计构造函数以接受引用而不是复制:

    class Cluster {
      public:
        T& strip;
        int first;
        int last;
    
        Cluster(T& s, int f, int l): strip(s), first(f), last(l) {}
    

    或者,您可以重新设计您的架构,不要过多地使用模板(您可以使用constexpr 参数代替)。

    【讨论】:

    • 我必须同意 pptaszni 的观点,即模板在这里不是最好的主意。一种对我有用的方法是类继承。基本上创建自己的类,该类继承基类 FastLED 的所有成员。新类(在您的情况下为 Strip)的构造函数可以为您自己的实现定义 LED 的数量和任何其他特定属性
    猜你喜欢
    • 1970-01-01
    • 2019-03-11
    • 2017-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-11
    • 2018-02-04
    • 2021-05-21
    相关资源
    最近更新 更多