]>
Commit | Line | Data |
---|---|---|
1 | +#include <TimerOne.h>\r | |
2 | /* first include Arduino.h, the IDE includes it after irmp*.h ... */\r | |
3 | #include "Arduino.h"\r | |
4 | /* ... and then chokes on uintX_t ... */\r | |
5 | \r | |
6 | #include <irmp.h>\r | |
7 | #include <irsnd.h>\r | |
8 | \r | |
9 | /* undefine this if you don't want blinking LED for diagnosis */\r | |
10 | #define LED_PIN 13\r | |
11 | #define SER_BAUD 115200\r | |
12 | \r | |
13 | /* F_INTERRUPTS is the interrupt frequency defined in irmpconfig.h */\r | |
14 | #define US (1000000 / F_INTERRUPTS)\r | |
15 | void setup()\r | |
16 | {\r | |
17 | Serial.begin(SER_BAUD);\r | |
18 | delay(1000);\r | |
19 | /* greeting string and debugging ouput */\r | |
20 | Serial.println("IRMP test sketch");\r | |
21 | Serial.print("US: ");\r | |
22 | Serial.println(US);\r | |
23 | Serial.println("Send example: P:02 A:916E C:000F (NEC Taste 1)");\r | |
24 | #ifdef LED_PIN\r | |
25 | pinMode(LED_PIN, OUTPUT);\r | |
26 | #endif\r | |
27 | irmp_init();\r | |
28 | irsnd_init();\r | |
29 | //sei();\r | |
30 | led(HIGH);\r | |
31 | delay(20); /* make sure the greeting string is out before starting */\r | |
32 | led(LOW);\r | |
33 | Timer1.initialize(US);\r | |
34 | Timer1.attachInterrupt(timerinterrupt);\r | |
35 | }\r | |
36 | \r | |
37 | IRMP_DATA irmp_data[3];\r | |
38 | uint8_t act_data = 0;\r | |
39 | int incomingByte = 0; // for incoming serial data\r | |
40 | \r | |
41 | void loop()\r | |
42 | {\r | |
43 | IRMP_DATA* data = &irmp_data[act_data];\r | |
44 | if (irmp_get_data(data))\r | |
45 | {\r | |
46 | led(HIGH);\r | |
47 | #if IRMP_PROTOCOL_NAMES == 1\r | |
48 | Serial.print(irmp_protocol_names[data->protocol]);\r | |
49 | Serial.print(" ");\r | |
50 | #endif\r | |
51 | Serial.print("P:");\r | |
52 | Serial.print(data->protocol, HEX);\r | |
53 | Serial.print(" A:");\r | |
54 | Serial.print(data->address, HEX);\r | |
55 | Serial.print(" C:");\r | |
56 | Serial.print(data->command, HEX);\r | |
57 | Serial.print(" ");\r | |
58 | Serial.print(data->flags, HEX);\r | |
59 | Serial.println("");\r | |
60 | /* Serial.print is asynchronous, so the LED is only flashing very lightly */\r | |
61 | led(LOW);\r | |
62 | \r | |
63 | data->flags = 0; // reset flags!\r | |
64 | int result = irsnd_send_data(data, TRUE);\r | |
65 | if (result != 1)\r | |
66 | {\r | |
67 | Serial.println("loop : irsnd_send_data ERROR");\r | |
68 | }\r | |
69 | else\r | |
70 | {\r | |
71 | if (++act_data >= 3)\r | |
72 | {\r | |
73 | act_data = 0;\r | |
74 | } \r | |
75 | }\r | |
76 | }\r | |
77 | \r | |
78 | if (Serial.available() == 18 && readAndCheck('P') && readAndCheck(':'))\r | |
79 | {\r | |
80 | // read the protocol\r | |
81 | data->protocol = readHex() * 16 + readHex();\r | |
82 | \r | |
83 | if (readAndCheck(' ') && readAndCheck('A') && readAndCheck(':'))\r | |
84 | {\r | |
85 | // read the address\r | |
86 | data->address = ((readHex() * 16 + readHex()) * 16 + readHex()) * 16 + readHex();\r | |
87 | \r | |
88 | if (readAndCheck(' ') && readAndCheck('C') && readAndCheck(':'))\r | |
89 | {\r | |
90 | // read the address\r | |
91 | data->command = ((readHex() * 16 + readHex()) * 16 + readHex()) * 16 + readHex();\r | |
92 | \r | |
93 | // send ir data\r | |
94 | data->flags = 0;\r | |
95 | int result = irsnd_send_data(data, TRUE);\r | |
96 | if (result)\r | |
97 | {\r | |
98 | Serial.print("Send IR data: ");\r | |
99 | Serial.print("P:");\r | |
100 | Serial.print(data->protocol, HEX);\r | |
101 | Serial.print(" A:");\r | |
102 | Serial.print(data->address, HEX);\r | |
103 | Serial.print(" C:");\r | |
104 | Serial.print(data->command, HEX);\r | |
105 | Serial.println("");\r | |
106 | \r | |
107 | if (++act_data >= 3)\r | |
108 | {\r | |
109 | act_data = 0;\r | |
110 | } \r | |
111 | }\r | |
112 | }\r | |
113 | }\r | |
114 | }\r | |
115 | }\r | |
116 | \r | |
117 | /* helper function: attachInterrupt wants void(), but irmp_ISR is uint8_t() */\r | |
118 | void timerinterrupt()\r | |
119 | {\r | |
120 | #if 1 // if TSOP receiver can't detect my own IR LED, call both functions:\r | |
121 | irsnd_ISR(); // call irsnd ISR\r | |
122 | irmp_ISR(); // call irmp ISR\r | |
123 | #else // if TSOP receiver also detects my own IR LED, don't receive while sending:\r | |
124 | if (! irsnd_ISR()) // call irsnd ISR\r | |
125 | { // if not busy...\r | |
126 | irmp_ISR(); // call irmp ISR\r | |
127 | }\r | |
128 | #endif\r | |
129 | }\r | |
130 | \r | |
131 | static inline void led(int state)\r | |
132 | {\r | |
133 | #ifdef LED_PIN\r | |
134 | digitalWrite(LED_PIN, state);\r | |
135 | #endif\r | |
136 | }\r | |
137 | \r | |
138 | static inline int readAndCheck(int c)\r | |
139 | {\r | |
140 | return c == Serial.read();\r | |
141 | }\r | |
142 | \r | |
143 | static inline int readHex()\r | |
144 | {\r | |
145 | int c = Serial.read();\r | |
146 | if (c >= '0' && c <= '9')\r | |
147 | {\r | |
148 | return c - '0';\r | |
149 | }\r | |
150 | \r | |
151 | c |= 0x20; // lowercase\r | |
152 | \r | |
153 | if (c >= 'a' && c <= 'f')\r | |
154 | {\r | |
155 | return c + 10 - 'a';\r | |
156 | }\r | |
157 | \r | |
158 | return -1;\r | |
159 | }\r |