-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathSerials.h
78 lines (60 loc) · 1.87 KB
/
Serials.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
#ifndef __SDS_ABSTRACT_SERIAL_H__
#define __SDS_ABSTRACT_SERIAL_H__
#if !defined(ARDUINO_SAMD_VARIANT_COMPLIANCE) && !defined(ESP32)
#include <SoftwareSerial.h>
#endif
#include <HardwareSerial.h>
namespace Serials {
// cpp is really weird...
// there was compilation warning about missing virtual destructor for class AbstractSerial
// I added virtual destructor with empty body
// remaining virtual methods didn't have body (they were 'pure virtual')
// after that the following error appeared: undefined reference to `vtable for Serials::AbstractSerial'
// what a nonsense ...
// just to satisfy linker in gcc I needed to add empty parentheses to other virtual methods...
class AbstractSerial {
public:
virtual void begin(int baudRate) = 0;
virtual Stream *getStream() = 0;
virtual ~AbstractSerial() {};
};
struct Hardware: public AbstractSerial {
Hardware(HardwareSerial &serial): serial(serial) {}
void begin(int baudRate) {
serial.begin(baudRate);
}
Stream *getStream() {
return &serial;
}
HardwareSerial &serial;
};
#if !defined(ARDUINO_SAMD_VARIANT_COMPLIANCE) && !defined(ESP32)
struct Software: public AbstractSerial {
Software(SoftwareSerial &serial): serial(serial) {}
void begin(int baudRate) {
serial.begin(baudRate);
}
Stream *getStream() {
return &serial;
}
SoftwareSerial &serial;
};
struct InternalSoftware: public AbstractSerial {
InternalSoftware(const int &pinRx, const int &pinTx):
serial(new SoftwareSerial(pinRx, pinTx)) {}
~InternalSoftware() {
if (serial != NULL) {
delete serial;
}
}
void begin(int baudRate) {
serial->begin(baudRate);
}
Stream *getStream() {
return serial;
}
SoftwareSerial *serial;
};
#endif // ARDUINO_SAMD_VARIANT_COMPLIANCE
}
#endif // __SDS_ABSTRACT_SERIAL_H__