-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathIR.cpp
More file actions
62 lines (51 loc) · 1.36 KB
/
IR.cpp
File metadata and controls
62 lines (51 loc) · 1.36 KB
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
/*
IR.cpp - Library for receiving data from a particular IR remote.
Created by Konstantin Tretyakov (http://kt.era.ee/), 2012.
MIT license.
*/
#include "Arduino.h"
#include "IR.h"
// We use IR detector on Arduino pin 2, which is atmega's PD2
#define PIN_ATMEGA PIND
static const int PINBIT_ATMEGA = 2;
static const int PIN_ARDUINO = 2;
void IRDetector::setup() {
pinMode(PIN_ARDUINO, INPUT);
PCICR |= 4; // enable PCIE2 which services PCINT18
PCMSK2 |= 4; // enable PCINT18 --> Pin Change Interrupt of PD2
lastBitTimestamp = 0;
numBitsAvailable = 0;
command = 0;
buffer.init();
}
// Interrupt handler
void IRDetector::interrupt() {
if (PIN_ATMEGA & (1<<PINBIT_ATMEGA)) return; // Only consider falling edges
curTime = micros();
elapsed = curTime - lastBitTimestamp;
lastBitTimestamp = curTime;
//buffer.push(elapsed);
numBitsAvailable++;
if (elapsed < 1500) { // Zero
command <<= 1;
}
else if (elapsed < 2500) { // One
command <<= 1;
command++;
}
else { // Start new command
// command = 0; // No need as we work in 16-bit chunks
numBitsAvailable = 0;
}
// Command is finished on 16 bits.
if (numBitsAvailable == 16) {
buffer.push(command);
numBitsAvailable = 0;
}
}
// Instantiate a single IR detector
IRDetector IR;
// Register interrupt handler
ISR(PCINT2_vect) {
IR.interrupt();
}