Format data (String?) returned by library

I am using the IRremote.h library which when used with the command

IrReceiver.printIRResultShort(&Serial);

prints a response in the form:

Protocol=NEC Address=0x0 Command=0x3 Raw-Data=0xFC03FF00 32 bits LSB first

I would like to reformat that response to put, for instance, line breaks where the white spaces are or to remove some of the information such as Raw-Data and LSB first. Is there a way to do that?

This function is a kind of debug information. The result cannot be redirected. When you need each piece of data, you need to read it using the usual library calls.

Thanks, but what are the usual library calls you refer to? Looking at IRReceive.cpp.h https://github.com/Arduino-IRremote/Arduino-IRremote/blob/master/src/IRReceive.cpp.h I have found the calls printIRResultminimal and printIRResultAsCVariables.

printIRResultAsCVariables seems interesting, it returns:

uint16_t address = 0x0;
uint16_t command = 0x3;
uint32_t data = 0xFC03FF00;

But how can I then use those individual variables in my script?

Look at how the library does what it does:

void IRrecv::printIRResultAsCVariables(Print *aSerial) {
// Now dump "known" codes
    if (decodedIRData.protocol != UNKNOWN) {

        /*
         * New decoders have address and command
         */
        aSerial->print(F("uint16_t"));
        aSerial->print(F(" address = 0x"));
        aSerial->print(decodedIRData.address, HEX);
        aSerial->println(';');

        aSerial->print(F("uint16_t"));
        aSerial->print(F(" command = 0x"));
        aSerial->print(decodedIRData.command, HEX);
        aSerial->println(';');

        // All protocols have data
        aSerial->print(F("uint32_t data = 0x"));
        aSerial->print(decodedIRData.decodedRawData, HEX);
        aSerial->println(';');
        aSerial->println();
    }
}

Thanks. I noticed how the library does that and it would be easy to modify the library in my IDE, but then if the library were updated my project would break. I naively thought I could somehow use those variables supplied by the library in my project without modifying the library it self. Perhaps I should look for another library.

This should work inside your sketch.

  if (IrReceiver.available())
  {
    IRData *decodedIRDataP = IrReceiver.read();
    if (decodedIRDataP->protocol != UNKNOWN)
    {
      Serial.print(F("uint16_t"));
      Serial.print(F(" address = 0x"));
      Serial.print(decodedIRDataP->address, HEX);
      Serial.println(';');


      Serial.print(F("uint16_t"));
      Serial.print(F(" command = 0x"));
      Serial.print(decodedIRDataP->command, HEX);
      Serial.println(';');


      // All protocols have data
      Serial.print(F("uint32_t data = 0x"));
      Serial.print(decodedIRDataP->decodedRawData, HEX);
      Serial.println(';');
      Serial.println();
    }
  }

but then if the library were updated my project would break.

Then you should make a copy of you library, rename it and include that 'new' library in your project.

I finally figured how to retrieve the variables from the library and use them in my sketch without modifying the library like this:

int address = IrReceiver.decodedIRData.address;
int command = IrReceiver.decodedIRData.command;
String make = getProtocolString(IrReceiver.decodedIRData.protocol);

And then use them in the usual way:

Serial.println(make);
Serial.println(address, HEX);
Serial.println(command, HEX);

steveinaustria:
I finally figured how to retrieve the variables from the library and use them in my sketch without modifying the library like this:

The code posted in Reply #5 does the same thing.

Yes, indeed. My thanks to johnwasser. I hadn't seen his reply and found out how to do this by trial and error. Maybe I should have waited for John's reply, but having found out how to do it for myself, I am sure I will never forget it. :slight_smile:

Plus, with my method I have the variables to use for other functions in the sketch, for example to use with IrSender to test the code after retrieving it.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.