-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote_express.cpp
156 lines (127 loc) · 3.72 KB
/
remote_express.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
#include "remote_express.h"
void defaultNumKeyCB(int num){}
void defaultNumKeyHoldCB(int num, unsigned long micros){}
void defaultKeyCB(unsigned long key){}
void defaultKeyHoldCB(unsigned long key, unsigned long micros){}
RemoteExpress::RemoteExpress(int pin) : irrecv(pin){
lookupKeysSize = 0;
for (int i = 0; i < REMOTE_EXPRESS_NUM_KEYS_COUNT; i++){
numkeys[i] = 0;
}
numkeyCB = defaultNumKeyCB;
numkeyHoldCB = defaultNumKeyHoldCB;
}
void RemoteExpress::setup(){
irrecv.enableIRIn();
}
int RemoteExpress::loop(){
int result = 0;
if (irrecv.decode(&remoteResults)){
if (remoteResults.value == REPEAT){
handleRepeat();
}
else{
lastCode = remoteResults.value;
lastCodeTime = millis();
result = handlePress(remoteResults.value);
}
irrecv.resume();
}
return result;
}
void RemoteExpress::handleRepeat(){
unsigned long time = millis();
if (time >= nextHoldTime){
unsigned long timeSincePress = time - lastCodeTime;
if (lastPressNumkey < 0){
if (lastPressIndex < 0)
return;
(* lookupHoldCB[lastPressIndex])(lookupKeys[lastPressIndex], timeSincePress);
}
else{
(* numkeyHoldCB)(lastPressNumkey, timeSincePress);
}
nextHoldTime = time + holdEveryMicros;
}
}
int RemoteExpress::handlePress(unsigned long value){
lastPressNumkey = -1;
lastPressIndex = -1;
int numkey = lookupNumkey(value);
if (numkey < 0){
int cbIndex = lookupKey(value);
if (cbIndex < 0)
return 0;
lastPressIndex = cbIndex;
holdEveryMicros = lookupHoldEveryMicros[cbIndex];
nextHoldTime = lastCodeTime + holdEveryMicros;
(*lookupCB[cbIndex])(value);
return 1;
}
else{
lastPressNumkey = numkey;
holdEveryMicros = numkeyHoldEveryMicros;
nextHoldTime = lastCodeTime + holdEveryMicros;
(* numkeyCB)(numkey);
return 1;
}
}
void RemoteExpress::onpress(unsigned long code, void (*cb) (unsigned long code)){
int i = lookupKey(code);
if (i < 0){
i = addLookupKey(code);
}
lookupCB[i] = cb;
}
void RemoteExpress::onhold(unsigned long code, void (*cb) (unsigned long code, unsigned long microsSincePress)){
onhold(code, 0, cb);
}
void RemoteExpress::onhold(unsigned long code, unsigned long everyMicro, void (*cb) (unsigned long code, unsigned long microsSincePress)){
int i = lookupKey(code);
if (i < 0){
i = addLookupKey(code);
}
lookupHoldEveryMicros[i] = everyMicro;
lookupHoldCB[i] = cb;
}
void RemoteExpress::setNum(int number, unsigned long code){
numkeys[number] = code;
}
void RemoteExpress::setNums(unsigned long codes[REMOTE_EXPRESS_NUM_KEYS_COUNT]){
for (int i = 0; i < REMOTE_EXPRESS_NUM_KEYS_COUNT; i++){
setNum(i, codes[i]);
}
}
void RemoteExpress::onnumpress(void (*cb) (int num)){
numkeyCB = cb;
}
void RemoteExpress::onnumhold(void (*cb) (int num, unsigned long microsSincePress)){
onnumhold(0, cb);
}
void RemoteExpress::onnumhold(unsigned long everyMicro, void (*cb) (int num, unsigned long microsSincePress)){
numkeyHoldEveryMicros = everyMicro;
numkeyHoldCB = cb;
}
int RemoteExpress::lookupNumkey(unsigned long key){
for (int i = 0; i < REMOTE_EXPRESS_NUM_KEYS_COUNT; i++){
if (numkeys[i] == key)
return i;
}
return -1;
}
int RemoteExpress::lookupKey(unsigned long key){
for (int i = 0; i < lookupKeysSize; i++){
if (lookupKeys[i] == key)
return i;
}
return -1;
}
int RemoteExpress::addLookupKey(unsigned long key){
if (lookupKeysSize >= REMOTE_EXPRESS_KEYS_SIZE)
--lookupKeysSize; //use the last slot if we're out
lookupKeys[lookupKeysSize] = key;
lookupCB[lookupKeysSize] = defaultKeyCB;
lookupHoldCB[lookupKeysSize] = defaultKeyHoldCB;
lookupHoldEveryMicros[lookupKeysSize] = 0x10000;
return lookupKeysSize++;
}