summaryrefslogtreecommitdiff
path: root/examples/Arduino.ino
blob: 3738b192ea5bf2021c0f53fa95a322b759781a17 (plain)
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
+#include <TimerOne.h>
/* first include Arduino.h, the IDE includes it after irmp*.h ... */
#include "Arduino.h"
/* ... and then chokes on uintX_t ... */

#include <irmp.h>
#include <irsnd.h>

/* undefine this if you don't want blinking LED for diagnosis */
#define LED_PIN 13
#define SER_BAUD 115200

/* F_INTERRUPTS is the interrupt frequency defined in irmpconfig.h */
#define US (1000000 / F_INTERRUPTS)
void setup()
{
    Serial.begin(SER_BAUD);
    delay(1000);
    /* greeting string and debugging ouput */
    Serial.println("IRMP test sketch");
    Serial.print("US: ");
    Serial.println(US);
    Serial.println("Send example: P:02 A:916E C:000F (NEC Taste 1)");
#ifdef LED_PIN
    pinMode(LED_PIN, OUTPUT);
#endif
    irmp_init();
    irsnd_init();
    //sei();
    led(HIGH);
    delay(20); /* make sure the greeting string is out before starting */
    led(LOW);
    Timer1.initialize(US);
    Timer1.attachInterrupt(timerinterrupt);
}

IRMP_DATA irmp_data[3];
uint8_t act_data = 0;
int incomingByte = 0;   // for incoming serial data

void loop()
{
    IRMP_DATA* data = &irmp_data[act_data];
    if (irmp_get_data(data))
    {
	led(HIGH);
#if IRMP_PROTOCOL_NAMES == 1
        Serial.print(irmp_protocol_names[data->protocol]);
        Serial.print(" ");
#endif
        Serial.print("P:");
        Serial.print(data->protocol, HEX);
        Serial.print(" A:");
        Serial.print(data->address, HEX);
        Serial.print(" C:");
        Serial.print(data->command, HEX);
        Serial.print(" ");
        Serial.print(data->flags, HEX);
        Serial.println("");
        /* Serial.print is asynchronous, so the LED is only flashing very lightly */
        led(LOW);

        data->flags = 0;    // reset flags!
        int result = irsnd_send_data(data, TRUE);
        if (result != 1)
	{
	    Serial.println("loop : irsnd_send_data ERROR");
	}
	else
	{
	    if (++act_data >= 3)
	    {
		act_data = 0;
	    } 
	}
    }

    if (Serial.available() == 18 && readAndCheck('P') && readAndCheck(':'))
    {
        // read the protocol
        data->protocol = readHex() * 16 + readHex();

        if (readAndCheck(' ') && readAndCheck('A') && readAndCheck(':'))
	{
	    // read the address
	    data->address = ((readHex() * 16 + readHex()) * 16 + readHex()) * 16 + readHex();

	    if (readAndCheck(' ') && readAndCheck('C') && readAndCheck(':'))
	    {
		// read the address
		data->command = ((readHex() * 16 + readHex()) * 16 + readHex()) * 16 + readHex();

		// send ir data
		data->flags = 0;
		int result = irsnd_send_data(data, TRUE);
		if (result)
		{
		    Serial.print("Send IR data: ");
		    Serial.print("P:");
		    Serial.print(data->protocol, HEX);
		    Serial.print(" A:");
		    Serial.print(data->address, HEX);
		    Serial.print(" C:");
		    Serial.print(data->command, HEX);
		    Serial.println("");

		    if (++act_data >= 3)
		    {
			act_data = 0;
		    } 
		}
	    }
	}
    }
}

/* helper function: attachInterrupt wants void(), but irmp_ISR is uint8_t() */
void timerinterrupt()
{
    // only when tsop dont see the ir_led flashing
    irsnd_ISR();			// call irsnd ISR
    irmp_ISR();               		// call irmp ISR

/*
    // do not receive when sending (tsop see the ir_led)
    if (! irsnd_ISR())          	// call irsnd ISR
    {                           	// if not busy...
	irmp_ISR();               	// call irmp ISR
    }
*/
}

static inline void led(int state)
{
#ifdef LED_PIN
    digitalWrite(LED_PIN, state);
#endif
}

static inline int readAndCheck(int c)
{
    return c == Serial.read();
}

static inline int readHex()
{
    int c = Serial.read();
    if (c >= '0' && c <= '9')
    {
	return c - '0';
    }

    c |= 0x20; // lowercase

    if (c >= 'a' && c <= 'f')
    {
	return c + 10 - 'a';
    }

    return -1;
}