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