问题描述
我有以下数组:
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 };
我希望能够遍历这个数组并相应地执行以下方法:
I'd like to be able to loop through this array and execute the following methods accordingly:
pulseIR(4216);
delayMicroseconds(8900);
pulseIR(4380);
delayMicroseconds(580);
...
这就是我到目前为止所拥有的,这显然偏离了轨道:
This is what I have so far, which is obviously way off track:
unsigned int* get(prog_uint16_t code[]) {
unsigned int c;
while ((c = pgm_read_word(code++))) {
//pulseIR(c); ??
//delayMicroseconds(c+1); ??
}
}
老实说,我不太确定我打算用 c
做什么.
Not quite sure what I'm meant to be doing with c
to be honest.
由于睡眠不足,我终生无法理解有关 PROGMEM
的以下文档:
Due to lack of sleep, I can't for the life of me make sense of the following documentation on PROGMEM
:
http://arduino.cc/en/Reference/PROGMEM
推荐答案
首先,您需要一些简写来找到数组的末尾.如果它是固定数量的值,那么编译器可以通过这种方式计算它以找到 int16 值的数量:
First you'll need some short hand to find the end of the array. If its a fixed number of values, then the compiler can calculate it this way to find the count of int16 values:
PROGMEM prog_uint16_t show_hide_info[] = { 4216, 8900, 4380, 580, ....etc
int arraySize = sizeof(show_hide_info) / sizeof(prog_uint16_t);
那么如果你想要一个函数只通过你的数组一次,这个函数可以这样声明:
Then if you want to have a function that goes thru your array just once, the function can be declared this way:
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+));
}
}
它可以通过这种方式从你的主程序中调用:
And it can be called from your main program this way:
cycleThruArrayOnce(show_hide_info, arraySize);
这篇关于Arduino - 有效地遍历 C 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!