]> cloudbase.mooo.com Git - irmp.git/blob - irmp-main-mbed.cpp
Add support for libopencm3 (STM32F1)
[irmp.git] / irmp-main-mbed.cpp
1 /*---------------------------------------------------------------------------------------------------------------------------------------------------
2 * irmp-main-mbed.cpp - demo main module to test IRMP decoder on AVR
3 *
4 * $Id: irmp-main-mbed.cpp,v 1.1 2016/01/12 11:55:05 fm Exp $
5 *
6 * This demo module is runnable on MBED boards
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *---------------------------------------------------------------------------------------------------------------------------------------------------
13 */
14
15 #include "mbed.h"
16 #include "irmp.h"
17
18 #define LED_ON 0
19 #define LED_OFF 1
20
21 DigitalOut led(P0_14, 1);
22 DigitalOut flash(P0_12, 1);
23
24 Ticker t;
25
26 // only for performance test
27 Timer timerPerfTest;
28 int timeISRMax = 0;
29 float timeISRAvg;
30 int timeISRAvgSum = 0;
31 int countISRCalls = 0;
32
33 void irmpISR(void)
34 {
35 int t1 = timerPerfTest.read_us();
36
37 irmp_ISR(); // call irmp ISR
38
39 int timeISR = timerPerfTest.read_us() - t1; // calc time spent in worker ISR
40 if (timeISR > timeISRMax) // store maximum
41 {
42 timeISRMax = timeISR;
43 }
44 timeISRAvgSum += timeISR; // sum for avg
45 countISRCalls++;
46 }
47
48 int main()
49 {
50 printf("IRMP on mbed\n");
51
52 led = LED_OFF;
53 timerPerfTest.start();
54
55 IRMP_DATA irmp_data;
56
57 irmp_init(); // initialize irmp
58 t.attach_us(&irmpISR, 1E6 / F_INTERRUPTS); // call ISR 15000/s
59
60 // infinite loop, interrupts will toggle PORTD pins and handle UART communications.
61 while (1)
62 {
63 flash = !flash;
64
65 if (irmp_get_data (&irmp_data))
66 {
67 // ir signal decoded, do something here...
68 // irmp_data.protocol is the protocol, see irmp.h
69 // irmp_data.address is the address/manufacturer code of ir sender
70 // irmp_data.command is the command code
71 // irm_data.flags is press/release information
72 // irmp_protocol_names[irmp_data.protocol] is the protocol name (if enabled, see irmpconfig.h)
73 // printf("proto %d addr %d cmd %d\n", irmp_data.protocol, irmp_data.address, irmp_data.command );
74
75 // sample decoding, toggle LED
76 if (irmp_data.protocol == IRMP_RC5_PROTOCOL && irmp_data.address == 5) // old RC5 VCR Remote. TV uses address 0
77 {
78 if (irmp_data.flags == 0) // switch only on button press
79 {
80 switch (irmp_data.command)
81 {
82 case 0: // Key '0'
83 led = LED_OFF;
84 break;
85 case 1: // Key '1'
86 led = LED_ON;
87 break;
88 case 53: // Key 'play'
89 printf("bring me a beer!\n");
90 break;
91 case 54: // Key 'stop'
92 timeISRAvg = (float)timeISRAvgSum / countISRCalls;
93 timeISRAvgSum = 0;
94 countISRCalls = 0;
95 printf("ISR max / avg runtime [microseconds] : %d / %5.2f\n", timeISRMax, timeISRAvg);
96 timeISRMax = 0;
97 break;
98 }
99 }
100 }
101
102 // log to stdout
103 printf("proto %d addr %d cmd %d flags %x name %s\n", irmp_data.protocol, irmp_data.address, irmp_data.command, irmp_data.flags, irmp_protocol_names[irmp_data.protocol] );
104 }
105 }
106 }