-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoSerial.h
109 lines (100 loc) · 2 KB
/
AutoSerial.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
/*
* AutoSerial.h
*
* Created on: 29 mar 2017
* Author: Radek Kubera (rkubera)
*/
#include "Arduino.h"
#include <Stream.h>
#if defined(ARDUINO_ARCH_AVR)
#include "SoftwareSerial.h"
#endif
#ifndef AUTOSERIAL_H_
#define AUTOSERIAL_H_
#define SERIAL_PORT_HARDWARE_COUNT 0
#ifdef SERIAL_PORT_HARDWARE
#undef SERIAL_PORT_SOFTWARE_COUNT
#define SERIAL_PORT_SOFTWARE_COUNT 1
#endif
#ifdef SERIAL_PORT_HARDWARE1
#undef SERIAL_PORT_SOFTWARE_COUNT
#define SERIAL_PORT_SOFTWARE_COUNT 2
#endif
#ifdef SERIAL_PORT_HARDWARE2
#undef SERIAL_PORT_SOFTWARE_COUNT
#define SERIAL_PORT_SOFTWARE_COUNT 3
#endif
#ifdef SERIAL_PORT_HARDWARE3
#undef SERIAL_PORT_SOFTWARE_COUNT
#define SERIAL_PORT_SOFTWARE_COUNT 4
#endif
class AutoSerial : public Stream {
private:
uint8_t myType;
uint8_t myNumber;
#if defined(ARDUINO_ARCH_AVR)
SoftwareSerial *mySerial = NULL;
#endif
bool RxTxDefined = false;
public:
AutoSerial();
virtual ~AutoSerial();
bool isHardwareSerial();
bool setRxTxPins (uint8_t Rx,uint8_t Tx);
uint8_t getSerialNumber();
void begin (long speed);
int peek();
bool listen();
void end();
bool isListening();
bool stopListening();
bool overflow();
virtual size_t write(uint8_t byte);
virtual int read();
virtual int available();
virtual void flush();
using Print::write;
operator bool() {
switch (this->myType) {
case 0:
switch (this->myNumber) {
#ifdef SERIAL_PORT_HARDWARE
case 0:
if (Serial) return true;
else return false;
break;
#endif
#ifdef SERIAL_PORT_HARDWARE1
case 1:
if (Serial1) return true;
else return false;
break;
#endif
#ifdef SERIAL_PORT_HARDWARE2
case 2:
if (Serial2) return true;
else return false;
break;
#endif
#ifdef SERIAL_PORT_HARDWARE3
case 3:
if (Serial3) return true;
else return false;
break;
#endif
default:
break;
}
break;
#if defined(ARDUINO_ARCH_AVR)
case 1:
if (this->RxTxDefined) {
if (this->mySerial) return true;
}
break;
#endif
}
return false;
}
};
#endif /* AUTOSERIAL_H_ */