c++ 用矢量填充类对象数组-- DMX夹具剖面图

nc1teljy  于 2023-02-10  发布在  其他
关注(0)|答案(1)|浏览(105)

我写一个程序来控制不同的灯光与DMX协议。每个灯有不同的通道来控制不同的方面(如强度,色温,颜色等)。
为此,我想创建一个简单的方法,把每个灯的配置文件放在一起。
这是我目前的成果我在和Arduino Due合作
当我在初始化后输出值时,它只输出0,有人能帮我解释一下,我做错了什么吗?或者有更好的方法吗?

#include <vector>

struct Channel {
  String name;
  String unit;
  int minValue;
  int maxValue;
  int color;
  int dmxChannels;
  int sliderChannels;  

}; Channel AladdinBiColor[2], AladdinRGB[6];

class Profile {
  public:
  std::vector<Channel> channels;
  int dmxChannels;
  int sliderChannels;
  String name[];
  String unit[];
  int minValue[];
  int maxValue[];
  int color[];

  Profile(Channel* input, int count){
    for (int i=0; i<count; i++){
      channels.push_back(*input);
      input++;
    }
  }
};

Profile AladdinBiColor_fixture(AladdinBiColor, 2);
Profile AladdinRGB_fixture(AladdinRGB, 6);

在设置中,我调用这个函数:

void setup(){

   void initializeProfiles();
   Serial.println(AladdinBiColor_fixture.channels[0].maxValue);
}

这将输出0
看起来像这样。它初始化数组。

void initializeProfiles(){
  AladdinBiColor[0] = {"INT","%", 0, 100, WHITE,2,2};
  AladdinBiColor[1] = {"INT","%", 0, 100, WHITE,2,2};

  AladdinRGB[0] = {"INT","%", 0, 100, WHITE,2,2};
  AladdinRGB[1] = {"INT","%", 0, 100, WHITE,2,2};
  AladdinRGB[2] = {"CF","%", 0, 100, WHITE,2,2};
  AladdinRGB[3] = {"RED","%", 0, 100, RED,2,2};
  AladdinRGB[4] = {"GREEN","%", 0, 100, GREEN,2,2};
  AladdinRGB[5] = {"BLUE","%", 0, 100, BLUE,2,2};
}
hof1towb

hof1towb1#

所以问题是初始化发生在Profile对象创建之后,我重新排列了顺序,并创建了一个指向Profile对象的数组,这样就可以轻松访问它。

#include <vector>

struct Channel {
  String name;
  String unit;
  int minValue;
  int maxValue;
  int color;
}; 

Channel AladdinBiColor[2] = {
  {"INT","%", 0, 100, WHITE},
  {"CCT","K", 0, 100, WHITE}
};

Channel AladdinRGB[6] = {
  {"INT","%", 0, 100, WHITE},
  {"CCT","K", 0, 100, WHITE},
  {"CF","%", 0, 100, WHITE},
  {"RED","%", 0, 100, RED},
  {"GREEN","%", 0, 100, GREEN},
  {"BLUE","%", 0, 100, BLUE}
};

class Profile {
  public:
  std::vector<Channel> channels;
  int dmxChannels;
  int sliderChannels;
  
  Profile(Channel* input, int count, int dmxChannelsA){
    for (int i=0; i<count; i++){
      channels.push_back(*input);
      input++;
    }
    dmxChannels = dmxChannelsA;
    sliderChannels = count;
  }
};

Profile AladdinBiColor_fixture(AladdinBiColor, 2,2);
Profile AladdinRGB_fixture(AladdinRGB, 6, 5);

Profile *lightProfiles[1][3] = { //index 1 = brand, index 2 = light
  {&AladdinRGB_fixture, &AladdinBiColor_fixture, &AladdinRGB_fixture}
};

这样-在我的主文件中-我可以用索引号访问Profile objects

void setup(){
  // first parameter of lightProfiles is the brand, second the light of it
  Serial.println(lightProfiles[0][1]->channel[0].maxValue);

}

相关问题