-
Notifications
You must be signed in to change notification settings - Fork 0
/
irmp-main-analyze.c
616 lines (546 loc) · 22.1 KB
/
irmp-main-analyze.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#include "irmp.c"
#ifdef ANALYZE
/*---------------------------------------------------------------------------------------------------------------------------------------------------
* main functions - for Unix/Linux + Windows only!
*
* AVR: see main.c!
*
* Compile it under linux with:
* cc irmp.c -o irmp
*
* usage: ./irmp [-v|-s|-a|-l] < file
*
* options:
* -v verbose
* -s silent
* -a analyze
* -l list pulse/pauses
*---------------------------------------------------------------------------------------------------------------------------------------------------
*/
void
print_spectrum (const char * text, int * buf, int is_pulse)
{
int i;
int j;
int min;
int max;
int max_value = 0;
int value;
int sum = 0;
int counter = 0;
double average = 0;
double tolerance;
puts ("-----------------------------------------------------------------------------");
printf ("%s:\n", text);
for (i = 0; i < 256; i++)
{
if (buf[i] > max_value)
{
max_value = buf[i];
}
}
for (i = 1; i < 200; i++)
{
if (buf[i] > 0)
{
printf ("%3d ", i);
value = (buf[i] * 60) / max_value;
for (j = 0; j < value; j++)
{
putchar ('o');
}
printf (" %d\n", buf[i]);
sum += i * buf[i];
counter += buf[i];
}
else
{
max = i - 1;
if (counter > 0)
{
average = (float) sum / (float) counter;
if (is_pulse)
{
printf ("pulse ");
}
else
{
printf ("pause ");
}
printf ("avg: %4.1f=%6.1f us, ", average, (1000000. * average) / (float) F_INTERRUPTS);
printf ("min: %2d=%6.1f us, ", min, (1000000. * min) / (float) F_INTERRUPTS);
printf ("max: %2d=%6.1f us, ", max, (1000000. * max) / (float) F_INTERRUPTS);
tolerance = (max - average);
if (average - min > tolerance)
{
tolerance = average - min;
}
tolerance = tolerance * 100 / average;
printf ("tol: %4.1f%%\n", tolerance);
}
counter = 0;
sum = 0;
min = i + 1;
}
}
}
#define STATE_LEFT_SHIFT 0x01
#define STATE_RIGHT_SHIFT 0x02
#define STATE_LEFT_CTRL 0x04
#define STATE_LEFT_ALT 0x08
#define STATE_RIGHT_ALT 0x10
#define KEY_ESCAPE 0x1B // keycode = 0x006e
#define KEY_MENUE 0x80 // keycode = 0x0070
#define KEY_BACK 0x81 // keycode = 0x0071
#define KEY_FORWARD 0x82 // keycode = 0x0072
#define KEY_ADDRESS 0x83 // keycode = 0x0073
#define KEY_WINDOW 0x84 // keycode = 0x0074
#define KEY_1ST_PAGE 0x85 // keycode = 0x0075
#define KEY_STOP 0x86 // keycode = 0x0076
#define KEY_MAIL 0x87 // keycode = 0x0077
#define KEY_FAVORITES 0x88 // keycode = 0x0078
#define KEY_NEW_PAGE 0x89 // keycode = 0x0079
#define KEY_SETUP 0x8A // keycode = 0x007a
#define KEY_FONT 0x8B // keycode = 0x007b
#define KEY_PRINT 0x8C // keycode = 0x007c
#define KEY_ON_OFF 0x8E // keycode = 0x007c
#define KEY_INSERT 0x90 // keycode = 0x004b
#define KEY_DELETE 0x91 // keycode = 0x004c
#define KEY_LEFT 0x92 // keycode = 0x004f
#define KEY_HOME 0x93 // keycode = 0x0050
#define KEY_END 0x94 // keycode = 0x0051
#define KEY_UP 0x95 // keycode = 0x0053
#define KEY_DOWN 0x96 // keycode = 0x0054
#define KEY_PAGE_UP 0x97 // keycode = 0x0055
#define KEY_PAGE_DOWN 0x98 // keycode = 0x0056
#define KEY_RIGHT 0x99 // keycode = 0x0059
#define KEY_MOUSE_1 0x9E // keycode = 0x0400
#define KEY_MOUSE_2 0x9F // keycode = 0x0800
static uint_fast8_t
get_fdc_key (uint_fast16_t cmd)
{
static const uint8_t key_table[128] =
{
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
0x00, '^', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 0xDF, 0xB4, 0x00, '\b',
'\t', 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', 'o', 'p', 0xFC, '+', 0x00, 0x00, 'a',
's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 0xF6, 0xE4, '#', '\r', 0x00, '<', 'y', 'x',
'c', 'v', 'b', 'n', 'm', ',', '.', '-', 0x00, 0x00, 0x00, 0x00, 0x00, ' ', 0x00, 0x00,
0x00, 0xB0, '!', '"', 0xA7, '$', '%', '&', '/', '(', ')', '=', '?', 0x60, 0x00, '\b',
'\t', 'Q', 'W', 'E', 'R', 'T', 'Z', 'U', 'I', 'O', 'P', 0xDC, '*', 0x00, 0x00, 'A',
'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 0xD6, 0xC4, '\'', '\r', 0x00, '>', 'Y', 'X',
'C', 'V', 'B', 'N', 'M', ';', ':', '_', 0x00, 0x00, 0x00, 0x00, 0x00, ' ', 0x00, 0x00
};
static uint_fast8_t state;
uint_fast8_t key = 0;
switch (cmd)
{
case 0x002C: state |= STATE_LEFT_SHIFT; break; // pressed left shift
case 0x00AC: state &= ~STATE_LEFT_SHIFT; break; // released left shift
case 0x0039: state |= STATE_RIGHT_SHIFT; break; // pressed right shift
case 0x00B9: state &= ~STATE_RIGHT_SHIFT; break; // released right shift
case 0x003A: state |= STATE_LEFT_CTRL; break; // pressed left ctrl
case 0x00BA: state &= ~STATE_LEFT_CTRL; break; // released left ctrl
case 0x003C: state |= STATE_LEFT_ALT; break; // pressed left alt
case 0x00BC: state &= ~STATE_LEFT_ALT; break; // released left alt
case 0x003E: state |= STATE_RIGHT_ALT; break; // pressed left alt
case 0x00BE: state &= ~STATE_RIGHT_ALT; break; // released left alt
case 0x006e: key = KEY_ESCAPE; break;
case 0x004b: key = KEY_INSERT; break;
case 0x004c: key = KEY_DELETE; break;
case 0x004f: key = KEY_LEFT; break;
case 0x0050: key = KEY_HOME; break;
case 0x0051: key = KEY_END; break;
case 0x0053: key = KEY_UP; break;
case 0x0054: key = KEY_DOWN; break;
case 0x0055: key = KEY_PAGE_UP; break;
case 0x0056: key = KEY_PAGE_DOWN; break;
case 0x0059: key = KEY_RIGHT; break;
case 0x0400: key = KEY_MOUSE_1; break;
case 0x0800: key = KEY_MOUSE_2; break;
default:
{
if (!(cmd & 0x80)) // pressed key
{
if (cmd >= 0x70 && cmd <= 0x7F) // function keys
{
key = cmd + 0x10; // 7x -> 8x
}
else if (cmd < 64) // key listed in key_table
{
if (state & (STATE_LEFT_ALT | STATE_RIGHT_ALT))
{
switch (cmd)
{
case 0x0003: key = 0xB2; break; // upper 2
case 0x0008: key = '{'; break;
case 0x0009: key = '['; break;
case 0x000A: key = ']'; break;
case 0x000B: key = '}'; break;
case 0x000C: key = '\\'; break;
case 0x001C: key = '~'; break;
case 0x002D: key = '|'; break;
case 0x0034: key = 0xB5; break; // Mu
}
}
else if (state & (STATE_LEFT_CTRL))
{
if (key_table[cmd] >= 'a' && key_table[cmd] <= 'z')
{
key = key_table[cmd] - 'a' + 1;
}
else
{
key = key_table[cmd];
}
}
else
{
int idx = cmd + ((state & (STATE_LEFT_SHIFT | STATE_RIGHT_SHIFT)) ? 64 : 0);
if (key_table[idx])
{
key = key_table[idx];
}
}
}
}
break;
}
}
return (key);
}
static int analyze = FALSE;
static int list = FALSE;
static IRMP_DATA irmp_data;
static int expected_protocol;
static int expected_address;
static int expected_command;
static int do_check_expected_values;
static void
next_tick (void)
{
if (! analyze && ! list)
{
(void) irmp_ISR ();
if (irmp_get_data (&irmp_data))
{
uint_fast8_t key;
ANALYZE_ONLY_NORMAL_PUTCHAR (' ');
if (verbose)
{
printf ("%8.3fms ", (float) (time_counter * 1000) / F_INTERRUPTS);
}
if (irmp_data.protocol == IRMP_ACP24_PROTOCOL)
{
uint16_t temp = (irmp_data.command & 0x000F) + 15;
printf ("p=%2d (%s), a=0x%04x, c=0x%04x, f=0x%02x, temp=%d",
irmp_data.protocol, irmp_protocol_names[irmp_data.protocol], irmp_data.address, irmp_data.command, irmp_data.flags, temp);
}
else if (irmp_data.protocol == IRMP_FDC_PROTOCOL && (key = get_fdc_key (irmp_data.command)) != 0)
{
if ((key >= 0x20 && key < 0x7F) || key >= 0xA0)
{
printf ("p=%2d (%s), a=0x%04x, c=0x%04x, f=0x%02x, asc=0x%02x, key='%c'",
irmp_data.protocol, irmp_protocol_names[irmp_data.protocol], irmp_data.address, irmp_data.command, irmp_data.flags, key, key);
}
else if (key == '\r' || key == '\t' || key == KEY_ESCAPE || (key >= 0x80 && key <= 0x9F)) // function keys
{
const char * p = (const char *) NULL;
switch (key)
{
case '\t' : p = "TAB"; break;
case '\r' : p = "CR"; break;
case KEY_ESCAPE : p = "ESCAPE"; break;
case KEY_MENUE : p = "MENUE"; break;
case KEY_BACK : p = "BACK"; break;
case KEY_FORWARD : p = "FORWARD"; break;
case KEY_ADDRESS : p = "ADDRESS"; break;
case KEY_WINDOW : p = "WINDOW"; break;
case KEY_1ST_PAGE : p = "1ST_PAGE"; break;
case KEY_STOP : p = "STOP"; break;
case KEY_MAIL : p = "MAIL"; break;
case KEY_FAVORITES : p = "FAVORITES"; break;
case KEY_NEW_PAGE : p = "NEW_PAGE"; break;
case KEY_SETUP : p = "SETUP"; break;
case KEY_FONT : p = "FONT"; break;
case KEY_PRINT : p = "PRINT"; break;
case KEY_ON_OFF : p = "ON_OFF"; break;
case KEY_INSERT : p = "INSERT"; break;
case KEY_DELETE : p = "DELETE"; break;
case KEY_LEFT : p = "LEFT"; break;
case KEY_HOME : p = "HOME"; break;
case KEY_END : p = "END"; break;
case KEY_UP : p = "UP"; break;
case KEY_DOWN : p = "DOWN"; break;
case KEY_PAGE_UP : p = "PAGE_UP"; break;
case KEY_PAGE_DOWN : p = "PAGE_DOWN"; break;
case KEY_RIGHT : p = "RIGHT"; break;
case KEY_MOUSE_1 : p = "KEY_MOUSE_1"; break;
case KEY_MOUSE_2 : p = "KEY_MOUSE_2"; break;
default : p = "<UNKNWON>"; break;
}
printf ("p=%2d (%s), a=0x%04x, c=0x%04x, f=0x%02x, asc=0x%02x, key=%s",
irmp_data.protocol, irmp_protocol_names[irmp_data.protocol], irmp_data.address, irmp_data.command, irmp_data.flags, key, p);
}
else
{
printf ("p=%2d (%s), a=0x%04x, c=0x%04x, f=0x%02x, asc=0x%02x",
irmp_data.protocol, irmp_protocol_names[irmp_data.protocol], irmp_data.address, irmp_data.command, irmp_data.flags, key);
}
}
else
{
printf ("p=%2d (%s), a=0x%04x, c=0x%04x, f=0x%02x",
irmp_data.protocol, irmp_protocol_names[irmp_data.protocol], irmp_data.address, irmp_data.command, irmp_data.flags);
}
if (do_check_expected_values)
{
if (irmp_data.protocol != expected_protocol ||
irmp_data.address != expected_address ||
irmp_data.command != expected_command)
{
printf ("\nerror 7: expected values differ: p=%2d (%s), a=0x%04x, c=0x%04x\n",
expected_protocol, irmp_protocol_names[expected_protocol], expected_address, expected_command);
}
else
{
printf (" checked!\n");
}
do_check_expected_values = FALSE; // only check 1st frame in a line!
}
else
{
putchar ('\n');
}
}
}
}
int
main (int argc, char ** argv)
{
int i;
int ch;
int last_ch = 0;
int pulse = 0;
int pause = 0;
int start_pulses[256];
int start_pauses[256];
int pulses[256];
int pauses[256];
int first_pulse = TRUE;
int first_pause = TRUE;
printf("IRMP F_INTERRUPTS = %u\n", F_INTERRUPTS);
if (argc == 2)
{
if (! strcmp (argv[1], "-v"))
{
verbose = TRUE;
}
else if (! strcmp (argv[1], "-l"))
{
list = TRUE;
}
else if (! strcmp (argv[1], "-a"))
{
analyze = TRUE;
}
else if (! strcmp (argv[1], "-s"))
{
silent = TRUE;
}
else if (! strcmp (argv[1], "-r"))
{
radio = TRUE;
}
}
for (i = 0; i < 256; i++)
{
start_pulses[i] = 0;
start_pauses[i] = 0;
pulses[i] = 0;
pauses[i] = 0;
}
IRMP_PIN = 0xFF;
while ((ch = getchar ()) != EOF)
{
if (ch == '_' || ch == '0')
{
if (last_ch != ch)
{
if (pause > 0)
{
if (list)
{
printf ("pause: %d\n", pause);
}
if (analyze)
{
if (first_pause)
{
if (pause < 256)
{
start_pauses[pause]++;
}
first_pause = FALSE;
}
else
{
if (pause < 256)
{
pauses[pause]++;
}
}
}
}
pause = 0;
}
pulse++;
IRMP_PIN = 0x00;
}
else if (ch == 0xaf || ch == '-' || ch == '1')
{
if (last_ch != ch)
{
if (list)
{
printf ("pulse: %d ", pulse);
}
if (analyze)
{
if (first_pulse)
{
if (pulse < 256)
{
start_pulses[pulse]++;
}
first_pulse = FALSE;
}
else
{
if (pulse < 256)
{
pulses[pulse]++;
}
}
}
pulse = 0;
}
pause++;
IRMP_PIN = 0xff;
}
else if (ch == '\n')
{
IRMP_PIN = 0xff;
time_counter = 0;
if (list && pause > 0)
{
printf ("pause: %d\n", pause);
}
pause = 0;
if (! analyze)
{
for (i = 0; i < (int) ((10000.0 * F_INTERRUPTS) / 10000); i++) // newline: long pause of 10000 msec
{
next_tick ();
}
}
first_pulse = TRUE;
first_pause = TRUE;
}
else if (ch == '#')
{
time_counter = 0;
if (analyze)
{
while ((ch = getchar()) != '\n' && ch != EOF)
{
;
}
}
else
{
char buf[1024];
char * p;
int idx = -1;
puts ("----------------------------------------------------------------------");
putchar (ch);
while ((ch = getchar()) != '\n' && ch != EOF)
{
if (ch != '\r') // ignore CR in DOS/Windows files
{
if (ch == '[' && idx == -1)
{
idx = 0;
}
else if (idx >= 0)
{
if (ch == ']')
{
do_check_expected_values = FALSE;
buf[idx] = '\0';
idx = -1;
expected_protocol = atoi (buf);
if (expected_protocol > 0)
{
p = buf;
while (*p)
{
if (*p == 'x')
{
p++;
if (sscanf (p, "%x", &expected_address) == 1)
{
do_check_expected_values = TRUE;
}
break;
}
p++;
}
if (do_check_expected_values)
{
do_check_expected_values = FALSE;
while (*p)
{
if (*p == 'x')
{
p++;
if (sscanf (p, "%x", &expected_command) == 1)
{
do_check_expected_values = TRUE;
}
break;
}
p++;
}
if (do_check_expected_values)
{
// printf ("!%2d %04x %04x!\n", expected_protocol, expected_address, expected_command);
}
}
}
}
else if (idx < 1024 - 2)
{
buf[idx++] = ch;
}
}
putchar (ch);
}
}
putchar ('\n');
}
}
last_ch = ch;
next_tick ();
}
if (analyze)
{
print_spectrum ("START PULSES", start_pulses, TRUE);
print_spectrum ("START PAUSES", start_pauses, FALSE);
print_spectrum ("PULSES", pulses, TRUE);
print_spectrum ("PAUSES", pauses, FALSE);
puts ("-----------------------------------------------------------------------------");
}
return 0;
}
#endif // ANALYZE