-
Notifications
You must be signed in to change notification settings - Fork 105
/
TransmitterIR.cpp
300 lines (287 loc) · 8.82 KB
/
TransmitterIR.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
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
/**
* IR transmitter (Version 0.0.4)
*
* Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
* http://shinta.main.jp/
*/
#include "TransmitterIR.h"
#define LOCK()
#define UNLOCK()
/**
* Constructor.
*
* @param txpin Pin for transmit IR signal.
*/
TransmitterIR::TransmitterIR(PinName txpin) : tx(txpin) {
tx.write(0.0);
tx.period_us(26.3);
work.state = Idle;
work.bitcount = 0;
work.leader = 0;
work.data = 0;
work.trailer = 0;
data.format = RemoteIR::UNKNOWN;
data.bitlength = 0;
}
/**
* Destructor.
*/
TransmitterIR::~TransmitterIR() {
}
/**
* Get state.
*
* @return Current state.
*/
TransmitterIR::State TransmitterIR::getState(void) {
LOCK();
State s = work.state;
UNLOCK();
return s;
}
/**
* Set data.
*
* @param format Format.
* @param buf Buffer of a data.
* @param bitlength Bit length of the data.
*
* @return Data bit length.
*/
int TransmitterIR::setData(RemoteIR::Format format, uint8_t *buf, int bitlength) {
LOCK();
if (work.state != Idle) {
UNLOCK();
return -1;
}
work.state = Leader;
work.bitcount = 0;
work.leader = 0;
work.data = 0;
work.trailer = 0;
data.format = format;
data.bitlength = bitlength;
const int n = bitlength / 8 + (((bitlength % 8) != 0) ? 1 : 0);
for (int i = 0; i < n; i++) {
data.buffer[i] = buf[i];
}
switch (format) {
case RemoteIR::NEC:
ticker.detach();
ticker.attach_us(this, &TransmitterIR::tick, RemoteIR::TUS_NEC);
break;
case RemoteIR::AEHA:
ticker.detach();
ticker.attach_us(this, &TransmitterIR::tick, RemoteIR::TUS_AEHA);
break;
case RemoteIR::SONY:
ticker.detach();
ticker.attach_us(this, &TransmitterIR::tick, RemoteIR::TUS_SONY);
break;
}
UNLOCK();
return bitlength;
}
void TransmitterIR::tick(void) {
LOCK();
switch (work.state) {
case Idle:
work.bitcount = 0;
work.leader = 0;
work.data = 0;
work.trailer = 0;
break;
case Leader:
if (data.format == RemoteIR::NEC) {
/*
* NEC.
*/
static const int LEADER_NEC_HEAD = 16;
static const int LEADER_NEC_TAIL = 8;
if (work.leader < LEADER_NEC_HEAD) {
tx.write(0.5);
} else {
tx.write(0.0);
}
work.leader++;
if ((LEADER_NEC_HEAD + LEADER_NEC_TAIL) <= work.leader) {
work.state = Data;
}
} else if (data.format == RemoteIR::AEHA) {
/*
* AEHA.
*/
static const int LEADER_AEHA_HEAD = 8;
static const int LEADER_AEHA_TAIL = 4;
if (work.leader < LEADER_AEHA_HEAD) {
tx.write(0.5);
} else {
tx.write(0.0);
}
work.leader++;
if ((LEADER_AEHA_HEAD + LEADER_AEHA_TAIL) <= work.leader) {
work.state = Data;
}
} else if (data.format == RemoteIR::SONY) {
/*
* SONY.
*/
static const int LEADER_SONY_HEAD = 4;
static const int LEADER_SONY_TAIL = 0;
if (work.leader < LEADER_SONY_HEAD) {
tx.write(0.5);
} else {
tx.write(0.0);
}
work.leader++;
if ((LEADER_SONY_HEAD + LEADER_SONY_TAIL) <= work.leader) {
work.state = Data;
}
} else {
}
break;
case Data:
if (data.format == RemoteIR::NEC) {
/*
* NEC.
*/
if (work.data == 0) {
tx.write(0.5);
work.data++;
} else {
tx.write(0.0);
if (0 != (data.buffer[work.bitcount / 8] & (1 << work.bitcount % 8))) {
if (3 <= work.data) {
work.bitcount++;
work.data = 0;
} else {
work.data++;
}
} else {
if (1 <= work.data) {
work.bitcount++;
work.data = 0;
} else {
work.data++;
}
}
}
if (data.bitlength <= work.bitcount) {
work.state = Trailer;
}
} else if (data.format == RemoteIR::AEHA) {
/*
* AEHA.
*/
if (work.data == 0) {
tx.write(0.5);
work.data++;
} else {
tx.write(0.0);
if (0 != (data.buffer[work.bitcount / 8] & (1 << work.bitcount % 8))) {
if (3 <= work.data) {
work.bitcount++;
work.data = 0;
} else {
work.data++;
}
} else {
if (1 <= work.data) {
work.bitcount++;
work.data = 0;
} else {
work.data++;
}
}
}
if (data.bitlength <= work.bitcount) {
work.state = Trailer;
}
} else if (data.format == RemoteIR::SONY) {
/*
* SONY.
*/
if (work.data == 0) {
tx.write(0.0);
work.data++;
} else {
tx.write(0.5);
if (0 != (data.buffer[work.bitcount / 8] & (1 << work.bitcount % 8))) {
if (2 <= work.data) {
work.bitcount++;
work.data = 0;
} else {
work.data++;
}
} else {
if (1 <= work.data) {
work.bitcount++;
work.data = 0;
} else {
work.data++;
}
}
}
if (data.bitlength <= work.bitcount) {
work.state = Trailer;
}
} else {
}
break;
case Trailer:
if (data.format == RemoteIR::NEC) {
/*
* NEC.
*/
static const int TRAILER_NEC_HEAD = 1;
static const int TRAILER_NEC_TAIL = 2;
if (work.trailer < TRAILER_NEC_HEAD) {
tx.write(0.5);
} else {
tx.write(0.0);
}
work.trailer++;
if ((TRAILER_NEC_HEAD + TRAILER_NEC_TAIL) <= work.trailer) {
work.state = Idle;
//ticker.detach();
}
} else if (data.format == RemoteIR::AEHA) {
/*
* AEHA.
*/
static const int TRAILER_AEHA_HEAD = 1;
static const int TRAILER_AEHA_TAIL = 8000 / RemoteIR::TUS_AEHA;
if (work.trailer < TRAILER_AEHA_HEAD) {
tx.write(0.5);
} else {
tx.write(0.0);
}
work.trailer++;
if ((TRAILER_AEHA_HEAD + TRAILER_AEHA_TAIL) <= work.trailer) {
work.state = Idle;
//ticker.detach();
}
} else if (data.format == RemoteIR::SONY) {
/*
* SONY.
*/
static const int TRAILER_SONY_HEAD = 0;
static const int TRAILER_SONY_TAIL = 0;
if (work.trailer < TRAILER_SONY_HEAD) {
tx.write(0.5);
} else {
tx.write(0.0);
}
work.trailer++;
if ((TRAILER_SONY_HEAD + TRAILER_SONY_TAIL) <= work.trailer) {
work.state = Idle;
//ticker.detach();
}
} else {
}
break;
default:
break;
}
UNLOCK();
}