本文介绍了Arduino的 - 到C数组迭代效率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数组:

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); ??
  }
}

不太清楚,我意思是用操作ç是诚实的。

由于睡眠不足,我不能为我的生活做出以下文档感对 PROGMEM

Due to lack of sleep, I can't for the life of me make sense of the following documentation on 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数组迭代效率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 15:09