++#include <TimerOne.h>\r
+/* first include Arduino.h, the IDE includes it after irmp*.h ... */\r
+#include "Arduino.h"\r
+/* ... and then chokes on uintX_t ... */\r
+\r
+#include <irmp.h>\r
+#include <irsnd.h>\r
+\r
+/* undefine this if you don't want blinking LED for diagnosis */\r
+#define LED_PIN 13\r
+#define SER_BAUD 115200\r
+\r
+/* F_INTERRUPTS is the interrupt frequency defined in irmpconfig.h */\r
+#define US (1000000 / F_INTERRUPTS)\r
+void setup()\r
+{\r
+ Serial.begin(SER_BAUD);\r
+ delay(1000);\r
+ /* greeting string and debugging ouput */\r
+ Serial.println("IRMP test sketch");\r
+ Serial.print("US: ");\r
+ Serial.println(US);\r
+ Serial.println("Send example: P:02 A:916E C:000F (NEC Taste 1)");\r
+#ifdef LED_PIN\r
+ pinMode(LED_PIN, OUTPUT);\r
+#endif\r
+ irmp_init();\r
+ irsnd_init();\r
+ //sei();\r
+ led(HIGH);\r
+ delay(20); /* make sure the greeting string is out before starting */\r
+ led(LOW);\r
+ Timer1.initialize(US);\r
+ Timer1.attachInterrupt(timerinterrupt);\r
+}\r
+\r
+IRMP_DATA irmp_data[3];\r
+uint8_t act_data = 0;\r
+int incomingByte = 0; // for incoming serial data\r
+\r
+void loop()\r
+{\r
+ IRMP_DATA* data = &irmp_data[act_data];\r
+ if (irmp_get_data(data))\r
+ {\r
+ led(HIGH);\r
+#if IRMP_PROTOCOL_NAMES == 1\r
+ Serial.print(irmp_protocol_names[data->protocol]);\r
+ Serial.print(" ");\r
+#endif\r
+ Serial.print("P:");\r
+ Serial.print(data->protocol, HEX);\r
+ Serial.print(" A:");\r
+ Serial.print(data->address, HEX);\r
+ Serial.print(" C:");\r
+ Serial.print(data->command, HEX);\r
+ Serial.print(" ");\r
+ Serial.print(data->flags, HEX);\r
+ Serial.println("");\r
+ /* Serial.print is asynchronous, so the LED is only flashing very lightly */\r
+ led(LOW);\r
+\r
+ data->flags = 0; // reset flags!\r
+ int result = irsnd_send_data(data, TRUE);\r
+ if (result != 1)\r
+ {\r
+ Serial.println("loop : irsnd_send_data ERROR");\r
+ }\r
+ else\r
+ {\r
+ if (++act_data >= 3)\r
+ {\r
+ act_data = 0;\r
+ } \r
+ }\r
+ }\r
+\r
+ if (Serial.available() == 18 && readAndCheck('P') && readAndCheck(':'))\r
+ {\r
+ // read the protocol\r
+ data->protocol = readHex() * 16 + readHex();\r
+\r
+ if (readAndCheck(' ') && readAndCheck('A') && readAndCheck(':'))\r
+ {\r
+ // read the address\r
+ data->address = ((readHex() * 16 + readHex()) * 16 + readHex()) * 16 + readHex();\r
+\r
+ if (readAndCheck(' ') && readAndCheck('C') && readAndCheck(':'))\r
+ {\r
+ // read the address\r
+ data->command = ((readHex() * 16 + readHex()) * 16 + readHex()) * 16 + readHex();\r
+\r
+ // send ir data\r
+ data->flags = 0;\r
+ int result = irsnd_send_data(data, TRUE);\r
+ if (result)\r
+ {\r
+ Serial.print("Send IR data: ");\r
+ Serial.print("P:");\r
+ Serial.print(data->protocol, HEX);\r
+ Serial.print(" A:");\r
+ Serial.print(data->address, HEX);\r
+ Serial.print(" C:");\r
+ Serial.print(data->command, HEX);\r
+ Serial.println("");\r
+\r
+ if (++act_data >= 3)\r
+ {\r
+ act_data = 0;\r
+ } \r
+ }\r
+ }\r
+ }\r
+ }\r
+}\r
+\r
+/* helper function: attachInterrupt wants void(), but irmp_ISR is uint8_t() */\r
+void timerinterrupt()\r
+{\r
+ // only when tsop dont see the ir_led flashing\r
+ irsnd_ISR(); // call irsnd ISR\r
+ irmp_ISR(); // call irmp ISR\r
+\r
+/*\r
+ // do not receive when sending (tsop see the ir_led)\r
+ if (! irsnd_ISR()) // call irsnd ISR\r
+ { // if not busy...\r
+ irmp_ISR(); // call irmp ISR\r
+ }\r
+*/\r
+}\r
+\r
+static inline void led(int state)\r
+{\r
+#ifdef LED_PIN\r
+ digitalWrite(LED_PIN, state);\r
+#endif\r
+}\r
+\r
+static inline int readAndCheck(int c)\r
+{\r
+ return c == Serial.read();\r
+}\r
+\r
+static inline int readHex()\r
+{\r
+ int c = Serial.read();\r
+ if (c >= '0' && c <= '9')\r
+ {\r
+ return c - '0';\r
+ }\r
+\r
+ c |= 0x20; // lowercase\r
+\r
+ if (c >= 'a' && c <= 'f')\r
+ {\r
+ return c + 10 - 'a';\r
+ }\r
+\r
+ return -1;\r
+}\r