iMac Apple遥控器ir解码

时间:2017-06-22 12:31:00

标签: arduino

我正在尝试修改Arduino草图以使用旧的Apple远程红外发射器。它有效,我有各种按钮的HEX代码列表。让我感到困惑的是草图不会使用包含的十六进制代码进行编译,但如果将它们转换为DEC等效,则会这样做。并且,草图线102至126中定义的串行输出起作用,但LED似乎不像所建议的那样执行。我不知道它是否与HEX / DEC问题有关,或者在哪里看。现在的代码如下所示。它包括有关遥控器频率的评论,我没有提到。感谢您帮助我理解这一点。

 /* 
   This sketch uses Ken Shirriff's *awesome* IRremote library:
       https://github.com/shirriff/Arduino-IRremote

   Hardware setup:
     * The output of an IR Receiver Diode (38 kHz demodulating
       version) should be connected to the Arduino's pin 11.
       * The IR Receiver diode should also be powered off the
         Arduino's 5V and GND rails.
     * A common cathode RGB LED is connected to Arduino's pins 
       5, 9, and 6 (red, green, and blue pins).
 */

#include <IRremote.h> // Include the IRremote library

/* Setup constants for SparkFun's IR Remote: */
#define NUM_BUTTONS 6 // The remote has 6 buttons
/* Define the IR remote button codes. We're only using the
   least signinficant two bytes of these codes. Each one 
   should actually have 0x10EF in front of it. Find these codes
   by running the IRrecvDump example sketch included with
   the IRremote library.*/
const uint16_t BUTTON_PLUS = 2011254893; // i.e. 0x10EFD827
const uint16_t BUTTON_MINUS = 2011246701;
const uint16_t BUTTON_LEFT = 2011271277;
const uint16_t BUTTON_RIGHT = 2011258989;
const uint16_t BUTTON_MENU = 2011283565;
const uint16_t BUTTON_STARTSTOP = 2011275373;
//const uint16_t BUTTON_LEFT = 0x10EF;
//const uint16_t BUTTON_RIGHT = 0x807F;
//const uint16_t BUTTON_CIRCLE = 0x20DF;

/* Connect the output of the IR receiver diode to pin 11. */
int RECV_PIN = 11;
/* Initialize the irrecv part of the IRremote  library */
IRrecv irrecv(RECV_PIN);
decode_results results; // This will store our IR received codes
uint16_t lastCode = 0; // This keeps track of the last code RX'd

/* Setup RGB LED pins: */
enum ledOrder // Make an enum to add some clarity in the code
{
  RED,   // 0
  GREEN, // 1
  BLUE   // 2
};
const int rgbPins[3] = {5, 9, 6}; // Red, green, blue pins respectively
byte rgbValues[3] = {55, 23, 200}; // This keeps track of channel brightness
byte activeChannel = RED; // Start with RED as the active channel
boolean ledEnable = 1; // Start with the LED on.

void setup()
{
  Serial.begin(9600); // Use serial to debug. 
  irrecv.enableIRIn(); // Start the receiver

  /* Set up the RGB LED pins: */
  for (int i=0; i<3; i++)
  {
    pinMode(rgbPins[i], OUTPUT);
    analogWrite(rgbPins[i], rgbValues[i]);
  }
}

// loop() constantly checks for any received IR codes. At the
// end it updates the RGB LED.
void loop() 
{
  if (irrecv.decode(&results)) 
  {
    /* read the RX'd IR into a 16-bit variable: */
    uint16_t resultCode = (results.value & 65535); //0xFFFF

    /* The remote will continue to spit out 0xFFFFFFFF if a 
     button is held down. If we get 0xFFFFFFF, let's just
     assume the previously pressed button is being held down */
    if (resultCode == 65535) //0xFFFF
      resultCode = lastCode;
    else
      lastCode = resultCode;

    // This switch statement checks the received IR code against
    // all of the known codes. Each button press produces a 
    // serial output, and has an effect on the LED output.
    switch (resultCode)
    {
      case BUTTON_PLUS:
        Serial.println("+");
        if (ledEnable) ledEnable = 0;
        else ledEnable = 1; // Flip ledEnable
        break;
      case BUTTON_MINUS:
        Serial.println("-");
        activeChannel = RED;
        break;
      case BUTTON_LEFT:
        Serial.println("<-");
        activeChannel = GREEN;
        break;
      case BUTTON_RIGHT:
        Serial.println("->");
        activeChannel = BLUE;
        break;
      case BUTTON_MENU:
        Serial.println("Menu");
        rgbValues[activeChannel]++; // Increment brightness
        break;
      case BUTTON_STARTSTOP:
        Serial.println("-> =");
        rgbValues[activeChannel]--; // Decrement brightness
        break;
//      case BUTTON_LEFT:
//        Serial.println("Left");
//        rgbValues[activeChannel] = 0; // Min brightness (off)
//        break;
//      case BUTTON_RIGHT:
//        Serial.println("Right");
//        rgbValues[activeChannel] =  255; // Max brightness
//        break;
//      case BUTTON_CIRCLE:
//        Serial.println("Circle");
//        rgbValues[activeChannel] = 127; // Medium brightness
//        break;
      default:
        Serial.print("Unrecognized code received: 0x");
        Serial.println(results.value, HEX);
        break;        
    }    
    irrecv.resume(); // Receive the next value
  }

  // Every time through the loop, update the RGB LEDs:
  if (ledEnable)
  {
    for (int i=0; i<3; i++)
    {
      analogWrite(rgbPins[i], rgbValues[i]);
    }
  }
  else
  {
    for (int i=0; i<3; i++)
    {
      analogWrite(rgbPins[i], 0);
    }
  }
}

1 个答案:

答案 0 :(得分:0)

如果你说你的代码没有编译,如果对这些数字使用十六进制表示法,那将有助于提供无法编译的实际代码,因为即使我输入十六进制数而不是十进制数,你在这里发布的代码也会编译。 / p>

正如gre_gor在他的评论中已经指出的那样,你的价值观也存在问题。

const uint16_t BUTTON_PLUS = 2011254893;

在这里,您尝试将2011254893存储在16位无符号整数中。

如果所有16位都是1,则最终得到2 ^ 16 -1,即65535。 这是你可以存储在uint16_t类型的变量中的最大数量。

如果为该变量分配较大的值,则会导致所谓的整数溢出。存储在变量中的实际值将是2011244893模数65536,即20589.这不是您应该分配的值。

如果您仔细阅读该代码中的注释:

  

定义IR遥控器按钮代码。我们只使用最少   这些代码的两个字节是有意义的。实际上每个人都应该   在它前面有0x10EF。

同样在整数溢出时读取此内容,我想如果您通常熟悉数据类型也不会有什么坏处。

https://en.wikipedia.org/wiki/Integer_overflow

http://www.cplusplus.com/articles/DE18T05o/

相关问题