-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEspSoftSerialRx.h
66 lines (47 loc) · 1.48 KB
/
EspSoftSerialRx.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
/* EspSoftSerial receiver
Scott Day 2015
github.com/scottwday/EspSoftSerial
Uses a pin change interrupt and the tick timer as a uart receiver
*/
#ifndef ESPSOFTSERIAL_H
#define ESPSOFTSERIAL_H
#define MAX_ESPSOFTSERIAL_INSTANCES 4
#define SOFTSERIAL_BUFFER_LEN 256
#define SOFTSERIAL_ERROR_LONGLOW 1
#define SOFTSERIAL_ERROR_STOPBIT 2
#include <Arduino.h>
#include "CircularBuffer.h"
class EspSoftSerialRx
{
private:
typedef void (EspSoftSerialRx::*InterruptHandler)();
unsigned long _halfBitRate;
byte _rxPin;
unsigned long _lastChangeTicks = 0;
byte _errorState = 0;
byte _bitCounter = 0;
unsigned short _bitBuffer = 0;
byte _inInterrupt = 0;
byte _instanceId = 0;
CircularBuffer<byte, SOFTSERIAL_BUFFER_LEN> _buffer;
static byte _numInstances;
static EspSoftSerialRx* _instances[MAX_ESPSOFTSERIAL_INSTANCES];
public:
// Supply the baud rate and the pin you want to use
void begin(const unsigned long baud, const byte rxPin);
// Call this at least every 10 seconds to prevent bytes getting lost
void service();
// Read the next byte from the buffer
bool read(byte& c);
void setEnabled(bool enabled);
void reset();
private:
static void onRxPinChange0();
static void onRxPinChange1();
static void onRxPinChange2();
static void onRxPinChange3();
byte getNumBitPeriodsSinceLastChange(unsigned long ticks);
inline void addBits(byte numBits, byte value);
void onRxPinChange();
};
#endif