]> cloudbase.mooo.com Git - irmp.git/blob - examples/Arduino/Arduino.ino
Version 2.9.7: added port to to ESP8266 and TEENSY, added PANASONIC protocol, added...
[irmp.git] / examples / Arduino / 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 #if 1 // if TSOP receiver can't detect my own IR LED, call both functions:
121 irsnd_ISR(); // call irsnd ISR
122 irmp_ISR(); // call irmp ISR
123 #else // if TSOP receiver also detects my own IR LED, don't receive while sending:
124 if (! irsnd_ISR()) // call irsnd ISR
125 { // if not busy...
126 irmp_ISR(); // call irmp ISR
127 }
128 #endif
129 }
130
131 static inline void led(int state)
132 {
133 #ifdef LED_PIN
134 digitalWrite(LED_PIN, state);
135 #endif
136 }
137
138 static inline int readAndCheck(int c)
139 {
140 return c == Serial.read();
141 }
142
143 static inline int readHex()
144 {
145 int c = Serial.read();
146 if (c >= '0' && c <= '9')
147 {
148 return c - '0';
149 }
150
151 c |= 0x20; // lowercase
152
153 if (c >= 'a' && c <= 'f')
154 {
155 return c + 10 - 'a';
156 }
157
158 return -1;
159 }