-
Notifications
You must be signed in to change notification settings - Fork 0
/
Receiver.h
109 lines (95 loc) · 2.27 KB
/
Receiver.h
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
#ifndef RECEIVER_H
#define RECEIVER_H
#include <Arduino.h>
#include "SimpleFIFO.h"
#include "LEDBitmap.h"
#include "Constants.h"
#include "Matrix.h"
#define T1 _transmit_period
#define T2 T1*2
#define TOLERANCE T1*RECEIVE_TOLERANCE
#define T1_LOW (T1 - TOLERANCE)
#define T1_HIGH (T1 + TOLERANCE)
#define T2_LOW (T2 - TOLERANCE)
#define T2_HIGH (T2 + TOLERANCE)
#define IN_T1(t) ((t > T1_LOW) && (t < T1_HIGH))
#define IN_T2(t) ((t > T2_LOW) && (t < T2_HIGH))
typedef union u_Data {
LEDBitmap bitmap;
String string;
u_Data() : bitmap(0,0) {};
~u_Data() {bitmap.~LEDBitmap();};
} Data;
class Receiver
{
public:
Receiver(int frequency = 1000);
virtual ~Receiver(){};
public: //methods
void setPin(int pin) { _pin = pin; };
int getPin() { return _pin; }
void start();
void stop();
int setFrequency(int frequency);
bool isReceiving() { return _receiving; };
bool isStarted() { return _started; };
bool hadError();
bool receptionSuccessful();
LEDBitmap getImage();
String getString();
int getType();
bool handleReception(Matrix* matrix);
private: //members
enum class State{
RX_IDLE,
RX_START_RECEPTION,
RX_BIT_REPITITION1,
RX_BIT_REPITITION2,
RX_BIT_ALTERATION,
RX_ERROR
};
enum class ProcessState{
FETCH_PREAMBLE,
FETCH_TYPE,
FETCH_SIZE,
FETCH_DATA,
PROCESS_DATA,
RECEPTION_FINISHED,
ERROR
};
uint16_t _transmit_period;
bool _started;
bool _receiving;
bool _hadError;
bool _success;
int _pin;
unsigned long _lastTime;
struct LastReception {
uint8_t type;
Data data = {};
} _lastReception;
struct CurrentReception {
uint8_t type;
uint8_t size[2];
} _currentReception;
uint8_t _data[260];
uint16_t _receivedByteCtr;
uint8_t _value;
SimpleFIFO _bitBuffer;
State _state;
ProcessState _processState;
static Receiver* _instance = NULL;
void process(uint8_t value);
void pushValue(uint8_t value);
void checkPreamble();
void readType();
void readSize();
bool checksumCorrect();
void buildString();
void buildImage();
void handleError();
void clear();
void switchState();
static void _switchState();
};
#endif