-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLightwaveRX.cpp
251 lines (231 loc) · 7.45 KB
/
LightwaveRX.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
/**
* LightwaveRF library for Raspberry Pi and Arduino. Utilising 433 MHz
* receiver to interpret LightwaveRF remote controls.
*
* @author James Reuss (jamesreuss.co.uk)
* @copyright James Reuss 2014
*/
#include "LightwaveRX.h"
/*
* Private variables
*/
static int rxPin_ = 2;
static unsigned long lastInterruptTime_ = 0;
static uint8_t rxState_ = 0;
static const uint8_t rxState_idle = 0;
static const uint8_t rxState_msgstartfound = 1;
static const uint8_t rxState_bytestartfound = 2;
static const uint8_t rxState_getbyte = 3;
static uint8_t rxCurrentBits_ = 0; // number of bits in the current byte
static uint8_t rxNumBytes_ = 0; // number of bytes received
static bool messageComplete_ = false;
static const int messageLength_ = 10;
static uint8_t rawBuffer_[messageLength_];
static uint8_t rxBuffer_[messageLength_];
static uint8_t rxRepeats_ = 2;
static uint8_t rxRepeatCount_ = 0;
static uint8_t rxTimeout_ = 20; // reset repeat window after this in 100ms
static unsigned long lastPacketTime_ = 0; // ms
static const uint8_t nibbleBytes_[] = {0xF6,0xEE,0xED,0xEB,0xDE,0xDD,0xDB,0xBE,
0xBD,0xBB,0xB7,0x7E,0x7D,0x7B,0x77,0x6F};
/*
* Private functions
*/
void lwrx_process();
int16_t lwrx_findNibble(uint8_t data);
/*
* Public function implementations
*/
void lwrx_setup(int rxPin, int interruptPin)
{
rxPin_ = rxPin;
pinMode(rxPin_, INPUT);
#ifdef ARDUINO
attachInterrupt(interruptPin, lwrx_process, CHANGE);
#else
wiringPiISR(interruptPin, INT_EDGE_BOTH, lwrx_process);
#endif
}
bool lwrx_gotMessage()
{
return messageComplete_;
}
bool lwrx_getMessage(uint8_t *buffer, uint8_t length)
{
bool ok = true;
int16_t j = 0;
if(messageComplete_ && length <= messageLength_)
{
for(uint8_t i = 0; ok && i < messageLength_; i++)
{
j = lwrx_findNibble(rawBuffer_[i]);
if(j < 0) ok = false;
switch(length) {
case 4:
if(i == 9) buffer[2] = j;
if(i == 2) buffer[3] = j;
case 2:
if(i == 3) buffer[0] = j;
if(i == 0) buffer[1] = j << 4;
if(i == 1) buffer[1] += j;
break;
case 10:
buffer[i] = j;
break;
}
}
messageComplete_ = false;
}
else
{
ok = false;
}
return ok;
}
bool lwrx_getMessageRaw(uint8_t *buffer)
{
bool ok = messageComplete_;
if (messageComplete_ == true)
{
for (int i = 0; i < messageLength_; i++)
{
buffer[i] = rawBuffer_[i];
}
messageComplete_ = false;
}
return ok;
}
/*
* Private function implementations
*/
void lwrx_process()
{
uint8_t event = digitalRead(rxPin_); // Start setting event to the current value
unsigned long curr = micros(); // The current time in microseconds
uint16_t dur = (curr- lastInterruptTime_);
lastInterruptTime_ = curr;
// Set event based on input and duration of previous pulse
if(dur < 120) { // 120 very short
} else if(dur < 500) { // normal short pulse
event +=2;
} else if(dur < 2000) { // normal long pulse
event +=4;
} else if(dur > 5000){ // gap between messages
event +=6;
} else { // 2000 > 5000
event = 8; // illegal gap
}
// State machine transitions
switch(rxState_) {
case rxState_idle:
switch(event) {
case 7: // 1 after a message gap
rxState_ = rxState_msgstartfound;
break;
}
break;
case rxState_msgstartfound:
switch(event) {
case 2: // 0 160->500
// Nothing to do wait for next positive edge
break;
case 3: // 1 160->500
rxNumBytes_ = 0;
rxState_ = rxState_bytestartfound;
break;
default:
// Not good start again
rxState_ = rxState_idle;
break;
}
break;
case rxState_bytestartfound:
switch(event) {
case 2: // 0 160->500
// Nothing to do wait for next positive edge
break;
case 3: // 1 160->500
rxState_ = rxState_getbyte;
rxCurrentBits_ = 0;
break;
case 5: // 0 500->1500
rxState_ = rxState_getbyte;
// Starts with 0 so put this into byte
rxCurrentBits_ = 1;
rxBuffer_[rxNumBytes_] = 0;
break;
default:
// Not good start again
rxState_ = rxState_idle;
break;
}
break;
case rxState_getbyte:
switch(event) {
case 2: // 0 160->500
// Nothing to do wait for next positive edge
break;
case 3: // 1 160->500
// A single 1
rxBuffer_[rxNumBytes_] = rxBuffer_[rxNumBytes_] << 1 | 1;
rxCurrentBits_++;
break;
case 5: // 1 500->1500
// A 1 followed by a 0
rxBuffer_[rxNumBytes_] = rxBuffer_[rxNumBytes_] << 2 | 2;
rxCurrentBits_++;
rxCurrentBits_++;
break;
default:
// Not good start again
rxState_ = rxState_idle;
break;
}
if(rxCurrentBits_ >= 8) {
rxNumBytes_++;
rxCurrentBits_ = 0;
if(rxNumBytes_ >= messageLength_) {
unsigned long currMillis = millis();
if(rxRepeats_ > 0) {
if((currMillis - lastPacketTime_) / 100 > rxTimeout_) {
rxRepeatCount_ = 1;
} else {
// Test message same as last one
int16_t i = messageLength_;
do {
i--;
}
while((i >= 0) && (rawBuffer_[i] == rxBuffer_[i]));
if(i < 0) {
rxRepeatCount_++;
} else {
rxRepeatCount_ = 1;
}
}
} else {
rxRepeatCount_ = 0;
}
lastPacketTime_ = currMillis;
// If last message hasn't been read it gets overwritten
memcpy(rawBuffer_, rxBuffer_, messageLength_);
if(rxRepeats_ == 0 || rxRepeatCount_ == rxRepeats_) {
messageComplete_ = true;
}
// And cycle round for next one
rxState_ = rxState_idle;
} else {
rxState_ = rxState_bytestartfound;
}
}
break;
}
}
int16_t lwrx_findNibble(uint8_t data)
{
int16_t i = 15;
do {
if(nibbleBytes_[i] == data) break;
i--;
} while (i >= 0);
return i;
}