-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIR.cpp
333 lines (259 loc) · 7.25 KB
/
IR.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
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
#include "IR.h"
IRSenderClass IRSender;
IRSenderClass::IRSenderClass() {
}
void IRSenderClass::begin(uint8_t aSendPin)
{
sendPin = aSendPin;
pinMode(sendPin, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
Serial.println("Clearing Buffer...");
memset(dataBuffer, 0, sizeof(dataBuffer));
Serial.println("INIT Timer...");
noInterrupts(); // disable all interrupts
// initialize timer1 to generate PWM:
const uint32_t pwmval = (SYSCLOCK / 2000) / 38; // 210,52 for 38 kHz @16 MHz clock, 2000 instead of 1000 because of Phase Correct PWM
TCCR1A = _BV(WGM11); // PWM, Phase Correct, Top is defined by ICR1
TCCR1B = _BV(WGM13) | _BV(CS10); // PWM, Phase Correct, CS10 -> no prescaling
ICR1 = pwmval - 1;
OCR1A = ((pwmval * IR_SEND_DUTY_CYCLE) / 100) - 1;
TCNT1 = 0; //Clear counter value.
// Initalise timer2 to perform the sending operations:
//Need it to cycle every 0.5625ms = 1777.777hz
TCCR2A = _BV(WGM21); //No PWM or pin output, WGM21 -> CTC Mode (Clear timer on compare).
TCCR2B = _BV(CS22); // CS22 -> 1/64 prescaling.
TCNT2 = 0; //Clear counter value.
OCR2A = 139; // = 16000000 / (64 * 1785.7142857142858) - 1 (must be <256)
//TIMSK2 = _BV(OCIE2A); //Enable the Interrupt for counter2 on match with OCR2A.
interrupts(); // enable all interrupts
Serial.println("DONE.");
}
void IRSenderClass::enableSending()
{
TIMSK2 = _BV(OCIE2A); //Enable the Interrupt for counter2 on match with OCR2A.
digitalWriteFast(LED_BUILTIN, true); //Make sure LED is ON.
sending = true;
//Serial.println("IR ON");
}
void IRSenderClass::disableSending()
{
TIMSK2 = 0; //Disable all Interrupts for counter2.
digitalWriteFast(LED_BUILTIN, false); //Make sure LED is off.
sending = false;
//Serial.println("IR OFF");
}
void IRSenderClass::send(uint16_t address, uint8_t command)
{
//https://www.sbprojects.net/knowledge/ir/nec.php
//https://techdocs.altium.com/display/FPGA/NEC+Infrared+Transmission+Protocol
LongUnion data;
// Address 16 bit LSB first
if ((address & 0xFF00) == 0)
{
// assume 8 bit address -> send 8 address bits and then 8 inverted address bits LSB first
data.Bytes.LowByte = address;
data.Bytes.MidLowByte = ~data.Bytes.LowByte;
} else
{
data.Words.LowWord = address;
}
// send 8 command bits and then 8 inverted command bits LSB first
data.Bytes.MidHighByte = command;
data.Bytes.HighByte = ~command;
uint8_t states[34];
states[0] = IR_STATE_HEADER;
uint32_t aData = data.Long;
for (uint_fast8_t bit = 0; bit < 32; bit++, aData >>= 1)
{
if (aData & 1) { // Send a 1
states[bit+1] = IR_STATE_ONE;
} else { // Send a 0
states[bit+1] = IR_STATE_ZERO;
}
}
states[33] = IR_STATE_TAIL;
writeData(states, 34);
}
void IRSenderClass::sendRepeat()
{
uint8_t states[2];
states[0] = IR_STATE_REPEAT;
states[1] = IR_STATE_TAIL;
writeData(states, 2);
}
void IRSenderClass::writeData(uint8_t data[], uint8_t len)
{
noInterrupts();
uint8_t cur = dataWriteCursor;
for (uint8_t i = 0; i < len; i++)
{
dataBuffer[cur] = data[i];
cur++;
if (cur == dataReadCursor) break;
}
dataWriteCursor = cur;
enableSending();
// Serial.print("WRITE CURSOR: ");
// Serial.print(dataWriteCursor);
// Serial.print(" READ CURSOR: ");
// Serial.println(dataReadCursor);
interrupts();
}
void IRSenderClass::setCommand()
{
uint8_t cur = dataReadCursor;
bool found = false;
while (cur != dataWriteCursor)
{
if ((dataBuffer[cur] == IR_STATE_HEADER) || (dataBuffer[cur] == IR_STATE_REPEAT)) //Found the start of a new command.
{
found = true;
dataReadCursor = cur;
break;
}
cur++;
}
if (!found)
{
disableSending(); //No more commands found. Sleep.
}
else
{
commandTicksLeft = TICKS_PER_COMMAND;
setNextState();
}
}
void IRSenderClass::setNextState()
{
state = dataBuffer[dataReadCursor];
switch (state)
{
case IR_STATE_NONE:
case IR_STATE_WAIT:
markTicksLeft = 0;
totalTicksLeft = commandTicksLeft;
//Serial.println("NONE");
break;
case IR_STATE_HEADER:
markTicksLeft = TICKS_MARK_HEADER;
totalTicksLeft = TICKS_TOTAL_HEADER;
//Serial.println("HEADER");
break;
case IR_STATE_ONE:
markTicksLeft = TICKS_MARK_ONE;
totalTicksLeft = TICKS_TOTAL_ONE;
//Serial.println("ONE");
break;
case IR_STATE_ZERO:
markTicksLeft = TICKS_MARK_ZERO;
totalTicksLeft = TICKS_TOTAL_ZERO;
//Serial.println("ZERO");
break;
case IR_STATE_TAIL:
markTicksLeft = TICKS_MARK_TAIL;
//totalTicksLeft = TICKS_TOTAL_TAIL;
totalTicksLeft = commandTicksLeft; //Tail is the last state of the command, set it's length to the total length of the command.
//Serial.println("TAIL");
break;
case IR_STATE_REPEAT:
markTicksLeft = TICKS_MARK_REPEAT;
totalTicksLeft = TICKS_TOTAL_REPEAT;
//Serial.println("REPEAT");
break;
}
if (markTicksLeft > 0) {
ENABLE_PWM;
}
else
{
DISABLE_PWM;
}
dataReadCursor++;
/*
for (uint16_t j = 0; j < 256; j++)
{
if (j == dataReadCursor) Serial.print("R"); else Serial.print(" ");
if (j == dataWriteCursor) Serial.print("W"); else Serial.print(" ");
Serial.print(dataBuffer[j]);
}
Serial.println();
*/
}
void IRSenderClass::tick()
{
if (commandTicksLeft > 0)
{
commandTicksLeft--;
if (totalTicksLeft > 0) //This state still has ticks left:
{
if (markTicksLeft > 0) //This state still has mark ticks left:
{
markTicksLeft--;
if (markTicksLeft == 0)
{
DISABLE_PWM;
}
}
totalTicksLeft--;
if (totalTicksLeft == 0 && commandTicksLeft > 0) //Make sure we are not just ending this command.
{
setNextState();
}
}
}
else
{
DISABLE_PWM;
setCommand(); //Load new command if found, or disable timer if not found.
}
}
bool IRSenderClass::isSending()
{
bool tmp;
noInterrupts();
tmp = sending;
interrupts();
return tmp;
}
ISR(TIMER2_COMPA_vect) // timer compare interrupt service routine
{
IRSender.tick();
TCNT2 = 0;
}
/**
*
* Remote Codes
POWER:
Address: 0x6CD2
Command: 0xCB
VOL UP:
Address: 0x6DD2
Command: 0x2
VOL DOWN:
Address: 0x6DD2
Command: 0x3
MUTE:
Address: 0x6DD2
Command: 0x5
CYCLE DIM BRIGHTNESS:
Address: 0x6DD2
Command: 0x95
SWAP DISPLAY:
Address: 0x6CD2
Command: 0x55
PLAYBACK: MOVIE/TV
Address: 0xACD2
Command: 0xD0
PLAYBACK: MUSIC
Address: 0xACD2
Command: 0xD1
PLAYBACK: GAME
Address: 0xACD2
Command: 0xD2
PLAYBACK: STEREO
Address: 0x6CD2
Command: 0x4C
Repeat last command:
Address: 0xFFFF
Command: 0x0
*/