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