Landzo C1试用体验】红外库之扩展 格力遥控



Landzo C1试用体验】红外库之扩展 格力遥控Arduino 已经集成了红外库,其地址如下:
D:\Program Files\arduino-1.7.10\libraries\RobotIRremote

我记得以前做格力遥控的分析,这次做起来就很快
原来的地址
红外遥控介绍与接收 http://blog.sina.com.cn/s/blog_7e7fa4c80102vmbc.html
红外编码与解码——GREE格力遥控 
http://blog.sina.com.cn/s/blog_7e7fa4c80102vmbd.html
http://blog.sina.com.cn/s/blog_7e7fa4c80102vmv3.html


明白了原理做起来就很快啦
写一下移植过程


1、IRremoteInt.h   RAWBUF的宏定义,记录高低电平的时间
怎么算?GREE格力:起始码(S)+35位数据码+连接码(C)+32位数据码   = 69 bit
buf len = 69*2 + 1(头)+1(尾)= 140 长度 ,默认为101

2、
USECPERTICK  timer中断间隔时  默认是50  表示中断间隔是50us

3、
GAP_TICKS  表示最大的时间间隔,超过即表示另外一个码,

GREE格力最长的是 连接码C电平宽度为:600us低电平+20000us高电平

_GAP 这里修改为比20000us大一些即可 我设置21000

4、IRremote.h
decode_type_t; 添加IR类型

以及class函数


5、编写ir_GREE.cpp 解码与编码函数
bool  IRrecv::decodeGree (decode_results *results)

void  IRsend::sendGree (unsigned long data,  int nbits)

6、添加Gree的解码程序到irRecv.cpp
int  IRrecv::decode (decode_results *results)中

代码在原来博文中都有
<ignore_js_op> 

  1. #include <IRremote.h>
  2. int RECV_PIN = 11;
  3. int BUTTON_PIN = 7;
  4. int STATUS_PIN = 8;
  5. IRrecv irrecv(RECV_PIN);
  6. IRsend irsend;
  7. decode_results results;
  8. void setup()
  9. {
  10.   Serial.begin(9600);
  11.   irrecv.enableIRIn(); // Start the receiver
  12.   pinMode(BUTTON_PIN, INPUT);
  13.   pinMode(STATUS_PIN, OUTPUT);
  14. }
  15. // Storage for the recorded code
  16. int codeType = -1; // The type of code
  17. unsigned long codeValue; // The code value if not raw
  18. unsigned int rawCodes[RAWBUF]; // The durations if raw
  19. int codeLen; // The length of the code
  20. int toggle = 0; // The RC5/6 toggle state
  21. // Stores the code for later playback
  22. // Most of this code is just logging
  23. void storeCode(decode_results *results) {
  24.   codeType = results->decode_type;
  25.   int count = results->rawlen;
  26.   if (codeType == UNKNOWN) {
  27.     Serial.println(“Received unknown code, saving as raw”);
  28.     codeLen = results->rawlen – 1;
  29.     // To store raw codes:
  30.     // Drop first value (gap)
  31.     // Convert from ticks to microseconds
  32.     // Tweak marks shorter, and spaces longer to cancel out IR receiver distortion
  33.     for (int i = 1; i <= codeLen; i++) {
  34.       if (i % 2) {
  35.         // Mark
  36.         rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK – MARK_EXCESS;
  37.         Serial.print(” m”);
  38.       }
  39.       else {
  40.         // Space
  41.         rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK + MARK_EXCESS;
  42.         Serial.print(” s”);
  43.       }
  44.       Serial.print(rawCodes[i - 1], DEC);
  45.     }
  46.     Serial.println(“”);
  47.   }
  48.   else {
  49.     if (codeType == NEC) {
  50.       Serial.print(“Received NEC: “);
  51.       if (results->value == REPEAT) {
  52.         // Don’t record a NEC repeat value as that’s useless.
  53.         Serial.println(“repeat; ignoring.”);
  54.         return;
  55.       }
  56.     }
  57.     else if (codeType == GREE) {
  58.       Serial.print(“Received GREE: “);
  59.     }
  60.     else if (codeType == SONY) {
  61.       Serial.print(“Received SONY: “);
  62.     }
  63.     else if (codeType == PANASONIC) {
  64.       Serial.print(“Received PANASONIC: “);
  65.     }
  66.     else if (codeType == JVC) {
  67.       Serial.print(“Received JVC: “);
  68.     }
  69.     else if (codeType == RC5) {
  70.       Serial.print(“Received RC5: “);
  71.     }
  72.     else if (codeType == RC6) {
  73.       Serial.print(“Received RC6: “);
  74.     }
  75.     else {
  76.       Serial.print(“Unexpected codeType “);
  77.       Serial.print(codeType, DEC);
  78.       Serial.println(“”);
  79.     }
  80.     Serial.println(results->value, HEX);
  81.     codeValue = results->value;
  82.     codeLen = results->bits;
  83.   }
  84. }
  85. void sendCode(int repeat) {
  86.   if (codeType == NEC) {
  87.     if (repeat) {
  88.       irsend.sendNEC(REPEAT, codeLen);
  89.       Serial.println(“Sent NEC repeat”);
  90.     }
  91.     else {
  92.       irsend.sendNEC(codeValue, codeLen);
  93.       Serial.print(“Sent NEC “);
  94.       Serial.println(codeValue, HEX);
  95.     }
  96.   }
  97.   else if (codeType == GREE){
  98.     irsend.sendGree(codeValue, codeLen);
  99.     Serial.print(“Sent NEC “);
  100.     Serial.println(codeValue, HEX);
  101.   }
  102.   else if (codeType == SONY) {
  103.     irsend.sendSony(codeValue, codeLen);
  104.     Serial.print(“Sent Sony “);
  105.     Serial.println(codeValue, HEX);
  106.   }
  107.   else if (codeType == PANASONIC) {
  108.     irsend.sendPanasonic(codeValue, codeLen);
  109.     Serial.print(“Sent Panasonic”);
  110.     Serial.println(codeValue, HEX);
  111.   }
  112.   else if (codeType == JVC) {
  113.     irsend.sendPanasonic(codeValue, codeLen);
  114.     Serial.print(“Sent JVC”);
  115.     Serial.println(codeValue, HEX);
  116.   }
  117.   else if (codeType == RC5 || codeType == RC6) {
  118.     if (!repeat) {
  119.       // Flip the toggle bit for a new button press
  120.       toggle = 1 – toggle;
  121.     }
  122.     // Put the toggle bit into the code to send
  123.     codeValue = codeValue & ~(1 << (codeLen – 1));
  124.     codeValue = codeValue | (toggle << (codeLen – 1));
  125.     if (codeType == RC5) {
  126.       Serial.print(“Sent RC5 “);
  127.       Serial.println(codeValue, HEX);
  128.       irsend.sendRC5(codeValue, codeLen);
  129.     }
  130.     else {
  131.       irsend.sendRC6(codeValue, codeLen);
  132.       Serial.print(“Sent RC6 “);
  133.       Serial.println(codeValue, HEX);
  134.     }
  135.   }
  136.   else if (codeType == UNKNOWN /* i.e. raw */) {
  137.     // Assume 38 KHz
  138.     irsend.sendRaw(rawCodes, codeLen, 38);
  139.     Serial.println(“Sent raw”);
  140.   }
  141. }
  142. int lastButtonState;
  143. void loop() {
  144.   // If button pressed, send the code.
  145.   int buttonState = digitalRead(BUTTON_PIN);
  146.   if (lastButtonState == HIGH && buttonState == LOW) {
  147.     Serial.println(“Released”);
  148.     irrecv.enableIRIn(); // Re-enable receiver
  149.   }
  150.   if (buttonState==LOW) {
  151.     Serial.println(“Pressed, sending”);
  152.     digitalWrite(STATUS_PIN, LOW);
  153.     sendCode(lastButtonState == buttonState);
  154.     digitalWrite(STATUS_PIN, HIGH);
  155.     delay(50); // Wait a bit between retransmissions
  156.   }
  157.   else if (irrecv.decode(&results)) {
  158.     digitalWrite(STATUS_PIN, LOW);
  159.     storeCode(&results);
  160.     irrecv.resume(); // resume receiver
  161.     digitalWrite(STATUS_PIN, HIGH);
  162.   }
  163.   lastButtonState = buttonState;
  164. }

复制代码



其实可以用来做万能遥控器,
Landzo C1 的套件中并没有红外发射模块,并不能对其进行测试