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