arrays - Arduino - 高效地迭代 C 数组

标签 arrays arduino

我有以下数组:

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 的文档:

http://arduino.cc/en/Reference/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/

相关文章:

c++ - 错误消息 : Serial communication between c++ GUI and Arduino

c - 替代 memcpy

c++ - 常量函数参数作为静态数组大小?

C Linked List char数组输入重用问题

c++ - 循环在此代码中仅迭代一次,而在另一代码中它正确迭代

c - Makefile 无法将代码刷新到 ATmega328p

arrays - Arduino PROGMEM 字节数组

javascript - 将字符串拆分为两个变量 - 未定义不是一个函数

c++ - C++ C样式数组的大小限制

json - Arduino JSON 包 - 格式化传感器值