]> cloudbase.mooo.com Git - irmp.git/blob - irmp.c
Version 1.2.1: added APPLE_PROTOCOL to irsnd
[irmp.git] / irmp.c
1 /*---------------------------------------------------------------------------------------------------------------------------------------------------
2 * irmp.c - infrared multi-protocol decoder, supports several remote control protocols
3 *
4 * Copyright (c) 2009-2010 Frank Meyer - frank(at)fli4l.de
5 *
6 * $Id: irmp.c,v 1.20 2010/04/19 13:42:17 fm Exp $
7 *
8 * ATMEGA88 @ 8 MHz
9 *
10 * Typical manufacturers:
11 *
12 * SIRCS - Sony
13 * NEC - NEC, Yamaha, Canon, Tevion, Harman/Kardon, Hitachi, JVC, Pioneer, Toshiba, Xoro, Orion, and many other Japanese manufacturers
14 * SAMSUNG - Samsung
15 * SAMSUNG32 - Samsung
16 * MATSUSHITA - Matsushita
17 * KASEIKYO - Panasonic, Denon & other Japanese manufacturers (members of "Japan's Association for Electric Home Application")
18 * RECS80 - Philips, Nokia, Thomson, Nordmende, Telefunken, Saba
19 * RC5 - Philips and other European manufacturers
20 * DENON - Denon
21 * RC6 - Philips and other European manufacturers
22 * APPLE - Apple
23 * NUBERT - Nubert Subwoofer System
24 * B&O - Bang & Olufsen
25 * PANASONIC - Panasonic (older, yet not implemented)
26 *
27 *---------------------------------------------------------------------------------------------------------------------------------------------------
28 *
29 * SIRCS
30 * -----
31 *
32 * frame: 1 start bit + 12-20 data bits + no stop bit
33 * data: 7 command bits + 5 address bits + 0 to 8 additional bits
34 *
35 * start bit: data "0": data "1": stop bit:
36 * -----------------_________ ------_____ ------------______
37 * 2400us 600us 600us 600us 1200us 600 us no stop bit
38 *
39 *---------------------------------------------------------------------------------------------------------------------------------------------------
40 *
41 * NEC + extended NEC
42 * -------------------------
43 *
44 * frame: 1 start bit + 32 data bits + 1 stop bit
45 * data NEC: 8 address bits + 8 inverted address bits + 8 command bits + 8 inverted command bits
46 * data extended NEC: 16 address bits + 8 command bits + 8 inverted command bits
47 *
48 * start bit: data "0": data "1": stop bit:
49 * -----------------_________ ------______ ------________________ ------______....
50 * 9000us 4500us 560us 560us 560us 1690 us 560us
51 *
52 *
53 * Repetition frame:
54 *
55 * -----------------_________------______ .... ~100ms Pause, then repeat
56 * 9000us 2250us 560us
57 *
58 *---------------------------------------------------------------------------------------------------------------------------------------------------
59 *
60 * SAMSUNG
61 * -------
62 *
63 * frame: 1 start bit + 16 data(1) bits + 1 sync bit + additional 20 data(2) bits + 1 stop bit
64 * data(1): 16 address bits
65 * data(2): 4 ID bits + 8 command bits + 8 inverted command bits
66 *
67 * start bit: data "0": data "1": sync bit: stop bit:
68 * ----------______________ ------______ ------________________ ------______________ ------______....
69 * 4500us 4500us 550us 450us 550us 1450us 550us 4500us 550us
70 *
71 *---------------------------------------------------------------------------------------------------------------------------------------------------
72 *
73 * SAMSUNG32
74 * ----------
75 *
76 * frame: 1 start bit + 32 data bits + 1 stop bit
77 * data: 16 address bits + 16 command bits
78 *
79 * start bit: data "0": data "1": stop bit:
80 * ----------______________ ------______ ------________________ ------______....
81 * 4500us 4500us 550us 450us 550us 1450us 550us
82 *
83 *---------------------------------------------------------------------------------------------------------------------------------------------------
84 *
85 * MATSUSHITA
86 * ----------
87 *
88 * frame: 1 start bit + 24 data bits + 1 stop bit
89 * data: 6 custom bits + 6 command bits + 12 address bits
90 *
91 * start bit: data "0": data "1": stop bit:
92 * ----------_________ ------______ ------________________ ------______....
93 * 3488us 3488us 872us 872us 872us 2616us 872us
94 *
95 *---------------------------------------------------------------------------------------------------------------------------------------------------
96 *
97 * KASEIKYO
98 * --------
99 *
100 * frame: 1 start bit + 48 data bits + 1 stop bit
101 * data: 16 manufacturer bits + 4 parity bits + 4 genre1 bits + 4 genre2 bits + 10 command bits + 2 id bits + 8 parity bits
102 *
103 * start bit: data "0": data "1": stop bit:
104 * ----------______ ------______ ------________________ ------______....
105 * 3380us 1690us 423us 423us 423us 1269us 423us
106 *
107 *---------------------------------------------------------------------------------------------------------------------------------------------------
108 *
109 * RECS80
110 * ------
111 *
112 * frame: 2 start bits + 10 data bits + 1 stop bit
113 * data: 1 toggle bit + 3 address bits + 6 command bits
114 *
115 * start bit: data "0": data "1": stop bit:
116 * -----_____________________ -----____________ -----______________ ------_______....
117 * 158us 7432us 158us 4902us 158us 7432us 158us
118 *
119 *---------------------------------------------------------------------------------------------------------------------------------------------------
120 *
121 * RECS80EXT
122 * ---------
123 *
124 * frame: 2 start bits + 11 data bits + 1 stop bit
125 * data: 1 toggle bit + 4 address bits + 6 command bits
126 *
127 * start bit: data "0": data "1": stop bit:
128 * -----_____________________ -----____________ -----______________ ------_______....
129 * 158us 3637us 158us 4902us 158us 7432us 158us
130 *
131 *---------------------------------------------------------------------------------------------------------------------------------------------------
132 *
133 * RC5 + RC5X
134 * ----------
135 *
136 * RC5 frame: 2 start bits + 12 data bits + no stop bit
137 * RC5 data: 1 toggle bit + 5 address bits + 6 command bits
138 * RC5X frame: 1 start bit + 13 data bits + no stop bit
139 * RC5X data: 1 inverted command bit + 1 toggle bit + 5 address bits + 6 command bits
140 *
141 * start bit: data "0": data "1":
142 * ______----- ------______ ______------
143 * 889us 889us 889us 889us 889us 889us
144 *
145 *---------------------------------------------------------------------------------------------------------------------------------------------------
146 *
147 * DENON
148 * -----
149 *
150 * frame: 0 start bits + 16 data bits + stop bit + 65ms pause + 16 inverted data bits + stop bit
151 * data: 5 address bits + 10 command bits
152 *
153 * data "0": data "1":
154 * ------________________ ------______________
155 * 275us 1050us 275us 1900us
156 *
157 *---------------------------------------------------------------------------------------------------------------------------------------------------
158 *
159 * RC6
160 * ---
161 *
162 * RC6 frame: 1 start bit + 1 bit "1" + 3 mode bits + 1 toggle bit + 16 data bits + 2666 µs pause
163 * RC6 data: 8 address bits + 8 command bits
164 *
165 * start bit toggle bit "0": toggle bit "1": data/mode "0": data/mode "1":
166 * ____________------- _______------- -------_______ _______------- -------_______
167 * 2666us 889us 889us 889us 889us 889us 444us 444us 444us 444us
168 *
169 *---------------------------------------------------------------------------------------------------------------------------------------------------
170 *
171 * APPLE
172 * -----
173 *
174 * frame: 1 start bit + 32 data bits + 1 stop bit
175 * data: 16 address bits + 11100000 + 8 command bits
176 *
177 * start bit: data "0": data "1": stop bit:
178 * -----------------_________ ------______ ------________________ ------______....
179 * 9000us 4500us 560us 560us 560us 1690 us 560us
180 *
181 *---------------------------------------------------------------------------------------------------------------------------------------------------
182 *
183 * NUBERT (subwoofer system)
184 * -------------------------
185 *
186 * frame: 1 start bit + 10 data bits + 1 stop bit
187 * data: 0 address bits + 10 command bits ?
188 *
189 * start bit: data "0": data "1": stop bit:
190 * ----------_____ ------______ ------________________ ------______....
191 * 1340us 340us 500us 1300us 1340us 340us 500us
192 *
193 *---------------------------------------------------------------------------------------------------------------------------------------------------
194 *
195 * BANG_OLUFSEN
196 * ------------
197 *
198 * frame: 4 start bits + 16 data bits + 1 trailer bit + 1 stop bit
199 * data: 0 address bits + 16 command bits
200 *
201 * 1st start bit: 2nd start bit: 3rd start bit: 4th start bit:
202 * -----________ -----________ -----_____________ -----________
203 * 210us 3000us 210us 3000us 210us 15000us 210us 3000us
204 *
205 * data "0": data "1": data "repeat bit": trailer bit: stop bit:
206 * -----________ -----_____________ -----___________ -----_____________ -----____...
207 * 210us 3000us 210us 9000us 210us 6000us 210us 12000us 210us
208 *
209 *---------------------------------------------------------------------------------------------------------------------------------------------------
210 *
211 * PANASONIC (older protocol, yet not implemented, see also MATSUSHITA, timing very similar)
212 * -----------------------------------------------------------------------------------------
213 *
214 * frame: 1 start bit + 22 data bits + 1 stop bit
215 * 22 data bits = 5 custom bits + 6 data bits + 5 inverted custom bits + 6 inverted data bits
216 *
217 * European version: T = 456us
218 * USA & Canada version: T = 422us
219 *
220 * start bit: data "0": data "1": stop bit:
221 * 8T 8T 2T 2T 2T 6T 2T
222 * -------------____________ ------_____ ------_____________ ------_______....
223 * 3648us 3648us 912us 912us 912us 2736us 912us (Europe)
224 * 3376us 3376us 844us 844us 844us 2532us 844us (US)
225 *
226 *---------------------------------------------------------------------------------------------------------------------------------------------------
227 *
228 * This program is free software; you can redistribute it and/or modify
229 * it under the terms of the GNU General Public License as published by
230 * the Free Software Foundation; either version 2 of the License, or
231 * (at your option) any later version.
232 *---------------------------------------------------------------------------------------------------------------------------------------------------
233 */
234
235 #if defined(__PCM__) || defined(__PCB__) || defined(__PCH__) // CCS PIC Compiler instead of AVR
236 #define PIC_CCS_COMPILER
237 #endif
238
239 #ifdef unix // test on linux/unix
240 #include <stdio.h>
241 #include <unistd.h>
242 #include <stdlib.h>
243 #include <string.h>
244 #include <inttypes.h>
245
246 #define DEBUG
247 #define PROGMEM
248 #define memcpy_P memcpy
249
250 #else // not unix:
251
252 #ifdef WIN32
253 #include <stdio.h>
254 #include <string.h>
255 typedef unsigned char uint8_t;
256 typedef unsigned short uint16_t;
257 #define DEBUG
258 #define PROGMEM
259 #define memcpy_P memcpy
260
261 #else
262
263 #ifndef CODEVISION
264
265 #ifdef PIC_CCS_COMPILER
266
267 #include <string.h>
268 typedef unsigned int8 uint8_t;
269 typedef unsigned int16 uint16_t;
270 #define PROGMEM
271 #define memcpy_P memcpy
272
273 #else // AVR:
274
275 #include <inttypes.h>
276 #include <stdio.h>
277 #include <string.h>
278 #include <avr/io.h>
279 #include <util/delay.h>
280 #include <avr/pgmspace.h>
281
282 #endif // PIC_CCS_COMPILER
283 #endif // CODEVISION
284
285 #endif // windows
286 #endif // unix
287
288 #include "irmp.h"
289 #include "irmpconfig.h"
290
291 #define IRMP_TIMEOUT_TIME 16500.0e-6 // timeout after 16.5 ms darkness
292 #define IRMP_TIMEOUT_LEN (uint8_t)(F_INTERRUPTS * IRMP_TIMEOUT_TIME + 0.5)
293 #define IRMP_REPETITION_TIME (uint16_t)(F_INTERRUPTS * 100.0e-3 + 0.5) // autodetect key repetition within 100 msec
294
295 #define MIN_TOLERANCE_00 1.0 // -0%
296 #define MAX_TOLERANCE_00 1.0 // +0%
297
298 #define MIN_TOLERANCE_05 0.95 // -5%
299 #define MAX_TOLERANCE_05 1.05 // +5%
300
301 #define MIN_TOLERANCE_10 0.9 // -10%
302 #define MAX_TOLERANCE_10 1.1 // +10%
303
304 #define MIN_TOLERANCE_15 0.85 // -15%
305 #define MAX_TOLERANCE_15 1.15 // +15%
306
307 #define MIN_TOLERANCE_20 0.8 // -20%
308 #define MAX_TOLERANCE_20 1.2 // +20%
309
310 #define MIN_TOLERANCE_30 0.7 // -30%
311 #define MAX_TOLERANCE_30 1.3 // +30%
312
313 #define MIN_TOLERANCE_40 0.6 // -40%
314 #define MAX_TOLERANCE_40 1.4 // +40%
315
316 #define MIN_TOLERANCE_50 0.5 // -50%
317 #define MAX_TOLERANCE_50 1.5 // +50%
318
319 #define MIN_TOLERANCE_60 0.4 // -60%
320 #define MAX_TOLERANCE_60 1.6 // +60%
321
322 #define SIRCS_START_BIT_PULSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * SIRCS_START_BIT_PULSE_TIME * MIN_TOLERANCE_10 + 0.5) - 1)
323 #define SIRCS_START_BIT_PULSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * SIRCS_START_BIT_PULSE_TIME * MAX_TOLERANCE_10 + 0.5) + 1)
324 #define SIRCS_START_BIT_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * SIRCS_START_BIT_PAUSE_TIME * MIN_TOLERANCE_20 + 0.5) - 1)
325 #define SIRCS_START_BIT_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * SIRCS_START_BIT_PAUSE_TIME * MAX_TOLERANCE_05 + 0.5) + 1) // only 5% to avoid conflict with RC6
326 #define SIRCS_1_PULSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * SIRCS_1_PULSE_TIME * MIN_TOLERANCE_10 + 0.5) - 1)
327 #define SIRCS_1_PULSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * SIRCS_1_PULSE_TIME * MAX_TOLERANCE_10 + 0.5) + 1)
328 #define SIRCS_0_PULSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * SIRCS_0_PULSE_TIME * MIN_TOLERANCE_10 + 0.5) - 1)
329 #define SIRCS_0_PULSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * SIRCS_0_PULSE_TIME * MAX_TOLERANCE_10 + 0.5) + 1)
330 #define SIRCS_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * SIRCS_PAUSE_TIME * MIN_TOLERANCE_10 + 0.5) - 1)
331 #define SIRCS_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * SIRCS_PAUSE_TIME * MAX_TOLERANCE_10 + 0.5) + 1)
332
333 #define NEC_START_BIT_PULSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * NEC_START_BIT_PULSE_TIME * MIN_TOLERANCE_40 + 0.5) - 1)
334 #define NEC_START_BIT_PULSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * NEC_START_BIT_PULSE_TIME * MAX_TOLERANCE_40 + 0.5) + 1)
335 #define NEC_START_BIT_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * NEC_START_BIT_PAUSE_TIME * MIN_TOLERANCE_40 + 0.5) - 1)
336 #define NEC_START_BIT_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * NEC_START_BIT_PAUSE_TIME * MAX_TOLERANCE_40 + 0.5) + 1)
337 #define NEC_REPEAT_START_BIT_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * NEC_REPEAT_START_BIT_PAUSE_TIME * MIN_TOLERANCE_40 + 0.5) - 1)
338 #define NEC_REPEAT_START_BIT_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * NEC_REPEAT_START_BIT_PAUSE_TIME * MAX_TOLERANCE_40 + 0.5) + 1)
339 #define NEC_PULSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * NEC_PULSE_TIME * MIN_TOLERANCE_40 + 0.5) - 1)
340 #define NEC_PULSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * NEC_PULSE_TIME * MAX_TOLERANCE_40 + 0.5) + 1)
341 #define NEC_1_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * NEC_1_PAUSE_TIME * MIN_TOLERANCE_40 + 0.5) - 1)
342 #define NEC_1_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * NEC_1_PAUSE_TIME * MAX_TOLERANCE_40 + 0.5) + 1)
343 #define NEC_0_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * NEC_0_PAUSE_TIME * MIN_TOLERANCE_40 + 0.5) - 1)
344 #define NEC_0_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * NEC_0_PAUSE_TIME * MAX_TOLERANCE_40 + 0.5) + 1)
345
346 #define SAMSUNG_START_BIT_PULSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * SAMSUNG_START_BIT_PULSE_TIME * MIN_TOLERANCE_10 + 0.5) - 1)
347 #define SAMSUNG_START_BIT_PULSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * SAMSUNG_START_BIT_PULSE_TIME * MAX_TOLERANCE_10 + 0.5) + 1)
348 #define SAMSUNG_START_BIT_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * SAMSUNG_START_BIT_PAUSE_TIME * MIN_TOLERANCE_10 + 0.5) - 1)
349 #define SAMSUNG_START_BIT_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * SAMSUNG_START_BIT_PAUSE_TIME * MAX_TOLERANCE_10 + 0.5) + 1)
350 #define SAMSUNG_PULSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * SAMSUNG_PULSE_TIME * MIN_TOLERANCE_50 + 0.5) - 1)
351 #define SAMSUNG_PULSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * SAMSUNG_PULSE_TIME * MAX_TOLERANCE_50 + 0.5) + 1)
352 #define SAMSUNG_1_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * SAMSUNG_1_PAUSE_TIME * MIN_TOLERANCE_50 + 0.5) - 1)
353 #define SAMSUNG_1_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * SAMSUNG_1_PAUSE_TIME * MAX_TOLERANCE_50 + 0.5) + 1)
354 #define SAMSUNG_0_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * SAMSUNG_0_PAUSE_TIME * MIN_TOLERANCE_50 + 0.5) - 1)
355 #define SAMSUNG_0_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * SAMSUNG_0_PAUSE_TIME * MAX_TOLERANCE_50 + 0.5) + 1)
356
357 #define MATSUSHITA_START_BIT_PULSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * MATSUSHITA_START_BIT_PULSE_TIME * MIN_TOLERANCE_20 + 0.5) - 1)
358 #define MATSUSHITA_START_BIT_PULSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * MATSUSHITA_START_BIT_PULSE_TIME * MAX_TOLERANCE_20 + 0.5) + 1)
359 #define MATSUSHITA_START_BIT_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * MATSUSHITA_START_BIT_PAUSE_TIME * MIN_TOLERANCE_20 + 0.5) - 1)
360 #define MATSUSHITA_START_BIT_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * MATSUSHITA_START_BIT_PAUSE_TIME * MAX_TOLERANCE_20 + 0.5) + 1)
361 #define MATSUSHITA_PULSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * MATSUSHITA_PULSE_TIME * MIN_TOLERANCE_40 + 0.5) - 1)
362 #define MATSUSHITA_PULSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * MATSUSHITA_PULSE_TIME * MAX_TOLERANCE_40 + 0.5) + 1)
363 #define MATSUSHITA_1_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * MATSUSHITA_1_PAUSE_TIME * MIN_TOLERANCE_40 + 0.5) - 1)
364 #define MATSUSHITA_1_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * MATSUSHITA_1_PAUSE_TIME * MAX_TOLERANCE_40 + 0.5) + 1)
365 #define MATSUSHITA_0_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * MATSUSHITA_0_PAUSE_TIME * MIN_TOLERANCE_40 + 0.5) - 1)
366 #define MATSUSHITA_0_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * MATSUSHITA_0_PAUSE_TIME * MAX_TOLERANCE_40 + 0.5) + 1)
367
368 #define KASEIKYO_START_BIT_PULSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * KASEIKYO_START_BIT_PULSE_TIME * MIN_TOLERANCE_10 + 0.5) - 1)
369 #define KASEIKYO_START_BIT_PULSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * KASEIKYO_START_BIT_PULSE_TIME * MAX_TOLERANCE_10 + 0.5) + 1)
370 #define KASEIKYO_START_BIT_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * KASEIKYO_START_BIT_PAUSE_TIME * MIN_TOLERANCE_10 + 0.5) - 1)
371 #define KASEIKYO_START_BIT_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * KASEIKYO_START_BIT_PAUSE_TIME * MAX_TOLERANCE_10 + 0.5) + 1)
372 #define KASEIKYO_PULSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * KASEIKYO_PULSE_TIME * MIN_TOLERANCE_50 + 0.5) - 1)
373 #define KASEIKYO_PULSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * KASEIKYO_PULSE_TIME * MAX_TOLERANCE_60 + 0.5) + 1)
374 #define KASEIKYO_1_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * KASEIKYO_1_PAUSE_TIME * MIN_TOLERANCE_50 + 0.5) - 1)
375 #define KASEIKYO_1_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * KASEIKYO_1_PAUSE_TIME * MAX_TOLERANCE_50 + 0.5) + 1)
376 #define KASEIKYO_0_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * KASEIKYO_0_PAUSE_TIME * MIN_TOLERANCE_50 + 0.5) - 1)
377 #define KASEIKYO_0_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * KASEIKYO_0_PAUSE_TIME * MAX_TOLERANCE_50 + 0.5) + 1)
378
379 #define RECS80_START_BIT_PULSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * RECS80_START_BIT_PULSE_TIME * MIN_TOLERANCE_00 + 0.5) - 1)
380 #define RECS80_START_BIT_PULSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * RECS80_START_BIT_PULSE_TIME * MAX_TOLERANCE_10 + 0.5) + 1)
381 #define RECS80_START_BIT_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * RECS80_START_BIT_PAUSE_TIME * MIN_TOLERANCE_10 + 0.5) - 1)
382 #define RECS80_START_BIT_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * RECS80_START_BIT_PAUSE_TIME * MAX_TOLERANCE_10 + 0.5) + 1)
383 #define RECS80_PULSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * RECS80_PULSE_TIME * MIN_TOLERANCE_10 + 0.5) - 1)
384 #define RECS80_PULSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * RECS80_PULSE_TIME * MAX_TOLERANCE_10 + 0.5) + 1)
385 #define RECS80_1_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * RECS80_1_PAUSE_TIME * MIN_TOLERANCE_10 + 0.5) - 1)
386 #define RECS80_1_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * RECS80_1_PAUSE_TIME * MAX_TOLERANCE_10 + 0.5) + 1)
387 #define RECS80_0_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * RECS80_0_PAUSE_TIME * MIN_TOLERANCE_10 + 0.5) - 1)
388 #define RECS80_0_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * RECS80_0_PAUSE_TIME * MAX_TOLERANCE_10 + 0.5) + 1)
389
390 #define RC5_START_BIT_LEN_MIN ((uint8_t)(F_INTERRUPTS * RC5_BIT_TIME * MIN_TOLERANCE_20 + 0.5) - 1)
391 #define RC5_START_BIT_LEN_MAX ((uint8_t)(F_INTERRUPTS * RC5_BIT_TIME * MAX_TOLERANCE_20 + 0.5) + 1)
392 #define RC5_BIT_LEN_MIN ((uint8_t)(F_INTERRUPTS * RC5_BIT_TIME * MIN_TOLERANCE_20 + 0.5) - 1)
393 #define RC5_BIT_LEN_MAX ((uint8_t)(F_INTERRUPTS * RC5_BIT_TIME * MAX_TOLERANCE_20 + 0.5) + 1)
394
395 #define DENON_PULSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * DENON_PULSE_TIME * MIN_TOLERANCE_20 + 0.5) - 1)
396 #define DENON_PULSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * DENON_PULSE_TIME * MAX_TOLERANCE_20 + 0.5) + 1)
397 #define DENON_1_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * DENON_1_PAUSE_TIME * MIN_TOLERANCE_20 + 0.5) - 1)
398 #define DENON_1_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * DENON_1_PAUSE_TIME * MAX_TOLERANCE_20 + 0.5) + 1)
399 #define DENON_0_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * DENON_0_PAUSE_TIME * MIN_TOLERANCE_20 + 0.5) - 1)
400 #define DENON_0_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * DENON_0_PAUSE_TIME * MAX_TOLERANCE_20 + 0.5) + 1)
401
402 #define RC6_START_BIT_PULSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * RC6_START_BIT_PULSE_TIME * MIN_TOLERANCE_10 + 0.5) - 1)
403 #define RC6_START_BIT_PULSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * RC6_START_BIT_PULSE_TIME * MAX_TOLERANCE_10 + 0.5) + 1)
404 #define RC6_START_BIT_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * RC6_START_BIT_PAUSE_TIME * MIN_TOLERANCE_10 + 0.5) - 1)
405 #define RC6_START_BIT_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * RC6_START_BIT_PAUSE_TIME * MAX_TOLERANCE_10 + 0.5) + 1)
406 #define RC6_TOGGLE_BIT_LEN_MIN ((uint8_t)(F_INTERRUPTS * RC6_TOGGLE_BIT_TIME * MIN_TOLERANCE_10 + 0.5) - 1)
407 #define RC6_TOGGLE_BIT_LEN_MAX ((uint8_t)(F_INTERRUPTS * RC6_TOGGLE_BIT_TIME * MAX_TOLERANCE_10 + 0.5) + 1)
408 #define RC6_BIT_LEN_MIN ((uint8_t)(F_INTERRUPTS * RC6_BIT_TIME * MIN_TOLERANCE_10 + 0.5) - 1)
409 #define RC6_BIT_LEN_MAX ((uint8_t)(F_INTERRUPTS * RC6_BIT_TIME * MAX_TOLERANCE_10 + 0.5) + 1)
410
411 #define RECS80EXT_START_BIT_PULSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * RECS80EXT_START_BIT_PULSE_TIME * MIN_TOLERANCE_00 + 0.5) - 1)
412 #define RECS80EXT_START_BIT_PULSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * RECS80EXT_START_BIT_PULSE_TIME * MAX_TOLERANCE_00 + 0.5) + 1)
413 #define RECS80EXT_START_BIT_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * RECS80EXT_START_BIT_PAUSE_TIME * MIN_TOLERANCE_05 + 0.5) - 1)
414 #define RECS80EXT_START_BIT_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * RECS80EXT_START_BIT_PAUSE_TIME * MAX_TOLERANCE_05 + 0.5) + 1)
415 #define RECS80EXT_PULSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * RECS80EXT_PULSE_TIME * MIN_TOLERANCE_10 + 0.5) - 1)
416 #define RECS80EXT_PULSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * RECS80EXT_PULSE_TIME * MAX_TOLERANCE_10 + 0.5) + 1)
417 #define RECS80EXT_1_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * RECS80EXT_1_PAUSE_TIME * MIN_TOLERANCE_10 + 0.5) - 1)
418 #define RECS80EXT_1_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * RECS80EXT_1_PAUSE_TIME * MAX_TOLERANCE_10 + 0.5) + 1)
419 #define RECS80EXT_0_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * RECS80EXT_0_PAUSE_TIME * MIN_TOLERANCE_10 + 0.5) - 1)
420 #define RECS80EXT_0_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * RECS80EXT_0_PAUSE_TIME * MAX_TOLERANCE_10 + 0.5) + 1)
421
422 #define NUBERT_START_BIT_PULSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * NUBERT_START_BIT_PULSE_TIME * MIN_TOLERANCE_20 + 0.5) - 1)
423 #define NUBERT_START_BIT_PULSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * NUBERT_START_BIT_PULSE_TIME * MAX_TOLERANCE_20 + 0.5) + 1)
424 #define NUBERT_START_BIT_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * NUBERT_START_BIT_PAUSE_TIME * MIN_TOLERANCE_20 + 0.5) - 1)
425 #define NUBERT_START_BIT_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * NUBERT_START_BIT_PAUSE_TIME * MAX_TOLERANCE_20 + 0.5) + 1)
426 #define NUBERT_1_PULSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * NUBERT_1_PULSE_TIME * MIN_TOLERANCE_20 + 0.5) - 1)
427 #define NUBERT_1_PULSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * NUBERT_1_PULSE_TIME * MAX_TOLERANCE_20 + 0.5) + 1)
428 #define NUBERT_1_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * NUBERT_1_PAUSE_TIME * MIN_TOLERANCE_20 + 0.5) - 1)
429 #define NUBERT_1_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * NUBERT_1_PAUSE_TIME * MAX_TOLERANCE_20 + 0.5) + 1)
430 #define NUBERT_0_PULSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * NUBERT_0_PULSE_TIME * MIN_TOLERANCE_20 + 0.5) - 1)
431 #define NUBERT_0_PULSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * NUBERT_0_PULSE_TIME * MAX_TOLERANCE_20 + 0.5) + 1)
432 #define NUBERT_0_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * NUBERT_0_PAUSE_TIME * MIN_TOLERANCE_20 + 0.5) - 1)
433 #define NUBERT_0_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * NUBERT_0_PAUSE_TIME * MAX_TOLERANCE_20 + 0.5) + 1)
434
435 #define BANG_OLUFSEN_START_BIT1_PULSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * BANG_OLUFSEN_START_BIT1_PULSE_TIME * MIN_TOLERANCE_05 + 0.5) - 1)
436 #define BANG_OLUFSEN_START_BIT1_PULSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * BANG_OLUFSEN_START_BIT1_PULSE_TIME * MAX_TOLERANCE_05 + 0.5) + 1)
437 #define BANG_OLUFSEN_START_BIT1_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * BANG_OLUFSEN_START_BIT1_PAUSE_TIME * MIN_TOLERANCE_05 + 0.5) - 1)
438 #define BANG_OLUFSEN_START_BIT1_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * BANG_OLUFSEN_START_BIT1_PAUSE_TIME * MAX_TOLERANCE_05 + 0.5) + 1)
439 #define BANG_OLUFSEN_START_BIT2_PULSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * BANG_OLUFSEN_START_BIT2_PULSE_TIME * MIN_TOLERANCE_05 + 0.5) - 1)
440 #define BANG_OLUFSEN_START_BIT2_PULSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * BANG_OLUFSEN_START_BIT2_PULSE_TIME * MAX_TOLERANCE_05 + 0.5) + 1)
441 #define BANG_OLUFSEN_START_BIT2_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * BANG_OLUFSEN_START_BIT2_PAUSE_TIME * MIN_TOLERANCE_05 + 0.5) - 1)
442 #define BANG_OLUFSEN_START_BIT2_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * BANG_OLUFSEN_START_BIT2_PAUSE_TIME * MAX_TOLERANCE_05 + 0.5) + 1)
443 #define BANG_OLUFSEN_START_BIT3_PULSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * BANG_OLUFSEN_START_BIT3_PULSE_TIME * MIN_TOLERANCE_05 + 0.5) - 1)
444 #define BANG_OLUFSEN_START_BIT3_PULSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * BANG_OLUFSEN_START_BIT3_PULSE_TIME * MAX_TOLERANCE_05 + 0.5) + 1)
445 #define BANG_OLUFSEN_START_BIT3_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * BANG_OLUFSEN_START_BIT3_PAUSE_TIME * MIN_TOLERANCE_05 + 0.5) - 1)
446 #define BANG_OLUFSEN_START_BIT3_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * BANG_OLUFSEN_START_BIT3_PAUSE_TIME * MAX_TOLERANCE_05 + 0.5) + 1)
447 #define BANG_OLUFSEN_START_BIT4_PULSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * BANG_OLUFSEN_START_BIT4_PULSE_TIME * MIN_TOLERANCE_05 + 0.5) - 1)
448 #define BANG_OLUFSEN_START_BIT4_PULSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * BANG_OLUFSEN_START_BIT4_PULSE_TIME * MAX_TOLERANCE_05 + 0.5) + 1)
449 #define BANG_OLUFSEN_START_BIT4_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * BANG_OLUFSEN_START_BIT4_PAUSE_TIME * MIN_TOLERANCE_05 + 0.5) - 1)
450 #define BANG_OLUFSEN_START_BIT4_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * BANG_OLUFSEN_START_BIT4_PAUSE_TIME * MAX_TOLERANCE_05 + 0.5) + 1)
451 #define BANG_OLUFSEN_PULSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * BANG_OLUFSEN_PULSE_TIME * MIN_TOLERANCE_05 + 0.5) - 1)
452 #define BANG_OLUFSEN_PULSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * BANG_OLUFSEN_PULSE_TIME * MAX_TOLERANCE_05 + 0.5) + 1)
453 #define BANG_OLUFSEN_1_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * BANG_OLUFSEN_1_PAUSE_TIME * MIN_TOLERANCE_05 + 0.5) - 1)
454 #define BANG_OLUFSEN_1_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * BANG_OLUFSEN_1_PAUSE_TIME * MAX_TOLERANCE_05 + 0.5) + 1)
455 #define BANG_OLUFSEN_0_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * BANG_OLUFSEN_0_PAUSE_TIME * MIN_TOLERANCE_05 + 0.5) - 1)
456 #define BANG_OLUFSEN_0_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * BANG_OLUFSEN_0_PAUSE_TIME * MAX_TOLERANCE_05 + 0.5) + 1)
457 #define BANG_OLUFSEN_R_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * BANG_OLUFSEN_R_PAUSE_TIME * MIN_TOLERANCE_05 + 0.5) - 1)
458 #define BANG_OLUFSEN_R_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * BANG_OLUFSEN_R_PAUSE_TIME * MAX_TOLERANCE_05 + 0.5) + 1)
459 #define BANG_OLUFSEN_TRAILER_BIT_PAUSE_LEN_MIN ((uint8_t)(F_INTERRUPTS * BANG_OLUFSEN_TRAILER_BIT_PAUSE_TIME * MIN_TOLERANCE_05 + 0.5) - 1)
460 #define BANG_OLUFSEN_TRAILER_BIT_PAUSE_LEN_MAX ((uint8_t)(F_INTERRUPTS * BANG_OLUFSEN_TRAILER_BIT_PAUSE_TIME * MAX_TOLERANCE_05 + 0.5) + 1)
461
462 #define AUTO_REPETITION_LEN (uint16_t)(F_INTERRUPTS * AUTO_REPETITION_TIME + 0.5) // use uint16_t!
463
464 #ifdef DEBUG
465 #define DEBUG_PUTCHAR(a) { if (! silent) { putchar (a); } }
466 #define DEBUG_PRINTF(...) { if (! silent) { printf (__VA_ARGS__); } }
467 static int silent;
468 #else
469 #define DEBUG_PUTCHAR(a)
470 #define DEBUG_PRINTF(...)
471 #endif
472
473 #if IRMP_LOGGING == 1
474 #define irmp_logIsr(x) irmp_logIr((x) ? 1:0)
475 #define UART_BAUD 9600L
476
477 // calculate real baud rate:
478 #define UBRR_VAL ((F_CPU + UART_BAUD * 8) / (UART_BAUD * 16) - 1) // round
479 #define BAUD_REAL (F_CPU / (16 * (UBRR_VAL + 1))) // real baudrate
480
481 #ifdef CODEVISION
482 #if ((BAUD_REAL * 1000) / UART_BAUD - 1000) > 10
483 # error Error of baud rate of RS232 UARTx is more than 1%. That is too high!
484 #endif
485
486 #else // not CODEVISION
487
488 #define BAUD_ERROR ((BAUD_REAL * 1000) / UART_BAUD - 1000) // error in promille
489
490 #if ((BAUD_ERROR > 10) || (-BAUD_ERROR < 10))
491 # error Error of baud rate of RS232 UARTx is more than 1%. That is too high!
492 #endif
493
494 #endif // CODEVISION
495
496 /*---------------------------------------------------------------------------------------------------------------------------------------------------
497 * Initialize UART
498 * @details Initializes UART
499 *---------------------------------------------------------------------------------------------------------------------------------------------------
500 */
501 void
502 irmp_uart_init (void)
503 {
504 UCSR0B |= (1<<TXEN0); // activate UART0 TX
505 UBRR0H = UBRR_VAL >> 8; // store baudrate (upper byte)
506 UBRR0L = UBRR_VAL & 0xFF; // store baudrate (lower byte)
507 }
508
509 /*---------------------------------------------------------------------------------------------------------------------------------------------------
510 * Send character
511 * @details Sends character
512 * @param ch character to be transmitted
513 *---------------------------------------------------------------------------------------------------------------------------------------------------
514 */
515 void
516 irmp_uart_putc (unsigned char ch)
517 {
518 while (!(UCSR0A & (1<<UDRE0)))
519 {
520 ;
521 }
522
523 UDR0 = ch;
524 }
525
526 /*---------------------------------------------------------------------------------------------------------------------------------------------------
527 * Log IR signal
528 *---------------------------------------------------------------------------------------------------------------------------------------------------
529 */
530 #define c_startcycles 2 // min count of zeros before start of logging
531 #define c_endBits 1000 // log buffer size
532 #define c_datalen 700 // number of sequenced highbits to detect end
533
534 static void
535 irmp_logIr (uint8_t val)
536 {
537 static uint8_t s_data[c_datalen]; // logging buffer
538 static uint16_t s_dataIdx; // number of written bits
539 static uint8_t s_startcycles; // current number of start-zeros
540 static uint16_t s_ctr; // counts sequenced highbits - to detect end
541
542 if ((val == 0) && (s_startcycles < c_startcycles) && !s_dataIdx) // prevent that single random zeros init logging
543 {
544 ++s_startcycles;
545 }
546 else
547 {
548 s_startcycles = 0;
549
550 if ( (val == 0) // start or continue logging on "0"
551 || ((val == 1) && (s_dataIdx != 0))) // "1" cannot init logging
552 {
553 if (val)
554 { // set or clear bit in bitarray
555 s_data[(s_dataIdx / 8)] |= (1<<(s_dataIdx % 8));
556 }
557 else
558 {
559 s_data[(s_dataIdx / 8)] &= ~(1<<(s_dataIdx % 8));
560 }
561
562 ++s_dataIdx;
563
564 if (val)
565 { // if high received then look at log-stop condition
566 ++s_ctr;
567
568 if (s_ctr > c_endBits)
569 { // if stop condition (200 sequenced ones) meets, output on uart
570 uint16_t i;
571
572 for (i = 0; i < c_startcycles; ++i)
573 {
574 irmp_uart_putc ('0'); // the ignored starting zeros
575 }
576
577 for (i = 0;i < (s_dataIdx - c_endBits + 20) / 8; ++i) // transform bitset into uart chars
578 {
579 uint8_t d = s_data[i];
580 uint8_t j;
581
582 for (j = 0;j<8;++j)
583 {
584 irmp_uart_putc ((d & 1) + '0');
585 d >>= 1;
586 }
587 }
588
589 irmp_uart_putc ('\n');
590 s_dataIdx = 0;
591 }
592 }
593 else
594 {
595 s_ctr = 0;
596 }
597 }
598 }
599 }
600
601 #else
602 #define irmp_logIsr(x)
603 #endif
604
605 typedef struct
606 {
607 uint8_t protocol; // ir protocol
608 uint8_t pulse_1_len_min; // minimum length of pulse with bit value 1
609 uint8_t pulse_1_len_max; // maximum length of pulse with bit value 1
610 uint8_t pause_1_len_min; // minimum length of pause with bit value 1
611 uint8_t pause_1_len_max; // maximum length of pause with bit value 1
612 uint8_t pulse_0_len_min; // minimum length of pulse with bit value 0
613 uint8_t pulse_0_len_max; // maximum length of pulse with bit value 0
614 uint8_t pause_0_len_min; // minimum length of pause with bit value 0
615 uint8_t pause_0_len_max; // maximum length of pause with bit value 0
616 uint8_t address_offset; // address offset
617 uint8_t address_end; // end of address
618 uint8_t command_offset; // command offset
619 uint8_t command_end; // end of command
620 uint8_t complete_len; // complete length of frame
621 uint8_t stop_bit; // flag: frame has stop bit
622 uint8_t lsb_first; // flag: LSB first
623 } IRMP_PARAMETER;
624
625 #if IRMP_SUPPORT_SIRCS_PROTOCOL == 1
626
627 static PROGMEM IRMP_PARAMETER sircs_param =
628 {
629 IRMP_SIRCS_PROTOCOL,
630 SIRCS_1_PULSE_LEN_MIN,
631 SIRCS_1_PULSE_LEN_MAX,
632 SIRCS_PAUSE_LEN_MIN,
633 SIRCS_PAUSE_LEN_MAX,
634 SIRCS_0_PULSE_LEN_MIN,
635 SIRCS_0_PULSE_LEN_MAX,
636 SIRCS_PAUSE_LEN_MIN,
637 SIRCS_PAUSE_LEN_MAX,
638 SIRCS_ADDRESS_OFFSET,
639 SIRCS_ADDRESS_OFFSET + SIRCS_ADDRESS_LEN,
640 SIRCS_COMMAND_OFFSET,
641 SIRCS_COMMAND_OFFSET + SIRCS_COMMAND_LEN,
642 SIRCS_COMPLETE_DATA_LEN,
643 SIRCS_STOP_BIT,
644 SIRCS_LSB
645 };
646
647 #endif
648
649 #if IRMP_SUPPORT_NEC_PROTOCOL == 1
650
651 static PROGMEM IRMP_PARAMETER nec_param =
652 {
653 IRMP_NEC_PROTOCOL,
654 NEC_PULSE_LEN_MIN,
655 NEC_PULSE_LEN_MAX,
656 NEC_1_PAUSE_LEN_MIN,
657 NEC_1_PAUSE_LEN_MAX,
658 NEC_PULSE_LEN_MIN,
659 NEC_PULSE_LEN_MAX,
660 NEC_0_PAUSE_LEN_MIN,
661 NEC_0_PAUSE_LEN_MAX,
662 NEC_ADDRESS_OFFSET,
663 NEC_ADDRESS_OFFSET + NEC_ADDRESS_LEN,
664 NEC_COMMAND_OFFSET,
665 NEC_COMMAND_OFFSET + NEC_COMMAND_LEN,
666 NEC_COMPLETE_DATA_LEN,
667 NEC_STOP_BIT,
668 NEC_LSB
669 };
670
671 static PROGMEM IRMP_PARAMETER nec_rep_param =
672 {
673 IRMP_NEC_PROTOCOL,
674 NEC_PULSE_LEN_MIN,
675 NEC_PULSE_LEN_MAX,
676 NEC_1_PAUSE_LEN_MIN,
677 NEC_1_PAUSE_LEN_MAX,
678 NEC_PULSE_LEN_MIN,
679 NEC_PULSE_LEN_MAX,
680 NEC_0_PAUSE_LEN_MIN,
681 NEC_0_PAUSE_LEN_MAX,
682 0,
683 0,
684 0,
685 0,
686 0,
687 NEC_STOP_BIT,
688 NEC_LSB
689 };
690
691 #endif
692
693 #if IRMP_SUPPORT_SAMSUNG_PROTOCOL == 1
694
695 static PROGMEM IRMP_PARAMETER samsung_param =
696 {
697 IRMP_SAMSUNG_PROTOCOL,
698 SAMSUNG_PULSE_LEN_MIN,
699 SAMSUNG_PULSE_LEN_MAX,
700 SAMSUNG_1_PAUSE_LEN_MIN,
701 SAMSUNG_1_PAUSE_LEN_MAX,
702 SAMSUNG_PULSE_LEN_MIN,
703 SAMSUNG_PULSE_LEN_MAX,
704 SAMSUNG_0_PAUSE_LEN_MIN,
705 SAMSUNG_0_PAUSE_LEN_MAX,
706 SAMSUNG_ADDRESS_OFFSET,
707 SAMSUNG_ADDRESS_OFFSET + SAMSUNG_ADDRESS_LEN,
708 SAMSUNG_COMMAND_OFFSET,
709 SAMSUNG_COMMAND_OFFSET + SAMSUNG_COMMAND_LEN,
710 SAMSUNG_COMPLETE_DATA_LEN,
711 SAMSUNG_STOP_BIT,
712 SAMSUNG_LSB
713 };
714
715 #endif
716
717 #if IRMP_SUPPORT_MATSUSHITA_PROTOCOL == 1
718
719 static PROGMEM IRMP_PARAMETER matsushita_param =
720 {
721 IRMP_MATSUSHITA_PROTOCOL,
722 MATSUSHITA_PULSE_LEN_MIN,
723 MATSUSHITA_PULSE_LEN_MAX,
724 MATSUSHITA_1_PAUSE_LEN_MIN,
725 MATSUSHITA_1_PAUSE_LEN_MAX,
726 MATSUSHITA_PULSE_LEN_MIN,
727 MATSUSHITA_PULSE_LEN_MAX,
728 MATSUSHITA_0_PAUSE_LEN_MIN,
729 MATSUSHITA_0_PAUSE_LEN_MAX,
730 MATSUSHITA_ADDRESS_OFFSET,
731 MATSUSHITA_ADDRESS_OFFSET + MATSUSHITA_ADDRESS_LEN,
732 MATSUSHITA_COMMAND_OFFSET,
733 MATSUSHITA_COMMAND_OFFSET + MATSUSHITA_COMMAND_LEN,
734 MATSUSHITA_COMPLETE_DATA_LEN,
735 MATSUSHITA_STOP_BIT,
736 MATSUSHITA_LSB
737 };
738
739 #endif
740
741 #if IRMP_SUPPORT_KASEIKYO_PROTOCOL == 1
742
743 static PROGMEM IRMP_PARAMETER kaseikyo_param =
744 {
745 IRMP_KASEIKYO_PROTOCOL,
746 KASEIKYO_PULSE_LEN_MIN,
747 KASEIKYO_PULSE_LEN_MAX,
748 KASEIKYO_1_PAUSE_LEN_MIN,
749 KASEIKYO_1_PAUSE_LEN_MAX,
750 KASEIKYO_PULSE_LEN_MIN,
751 KASEIKYO_PULSE_LEN_MAX,
752 KASEIKYO_0_PAUSE_LEN_MIN,
753 KASEIKYO_0_PAUSE_LEN_MAX,
754 KASEIKYO_ADDRESS_OFFSET,
755 KASEIKYO_ADDRESS_OFFSET + KASEIKYO_ADDRESS_LEN,
756 KASEIKYO_COMMAND_OFFSET,
757 KASEIKYO_COMMAND_OFFSET + KASEIKYO_COMMAND_LEN,
758 KASEIKYO_COMPLETE_DATA_LEN,
759 KASEIKYO_STOP_BIT,
760 KASEIKYO_LSB
761 };
762
763 #endif
764
765 #if IRMP_SUPPORT_RECS80_PROTOCOL == 1
766
767 static PROGMEM IRMP_PARAMETER recs80_param =
768 {
769 IRMP_RECS80_PROTOCOL,
770 RECS80_PULSE_LEN_MIN,
771 RECS80_PULSE_LEN_MAX,
772 RECS80_1_PAUSE_LEN_MIN,
773 RECS80_1_PAUSE_LEN_MAX,
774 RECS80_PULSE_LEN_MIN,
775 RECS80_PULSE_LEN_MAX,
776 RECS80_0_PAUSE_LEN_MIN,
777 RECS80_0_PAUSE_LEN_MAX,
778 RECS80_ADDRESS_OFFSET,
779 RECS80_ADDRESS_OFFSET + RECS80_ADDRESS_LEN,
780 RECS80_COMMAND_OFFSET,
781 RECS80_COMMAND_OFFSET + RECS80_COMMAND_LEN,
782 RECS80_COMPLETE_DATA_LEN,
783 RECS80_STOP_BIT,
784 RECS80_LSB
785 };
786
787 #endif
788
789 #if IRMP_SUPPORT_RC5_PROTOCOL == 1
790
791 static PROGMEM IRMP_PARAMETER rc5_param =
792 {
793 IRMP_RC5_PROTOCOL,
794 RC5_BIT_LEN_MIN,
795 RC5_BIT_LEN_MAX,
796 RC5_BIT_LEN_MIN,
797 RC5_BIT_LEN_MAX,
798 1, // tricky: use this as stop bit length
799 1,
800 1,
801 1,
802 RC5_ADDRESS_OFFSET,
803 RC5_ADDRESS_OFFSET + RC5_ADDRESS_LEN,
804 RC5_COMMAND_OFFSET,
805 RC5_COMMAND_OFFSET + RC5_COMMAND_LEN,
806 RC5_COMPLETE_DATA_LEN,
807 RC5_STOP_BIT,
808 RC5_LSB
809 };
810
811 #endif
812
813 #if IRMP_SUPPORT_DENON_PROTOCOL == 1
814
815 static PROGMEM IRMP_PARAMETER denon_param =
816 {
817 IRMP_DENON_PROTOCOL,
818 DENON_PULSE_LEN_MIN,
819 DENON_PULSE_LEN_MAX,
820 DENON_1_PAUSE_LEN_MIN,
821 DENON_1_PAUSE_LEN_MAX,
822 DENON_PULSE_LEN_MIN,
823 DENON_PULSE_LEN_MAX,
824 DENON_0_PAUSE_LEN_MIN,
825 DENON_0_PAUSE_LEN_MAX,
826 DENON_ADDRESS_OFFSET,
827 DENON_ADDRESS_OFFSET + DENON_ADDRESS_LEN,
828 DENON_COMMAND_OFFSET,
829 DENON_COMMAND_OFFSET + DENON_COMMAND_LEN,
830 DENON_COMPLETE_DATA_LEN,
831 DENON_STOP_BIT,
832 DENON_LSB
833 };
834
835 #endif
836
837 #if IRMP_SUPPORT_RC6_PROTOCOL == 1
838
839 static PROGMEM IRMP_PARAMETER rc6_param =
840 {
841 IRMP_RC6_PROTOCOL,
842 RC6_BIT_LEN_MIN,
843 RC6_BIT_LEN_MAX,
844 RC6_BIT_LEN_MIN,
845 RC6_BIT_LEN_MAX,
846 1, // tricky: use this as stop bit length
847 1,
848 1,
849 1,
850 RC6_ADDRESS_OFFSET,
851 RC6_ADDRESS_OFFSET + RC6_ADDRESS_LEN,
852 RC6_COMMAND_OFFSET,
853 RC6_COMMAND_OFFSET + RC6_COMMAND_LEN,
854 RC6_COMPLETE_DATA_LEN_SHORT,
855 RC6_STOP_BIT,
856 RC6_LSB
857 };
858
859 #endif
860
861 #if IRMP_SUPPORT_RECS80EXT_PROTOCOL == 1
862
863 static PROGMEM IRMP_PARAMETER recs80ext_param =
864 {
865 IRMP_RECS80EXT_PROTOCOL,
866 RECS80EXT_PULSE_LEN_MIN,
867 RECS80EXT_PULSE_LEN_MAX,
868 RECS80EXT_1_PAUSE_LEN_MIN,
869 RECS80EXT_1_PAUSE_LEN_MAX,
870 RECS80EXT_PULSE_LEN_MIN,
871 RECS80EXT_PULSE_LEN_MAX,
872 RECS80EXT_0_PAUSE_LEN_MIN,
873 RECS80EXT_0_PAUSE_LEN_MAX,
874 RECS80EXT_ADDRESS_OFFSET,
875 RECS80EXT_ADDRESS_OFFSET + RECS80EXT_ADDRESS_LEN,
876 RECS80EXT_COMMAND_OFFSET,
877 RECS80EXT_COMMAND_OFFSET + RECS80EXT_COMMAND_LEN,
878 RECS80EXT_COMPLETE_DATA_LEN,
879 RECS80EXT_STOP_BIT,
880 RECS80EXT_LSB
881 };
882
883 #endif
884
885 #if IRMP_SUPPORT_NUBERT_PROTOCOL == 1
886
887 static PROGMEM IRMP_PARAMETER nubert_param =
888 {
889 IRMP_NUBERT_PROTOCOL,
890 NUBERT_1_PULSE_LEN_MIN,
891 NUBERT_1_PULSE_LEN_MAX,
892 NUBERT_1_PAUSE_LEN_MIN,
893 NUBERT_1_PAUSE_LEN_MAX,
894 NUBERT_0_PULSE_LEN_MIN,
895 NUBERT_0_PULSE_LEN_MAX,
896 NUBERT_0_PAUSE_LEN_MIN,
897 NUBERT_0_PAUSE_LEN_MAX,
898 NUBERT_ADDRESS_OFFSET,
899 NUBERT_ADDRESS_OFFSET + NUBERT_ADDRESS_LEN,
900 NUBERT_COMMAND_OFFSET,
901 NUBERT_COMMAND_OFFSET + NUBERT_COMMAND_LEN,
902 NUBERT_COMPLETE_DATA_LEN,
903 NUBERT_STOP_BIT,
904 NUBERT_LSB
905 };
906
907 #endif
908
909 #if IRMP_SUPPORT_BANG_OLUFSEN_PROTOCOL == 1
910
911 static PROGMEM IRMP_PARAMETER bang_olufsen_param =
912 {
913 IRMP_BANG_OLUFSEN_PROTOCOL,
914 BANG_OLUFSEN_PULSE_LEN_MIN,
915 BANG_OLUFSEN_PULSE_LEN_MAX,
916 BANG_OLUFSEN_1_PAUSE_LEN_MIN,
917 BANG_OLUFSEN_1_PAUSE_LEN_MAX,
918 BANG_OLUFSEN_PULSE_LEN_MIN,
919 BANG_OLUFSEN_PULSE_LEN_MAX,
920 BANG_OLUFSEN_0_PAUSE_LEN_MIN,
921 BANG_OLUFSEN_0_PAUSE_LEN_MAX,
922 BANG_OLUFSEN_ADDRESS_OFFSET,
923 BANG_OLUFSEN_ADDRESS_OFFSET + BANG_OLUFSEN_ADDRESS_LEN,
924 BANG_OLUFSEN_COMMAND_OFFSET,
925 BANG_OLUFSEN_COMMAND_OFFSET + BANG_OLUFSEN_COMMAND_LEN,
926 BANG_OLUFSEN_COMPLETE_DATA_LEN,
927 BANG_OLUFSEN_STOP_BIT,
928 BANG_OLUFSEN_LSB
929 };
930
931 #endif
932
933 static uint8_t irmp_bit; // current bit position
934 static IRMP_PARAMETER irmp_param;
935
936 static volatile uint8_t irmp_ir_detected;
937 static volatile uint8_t irmp_protocol;
938 static volatile uint16_t irmp_address;
939 static volatile uint16_t irmp_command;
940 static volatile uint16_t irmp_id; // only used for SAMSUNG protocol
941 static volatile uint8_t irmp_flags;
942
943 #ifdef DEBUG
944 static uint8_t IRMP_PIN;
945 #endif
946
947 /*---------------------------------------------------------------------------------------------------------------------------------------------------
948 * Initialize IRMP decoder
949 * @details Configures IRMP input pin
950 *---------------------------------------------------------------------------------------------------------------------------------------------------
951 */
952 #ifndef DEBUG
953 void
954 irmp_init (void)
955 {
956 #ifndef PIC_CCS_COMPILER
957 IRMP_PORT &= ~(1<<IRMP_BIT); // deactivate pullup
958 IRMP_DDR &= ~(1<<IRMP_BIT); // set pin to input
959 #endif // PIC_CCS_COMPILER
960
961 #if IRMP_LOGGING == 1
962 irmp_uart_init ();
963 #endif
964 }
965 #endif
966 /*---------------------------------------------------------------------------------------------------------------------------------------------------
967 * Get IRMP data
968 * @details gets decoded IRMP data
969 * @param pointer in order to store IRMP data
970 * @return TRUE: successful, FALSE: failed
971 *---------------------------------------------------------------------------------------------------------------------------------------------------
972 */
973 uint8_t
974 irmp_get_data (IRMP_DATA * irmp_data_p)
975 {
976 uint8_t rtc = FALSE;
977
978 if (irmp_ir_detected)
979 {
980 switch (irmp_protocol)
981 {
982 #if IRMP_SUPPORT_SAMSUNG_PROTOCOL == 1
983 case IRMP_SAMSUNG_PROTOCOL:
984 if ((irmp_command >> 8) == (~irmp_command & 0x00FF))
985 {
986 irmp_command &= 0xff;
987 irmp_command |= irmp_id << 8;
988 rtc = TRUE;
989 }
990 break;
991 #endif
992 #if IRMP_SUPPORT_NEC_PROTOCOL == 1
993 case IRMP_NEC_PROTOCOL:
994 if ((irmp_command >> 8) == (~irmp_command & 0x00FF))
995 {
996 irmp_command &= 0xff;
997 rtc = TRUE;
998 }
999 else if ((irmp_command & 0xFF00) == 0xD100)
1000 {
1001 DEBUG_PRINTF ("Switching to APPLE protocol\n");
1002 irmp_protocol = IRMP_APPLE_PROTOCOL;
1003 irmp_command &= 0xff;
1004 rtc = TRUE;
1005 }
1006 break;
1007 #endif
1008 default:
1009 rtc = TRUE;
1010 }
1011
1012 if (rtc)
1013 {
1014 irmp_data_p->protocol = irmp_protocol;
1015 irmp_data_p->address = irmp_address;
1016 irmp_data_p->command = irmp_command;
1017 irmp_data_p->flags = irmp_flags;
1018 irmp_command = 0;
1019 irmp_address = 0;
1020 irmp_flags = 0;
1021 }
1022
1023 irmp_ir_detected = FALSE;
1024 }
1025
1026 return rtc;
1027 }
1028
1029 // these statics must not be volatile, because they are only used by irmp_store_bit(), which is called by irmp_ISR()
1030 static uint16_t irmp_tmp_address; // ir address
1031 static uint16_t irmp_tmp_command; // ir command
1032 #if IRMP_SUPPORT_SAMSUNG_PROTOCOL == 1
1033 static uint16_t irmp_tmp_id; // ir id (only SAMSUNG)
1034 #endif
1035
1036 static uint8_t irmp_bit; // current bit position
1037
1038 /*---------------------------------------------------------------------------------------------------------------------------------------------------
1039 * store bit
1040 * @details store bit in temp address or temp command
1041 * @param value to store: 0 or 1
1042 *---------------------------------------------------------------------------------------------------------------------------------------------------
1043 */
1044 static void
1045 irmp_store_bit (uint8_t value)
1046 {
1047 if (irmp_bit >= irmp_param.address_offset && irmp_bit < irmp_param.address_end)
1048 {
1049 if (irmp_param.lsb_first)
1050 {
1051 irmp_tmp_address |= (((uint16_t) (value)) << (irmp_bit - irmp_param.address_offset)); // CV wants cast
1052 }
1053 else
1054 {
1055 irmp_tmp_address <<= 1;
1056 irmp_tmp_address |= value;
1057 }
1058 }
1059 else if (irmp_bit >= irmp_param.command_offset && irmp_bit < irmp_param.command_end)
1060 {
1061 if (irmp_param.lsb_first)
1062 {
1063 irmp_tmp_command |= (((uint16_t) (value)) << (irmp_bit - irmp_param.command_offset)); // CV wants cast
1064 }
1065 else
1066 {
1067 irmp_tmp_command <<= 1;
1068 irmp_tmp_command |= value;
1069 }
1070 }
1071 #if IRMP_SUPPORT_SAMSUNG_PROTOCOL == 1
1072 else if (irmp_param.protocol == IRMP_SAMSUNG_PROTOCOL && irmp_bit >= SAMSUNG_ID_OFFSET && irmp_bit < SAMSUNG_ID_OFFSET + SAMSUNG_ID_LEN)
1073 {
1074 irmp_tmp_id |= (((uint16_t) (value)) << (irmp_bit - SAMSUNG_ID_OFFSET)); // store with LSB first
1075 }
1076 #endif
1077 irmp_bit++;
1078 }
1079
1080 /*---------------------------------------------------------------------------------------------------------------------------------------------------
1081 * ISR routine
1082 * @details ISR routine, called 10000 times per second
1083 *---------------------------------------------------------------------------------------------------------------------------------------------------
1084 */
1085 void
1086 irmp_ISR (void)
1087 {
1088 static uint8_t irmp_start_bit_detected; // flag: start bit detected
1089 static uint8_t wait_for_space; // flag: wait for data bit space
1090 static uint8_t wait_for_start_space; // flag: wait for start bit space
1091 static uint8_t irmp_pulse_time; // count bit time for pulse
1092 static uint8_t irmp_pause_time; // count bit time for pause
1093 static uint16_t last_irmp_address = 0xFFFF; // save last irmp address to recognize key repetition
1094 static uint16_t last_irmp_command = 0xFFFF; // save last irmp command to recognize key repetition
1095 static uint16_t repetition_counter; // SIRCS repeats frame 2-5 times with 45 ms pause
1096 #if IRMP_SUPPORT_DENON_PROTOCOL == 1
1097 static uint16_t last_irmp_denon_command; // save last irmp command to recognize DENON frame repetition
1098 #endif
1099 #if IRMP_SUPPORT_RC5_PROTOCOL == 1
1100 static uint8_t rc5_cmd_bit6; // bit 6 of RC5 command is the inverted 2nd start bit
1101 #endif
1102 #if IRMP_SUPPORT_RC5_PROTOCOL == 1 || IRMP_SUPPORT_RC6_PROTOCOL == 1
1103 static uint8_t last_pause; // last pause value
1104 #endif
1105 #if IRMP_SUPPORT_RC5_PROTOCOL == 1 || IRMP_SUPPORT_RC6_PROTOCOL == 1 || IRMP_SUPPORT_BANG_OLUFSEN_PROTOCOL == 1
1106 static uint8_t last_value; // last bit value
1107 #endif
1108 uint8_t irmp_input; // input value
1109
1110 irmp_input = input(IRMP_PIN);
1111
1112 irmp_logIsr(irmp_input); // log ir signal, if IRMP_LOGGING defined
1113
1114 if (! irmp_ir_detected) // ir code already detected?
1115 { // no...
1116 if (! irmp_start_bit_detected) // start bit detected?
1117 { // no...
1118 if (!irmp_input) // receiving burst?
1119 { // yes...
1120 irmp_pulse_time++; // increment counter
1121 }
1122 else
1123 { // no...
1124 if (irmp_pulse_time) // it's dark....
1125 { // set flags for counting the time of darkness...
1126 irmp_start_bit_detected = 1;
1127 wait_for_start_space = 1;
1128 wait_for_space = 0;
1129 irmp_tmp_command = 0;
1130 irmp_tmp_address = 0;
1131 irmp_bit = 0xff;
1132 irmp_pause_time = 1; // 1st pause: set to 1, not to 0!
1133 #if IRMP_SUPPORT_RC5_PROTOCOL == 1
1134 rc5_cmd_bit6 = 0; // fm 2010-03-07: bugfix: reset it after incomplete RC5 frame!
1135 #endif
1136 }
1137 else
1138 {
1139 repetition_counter++;
1140 }
1141 }
1142 }
1143 else
1144 {
1145 if (wait_for_start_space) // we have received start bit...
1146 { // ...and are counting the time of darkness
1147 if (irmp_input) // still dark?
1148 { // yes
1149 irmp_pause_time++; // increment counter
1150
1151 if (irmp_pause_time > IRMP_TIMEOUT_LEN) // timeout?
1152 { // yes...
1153 DEBUG_PRINTF ("error 1: pause after start bit %d too long: %d\n", irmp_pulse_time, irmp_pause_time);
1154 irmp_start_bit_detected = 0; // reset flags, let's wait for another start bit
1155 irmp_pulse_time = 0;
1156 irmp_pause_time = 0;
1157 }
1158 }
1159 else
1160 { // receiving first data pulse!
1161 IRMP_PARAMETER * irmp_param_p = (IRMP_PARAMETER *) 0;
1162
1163 DEBUG_PRINTF ("start-bit: pulse = %d, pause = %d\n", irmp_pulse_time, irmp_pause_time);
1164
1165 #if IRMP_SUPPORT_SIRCS_PROTOCOL == 1
1166 if (irmp_pulse_time >= SIRCS_START_BIT_PULSE_LEN_MIN && irmp_pulse_time <= SIRCS_START_BIT_PULSE_LEN_MAX &&
1167 irmp_pause_time >= SIRCS_START_BIT_PAUSE_LEN_MIN && irmp_pause_time <= SIRCS_START_BIT_PAUSE_LEN_MAX)
1168 { // it's SIRCS
1169 DEBUG_PRINTF ("protocol = SIRCS, start bit timings: pulse: %3d - %3d, pause: %3d - %3d\n",
1170 SIRCS_START_BIT_PULSE_LEN_MIN, SIRCS_START_BIT_PULSE_LEN_MAX,
1171 SIRCS_START_BIT_PAUSE_LEN_MIN, SIRCS_START_BIT_PAUSE_LEN_MAX);
1172 irmp_param_p = (IRMP_PARAMETER *) (IRMP_PARAMETER *) &sircs_param;
1173 }
1174 else
1175 #endif // IRMP_SUPPORT_SIRCS_PROTOCOL == 1
1176
1177 #if IRMP_SUPPORT_NEC_PROTOCOL == 1
1178 if (irmp_pulse_time >= NEC_START_BIT_PULSE_LEN_MIN && irmp_pulse_time <= NEC_START_BIT_PULSE_LEN_MAX &&
1179 irmp_pause_time >= NEC_START_BIT_PAUSE_LEN_MIN && irmp_pause_time <= NEC_START_BIT_PAUSE_LEN_MAX)
1180 {
1181 DEBUG_PRINTF ("protocol = NEC, start bit timings: pulse: %3d - %3d, pause: %3d - %3d\n",
1182 NEC_START_BIT_PULSE_LEN_MIN, NEC_START_BIT_PULSE_LEN_MAX,
1183 NEC_START_BIT_PAUSE_LEN_MIN, NEC_START_BIT_PAUSE_LEN_MAX);
1184 irmp_param_p = (IRMP_PARAMETER *) &nec_param;
1185 }
1186 else if (irmp_pulse_time >= NEC_START_BIT_PULSE_LEN_MIN && irmp_pulse_time <= NEC_START_BIT_PULSE_LEN_MAX &&
1187 irmp_pause_time >= NEC_REPEAT_START_BIT_PAUSE_LEN_MIN && irmp_pause_time <= NEC_REPEAT_START_BIT_PAUSE_LEN_MAX)
1188 { // it's NEC
1189 DEBUG_PRINTF ("protocol = NEC (repetition frame), start bit timings: pulse: %3d - %3d, pause: %3d - %3d\n",
1190 NEC_START_BIT_PULSE_LEN_MIN, NEC_START_BIT_PULSE_LEN_MAX,
1191 NEC_REPEAT_START_BIT_PAUSE_LEN_MIN, NEC_REPEAT_START_BIT_PAUSE_LEN_MAX);
1192
1193 irmp_param_p = (IRMP_PARAMETER *) &nec_rep_param;
1194 }
1195 else
1196 #endif // IRMP_SUPPORT_NEC_PROTOCOL == 1
1197
1198 #if IRMP_SUPPORT_SAMSUNG_PROTOCOL == 1
1199 if (irmp_pulse_time >= SAMSUNG_START_BIT_PULSE_LEN_MIN && irmp_pulse_time <= SAMSUNG_START_BIT_PULSE_LEN_MAX &&
1200 irmp_pause_time >= SAMSUNG_START_BIT_PAUSE_LEN_MIN && irmp_pause_time <= SAMSUNG_START_BIT_PAUSE_LEN_MAX)
1201 { // it's SAMSUNG
1202 DEBUG_PRINTF ("protocol = SAMSUNG, start bit timings: pulse: %3d - %3d, pause: %3d - %3d\n",
1203 SAMSUNG_START_BIT_PULSE_LEN_MIN, SAMSUNG_START_BIT_PULSE_LEN_MAX,
1204 SAMSUNG_START_BIT_PAUSE_LEN_MIN, SAMSUNG_START_BIT_PAUSE_LEN_MAX);
1205 irmp_param_p = (IRMP_PARAMETER *) &samsung_param;
1206 }
1207 else
1208 #endif // IRMP_SUPPORT_SAMSUNG_PROTOCOL == 1
1209
1210 #if IRMP_SUPPORT_MATSUSHITA_PROTOCOL == 1
1211 if (irmp_pulse_time >= MATSUSHITA_START_BIT_PULSE_LEN_MIN && irmp_pulse_time <= MATSUSHITA_START_BIT_PULSE_LEN_MAX &&
1212 irmp_pause_time >= MATSUSHITA_START_BIT_PAUSE_LEN_MIN && irmp_pause_time <= MATSUSHITA_START_BIT_PAUSE_LEN_MAX)
1213 { // it's MATSUSHITA
1214 DEBUG_PRINTF ("protocol = MATSUSHITA, start bit timings: pulse: %3d - %3d, pause: %3d - %3d\n",
1215 MATSUSHITA_START_BIT_PULSE_LEN_MIN, MATSUSHITA_START_BIT_PULSE_LEN_MAX,
1216 MATSUSHITA_START_BIT_PAUSE_LEN_MIN, MATSUSHITA_START_BIT_PAUSE_LEN_MAX);
1217 irmp_param_p = (IRMP_PARAMETER *) &matsushita_param;
1218 }
1219 else
1220 #endif // IRMP_SUPPORT_MATSUSHITA_PROTOCOL == 1
1221
1222 #if IRMP_SUPPORT_KASEIKYO_PROTOCOL == 1
1223 if (irmp_pulse_time >= KASEIKYO_START_BIT_PULSE_LEN_MIN && irmp_pulse_time <= KASEIKYO_START_BIT_PULSE_LEN_MAX &&
1224 irmp_pause_time >= KASEIKYO_START_BIT_PAUSE_LEN_MIN && irmp_pause_time <= KASEIKYO_START_BIT_PAUSE_LEN_MAX)
1225 { // it's KASEIKYO
1226 DEBUG_PRINTF ("protocol = KASEIKYO, start bit timings: pulse: %3d - %3d, pause: %3d - %3d\n",
1227 KASEIKYO_START_BIT_PULSE_LEN_MIN, KASEIKYO_START_BIT_PULSE_LEN_MAX,
1228 KASEIKYO_START_BIT_PAUSE_LEN_MIN, KASEIKYO_START_BIT_PAUSE_LEN_MAX);
1229 irmp_param_p = (IRMP_PARAMETER *) &kaseikyo_param;
1230 }
1231 else
1232 #endif // IRMP_SUPPORT_KASEIKYO_PROTOCOL == 1
1233
1234 #if IRMP_SUPPORT_RECS80_PROTOCOL == 1
1235 if (irmp_pulse_time >= RECS80_START_BIT_PULSE_LEN_MIN && irmp_pulse_time <= RECS80_START_BIT_PULSE_LEN_MAX &&
1236 irmp_pause_time >= RECS80_START_BIT_PAUSE_LEN_MIN && irmp_pause_time <= RECS80_START_BIT_PAUSE_LEN_MAX)
1237 { // it's RECS80
1238 DEBUG_PRINTF ("protocol = RECS80, start bit timings: pulse: %3d - %3d, pause: %3d - %3d\n",
1239 RECS80_START_BIT_PULSE_LEN_MIN, RECS80_START_BIT_PULSE_LEN_MAX,
1240 RECS80_START_BIT_PAUSE_LEN_MIN, RECS80_START_BIT_PAUSE_LEN_MAX);
1241 irmp_param_p = (IRMP_PARAMETER *) &recs80_param;
1242 }
1243 else
1244 #endif // IRMP_SUPPORT_RECS80_PROTOCOL == 1
1245
1246 #if IRMP_SUPPORT_RC5_PROTOCOL == 1
1247 if (((irmp_pulse_time >= RC5_START_BIT_LEN_MIN && irmp_pulse_time <= RC5_START_BIT_LEN_MAX) ||
1248 (irmp_pulse_time >= 2 * RC5_START_BIT_LEN_MIN && irmp_pulse_time <= 2 * RC5_START_BIT_LEN_MAX)) &&
1249 ((irmp_pause_time >= RC5_START_BIT_LEN_MIN && irmp_pause_time <= RC5_START_BIT_LEN_MAX) ||
1250 (irmp_pause_time >= 2 * RC5_START_BIT_LEN_MIN && irmp_pause_time <= 2 * RC5_START_BIT_LEN_MAX)))
1251 { // it's RC5
1252 DEBUG_PRINTF ("protocol = RC5, start bit timings: pulse: %3d - %3d, pause: %3d - %3d\n",
1253 RC5_START_BIT_LEN_MIN, RC5_START_BIT_LEN_MAX,
1254 RC5_START_BIT_LEN_MIN, RC5_START_BIT_LEN_MAX);
1255 irmp_param_p = (IRMP_PARAMETER *) &rc5_param;
1256 last_pause = irmp_pause_time;
1257
1258 if ((irmp_pulse_time > RC5_START_BIT_LEN_MAX && irmp_pulse_time <= 2 * RC5_START_BIT_LEN_MAX) ||
1259 (irmp_pause_time > RC5_START_BIT_LEN_MAX && irmp_pause_time <= 2 * RC5_START_BIT_LEN_MAX))
1260 {
1261 last_value = 0;
1262 rc5_cmd_bit6 = 1<<6;
1263 }
1264 else
1265 {
1266 last_value = 1;
1267 }
1268 }
1269 else
1270 #endif // IRMP_SUPPORT_RC5_PROTOCOL == 1
1271
1272 #if IRMP_SUPPORT_DENON_PROTOCOL == 1
1273 if ( (irmp_pulse_time >= DENON_PULSE_LEN_MIN && irmp_pulse_time <= DENON_PULSE_LEN_MAX) &&
1274 ((irmp_pause_time >= DENON_1_PAUSE_LEN_MIN && irmp_pause_time <= DENON_1_PAUSE_LEN_MAX) ||
1275 (irmp_pause_time >= DENON_0_PAUSE_LEN_MIN && irmp_pause_time <= DENON_0_PAUSE_LEN_MAX)))
1276 { // it's DENON
1277 DEBUG_PRINTF ("protocol = DENON, start bit timings: pulse: %3d - %3d, pause: %3d - %3d or %3d - %3d\n",
1278 DENON_PULSE_LEN_MIN, DENON_PULSE_LEN_MAX,
1279 DENON_1_PAUSE_LEN_MIN, DENON_1_PAUSE_LEN_MAX,
1280 DENON_0_PAUSE_LEN_MIN, DENON_0_PAUSE_LEN_MAX);
1281 irmp_param_p = (IRMP_PARAMETER *) &denon_param;
1282 }
1283 else
1284 #endif // IRMP_SUPPORT_DENON_PROTOCOL == 1
1285
1286 #if IRMP_SUPPORT_RC6_PROTOCOL == 1
1287 if (irmp_pulse_time >= RC6_START_BIT_PULSE_LEN_MIN && irmp_pulse_time <= RC6_START_BIT_PULSE_LEN_MAX &&
1288 irmp_pause_time >= RC6_START_BIT_PAUSE_LEN_MIN && irmp_pause_time <= RC6_START_BIT_PAUSE_LEN_MAX)
1289 { // it's RC6
1290 DEBUG_PRINTF ("protocol = RC6, start bit timings: pulse: %3d - %3d, pause: %3d - %3d\n",
1291 RC6_START_BIT_PULSE_LEN_MIN, RC6_START_BIT_PULSE_LEN_MAX,
1292 RC6_START_BIT_PAUSE_LEN_MIN, RC6_START_BIT_PAUSE_LEN_MAX);
1293 irmp_param_p = (IRMP_PARAMETER *) &rc6_param;
1294 last_pause = 0;
1295 last_value = 0;
1296 }
1297 else
1298 #endif // IRMP_SUPPORT_RC6_PROTOCOL == 1
1299
1300 #if IRMP_SUPPORT_RECS80EXT_PROTOCOL == 1
1301 if (irmp_pulse_time >= RECS80EXT_START_BIT_PULSE_LEN_MIN && irmp_pulse_time <= RECS80EXT_START_BIT_PULSE_LEN_MAX &&
1302 irmp_pause_time >= RECS80EXT_START_BIT_PAUSE_LEN_MIN && irmp_pause_time <= RECS80EXT_START_BIT_PAUSE_LEN_MAX)
1303 { // it's RECS80EXT
1304 DEBUG_PRINTF ("protocol = RECS80EXT, start bit timings: pulse: %3d - %3d, pause: %3d - %3d\n",
1305 RECS80EXT_START_BIT_PULSE_LEN_MIN, RECS80EXT_START_BIT_PULSE_LEN_MAX,
1306 RECS80EXT_START_BIT_PAUSE_LEN_MIN, RECS80EXT_START_BIT_PAUSE_LEN_MAX);
1307 irmp_param_p = (IRMP_PARAMETER *) &recs80ext_param;
1308 }
1309 else
1310 #endif // IRMP_SUPPORT_RECS80EXT_PROTOCOL == 1
1311
1312 #if IRMP_SUPPORT_NUBERT_PROTOCOL == 1
1313 if (irmp_pulse_time >= NUBERT_START_BIT_PULSE_LEN_MIN && irmp_pulse_time <= NUBERT_START_BIT_PULSE_LEN_MAX &&
1314 irmp_pause_time >= NUBERT_START_BIT_PAUSE_LEN_MIN && irmp_pause_time <= NUBERT_START_BIT_PAUSE_LEN_MAX)
1315 { // it's NUBERT
1316 DEBUG_PRINTF ("protocol = NUBERT, start bit timings: pulse: %3d - %3d, pause: %3d - %3d\n",
1317 NUBERT_START_BIT_PULSE_LEN_MIN, NUBERT_START_BIT_PULSE_LEN_MAX,
1318 NUBERT_START_BIT_PAUSE_LEN_MIN, NUBERT_START_BIT_PAUSE_LEN_MAX);
1319 irmp_param_p = (IRMP_PARAMETER *) &nubert_param;
1320 }
1321 else
1322 #endif // IRMP_SUPPORT_NUBERT_PROTOCOL == 1
1323
1324 #if IRMP_SUPPORT_BANG_OLUFSEN_PROTOCOL == 1
1325 if (irmp_pulse_time >= BANG_OLUFSEN_START_BIT1_PULSE_LEN_MIN && irmp_pulse_time <= BANG_OLUFSEN_START_BIT1_PULSE_LEN_MAX &&
1326 irmp_pause_time >= BANG_OLUFSEN_START_BIT1_PAUSE_LEN_MIN && irmp_pause_time <= BANG_OLUFSEN_START_BIT1_PAUSE_LEN_MAX)
1327 { // it's BANG_OLUFSEN
1328 DEBUG_PRINTF ("protocol = BANG_OLUFSEN\n");
1329 DEBUG_PRINTF ("start bit 1 timings: pulse: %3d - %3d, pause: %3d - %3d\n",
1330 BANG_OLUFSEN_START_BIT1_PULSE_LEN_MIN, BANG_OLUFSEN_START_BIT1_PULSE_LEN_MAX,
1331 BANG_OLUFSEN_START_BIT1_PAUSE_LEN_MIN, BANG_OLUFSEN_START_BIT1_PAUSE_LEN_MAX);
1332 DEBUG_PRINTF ("start bit 2 timings: pulse: %3d - %3d, pause: %3d - %3d\n",
1333 BANG_OLUFSEN_START_BIT2_PULSE_LEN_MIN, BANG_OLUFSEN_START_BIT2_PULSE_LEN_MAX,
1334 BANG_OLUFSEN_START_BIT2_PAUSE_LEN_MIN, BANG_OLUFSEN_START_BIT2_PAUSE_LEN_MAX);
1335 DEBUG_PRINTF ("start bit 3 timings: pulse: %3d - %3d, pause: %3d - %3d\n",
1336 BANG_OLUFSEN_START_BIT3_PULSE_LEN_MIN, BANG_OLUFSEN_START_BIT3_PULSE_LEN_MAX,
1337 BANG_OLUFSEN_START_BIT3_PAUSE_LEN_MIN, BANG_OLUFSEN_START_BIT3_PAUSE_LEN_MAX);
1338 DEBUG_PRINTF ("start bit 4 timings: pulse: %3d - %3d, pause: %3d - %3d\n",
1339 BANG_OLUFSEN_START_BIT4_PULSE_LEN_MIN, BANG_OLUFSEN_START_BIT4_PULSE_LEN_MAX,
1340 BANG_OLUFSEN_START_BIT4_PAUSE_LEN_MIN, BANG_OLUFSEN_START_BIT4_PAUSE_LEN_MAX);
1341 irmp_param_p = (IRMP_PARAMETER *) &bang_olufsen_param;
1342 last_value = 0;
1343 }
1344 else
1345 #endif // IRMP_SUPPORT_BANG_OLUFSEN_PROTOCOL == 1
1346
1347 {
1348 DEBUG_PRINTF ("protocol = UNKNOWN\n");
1349 irmp_start_bit_detected = 0; // wait for another start bit...
1350 }
1351
1352 if (irmp_start_bit_detected)
1353 {
1354 memcpy_P (&irmp_param, irmp_param_p, sizeof (IRMP_PARAMETER));
1355
1356 DEBUG_PRINTF ("pulse_1: %3d - %3d\n", irmp_param.pulse_1_len_min, irmp_param.pulse_1_len_max);
1357 DEBUG_PRINTF ("pause_1: %3d - %3d\n", irmp_param.pause_1_len_min, irmp_param.pause_1_len_max);
1358
1359 #if IRMP_SUPPORT_RC6_PROTOCOL == 1
1360 if (irmp_param.protocol == IRMP_RC6_PROTOCOL)
1361 {
1362 DEBUG_PRINTF ("pulse_toggle: %3d - %3d\n", RC6_TOGGLE_BIT_LEN_MIN, RC6_TOGGLE_BIT_LEN_MAX);
1363 }
1364 #endif
1365 DEBUG_PRINTF ("pulse_0: %3d - %3d\n", irmp_param.pulse_0_len_min, irmp_param.pulse_0_len_max);
1366 DEBUG_PRINTF ("pause_0: %3d - %3d\n", irmp_param.pause_0_len_min, irmp_param.pause_0_len_max);
1367
1368 #if IRMP_SUPPORT_BANG_OLUFSEN_PROTOCOL == 1
1369 if (irmp_param.protocol == IRMP_BANG_OLUFSEN_PROTOCOL)
1370 {
1371 DEBUG_PRINTF ("pulse_r: %3d - %3d\n", irmp_param.pulse_0_len_min, irmp_param.pulse_0_len_max);
1372 DEBUG_PRINTF ("pause_r: %3d - %3d\n", BANG_OLUFSEN_R_PAUSE_LEN_MIN, BANG_OLUFSEN_R_PAUSE_LEN_MAX);
1373 }
1374 #endif
1375
1376 DEBUG_PRINTF ("command_offset: %2d\n", irmp_param.command_offset);
1377 DEBUG_PRINTF ("command_len: %3d\n", irmp_param.command_end - irmp_param.command_offset);
1378 DEBUG_PRINTF ("complete_len: %3d\n", irmp_param.complete_len);
1379 DEBUG_PRINTF ("stop_bit: %3d\n", irmp_param.stop_bit);
1380 }
1381
1382 irmp_bit = 0;
1383
1384 #if IRMP_SUPPORT_RC5_PROTOCOL == 1
1385 if (irmp_param.protocol == IRMP_RC5_PROTOCOL)
1386 {
1387 if (irmp_pause_time > RC5_START_BIT_LEN_MAX && irmp_pause_time <= 2 * RC5_START_BIT_LEN_MAX)
1388 {
1389 DEBUG_PRINTF ("[bit %2d: pulse = %3d, pause = %3d] ", irmp_bit, irmp_pulse_time, irmp_pause_time);
1390 DEBUG_PUTCHAR ('1');
1391 DEBUG_PUTCHAR ('\n');
1392 irmp_store_bit (1);
1393 }
1394 else if (! last_value)
1395 {
1396 DEBUG_PRINTF ("[bit %2d: pulse = %3d, pause = %3d] ", irmp_bit, irmp_pulse_time, irmp_pause_time);
1397 DEBUG_PUTCHAR ('0');
1398 DEBUG_PUTCHAR ('\n');
1399 irmp_store_bit (0);
1400 }
1401 }
1402 else
1403 #endif // IRMP_SUPPORT_RC5_PROTOCOL == 1
1404
1405 #if IRMP_SUPPORT_DENON_PROTOCOL == 1
1406 if (irmp_param.protocol == IRMP_DENON_PROTOCOL)
1407 {
1408 DEBUG_PRINTF ("[bit %2d: pulse = %3d, pause = %3d] ", irmp_bit, irmp_pulse_time, irmp_pause_time);
1409
1410 if (irmp_pause_time >= DENON_1_PAUSE_LEN_MIN && irmp_pause_time <= DENON_1_PAUSE_LEN_MAX)
1411 { // pause timings correct for "1"?
1412 DEBUG_PUTCHAR ('1'); // yes, store 1
1413 DEBUG_PUTCHAR ('\n');
1414 irmp_store_bit (1);
1415 }
1416 else // if (irmp_pause_time >= DENON_0_PAUSE_LEN_MIN && irmp_pause_time <= DENON_0_PAUSE_LEN_MAX)
1417 { // pause timings correct for "0"?
1418 DEBUG_PUTCHAR ('0'); // yes, store 0
1419 DEBUG_PUTCHAR ('\n');
1420 irmp_store_bit (0);
1421 }
1422 }
1423 #endif // IRMP_SUPPORT_DENON_PROTOCOL == 1
1424
1425 irmp_pulse_time = 1; // set counter to 1, not 0
1426 irmp_pause_time = 0;
1427 wait_for_start_space = 0;
1428 }
1429 }
1430 else if (wait_for_space) // the data section....
1431 { // counting the time of darkness....
1432 uint8_t got_light = FALSE;
1433
1434 if (irmp_input) // still dark?
1435 { // yes...
1436 if (irmp_bit == irmp_param.complete_len && irmp_param.stop_bit == 1)
1437 {
1438 if (irmp_pulse_time >= irmp_param.pulse_0_len_min && irmp_pulse_time <= irmp_param.pulse_0_len_max)
1439 {
1440 #ifdef DEBUG
1441 if (irmp_param.protocol != IRMP_RC5_PROTOCOL)
1442 {
1443 DEBUG_PRINTF ("stop bit detected\n");
1444 }
1445 #endif
1446 irmp_param.stop_bit = 0;
1447 }
1448 else
1449 {
1450 DEBUG_PRINTF ("stop bit timing wrong\n");
1451
1452 irmp_start_bit_detected = 0; // wait for another start bit...
1453 irmp_pulse_time = 0;
1454 irmp_pause_time = 0;
1455 }
1456 }
1457 else
1458 {
1459 irmp_pause_time++; // increment counter
1460
1461 #if IRMP_SUPPORT_SIRCS_PROTOCOL == 1
1462 if (irmp_param.protocol == IRMP_SIRCS_PROTOCOL && // Sony has a variable number of bits:
1463 irmp_pause_time > SIRCS_PAUSE_LEN_MAX && // minimum is 12
1464 irmp_bit >= 12 - 1) // pause too long?
1465 { // yes, break and close this frame
1466 irmp_param.complete_len = irmp_bit + 1; // set new complete length
1467 got_light = TRUE; // this is a lie, but helps (generates stop bit)
1468 irmp_param.command_end = irmp_param.command_offset + irmp_bit + 1; // correct command length
1469 irmp_pause_time = SIRCS_PAUSE_LEN_MAX - 1; // correct pause length
1470 }
1471 else
1472 #endif
1473 #if IRMP_SUPPORT_RC5_PROTOCOL == 1
1474 if (irmp_param.protocol == IRMP_RC5_PROTOCOL &&
1475 irmp_pause_time > 2 * RC5_BIT_LEN_MAX && irmp_bit >= RC5_COMPLETE_DATA_LEN - 2 && !irmp_param.stop_bit)
1476 { // special rc5 decoder
1477 got_light = TRUE; // this is a lie, but generates a stop bit ;-)
1478 irmp_param.stop_bit = TRUE; // set flag
1479 }
1480 else
1481 #endif
1482 #if IRMP_SUPPORT_RC6_PROTOCOL == 1
1483 if (irmp_param.protocol == IRMP_RC6_PROTOCOL &&
1484 irmp_pause_time > 2 * RC6_BIT_LEN_MAX && irmp_bit >= irmp_param.complete_len - 2 && !irmp_param.stop_bit)
1485 { // special rc6 decoder
1486 got_light = TRUE; // this is a lie, but generates a stop bit ;-)
1487 irmp_param.stop_bit = TRUE; // set flag
1488 }
1489 else
1490 #endif
1491 if (irmp_pause_time > IRMP_TIMEOUT_LEN) // timeout?
1492 { // yes...
1493 if (irmp_bit == irmp_param.complete_len - 1 && irmp_param.stop_bit == 0)
1494 {
1495 irmp_bit++;
1496 }
1497 else
1498 {
1499 DEBUG_PRINTF ("error 2: pause %d after data bit %d too long\n", irmp_pause_time, irmp_bit);
1500
1501 irmp_start_bit_detected = 0; // wait for another start bit...
1502 irmp_pulse_time = 0;
1503 irmp_pause_time = 0;
1504 }
1505 }
1506 }
1507 }
1508 else
1509 { // got light now!
1510 got_light = TRUE;
1511 }
1512
1513 if (got_light)
1514 {
1515 DEBUG_PRINTF ("[bit %2d: pulse = %3d, pause = %3d] ", irmp_bit, irmp_pulse_time, irmp_pause_time);
1516
1517 #if IRMP_SUPPORT_RC5_PROTOCOL == 1
1518 if (irmp_param.protocol == IRMP_RC5_PROTOCOL) // special rc5 decoder
1519 {
1520 if (irmp_pulse_time > RC5_BIT_LEN_MAX && irmp_pulse_time <= 2 * RC5_BIT_LEN_MAX)
1521 {
1522 DEBUG_PUTCHAR ('1');
1523 irmp_store_bit (1);
1524 DEBUG_PUTCHAR ('0');
1525 DEBUG_PUTCHAR ('\n');
1526 irmp_store_bit (0);
1527 last_value = 0;
1528 }
1529
1530 else // if (irmp_pulse_time >= RC5_BIT_LEN_MIN && irmp_pulse_time <= RC5_BIT_LEN_MAX)
1531 {
1532 uint8_t rc5_value;
1533
1534 if (last_pause > RC5_BIT_LEN_MAX && last_pause <= 2 * RC5_BIT_LEN_MAX)
1535 {
1536 rc5_value = last_value ? 0 : 1;
1537 last_value = rc5_value;
1538 }
1539 else
1540 {
1541 rc5_value = last_value;
1542 }
1543
1544 DEBUG_PUTCHAR (rc5_value + '0');
1545 DEBUG_PUTCHAR ('\n');
1546 irmp_store_bit (rc5_value);
1547 }
1548
1549 last_pause = irmp_pause_time;
1550 wait_for_space = 0;
1551 }
1552 else
1553 #endif
1554
1555 #if IRMP_SUPPORT_RC6_PROTOCOL == 1
1556 if (irmp_param.protocol == IRMP_RC6_PROTOCOL) // special rc6 decoder
1557 {
1558 switch (irmp_bit)
1559 { // handle toggle bit, which is 2 times longer than other bits
1560 case 3:
1561 case 4:
1562 case 5:
1563 if (irmp_pulse_time > RC6_TOGGLE_BIT_LEN_MAX && irmp_pause_time > RC6_TOGGLE_BIT_LEN_MAX)
1564 {
1565 DEBUG_PUTCHAR ('1');
1566 irmp_store_bit (1);
1567 }
1568
1569 DEBUG_PUTCHAR ('0');
1570 irmp_store_bit (0);
1571 last_value = 0;
1572 DEBUG_PUTCHAR ('\n');
1573 break;
1574
1575 default:
1576 if (irmp_pulse_time > RC6_BIT_LEN_MAX && irmp_pulse_time <= 2 * RC6_BIT_LEN_MAX)
1577 {
1578 DEBUG_PUTCHAR ('0');
1579 irmp_store_bit (0);
1580 DEBUG_PUTCHAR ('1');
1581 DEBUG_PUTCHAR ('\n');
1582 irmp_store_bit (1);
1583 last_value = 1;
1584 }
1585 else // if (irmp_pulse_time >= RC6_BIT_LEN_MIN && irmp_pulse_time <= RC6_BIT_LEN_MAX)
1586 {
1587 uint8_t rc5_value;
1588
1589 if (last_pause > RC6_BIT_LEN_MAX && last_pause <= 2 * RC6_BIT_LEN_MAX)
1590 {
1591 rc5_value = last_value ? 0 : 1;
1592 last_value = rc5_value;
1593 }
1594 else
1595 {
1596 rc5_value = last_value;
1597 }
1598
1599 if (irmp_bit == 1 && rc5_value == 0)
1600 {
1601 irmp_param.complete_len = RC6_COMPLETE_DATA_LEN_LONG;
1602 }
1603
1604 DEBUG_PUTCHAR (rc5_value + '0');
1605 DEBUG_PUTCHAR ('\n');
1606 irmp_store_bit (rc5_value);
1607 }
1608
1609 last_pause = irmp_pause_time;
1610 break;
1611 } // switch
1612
1613 wait_for_space = 0;
1614 }
1615 else
1616 #endif // IRMP_SUPPORT_RC6_PROTOCOL == 1
1617
1618 #if IRMP_SUPPORT_SAMSUNG_PROTOCOL == 1
1619 if (irmp_param.protocol == IRMP_SAMSUNG_PROTOCOL && irmp_bit == 16) // Samsung: 16th bit
1620 {
1621 if (irmp_pulse_time >= SAMSUNG_PULSE_LEN_MIN && irmp_pulse_time <= SAMSUNG_PULSE_LEN_MAX &&
1622 irmp_pause_time >= SAMSUNG_START_BIT_PAUSE_LEN_MIN && irmp_pause_time <= SAMSUNG_START_BIT_PAUSE_LEN_MAX)
1623 {
1624 DEBUG_PRINTF ("SYNC\n");
1625 wait_for_space = 0;
1626 irmp_tmp_id = 0;
1627 irmp_bit++;
1628 }
1629 else if (irmp_pulse_time >= SAMSUNG_PULSE_LEN_MIN && irmp_pulse_time <= SAMSUNG_PULSE_LEN_MAX)
1630 {
1631 if (irmp_pause_time >= SAMSUNG_1_PAUSE_LEN_MIN && irmp_pause_time <= SAMSUNG_1_PAUSE_LEN_MAX)
1632 {
1633 DEBUG_PUTCHAR ('1');
1634 DEBUG_PUTCHAR ('\n');
1635 irmp_store_bit (1);
1636 wait_for_space = 0;
1637 }
1638 else
1639 {
1640 DEBUG_PUTCHAR ('0');
1641 DEBUG_PUTCHAR ('\n');
1642 irmp_store_bit (0);
1643 wait_for_space = 0;
1644 }
1645
1646 DEBUG_PRINTF ("Switching to SAMSUNG32 protocol\n");
1647
1648 irmp_param.protocol = IRMP_SAMSUNG32_PROTOCOL;
1649 irmp_param.command_offset = SAMSUNG32_COMMAND_OFFSET;
1650 irmp_param.command_end = SAMSUNG32_COMMAND_OFFSET + SAMSUNG32_COMMAND_LEN;
1651 irmp_param.complete_len = SAMSUNG32_COMPLETE_DATA_LEN;
1652 }
1653 else
1654 { // timing incorrect!
1655 DEBUG_PRINTF ("error 3 Samsung: timing not correct: data bit %d, pulse: %d, pause: %d\n", irmp_bit, irmp_pulse_time, irmp_pause_time);
1656 irmp_start_bit_detected = 0; // reset flags and wait for next start bit
1657 irmp_pause_time = 0;
1658 }
1659 }
1660 else
1661 #endif // IRMP_SUPPORT_SAMSUNG_PROTOCOL
1662
1663 #if IRMP_SUPPORT_BANG_OLUFSEN_PROTOCOL == 1
1664 if (irmp_param.protocol == IRMP_BANG_OLUFSEN_PROTOCOL)
1665 {
1666 if (irmp_pulse_time >= BANG_OLUFSEN_PULSE_LEN_MIN && irmp_pulse_time <= BANG_OLUFSEN_PULSE_LEN_MAX)
1667 {
1668 if (irmp_bit == 1) // Bang & Olufsen: 3rd bit
1669 {
1670 if (irmp_pause_time >= BANG_OLUFSEN_START_BIT3_PAUSE_LEN_MIN && irmp_pause_time <= BANG_OLUFSEN_START_BIT3_PAUSE_LEN_MAX)
1671 {
1672 DEBUG_PRINTF ("3rd start bit\n");
1673 wait_for_space = 0;
1674 irmp_tmp_id = 0;
1675 irmp_bit++;
1676 }
1677 else
1678 { // timing incorrect!
1679 DEBUG_PRINTF ("error 3a B&O: timing not correct: data bit %d, pulse: %d, pause: %d\n", irmp_bit, irmp_pulse_time, irmp_pause_time);
1680 irmp_start_bit_detected = 0; // reset flags and wait for next start bit
1681 irmp_pause_time = 0;
1682 }
1683 }
1684 else if (irmp_bit == 19) // Bang & Olufsen: trailer bit
1685 {
1686 if (irmp_pause_time >= BANG_OLUFSEN_TRAILER_BIT_PAUSE_LEN_MIN && irmp_pause_time <= BANG_OLUFSEN_TRAILER_BIT_PAUSE_LEN_MAX)
1687 {
1688 DEBUG_PRINTF ("trailer bit\n");
1689 wait_for_space = 0;
1690 irmp_tmp_id = 0;
1691 irmp_bit++;
1692 }
1693 else
1694 { // timing incorrect!
1695 DEBUG_PRINTF ("error 3b B&O: timing not correct: data bit %d, pulse: %d, pause: %d\n", irmp_bit, irmp_pulse_time, irmp_pause_time);
1696 irmp_start_bit_detected = 0; // reset flags and wait for next start bit
1697 irmp_pause_time = 0;
1698 }
1699 }
1700 else
1701 {
1702 if (irmp_pause_time >= BANG_OLUFSEN_1_PAUSE_LEN_MIN && irmp_pause_time <= BANG_OLUFSEN_1_PAUSE_LEN_MAX)
1703 { // pulse & pause timings correct for "1"?
1704 DEBUG_PUTCHAR ('1');
1705 DEBUG_PUTCHAR ('\n');
1706 irmp_store_bit (1);
1707 last_value = 1;
1708 wait_for_space = 0;
1709 }
1710 else if (irmp_pause_time >= BANG_OLUFSEN_0_PAUSE_LEN_MIN && irmp_pause_time <= BANG_OLUFSEN_0_PAUSE_LEN_MAX)
1711 { // pulse & pause timings correct for "0"?
1712 DEBUG_PUTCHAR ('0');
1713 DEBUG_PUTCHAR ('\n');
1714 irmp_store_bit (0);
1715 last_value = 0;
1716 wait_for_space = 0;
1717 }
1718 else if (irmp_pause_time >= BANG_OLUFSEN_R_PAUSE_LEN_MIN && irmp_pause_time <= BANG_OLUFSEN_R_PAUSE_LEN_MAX)
1719 {
1720 DEBUG_PUTCHAR (last_value + '0');
1721 DEBUG_PUTCHAR ('\n');
1722 irmp_store_bit (last_value);
1723 wait_for_space = 0;
1724 }
1725 else
1726 { // timing incorrect!
1727 DEBUG_PRINTF ("error 3c B&O: timing not correct: data bit %d, pulse: %d, pause: %d\n", irmp_bit, irmp_pulse_time, irmp_pause_time);
1728 irmp_start_bit_detected = 0; // reset flags and wait for next start bit
1729 irmp_pause_time = 0;
1730 }
1731 }
1732 }
1733 else
1734 { // timing incorrect!
1735 DEBUG_PRINTF ("error 3d B&O: timing not correct: data bit %d, pulse: %d, pause: %d\n", irmp_bit, irmp_pulse_time, irmp_pause_time);
1736 irmp_start_bit_detected = 0; // reset flags and wait for next start bit
1737 irmp_pause_time = 0;
1738 }
1739 }
1740 else
1741 #endif // IRMP_SUPPORT_BANG_OLUFSEN_PROTOCOL
1742
1743 if (irmp_pulse_time >= irmp_param.pulse_1_len_min && irmp_pulse_time <= irmp_param.pulse_1_len_max &&
1744 irmp_pause_time >= irmp_param.pause_1_len_min && irmp_pause_time <= irmp_param.pause_1_len_max)
1745 { // pulse & pause timings correct for "1"?
1746 DEBUG_PUTCHAR ('1');
1747 DEBUG_PUTCHAR ('\n');
1748 irmp_store_bit (1);
1749 wait_for_space = 0;
1750 }
1751 else if (irmp_pulse_time >= irmp_param.pulse_0_len_min && irmp_pulse_time <= irmp_param.pulse_0_len_max &&
1752 irmp_pause_time >= irmp_param.pause_0_len_min && irmp_pause_time <= irmp_param.pause_0_len_max)
1753 { // pulse & pause timings correct for "0"?
1754 DEBUG_PUTCHAR ('0');
1755 DEBUG_PUTCHAR ('\n');
1756 irmp_store_bit (0);
1757 wait_for_space = 0;
1758 }
1759 else
1760 { // timing incorrect!
1761 DEBUG_PRINTF ("error 3: timing not correct: data bit %d, pulse: %d, pause: %d\n", irmp_bit, irmp_pulse_time, irmp_pause_time);
1762 irmp_start_bit_detected = 0; // reset flags and wait for next start bit
1763 irmp_pause_time = 0;
1764 }
1765
1766 irmp_pulse_time = 1; // set counter to 1, not 0
1767 }
1768 }
1769 else
1770 { // counting the pulse length ...
1771 if (!irmp_input) // still light?
1772 { // yes...
1773 irmp_pulse_time++; // increment counter
1774 }
1775 else
1776 { // now it's dark!
1777 wait_for_space = 1; // let's count the time (see above)
1778 irmp_pause_time = 1; // set pause counter to 1, not 0
1779 }
1780 }
1781
1782 if (irmp_bit == irmp_param.complete_len && irmp_param.stop_bit == 0) // enough bits received?
1783 {
1784 // if SIRCS/SAMSUNG32 protocol and the code will be repeated within 50 ms, we will ignore it.
1785 if ((irmp_param.protocol == IRMP_SIRCS_PROTOCOL ||
1786 irmp_param.protocol == IRMP_SAMSUNG32_PROTOCOL ||
1787 irmp_param.protocol == IRMP_NUBERT_PROTOCOL) &&
1788 last_irmp_command == irmp_tmp_command && repetition_counter < AUTO_REPETITION_LEN)
1789 {
1790 DEBUG_PRINTF ("code skipped, recognized SIRCS, SAMSUNG32 or NUBERT repetition, counter = %d, auto repetition len = %d\n",
1791 repetition_counter, AUTO_REPETITION_LEN);
1792 repetition_counter = 0;
1793 }
1794 else
1795 {
1796 DEBUG_PRINTF ("code detected, length = %d\n", irmp_bit);
1797 irmp_ir_detected = TRUE;
1798
1799 #if IRMP_SUPPORT_DENON_PROTOCOL == 1
1800 if (irmp_param.protocol == IRMP_DENON_PROTOCOL)
1801 { // check for repetition frame
1802 if ((~irmp_tmp_command & 0x3FF) == last_irmp_denon_command) // command bits must be inverted
1803 {
1804 irmp_tmp_command = last_irmp_denon_command; // use command received before!
1805
1806 irmp_protocol = irmp_param.protocol; // store protocol
1807 irmp_address = irmp_tmp_address; // store address
1808 irmp_command = irmp_tmp_command ; // store command
1809 }
1810 else
1811 {
1812 DEBUG_PRINTF ("waiting for inverted command repetition\n");
1813 irmp_ir_detected = FALSE;
1814 last_irmp_denon_command = irmp_tmp_command;
1815 }
1816 }
1817 else
1818 #endif // IRMP_SUPPORT_DENON_PROTOCOL
1819 {
1820 #if IRMP_SUPPORT_NEC_PROTOCOL == 1
1821 if (irmp_param.protocol == IRMP_NEC_PROTOCOL && irmp_bit == 0) // repetition frame
1822 {
1823 irmp_tmp_address = last_irmp_address; // address is last address
1824 irmp_tmp_command = last_irmp_command; // command is last command
1825 irmp_flags |= IRMP_FLAG_REPETITION;
1826 }
1827 #endif // IRMP_SUPPORT_NEC_PROTOCOL
1828 irmp_protocol = irmp_param.protocol;
1829 irmp_address = irmp_tmp_address; // store address
1830 #if IRMP_SUPPORT_NEC_PROTOCOL == 1
1831 last_irmp_address = irmp_tmp_address; // store as last address, too
1832 #endif
1833
1834 #if IRMP_SUPPORT_RC5_PROTOCOL == 1
1835 irmp_tmp_command |= rc5_cmd_bit6; // store bit 6
1836 #endif
1837 irmp_command = irmp_tmp_command; // store command
1838
1839 #if IRMP_SUPPORT_SAMSUNG_PROTOCOL == 1
1840 irmp_id = irmp_tmp_id;
1841 #endif
1842 }
1843 }
1844
1845 if (irmp_ir_detected)
1846 {
1847 if (last_irmp_command == irmp_command &&
1848 last_irmp_address == irmp_address &&
1849 repetition_counter < IRMP_REPETITION_TIME)
1850 {
1851 irmp_flags |= IRMP_FLAG_REPETITION;
1852 }
1853
1854 last_irmp_address = irmp_tmp_address; // store as last address, too
1855 last_irmp_command = irmp_tmp_command; // store as last command, too
1856
1857 repetition_counter = 0;
1858 }
1859
1860 irmp_start_bit_detected = 0; // and wait for next start bit
1861 irmp_tmp_command = 0;
1862 irmp_pulse_time = 0;
1863 irmp_pause_time = 0;
1864 }
1865 }
1866 }
1867 }
1868
1869 #ifdef DEBUG
1870
1871 // main function - for unix/linux + windows only!
1872 // AVR: see main.c!
1873 // Compile it under linux with:
1874 // cc irmp.c -o irmp
1875 //
1876 // usage: ./irmp [-v|-s|-a] < file
1877
1878 static void
1879 print_timings (void)
1880 {
1881 printf ("PROTOCOL START BIT NO. START BIT PULSE START BIT PAUSE\n");
1882 printf ("====================================================================================\n");
1883 printf ("SIRCS 1 %3d - %3d %3d - %3d\n",
1884 SIRCS_START_BIT_PULSE_LEN_MIN, SIRCS_START_BIT_PULSE_LEN_MAX, SIRCS_START_BIT_PAUSE_LEN_MIN, SIRCS_START_BIT_PAUSE_LEN_MAX);
1885 printf ("NEC 1 %3d - %3d %3d - %3d\n",
1886 NEC_START_BIT_PULSE_LEN_MIN, NEC_START_BIT_PULSE_LEN_MAX, NEC_START_BIT_PAUSE_LEN_MIN, NEC_START_BIT_PAUSE_LEN_MAX);
1887 printf ("NEC (rep) 1 %3d - %3d %3d - %3d\n",
1888 NEC_START_BIT_PULSE_LEN_MIN, NEC_START_BIT_PULSE_LEN_MAX, NEC_REPEAT_START_BIT_PAUSE_LEN_MIN, NEC_REPEAT_START_BIT_PAUSE_LEN_MAX);
1889 printf ("SAMSUNG 1 %3d - %3d %3d - %3d\n",
1890 SAMSUNG_START_BIT_PULSE_LEN_MIN, SAMSUNG_START_BIT_PULSE_LEN_MAX, SAMSUNG_START_BIT_PAUSE_LEN_MIN, SAMSUNG_START_BIT_PAUSE_LEN_MAX);
1891 printf ("MATSUSHITA 1 %3d - %3d %3d - %3d\n",
1892 MATSUSHITA_START_BIT_PULSE_LEN_MIN, MATSUSHITA_START_BIT_PULSE_LEN_MAX, MATSUSHITA_START_BIT_PAUSE_LEN_MIN, MATSUSHITA_START_BIT_PAUSE_LEN_MAX);
1893 printf ("KASEIKYO 1 %3d - %3d %3d - %3d\n",
1894 KASEIKYO_START_BIT_PULSE_LEN_MIN, KASEIKYO_START_BIT_PULSE_LEN_MAX, KASEIKYO_START_BIT_PAUSE_LEN_MIN, KASEIKYO_START_BIT_PAUSE_LEN_MAX);
1895 printf ("RECS80 1 %3d - %3d %3d - %3d\n",
1896 RECS80_START_BIT_PULSE_LEN_MIN, RECS80_START_BIT_PULSE_LEN_MAX, RECS80_START_BIT_PAUSE_LEN_MIN, RECS80_START_BIT_PAUSE_LEN_MAX);
1897 printf ("RC5 1 %3d - %3d %3d - %3d\n",
1898 RC5_START_BIT_LEN_MIN, RC5_START_BIT_LEN_MAX, RC5_START_BIT_LEN_MIN, RC5_START_BIT_LEN_MAX);
1899 printf ("DENON 1 %3d - %3d %3d - %3d or %3d - %3d\n",
1900 DENON_PULSE_LEN_MIN, DENON_PULSE_LEN_MAX, DENON_1_PAUSE_LEN_MIN, DENON_1_PAUSE_LEN_MAX, DENON_0_PAUSE_LEN_MIN, DENON_0_PAUSE_LEN_MAX);
1901 printf ("RC6 1 %3d - %3d %3d - %3d\n",
1902 RC6_START_BIT_PULSE_LEN_MIN, RC6_START_BIT_PULSE_LEN_MAX, RC6_START_BIT_PAUSE_LEN_MIN, RC6_START_BIT_PAUSE_LEN_MAX);
1903 printf ("RECS80EXT 1 %3d - %3d %3d - %3d\n",
1904 RECS80EXT_START_BIT_PULSE_LEN_MIN, RECS80EXT_START_BIT_PULSE_LEN_MAX, RECS80EXT_START_BIT_PAUSE_LEN_MIN, RECS80EXT_START_BIT_PAUSE_LEN_MAX);
1905 printf ("NUBERT 1 %3d - %3d %3d - %3d\n",
1906 NUBERT_START_BIT_PULSE_LEN_MIN, NUBERT_START_BIT_PULSE_LEN_MAX, NUBERT_START_BIT_PAUSE_LEN_MIN, NUBERT_START_BIT_PAUSE_LEN_MAX);
1907 printf ("BANG_OLUFSEN 1 %3d - %3d %3d - %3d\n",
1908 BANG_OLUFSEN_START_BIT1_PULSE_LEN_MIN, BANG_OLUFSEN_START_BIT1_PULSE_LEN_MAX, BANG_OLUFSEN_START_BIT1_PAUSE_LEN_MIN, BANG_OLUFSEN_START_BIT1_PAUSE_LEN_MAX);
1909 printf ("BANG_OLUFSEN 2 %3d - %3d %3d - %3d\n",
1910 BANG_OLUFSEN_START_BIT2_PULSE_LEN_MIN, BANG_OLUFSEN_START_BIT2_PULSE_LEN_MAX, BANG_OLUFSEN_START_BIT2_PAUSE_LEN_MIN, BANG_OLUFSEN_START_BIT2_PAUSE_LEN_MAX);
1911 printf ("BANG_OLUFSEN 3 %3d - %3d %3d - %3d\n",
1912 BANG_OLUFSEN_START_BIT3_PULSE_LEN_MIN, BANG_OLUFSEN_START_BIT3_PULSE_LEN_MAX, BANG_OLUFSEN_START_BIT3_PAUSE_LEN_MIN, BANG_OLUFSEN_START_BIT3_PAUSE_LEN_MAX);
1913 printf ("BANG_OLUFSEN 4 %3d - %3d %3d - %3d\n",
1914 BANG_OLUFSEN_START_BIT4_PULSE_LEN_MIN, BANG_OLUFSEN_START_BIT4_PULSE_LEN_MAX, BANG_OLUFSEN_START_BIT4_PAUSE_LEN_MIN, BANG_OLUFSEN_START_BIT4_PAUSE_LEN_MAX);
1915 }
1916
1917 int
1918 main (int argc, char ** argv)
1919 {
1920 int i;
1921 int verbose = FALSE;
1922 int analyze = FALSE;
1923 int ch;
1924 int last_ch = 0;
1925 int pulse = 0;
1926 int pause = 0;
1927
1928 int min_pulse_long = 100000;
1929 int max_pulse_long = 0;
1930 int sum_pulses_long = 0;
1931 int n_pulses_long = 0;
1932
1933 int min_pulse_short = 100000;
1934 int max_pulse_short = 0;
1935 int sum_pulses_short = 0;
1936 int n_pulses_short = 0;
1937
1938 int min_pause_long = 100000;
1939 int max_pause_long = 0;
1940 int sum_pauses_long = 0;
1941 int n_pauses_long = 0;
1942
1943 int min_pause_short = 100000;
1944 int max_pause_short = 0;
1945 int sum_pauses_short = 0;
1946 int n_pauses_short = 0;
1947
1948 int min_start_pulse = 100000;
1949 int max_start_pulse = 0;
1950 int sum_start_pulses = 0;
1951 int n_start_pulses = 0;
1952
1953 int min_start_pause = 100000;
1954 int max_start_pause = 0;
1955 int sum_start_pauses = 0;
1956 int n_start_pauses = 0;
1957
1958 int first_pulse = TRUE;
1959 int first_pause = TRUE;
1960
1961 IRMP_DATA irmp_data;
1962
1963 if (argc == 2)
1964 {
1965 if (! strcmp (argv[1], "-v"))
1966 {
1967 verbose = TRUE;
1968 }
1969 else if (! strcmp (argv[1], "-a"))
1970 {
1971 analyze = TRUE;
1972 verbose = TRUE;
1973 }
1974 else if (! strcmp (argv[1], "-s"))
1975 {
1976 silent = TRUE;
1977 }
1978 else if (! strcmp (argv[1], "-p"))
1979 {
1980 print_timings ();
1981 return (0);
1982 }
1983 }
1984
1985 IRMP_PIN = 0xFF;
1986
1987 while ((ch = getchar ()) != EOF)
1988 {
1989 if (ch == '_' || ch == '0')
1990 {
1991 if (last_ch != ch)
1992 {
1993 if (verbose && pause > 0)
1994 {
1995 printf ("pause: %d\n", pause);
1996
1997 if (first_pause)
1998 {
1999 if (min_start_pause > pause)
2000 {
2001 min_start_pause = pause;
2002 }
2003 if (max_start_pause < pause)
2004 {
2005 max_start_pause = pause;
2006 }
2007 n_start_pauses++;
2008 sum_start_pauses += pause;
2009 first_pause = FALSE;
2010 }
2011 else
2012 {
2013 if (pause >= 10)
2014 {
2015 if (pause > 100) // perhaps repetition frame follows
2016 {
2017 first_pulse = TRUE;
2018 first_pause = TRUE;
2019 }
2020 else
2021 {
2022 if (min_pause_long > pause)
2023 {
2024 min_pause_long = pause;
2025 }
2026 if (max_pause_long < pause)
2027 {
2028 max_pause_long = pause;
2029 }
2030 n_pauses_long++;
2031 sum_pauses_long += pause;
2032 }
2033 }
2034 else
2035 {
2036 if (min_pause_short > pause)
2037 {
2038 min_pause_short = pause;
2039 }
2040 if (max_pause_short < pause)
2041 {
2042 max_pause_short = pause;
2043 }
2044 n_pauses_short++;
2045 sum_pauses_short += pause;
2046 }
2047 }
2048 }
2049 pause = 0;
2050 }
2051 pulse++;
2052 IRMP_PIN = 0x00;
2053 }
2054 else if (ch == 0xaf || ch == '-' || ch == '1')
2055 {
2056 if (last_ch != ch)
2057 {
2058 if (verbose)
2059 {
2060 printf ("pulse: %d ", pulse);
2061
2062 if (first_pulse)
2063 {
2064 if (min_start_pulse > pulse)
2065 {
2066 min_start_pulse = pulse;
2067 }
2068 if (max_start_pulse < pulse)
2069 {
2070 max_start_pulse = pulse;
2071 }
2072 n_start_pulses++;
2073 sum_start_pulses += pulse;
2074 first_pulse = FALSE;
2075 }
2076 else
2077 {
2078 if (pulse >= 10)
2079 {
2080 if (min_pulse_long > pulse)
2081 {
2082 min_pulse_long = pulse;
2083 }
2084 if (max_pulse_long < pulse)
2085 {
2086 max_pulse_long = pulse;
2087 }
2088 n_pulses_long++;
2089 sum_pulses_long += pulse;
2090 }
2091 else
2092 {
2093 if (min_pulse_short > pulse)
2094 {
2095 min_pulse_short = pulse;
2096 }
2097 if (max_pulse_short < pulse)
2098 {
2099 max_pulse_short = pulse;
2100 }
2101 n_pulses_short++;
2102 sum_pulses_short += pulse;
2103 }
2104 }
2105 }
2106 pulse = 0;
2107 }
2108 pause++;
2109 IRMP_PIN = 0xff;
2110 }
2111 else if (ch == '\n')
2112 {
2113 IRMP_PIN = 0xff;
2114
2115 if (verbose && pause > 0)
2116 {
2117 printf ("pause: %d\n", pause);
2118 }
2119 pause = 0;
2120
2121 if (! analyze)
2122 {
2123 for (i = 0; i < 8000; i++) // newline: long pause of 800 msec
2124 {
2125 irmp_ISR ();
2126 }
2127 }
2128 first_pulse = TRUE;
2129 first_pause = TRUE;
2130 }
2131 else if (ch == '#')
2132 {
2133 puts ("-------------------------------------------------------------------");
2134 putchar (ch);
2135
2136 while ((ch = getchar()) != '\n' && ch != EOF)
2137 {
2138 if (ch != '\r') // ignore CR in DOS/Windows files
2139 {
2140 putchar (ch);
2141 }
2142 }
2143 putchar ('\n');
2144 }
2145
2146 last_ch = ch;
2147
2148 if (! analyze)
2149 {
2150 irmp_ISR ();
2151 }
2152
2153 if (irmp_get_data (&irmp_data))
2154 {
2155 printf ("protcol = %d, address = 0x%04x, code = 0x%04x, flags = 0x%02x\n",
2156 irmp_data.protocol, irmp_data.address, irmp_data.command, irmp_data.flags);
2157 }
2158 }
2159
2160 if (analyze)
2161 {
2162 printf ("\nSTATITSTICS:\n");
2163 printf ("---------------------------------\n");
2164 printf ("number of start pulses: %d\n", n_start_pulses);
2165 printf ("minimum start pulse length: %d usec\n", (1000000 * min_start_pulse) / F_INTERRUPTS);
2166 printf ("maximum start pulse length: %d usec\n", (1000000 * max_start_pulse) / F_INTERRUPTS);
2167 if (n_start_pulses > 0)
2168 {
2169 printf ("average start pulse length: %d usec\n", ((1000000 * sum_start_pulses) / n_start_pulses) / F_INTERRUPTS);
2170 }
2171 putchar ('\n');
2172 printf ("number of start pauses: %d\n", n_start_pauses);
2173 if (n_start_pauses > 0)
2174 {
2175 printf ("minimum start pause length: %d usec\n", (1000000 * min_start_pause) / F_INTERRUPTS);
2176 printf ("maximum start pause length: %d usec\n", (1000000 * max_start_pause) / F_INTERRUPTS);
2177 printf ("average start pause length: %d usec\n", ((1000000 * sum_start_pauses) / n_start_pauses) / F_INTERRUPTS);
2178 }
2179 putchar ('\n');
2180 printf ("number of long pulses: %d\n", n_pulses_long);
2181 if (n_pulses_long > 0)
2182 {
2183 printf ("minimum long pulse length: %d usec\n", (1000000 * min_pulse_long) / F_INTERRUPTS);
2184 printf ("maximum long pulse length: %d usec\n", (1000000 * max_pulse_long) / F_INTERRUPTS);
2185 printf ("average long pulse length: %d usec\n", ((1000000 * sum_pulses_long) / n_pulses_long) / F_INTERRUPTS);
2186 }
2187 putchar ('\n');
2188 printf ("number of short pulses: %d\n", n_pulses_short);
2189 if (n_pulses_short > 0)
2190 {
2191 printf ("minimum short pulse length: %d usec\n", (1000000 * min_pulse_short) / F_INTERRUPTS);
2192 printf ("maximum short pulse length: %d usec\n", (1000000 * max_pulse_short) / F_INTERRUPTS);
2193 printf ("average short pulse length: %d usec\n", ((1000000 * sum_pulses_short) / n_pulses_short) / F_INTERRUPTS);
2194
2195 }
2196 putchar ('\n');
2197 printf ("number of long pauses: %d\n", n_pauses_long);
2198 if (n_pauses_long > 0)
2199 {
2200 printf ("minimum long pause length: %d usec\n", (1000000 * min_pause_long) / F_INTERRUPTS);
2201 printf ("maximum long pause length: %d usec\n", (1000000 * max_pause_long) / F_INTERRUPTS);
2202 printf ("average long pause length: %d usec\n", ((1000000 * sum_pauses_long) / n_pauses_long) / F_INTERRUPTS);
2203 }
2204 putchar ('\n');
2205 printf ("number of short pauses: %d\n", n_pauses_short);
2206 if (n_pauses_short > 0)
2207 {
2208 printf ("minimum short pause length: %d usec\n", (1000000 * min_pause_short) / F_INTERRUPTS);
2209 printf ("maximum short pause length: %d usec\n", (1000000 * max_pause_short) / F_INTERRUPTS);
2210 printf ("average short pause length: %d usec\n", ((1000000 * sum_pauses_short) / n_pauses_short) / F_INTERRUPTS);
2211 }
2212 }
2213 return 0;
2214 }
2215
2216 #endif // DEBUG