Phidget IR: Simple TV Remote and Understanding codeInfo

Supporting 10.7 and up
maxhirez
Phidgetsian
Posts: 8
Joined: Thu Mar 10, 2011 10:46 pm
Contact:

Phidget IR: Simple TV Remote and Understanding codeInfo

Postby maxhirez » Tue Mar 29, 2011 8:33 pm

After not touching C since CodeWarrior in System 9, with Patrick's guidance I was able to cobble together the following simple command-line "clicker" for my TV (Code at the end.) It compiles flawlessly in xCode 3.1.3 (I'll upgrade to an Intel machine when they put me in the ground. Or when someone gives me one for free. Whichever.)

Essentially what it does is creates the handle, fills in the codeInfo structure for my particular TV, then enters a loop, waiting for a command which determines which code to send and sending that code out and beginning the loop again until quitting.

It can be daunting to try to understand the structure if you're just coming to Cocoa from any other programming environment or language. I've attached a screen cap of the Phidget IR application after it's received a "Power Toggle" command for the TV I'm working with (a Magnavox 19MD357blahblahblah...) so that you can see how the fields relate to the codeInfo info.
TVOn.png
TVOn.png (57.8 KiB) Viewed 1956 times
One thing you'll notice is that some fields (header/one/zero/gap/toggle mask) disappear from the codeInfo declaration, which is because (again thanks to Patrick) these are filled out by default when you declare the RC_6 encoding. (My original code included lines like:

codeInfo.header[0] = 2616, codeInfo.header[1] = 931;)

The other thing to remember is that in establishing the code to be sent, you have to break the code into 4-character chunks from the captured code where each chunk includes the 2 character prefix of the code to each chunk (so that 0x11000c becomes 0x11, 0x00, 0x0c in this example.) Longer or shorter codes work the same way, just make sure that the number of chunks is declared in brackets after the code[x] declaration.

Code: Select all

//EZClickerMagnavox 2011 Jason Scott Hoffman
//Please feel free to copy, redistribute, modify and take credit for any and all elements of this program that Phidgets doesn't lay claim to.
#include <stdio.h>
#include <stdbool.h>
#include <Phidget21/phidget21.h>

int     main()
{
     char    vOperation;
    bool    vFinished;
   
   //fill in most of CodeInfo structure and Handle
   CPhidgetIRHandle ir = 0;
   CPhidgetIR_create (&ir);
   CPhidget_open((CPhidgetHandle)ir, -1);
   CPhidget_waitForAttachment((CPhidgetHandle)ir, 0);
   CPhidgetIR_CodeInfo codeInfo = {0};
   codeInfo.bitCount = 21;
   codeInfo.encoding = PHIDGET_IR_ENCODING_RC6;
   
   // initialize flag
    vFinished = false;
   
    // loop until user screws pooch:
    while( vFinished != true )
    {
      
      printf( "Power...:...0\n" );
      printf( "Channel Up...:...+\n" );
      printf( "Channel Down...:...-\n" );
      printf( "Volume Up...:...<\n" );
      printf( "Volume Down...:...>\n" );
      printf( "Channel Up...:...+\n" );
      printf( "Mute...:...m\n" );
           printf( "Quit...:...Anything Else\n" );
        scanf( "%c", &vOperation );
        fpurge( stdin );
        if( vOperation == '+' )
        {
         unsigned char code[3] = {0x11,0x00,0x20};//Channel up
         CPhidgetIR_Transmit(ir, code, &codeInfo);
         printf( "Channel Increased.\n" );
        }
        else
        {
            if( vOperation == '-' )
            {
            unsigned char code[3] = {0x11,0x00,0x21};//Channel down
            CPhidgetIR_Transmit(ir, code, &codeInfo);
            printf( "Channel Decreased.\n" );
            }
            else
            {
                if( vOperation == '<' )
                {
                      unsigned char code[3] = {0x11,0x00,0x10};//Volume up
               CPhidgetIR_Transmit(ir, code, &codeInfo);
               printf( "Volume Increased.\n" );
            }
                else
                {
               if( vOperation == '>' )
               {
                  unsigned char code[3] = {0x11,0x00,0x11};//Volume Down
                  CPhidgetIR_Transmit(ir, code, &codeInfo);
                  printf( "Volume Decreased.\n" );
               }
               else
               {
                  if( vOperation == 'm' )
                  {
                     unsigned char code[3] = {0x11,0x00,0x0d};//mute
                     CPhidgetIR_Transmit(ir, code, &codeInfo);
                     printf( "Mute Toggled.\n" );
                  }
                  else
                  {
                     if( vOperation == '0' )
                     {
                        unsigned char code[3] = {0x11,0x00,0x0c};//power   
                        CPhidgetIR_Transmit(ir, code, &codeInfo);
                        printf( "Power Toggled.\n" );
                     }
                     else
                        vFinished = true;
                  }
               }
            }
         }
         
         printf( "Finished.\n" );
         
      }
   }
   
}

Return to “macOS”

Who is online

Users browsing this forum: No registered users and 2 guests