-1

I'm currently stuck with a problem, related to an IR remote controlled using an Arduino Uno.

I'm having the raw data signal contained in an array:

Samsung_power[68] = {4500, 243, .... and so on};

Due to the fact, that there are quite a lot of functions on a Samsung remote control, I would find it a lot easier, than when I receive a command from UART, like a value ranging from 0 - 20, then the number would be looked up in a table and the appropiate raw data array would be chosen.

So:

FunctionArray[20] = {Samsung_power, Channel_1, Channel_2, Channel_3.. etc};

However, the compiler is by no chance letting me do so, and so I can imagine something is completely wrong here :). So I was hoping some of you had an idea, how to solve this particular problem:

PSEUDOCODE:

receive = UART_READ();
sendRawDataToIRLED(FunctionArray[receive]);
nmichaels
  • 49,466
  • 12
  • 107
  • 135

4 Answers4

3

If you don`t want to declare an auxiliary struct, you need at least an array to store the sizes of the data arrays, and pass such length to the function. You can conveniently use sizeof to ensure they are correctly computed. I have tried the following code in arduino and it builds:

#include "Arduino.h"

//bii:#entry_point()
void setup(){
}
void sendRawDataToIRLED(int array[], int len){
//your code here
}
void loop()
{
    int Samsung_power[] = {4500, 243, 23};
    int Channel_1[] = {450, 23, 233, 44, 55};
    int* FunctionArray[2] = {Samsung_power, Channel_1};
    int sizeArray[] = {sizeof(Samsung_power)/sizeof(int), sizeof(Channel_1)/sizeof(int)};
    int index = 0;//whatever your index
    sendRawDataToIRLED(FunctionArray[index], sizeArray[index]);
}
drodri
  • 5,157
  • 15
  • 21
  • Sounds like a good solution :) Going to try it as soon as I can :) Thanks a lot! – user3008529 Feb 03 '14 at 18:35
  • This did actually work. Hoever, I can't have more than 9 commands in the array, before I start to get some weird output. Probably cause of running out of SRAM memory, just odd that only one single Serail.print in the entire program, can use up 2048 bytes of ram, when the array exceeds 9 commands :/ – user3008529 Feb 04 '14 at 11:47
  • But how long are your individual commands? If just some elements, that should not be a problem. In any case, if your commands are actually constant, probably the best is to get them out of the loop function, to global scope, so they are not redefined at each loop iteration. – drodri Feb 05 '14 at 11:34
1

you CAN'T do an array of function, BUT you can do an array of POINTER TO FUNCTION. but the function in the array must have the same input parameter.

see How can I use an array of function pointers?

Community
  • 1
  • 1
Lesto
  • 2,260
  • 2
  • 19
  • 26
0

Define a struct that has as its first field an array that will hold your Samsung_power in an array in the struct.

Martin Dinov
  • 8,757
  • 3
  • 29
  • 41
0

The syntax for declaring an array of arrays in C is pretty straightforward:

int twodee[OUTER_SIZE][INNER_SIZE];

Lookups are similarly simple:

twodee[3]; // This is the 4th INNER_SIZE-element array in the table.
           // It's of type int[INNER_SIZE].

If all the arrays in your lookup table are the same size, use that. If they're not, you'll need a way to point to different-sized arrays with your table. The best way to do that is with structs:

struct array
{
    int len;
    int data[];
};

struct array *table[NUMBER_OF_ARRAYS];

That way, you can fill your table with pointers to arrays and keep track of the sizes of the arrays being pointed to. To assign a struct array a to the lookup table, just do this:

table[n] = &a;
nmichaels
  • 49,466
  • 12
  • 107
  • 135
  • They all are of the same size, so I assume a 2D table is more suitable in this case, just as you described. Definitely going to try this :) Thanks for the help! – user3008529 Feb 03 '14 at 18:36