-
Notifications
You must be signed in to change notification settings - Fork 0
/
Receiver.cpp
484 lines (450 loc) · 13.1 KB
/
Receiver.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#include <Receiver.h>
//#define DEBUG 1
Receiver::Receiver(int frequency) :
_transmit_period((1.0/frequency)*1000000.0),
_currentReception(),
_lastReception(),
_started(false),
_bitBuffer(8)
{
_pin = 0;
_hadError = false;
_state = State::RX_IDLE;
_lastTime = micros();
clear();
#ifdef DEBUG
Serial.println("\n---\nReceiver initialized.");
Serial.print(" Frequency is "); Serial.print(frequency); Serial.println("Hz");
Serial.print(" Transmit Period is "); Serial.print(_transmit_period); Serial.println("µs");
Serial.println("---\n");
#endif
}
Receiver* Receiver::_instance;
void Receiver::start() {
if(_pin != 0) {
_instance = this;
attachInterrupt(digitalPinToInterrupt(_pin), _switchState, CHANGE);
_started = true;
#ifdef DEBUG
Serial.println("Receiver started!");
#endif
}
}
void Receiver::_switchState() {
_instance->switchState();
}
void Receiver::stop() {
detachInterrupt(digitalPinToInterrupt(_pin));
_started = false;
#ifdef DEBUG
Serial.println("Receiver stopped!");
#endif
}
int Receiver::setFrequency(int frequency) {
if(_started) {
return 1;
}
_transmit_period = (1.0/frequency)*1000000.0; //(1/f)*10^6
#ifdef DEBUG
Serial.print("Frequency updated to "); Serial.print(frequency); Serial.println("Hz");
Serial.print("Transmit Period updated to "); Serial.print(_transmit_period); Serial.println("µs");
#endif
return 0;
}
bool Receiver::hadError() {
bool tmp = _hadError;
_hadError = false;
return tmp;
}
bool Receiver::receptionSuccessful() {
bool tmp = _success;
_success = false;
return tmp;
}
void Receiver::switchState() {
unsigned long t = micros() - _lastTime;
_lastTime = micros();
switch (_state) {
//Idle
case State::RX_IDLE:
if (IN_T1(t)) {
clear();
} else if(IN_T2(t)) {
_state = State::RX_START_RECEPTION;
_value = digitalRead(_pin);
pushValue(_value);
} else {
#ifdef DEBUG
Serial.print("Measured time: "); Serial.print(t); Serial.println(" (idle)");
#endif
handleError();
}
break;
//Start
case State::RX_START_RECEPTION:
//if recorded period is T
if(IN_T1(t)) {
handleError();
_state = State::RX_IDLE;
//if recorded period is 2T
} else if(IN_T2(t)) {
_state = State::RX_BIT_ALTERATION;
_value = !_value;
pushValue(_value);
_receiving = true;
} else {
#ifdef DEBUG
Serial.print("Measured time: "); Serial.print(t); Serial.println(" (start)");
#endif
handleError();
_state = State::RX_IDLE;
}
break;
//Bit Alteration
case State::RX_BIT_ALTERATION:
//if recorded period is 2T
if(IN_T2(t)) {
_value = !_value;
process(_value);
if(_processState == ProcessState::RECEPTION_FINISHED) {
_state = State::RX_IDLE;
_receiving = false;
} else {
if(_processState == ProcessState::ERROR) {
handleError();
_state = State::RX_IDLE;
} else {
_state = State::RX_BIT_ALTERATION;
}
}
//if recorded period is T
} else if(IN_T1(t)) {
_state = State::RX_BIT_REPITITION1;
} else {
#ifdef DEBUG
Serial.print("Measured time: "); Serial.print(t); Serial.println(" (alt)");
#endif
handleError();
_state = State::RX_IDLE;
}
break;
//Bit Repitition (1st State)
case State::RX_BIT_REPITITION1:
//if recorded period is T
if(IN_T1(t)) {
process(_value);
if(_processState == ProcessState::RECEPTION_FINISHED) {
_state = State::RX_IDLE;
_receiving = false;
} else {
if(_processState == ProcessState::ERROR) {
handleError();
_state = State::RX_IDLE;
} else {
_state = State::RX_BIT_REPITITION2;
}
}
//if recorded period is 2T or anything else
} else {
#ifdef DEBUG
Serial.print("Measured time: "); Serial.print(t); Serial.println(" (rep1)");
#endif
handleError();
_state = State::RX_IDLE;
}
break;
//Bit Repitition (2nd State)
case State::RX_BIT_REPITITION2:
//if recorded period is T
if(IN_T1(t)) {
_state = State::RX_BIT_REPITITION1;
//if recorded period is 2T
} else if(IN_T2(t)) {
_value = !_value;
process(_value);
if(_processState == ProcessState::RECEPTION_FINISHED) {
_state = State::RX_IDLE;
_receiving = false;
} else {
if(_processState == ProcessState::ERROR) {
handleError();
_state = State::RX_IDLE;
} else {
_state = State::RX_BIT_ALTERATION;
}
}
//if recorded period is 2T or anything else
} else {
#ifdef DEBUG
Serial.print("Measured time: "); Serial.print(t); Serial.println(" (rep2)");
#endif
handleError();
_state = State::RX_IDLE;
}
}
}
// -------------------------------------------------------------------------- //
void Receiver::process(uint8_t value) {
//Serial.println("In process...");
pushValue(value);
switch (_processState) {
// if the preamble has not been checked yet...
case ProcessState::FETCH_PREAMBLE:
#if DEBUG>1
Serial.println("Fetch preamble");
#endif
checkPreamble(); // ...check it.
break;
// if the preamble was correct, fetch the data type...
case ProcessState::FETCH_TYPE:
#if DEBUG>1
Serial.println("Fetch type");
#endif
readType();
break;
// if the type has been fetched, fetch the size next...
case ProcessState::FETCH_SIZE:
#if DEBUG>1
Serial.println("Fetch size");
#endif
readSize();
break;
// if the size has been fetched, fetch the data...
case ProcessState::FETCH_DATA:
#if DEBUG>1
Serial.println("Fetch data");
#endif
// if all data has been received...
if(_receivedByteCtr == _currentReception.size[0]+1) {
#if DEBUG>1
Serial.print("Received "); Serial.print(_receivedByteCtr);
Serial.print(" data bytes (of "); Serial.print(_currentReception.size[0]+1); Serial.println(")");
#endif
_processState = ProcessState::PROCESS_DATA; // ...go to the next step
}
break;
// if the data is complete...
case ProcessState::PROCESS_DATA:
#if DEBUG>1
Serial.println("Process data");
#endif
// ...check the checksum...
if(checksumCorrect()) {
_success = true;
//... and if it was correct, build whatever we received.
switch (_currentReception.type) {
case STRING: //string
buildString();
break;
case LEDBITMAP: //bitmap
buildImage();
break;
default:
#ifdef DEBUG
Serial.print("Invalid data type "); Serial.println(_currentReception.type);
#endif
handleError();
break;
}
_processState = ProcessState::RECEPTION_FINISHED;
} else {
_processState = ProcessState::ERROR; // ...or signal an error.
}
break;
// this is the error state. It should not be necessary, but just in case...
case ProcessState::ERROR:
handleError();
break;
}
// sets the _processState to RECEPTION_FINISHED if
// the reception is completed and was successful
}
void Receiver::pushValue(uint8_t value) {
// if the _bitBuffer is full (holds an entire byte)...
#if DEBUG>2
Serial.print("Pushing value "); Serial.println(value, BIN);
#endif
if(!_bitBuffer.push(value)) {
#ifdef DEBUG
Serial.println("Buffer full");
#endif
uint8_t byte = 0;
//...empty out _bitBuffer...
while (!_bitBuffer.isEmpty()) {
uint8_t ctr = _bitBuffer.getCount();
byte |= _bitBuffer.pop() << (ctr - 1);
}
#ifdef DEBUG
Serial.print("Read "); Serial.print(byte, HEX); Serial.println(" from buffer");
#endif
//...save byte in the _data array...
_data[_receivedByteCtr] = byte;
_receivedByteCtr++;
//...and finally push the value
_bitBuffer.push(value);
#if DEBUG>2
Serial.print("Repushing value "); Serial.println(value);
#endif
}
//increases the _receivedByteCtr if an entire byte has been received.
}
void Receiver::checkPreamble() {
//if the preamble has not been checked yet but an entire byte has been received...
if (_receivedByteCtr == 1) {
#ifdef DEBUG
Serial.println("Checking preamble");
Serial.print("Received byte was "); Serial.println(_data[0], HEX);
#endif
//...check if it is the PREAMBLE...
if (_data[0] == PREAMBLE) {
#ifdef DEBUG
Serial.println("Check successful");
#endif
_processState = ProcessState::FETCH_TYPE;
} else {
#ifdef DEBUG
Serial.println("Check failed");
#endif
_processState = ProcessState::ERROR;
}
//...and put the counter and data back to 0
_receivedByteCtr = 0;
_data[0] = 0;
}
}
void Receiver::readType() {
#ifdef DEBUG
Serial.println("Reading type");
#endif
//if there is an entire byte in the buffer...
if (_receivedByteCtr == 1) {
//... read it and put it into the transmission...
_currentReception.type = _data[0]; //1: string, 2: bitmap
#ifdef DEBUG
Serial.print("Read type "); Serial.println(_data[0]);
#endif
_processState = ProcessState::FETCH_SIZE;
//... and reset the counter and data back to 0
_receivedByteCtr = 0;
_data[0] = 0;
}
}
void Receiver::readSize() {
//distinguish between bitmap and string. Btimap has two sizes, string has one.
switch (_currentReception.type) {
case STRING: //string
//if the single size byte has been received...
if(_receivedByteCtr == 1) {
//... read it to the first position of _dataSize...
_currentReception.size[0] = _data[0];
_currentReception.size[1] = 0; //... and clear the second position just to be safe
#ifdef DEBUG
Serial.print("String size: "); Serial.println(_data[0]);
#endif
//also reset the counter and data back to 0
_receivedByteCtr = 0;
_data[0] = 0;
_processState = ProcessState::FETCH_DATA;
}
break;
case LEDBITMAP: //bitmap
//if the two size bytes have been received...
if(_receivedByteCtr == 2) {
//... read them into _dataSize...
_currentReception.size[0] = _data[0];
_currentReception.size[1] = _data[1];
#ifdef DEBUG
Serial.print("Image size: "); Serial.print(_data[0]);
Serial.print("\tImage height: "); Serial.println(_data[1]);
#endif
//... and reset the counter and data back to 0
_receivedByteCtr = 0;
_data[0] = 0;
_data[1] = 0;
_processState = ProcessState::FETCH_DATA;
}
}
}
bool Receiver::checksumCorrect() {
#ifdef DEBUG
Serial.println("Checking checksum");
#endif
uint8_t receivedChecksum = 0;
for (uint8_t i = 0; i < _currentReception.size[0]; i++) {
receivedChecksum += _data[i];
receivedChecksum %= 256;
}
#ifdef DEBUG
Serial.print("Own Checksum: "); Serial.print(receivedChecksum, HEX);
Serial.print("\tSent Checksum: "); Serial.println(_data[_currentReception.size[0]], HEX);
#endif
return (receivedChecksum == _data[_currentReception.size[0]]);
}
//TODO!!!! ----------------------------------------- !!!!! --------------------------------
void Receiver::buildImage() {
_lastReception.type = LEDBITMAP;
uint8_t height = _currentReception.size[0];
uint8_t size = _currentReception.size[1];
uint8_t width = size / height;
_lastReception.data.bitmap = LEDBitmap(width, height);
uint8_t map[size];
for(uint8_t a = 0; a < size; a++) {
map[a] = _data[a];
}
_lastReception.data.bitmap.buildFromBytemap(map);
}
void Receiver::buildString() {
_lastReception.type = STRING;
_lastReception.data.string = (char*) _data; // This SHOULD work
#ifdef DEBUG
Serial.print("Built string: \""); Serial.print(_lastReception.data.string); Serial.println('"');
#endif
}
void Receiver::handleError() {
#ifdef DEBUG
Serial.println("Error detected");
#endif
_hadError = true;
clear();
}
void Receiver::clear() {
_processState = ProcessState::FETCH_PREAMBLE;
_success = false;
_bitBuffer.flush();
_receivedByteCtr = 0;
_receiving = false;
}
LEDBitmap Receiver::getImage() {
if(_lastReception.type==2)
return _lastReception.data.bitmap;
else
return LEDBitmap(0, 0);
}
String Receiver::getString() {
if(_lastReception.type==1)
return _lastReception.data.string;
else
return String("");
}
int Receiver::getType() {
return _lastReception.type;
}
bool Receiver::handleReception(Matrix* matrix) {
if(receptionSuccessful()){
matrix->fillScreen(LOW);
matrix->write();
switch(getType()) {
case 1: {
Serial.println();
Serial.println(getString());
Serial.println();
matrix->scrollDrawText(getString());
} break;
case 2: {
matrix->drawImage(0, 0, getImage());
matrix->write();
} break;
}
}
return false;
}