我想我失去了我的全部 C++ 知识......
我想要的是以正确(可读)的方式初始化 2D unsigned char 数组:我的方法:
#define RADIO_ONOFF 0
#define RADIO_P1 1
#define RADIO_P2 2
...
#define NR_OF_CODES = 5
#define CODE_LENGTH = 10
#1:
unsigned char** codes = new unsigned char*[NR_OF_CODES];
codes[RADIO_ONOFF] = new unsigned char[CODE_LENGTH]{ 9,180,88,11,33,4,0,255,64,191 }; // does not work
...
#2:
unsigned char ICODES[NR_OF_CODES][CODE_LENGTH];
ICODES[RADIO_ONOFF] = { 9,180,88,11,33,4,0,255,64,191 }; // same as above
...
#3:
class Test {
private:
unsigned char data[CODE_LENGTH];
public:
Test(unsigned char a1, unsigned char a2, unsigned char a3, unsigned char a4, unsigned char a5, unsigned char a6, unsigned char a7, unsigned char a8, unsigned char a9, unsigned char a10);
unsigned char* getData(void);
};
Test::Test(unsigned char a1, unsigned char a2, unsigned char a3, unsigned char a4, unsigned char a5, unsigned char a6, unsigned char a7, unsigned char a8, unsigned char a9, unsigned char a10) {
this->data[0] = a1;
this->data[1] = a2;
this->data[2] = a3;
this->data[3] = a4;
this->data[4] = a5;
this->data[5] = a6;
this->data[6] = a7;
this->data[7] = a8;
this->data[8] = a9;
this->data[9] = a10;
}
unsigned char* Test::getData(void) {
return data;
}
void setup() {
test[RADIO_ONOFF] = new Test( 9,180,88,11,33,4,0,255,64,191 );
test[RADIO_P1] = new Test( 9,180,88,11,33,4,0,255,64,192 );
...
}
#4
const unsigned char RADIO_ONOFF[] = { 9,180,88,11,33,4,0,255,64,191 };
const unsigned char RADIO_P1[] = { 9,180,88,11,33,4,0,255,64,192 };
...
我收到#1 和#2 的错误消息:(代码应该为 Arduino 编译,它需要一个设置函数)
在函数'void setup()'中:revTest:58:错误:'{'令牌revTest:58之前的预期主表达式:错误:预期';' '{' 标记之前
好的 - 我的问题:
对我来说 - #3 和 #4 很好读。#3 的努力是最高的 - 但我认为如果我想在 switch 语句中使用数组,这是最快的方法。- 真的?
我认为#1和#2中的数组初始化应该这样工作????
非常Arduino特定:
我不确定必须在 setup() 内部定义什么以及应该在 setup() 之外定义什么。静态初始化和全局外部,动态内部还是什么?
我对 Arduino 的 PROGMEM 很感兴趣——我认为在这种情况下不值得付出努力。我是对的?(我想我会有大约 50 个不同的代码......)
谢谢!