-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIrRc5.cpp
243 lines (211 loc) · 4.21 KB
/
IrRc5.cpp
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
/*
StartModule
Copyright (C) 2015 Marcus Ahlberg
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Arduino.h"
#include "IrRc5.h"
////////////////////////
// Defines
////////////////////////
#define HALF_BIT_LEN 889
#define BIT_LEN 1778
#define RC5_LEN 24900
#define ADDR_LEN 5
#define CMD_LEN 6
#define TOLERANCE 10
#define APPROX_EQ(A, B) ( (abs((A) - (B)) * 200 / ((A) + (B))) < (TOLERANCE) )
static void irIsr(void);
////////////////////////
// Types
////////////////////////
typedef enum {
IR_STARTBIT1 = 0,
IR_STARTBIT2 = 1,
IR_TOGGLEBIT = 2,
IR_ADDR = 3,
IR_CMD = 4
} status_t;
////////////////////////
// Local variables
////////////////////////
static int irPin;
static status_t status;
static int toggle = -1;
static void(*ir_data_func)(rc5_cmd * data) = 0;
////////////////////////
// Public interface
////////////////////////
void IrRc5::init(int pin)
{
irPin = pin;
pinMode(irPin, INPUT);
attachInterrupt(irPin, irIsr, CHANGE);
}
void IrRc5::registerCmdHandler(void(*func)(rc5_cmd * cmd))
{
ir_data_func = func;
}
////////////////////////
// Interrupt handler
////////////////////////
static void irIsr(void)
{
static int index = 0;
static int addr = 0;
static int cmd = 0;
static int toggle_last = 0;
static int cnt = 0;
static int lastCnt = 0;
const int time = micros();
const int edge = digitalRead(irPin);
// Save count
cnt += time - lastCnt;
lastCnt = time;
if (cnt > RC5_LEN)
{
// Reset state
status = IR_STARTBIT1;
}
switch (status)
{
case IR_STARTBIT1:
if (edge == LOW)
{
status = IR_STARTBIT2;
cnt = 0;
}
else
{
status = IR_STARTBIT1;
}
break;
case IR_STARTBIT2:
if (APPROX_EQ(cnt, BIT_LEN))
{
status = IR_TOGGLEBIT;
cnt = 0;
}
else if (APPROX_EQ(cnt, HALF_BIT_LEN))
{
// Synchronize
cnt = HALF_BIT_LEN;
return;
}
else
{
status = IR_STARTBIT1;
return;
}
break;
case IR_TOGGLEBIT:
if (APPROX_EQ(cnt, BIT_LEN))
{
toggle_last = toggle;
if (LOW == edge)
{
toggle = 1;
}
else
{
toggle = 0;
}
index = 0;
addr = 0;
cmd = 0;
cnt = 0;
status = IR_ADDR;
}
else if (APPROX_EQ(cnt, HALF_BIT_LEN))
{
// Synchronize
cnt = HALF_BIT_LEN;
return;
}
else
{
status = IR_STARTBIT1;
return;
}
break;
case IR_ADDR:
if (APPROX_EQ(cnt, BIT_LEN))
{
if (edge == LOW)
{
addr |= 1 << (ADDR_LEN - index - 1);
}
else
{
// Zero at position index
}
index++;
if (index == ADDR_LEN)
{
index = 0;
status = IR_CMD;
}
cnt = 0;
}
else if (APPROX_EQ(cnt, HALF_BIT_LEN))
{
cnt = HALF_BIT_LEN;
return;
}
else
{
status = IR_STARTBIT1;
return;
}
break;
case IR_CMD:
if (APPROX_EQ(cnt, BIT_LEN))
{
if (edge == LOW)
{
cmd |= 1 << (CMD_LEN - index - 1);
}
else
{
// Zero at position index
}
index++;
if (index == CMD_LEN)
{
if (toggle != toggle_last)
{
if (ir_data_func)
{
static rc5_cmd tmp_data;
tmp_data.address = addr;
tmp_data.command = cmd;
ir_data_func(&tmp_data);
}
}
status = IR_STARTBIT1;
}
cnt = 0;
}
else if (APPROX_EQ(cnt, HALF_BIT_LEN))
{
// Synchronize
cnt = HALF_BIT_LEN;
return;
}
else
{
status = IR_STARTBIT1;
return;
}
break;
}
}