-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSendIr.cpp
More file actions
55 lines (46 loc) · 1.14 KB
/
SendIr.cpp
File metadata and controls
55 lines (46 loc) · 1.14 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
#define IR_PWM_FREQUENCY 38 // What frequency is the IR
#define TX_IO_Pin 3 // Pin to send data on
#if IR_PWM_FREQUENCY == 38
#define PWM_DELAY 13
#define IR_CLOCK_RATE 38000
#endif
void MT5_TX_init(){
// toggle on compare, clk/1
TCCR2A = _BV(WGM21);
TCCR2B = _BV(CS20);
OCR2A = (F_CPU/(IR_CLOCK_RATE*2L)-1);
OCR2B = OCR2A; // / 2;
pinMode(TX_IO_Pin, OUTPUT);
digitalWrite(TX_IO_Pin, LOW);
}
void MT5_TX(long uSecs){
TCCR2A |= _BV(COM2B0);
delayMicroseconds(uSecs); //PWM_DELAY - 3);
TCCR2A &= ~(_BV(COM2B0));
}
void MT5_TX_header(){
MT5_TX(2400);
delayMicroseconds(600);
}
void MT5_TX_logic1(){
MT5_TX(1200);
delayMicroseconds(600);
}
void MT5_TX_logic0(){
MT5_TX(600);
delayMicroseconds(600);
}
void MT5_TX_byte(unsigned char dataByte){
for(signed char byteBit=7; byteBit>=0; byteBit--){
if((dataByte >> byteBit) & 0x01 == 1){
MT5_TX_logic1();
}else{
MT5_TX_logic0();
}
}
}
void MT5_TX_shot(unsigned char teamID, unsigned char playerID, unsigned char damage){
MT5_TX_header();
MT5_TX_byte(playerID & 0x7F);
MT5_TX_byte( ( (teamID & 0x03) << 6 ) + ((damage & 0x0F) << 2) );
}