Proccessing command with differents arguments

Hello guys,

I'm currently working on a project, basically it is and arduino connected with a esp-8266 to receive command over tcp and then use it to send code with and IR Led. I'll use it as an universal remote for all my devices.

For now I got everything working. On boot in my setup, I send command over serial to my esp-8266 to connect to a wifi and open a tcp server to receive command, then in my loop, when I received something on my Esp-8266 it send it to my Arduino. I'll already have a proccess command sketch that I can use to receive one char command with one or multiple arguments.

But Now I want to make the main sketch the easier to do and modify when I'll add different remote. I'm looking to build a table to match an argument to a Ir code output, so I won't have to make a different case for each possible buttons.

Any suggestion is welcome, thank you guys

ze3one:
Any suggestion is welcome

Give you two guesses …

1 …

2 …

Answer

How are we supposed to know what the problem might be when you have not posted your program.

And please use the code button </> so your code looks like this and is easy to copy to a text editor

…R

Where do you want to put the 'smarts' in the system? Do you want the Arduino to remember the complex codes for every piece of equipment so you can just send the "p" command to it, or do you want to have the PC remember those codes so it sends a long string of bits to the Arduino? It depends on which one is easier to modify in the future.

MorganS:
Where do you want to put the ‘smarts’ in the system? Do you want the Arduino to remember the complex codes for every piece of equipment so you can just send the “p” command to it, or do you want to have the PC remember those codes so it sends a long string of bits to the Arduino? It depends on which one is easier to modify in the future.

I’ll prefer the Arduino to remember the complex codes, So on the other end I simply send something like (" Char 123 ") where char would be assign to a identify a device I want to use and then 123 would match to a IR hex code from a table made for that device. So I would build a table for each remote I have.

I’m already able to make something of a switch case, but I would have so many cases.

Here’s an idea of some structure, run it with different values for “deviceName” and watch the serial monitor. Not sure how your code is structured but maybe this will give you some ideas.

// definition of one device
typedef struct device {
  char name[10];          // name of device
  // ir codes
  unsigned int powerOnCode;
  unsigned int volumeUpCode;
  unsigned int volumeDownCode;

};

// all device devinitions
device deviceList[] = { 
  "tv", 0x0, 0x1, 0x3, "reciever", 0x4, 0x5, 0x6, "dvr", 0x7, 0x8, 0x9};

byte deviceCnt = sizeof(deviceList)/sizeof(deviceList[0]); // # of devices

void setup()
{
  Serial.begin(9600);
}

// return index for passed device
int setDevice(char *dName)
{
  int retVal = (-1);

  for(int i=0; i < deviceCnt; i++)
  {
    // if a match
    if( !strcmp(dName, deviceList[i].name) )
    {
      // set return value
      retVal = i;

      // stop looking
      break;

    } // if

  } // for

  return(retVal);
}

// power device on
void powerOn(int idx)
{
  Serial.print("Turning on  ");
  Serial.println(deviceList[idx].name);
  Serial.print("Code is  "); 
  Serial.println(deviceList[idx].powerOnCode);
}

// volume up
void volumeUp(int idx)
{
  Serial.print("Volume up on  ");
  Serial.println(deviceList[idx].name);
  Serial.print("Code is  "); 
  Serial.println(deviceList[idx].volumeUpCode);  
}


// volume down
void volumeDown(int idx)
{
  Serial.print("Volume down on  ");
  Serial.println(deviceList[idx].name);
  Serial.print("Code is  "); 
  Serial.println(deviceList[idx].volumeDownCode);  
}

void loop()
{
  char *deviceName = "dvr";  // could be "tv", "dvr", "reciever"

  // set index for device
  int dIdx = setDevice(deviceName);

  if( dIdx >= 0 )
  {

    powerOn(dIdx);
    volumeUp(dIdx);
    volumeDown(dIdx);

  } // if

  delay(3000);

}

That's look like it should work, And on time because I just start to learn how use struct and typedef this week. I had to make a kind of software SPI communication without using SPI pin at school to write and read on different adresses of a FPGA board. It was fun :)

I never thought to make an array out of a struct. So I would use one pattern that include all my buttons to keep them with the same index for every device and simply declare them as NULL if the button doesn't exist on that remote.

Correct me if I'm wrong

// definition of one device
typedef struct device {
  char name[10];          // name of device
  // ir codes
  unsigned int powerOnCode;
  unsigned int volumeUpCode;
  unsigned int volumeDownCode;
  unsigned int tvGuideCode;      // only one remote use this one
};

// all device devinitions
device deviceList[] = {
  "tv", 0x0, 0x1, 0x3, NULL,     // tvGuideCode will not be use, but still need to be declare
  "Bluray", 0x4, 0x5, 0x6, NULL, // tvGuideCode will not be use, but still need to be declare
  "cbl", 0x7, 0x8, 0x9, 0xF      // tvGuideCode will be use
  };