-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIR.c
148 lines (124 loc) · 3.19 KB
/
IR.c
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
#include "IR.h"
#include "system.h"
#include "io430x21x2.h"
#include "in430.h"
#include "buttons.h"
#define MAX_TIMEOUT 10000
#define MIN_ZERO 600
#define MIN_ONE 1000
#define MAX_ONE 2000
#define MAX_TIMINGS 33
typedef struct
{
unsigned char nAddress;
unsigned char nCommand;
} TIR;
TIR ReadIR_RC5(void)
{
unsigned int n;
unsigned int nBit;
unsigned int pBits[RC5_BITS];
TIR rc5Data;
rc5Data.nAddress = 0;
rc5Data.nCommand = 0;
for (nBit = 0; nBit < RC5_BITS; nBit++)
{
pBits[nBit] = (~P1IN_bit.P2) & 0x01;
for (n = 0; n < 2762; n++);
}
rc5Data.nAddress = 0;
for (nBit = 0; nBit < 5; nBit++)
rc5Data.nAddress |= pBits[7 - nBit] << nBit;
rc5Data.nCommand = 0;
for (nBit = 0; nBit < 6; nBit++)
rc5Data.nCommand |= pBits[13 - nBit] << nBit;
return rc5Data;
}
TIR ReadIR_NEC(void)
{
unsigned int n;
unsigned int nBit;
unsigned int nData;
unsigned int nCount;
unsigned char pData[4];
unsigned int pIRTiming[MAX_TIMINGS];
TIR necData;
necData.nAddress = 0;
necData.nCommand = 0;
for (n = 0; n < MAX_TIMINGS; n++)
{
pIRTiming[n] = 0;
nCount = 0;
while ((nCount < MAX_TIMEOUT) && !(P1IN_bit.P2))
nCount++;
if (nCount >= MAX_TIMEOUT) break;
while ((nCount < MAX_TIMEOUT) && (P1IN_bit.P2))
nCount++;
if (nCount >= MAX_TIMEOUT) break;
pIRTiming[n] = nCount;
}
if (n != MAX_TIMINGS) return necData; // a timeout has occurred -> not a valid NEC code
for (n = 0; n < 4; n++)
{
nData = 0;
for (nBit = 0; nBit < 8; nBit++)
{
nCount = pIRTiming[1 + n * 8 + nBit];
if (nCount < MIN_ZERO) break; // not a valid zero -> not a valid NEC code
if (nCount < MIN_ONE) continue; // a valid zero -> continue with next bit
if (nCount > MAX_ONE) break; // not a valid one -> not a valid NEC code
nData |= 1 << (7 - nBit);
}
if (nBit != 8) return necData; // bit timing not within margins -> not a valid NEC code
pData[n] = nData;
}
if (pData[0] != (~(pData[1]) & 0xFF)) return necData; // test NEC address complement and return if not correct
if (pData[2] != (~(pData[3]) & 0xFF)) return necData; // test NEC command complement and return if not correct
necData.nAddress = pData[0];
necData.nCommand = pData[2];
return necData;
}
void DispatchIR_NEC(TIR necData)
{
if (!necData.nAddress && !necData.nCommand) return;
if (necData.nAddress != NEC_ADDRESS) return;
switch (necData.nCommand)
{
case NEC_CMD_VOL_UP:
Buttons(volumeUp);
break;
case NEC_CMD_VOL_DN:
Buttons(volumeDown);
break;
case NEC_CMD_MUTE:
Buttons(muteMode);
break;
default:;
}
}
void DispatchIR_RC5(TIR rc5Data)
{
if (!rc5Data.nAddress && !rc5Data.nCommand) return;
if (rc5Data.nAddress != RC5_ADDRESS) return;
switch (rc5Data.nCommand)
{
case RC5_CMD_VOL_UP:
Buttons(volumeUp);
break;
case RC5_CMD_VOL_DN:
Buttons(volumeDown);
break;
case RC5_CMD_MUTE:
Buttons(muteMode);
Delay_ms(1000);
break;
default:;
}
}
void ProcessIR(void)
{
// TIR necData = ReadIR_NEC();
// DispatchIR_NEC(necData);
TIR irData = ReadIR_RC5();
DispatchIR_RC5(irData);
}