我有以下数组:
PROGMEM prog_uint16_t show_hide_info[] = { 4216, 8900, 4380, 580, 500, 600, 500, 580, 1620, 580, 500, 600, 500, 580, 500, 600, 480, 600, 500, 580, 1620, 580, 1620, 600, 500, 580, 1620, 580, 1620, 600, 1600, 600, 1620, 580, 1620, 600, 500, 580, 1620, 580, 500, 600, 1600, 600, 500, 580, 1620, 580, 500, 600, 1620, 580, 1620, 600, 480, 600, 1620, 580, 500, 600, 1600, 600, 500, 580, 1620, 580, 500, 600, 39300, 8860, 2160, 580, 0 };
我希望能够循环遍历这个数组并相应地执行以下方法:
pulseIR(4216);
delayMicroseconds(8900);
pulseIR(4380);
delayMicroseconds(580);
...
这是我到目前为止所拥有的,这显然偏离了轨道:
unsigned int* get(prog_uint16_t code[]) {
unsigned int c;
while ((c = pgm_read_word(code++))) {
//pulseIR(c); ??
//delayMicroseconds(c+1); ??
}
}
说实话,不太确定我要用 c
做什么。
由于缺乏 sleep ,我一生都无法理解以下有关 PROGMEM
的文档:
最佳答案
首先,您需要一些速记来找到数组的末尾。如果它的值的数量是固定的,那么编译器可以通过这种方式计算它以找到 int16 值的数量:
PROGMEM prog_uint16_t show_hide_info[] = { 4216, 8900, 4380, 580, ....etc
int arraySize = sizeof(show_hide_info) / sizeof(prog_uint16_t);
然后,如果您想让一个函数仅遍历数组一次,则可以这样声明该函数:
void cycleThruArrayOnce(prog_uint16_t *pInArray, int nLimit) {
for (int i=0;i<nLimit;i=i+2) {
pulseIR(pgm_read_word(pInArray++));
delayMicroseconds(pgm_read_word(pInArray+));
}
}
并且可以通过以下方式从主程序调用它:
cycleThruArrayOnce(show_hide_info, arraySize);
关于arrays - Arduino - 高效地迭代 C 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8939937/