-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHalf_Duplex_Turnaround.h
172 lines (165 loc) · 5.29 KB
/
Half_Duplex_Turnaround.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
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
#include <wprogram.h>
#include <HardwareSerial.h>
class SerialHalf : public Stream {
protected:
unsigned char _turnaroudpin;
bool _normaly_low;
public:
HardwareSerial& _thisSerial;
SerialHalf(HardwareSerial* thisSerial, unsigned char turnaroudpin, bool normaly_low):
_thisSerial(*thisSerial)
{
_turnaroudpin = turnaroudpin;
_normaly_low = normaly_low;
};
void begin(unsigned long baudRate)
{
_thisSerial.begin(baudRate);
pinMode(_turnaroudpin,OUTPUT);
digitalWrite(_turnaroudpin,HIGH^_normaly_low);
};
void write(uint8_t in) {
digitalWrite(_turnaroudpin,LOW^_normaly_low);
_thisSerial.write(in);
waitForTransmitToComplete();
digitalWrite(_turnaroudpin,HIGH^_normaly_low);
};
void write(const char *str) {
digitalWrite(_turnaroudpin,LOW^_normaly_low);
_thisSerial.write(str);
waitForTransmitToComplete();
digitalWrite(_turnaroudpin,HIGH^_normaly_low);
};
void write(const uint8_t *buffer, size_t size) {
digitalWrite(_turnaroudpin,LOW^_normaly_low);
_thisSerial.write(buffer,size);
waitForTransmitToComplete();
digitalWrite(_turnaroudpin,HIGH^_normaly_low);
};
int available() {
return _thisSerial.available();
}
int read() {
return _thisSerial.read();
}
int peek() {
return _thisSerial.peek();
}
void flush() {
_thisSerial.flush();
}
void print(const String &in) {
digitalWrite(_turnaroudpin,LOW^_normaly_low);
_thisSerial.print(in);
waitForTransmitToComplete();
digitalWrite(_turnaroudpin,HIGH^_normaly_low);
};
void print(const char* in) {
digitalWrite(_turnaroudpin,LOW^_normaly_low);
_thisSerial.print(in);
waitForTransmitToComplete();
digitalWrite(_turnaroudpin,((HIGH^_normaly_low)&1));
};
void print(char in, int type = PRINT_BYTE) {
digitalWrite(_turnaroudpin,LOW^_normaly_low);
_thisSerial.print(in, type);
waitForTransmitToComplete();
digitalWrite(_turnaroudpin,HIGH^_normaly_low);
};
void print(unsigned char in, int type = PRINT_BYTE) {
digitalWrite(_turnaroudpin,LOW^_normaly_low);
_thisSerial.print(in,type);
waitForTransmitToComplete();
digitalWrite(_turnaroudpin,HIGH^_normaly_low);
};
void print(int in, int type = PRINT_DEC) {
digitalWrite(_turnaroudpin,LOW^_normaly_low);
_thisSerial.print(in,type);
waitForTransmitToComplete();
digitalWrite(_turnaroudpin,HIGH^_normaly_low);
};
void print(unsigned int in, int type = PRINT_DEC) {
digitalWrite(_turnaroudpin,LOW^_normaly_low);
_thisSerial.print(in,type);
waitForTransmitToComplete();
digitalWrite(_turnaroudpin,HIGH^_normaly_low);
};
void print(long in, int type = PRINT_DEC) {
digitalWrite(_turnaroudpin,LOW^_normaly_low);
_thisSerial.print(in,type);
waitForTransmitToComplete();
digitalWrite(_turnaroudpin,HIGH^_normaly_low);
};
void print(unsigned long in, int type = PRINT_DEC) {
digitalWrite(_turnaroudpin,LOW^_normaly_low);
_thisSerial.print(in,type);
waitForTransmitToComplete();
digitalWrite(_turnaroudpin,HIGH^_normaly_low);
};
void print(double in, int type = 2) {
digitalWrite(_turnaroudpin,LOW^_normaly_low);
_thisSerial.print(in,type);
waitForTransmitToComplete();
digitalWrite(_turnaroudpin,HIGH^_normaly_low);
};
void println(const String &s) {
digitalWrite(_turnaroudpin,LOW^_normaly_low);
delayMicroseconds(5);
_thisSerial.println(s);
waitForTransmitToComplete();
delay(100);
digitalWrite(_turnaroudpin,HIGH^_normaly_low);
};
void println(const char* in) {
digitalWrite(_turnaroudpin,LOW^_normaly_low);
_thisSerial.println(in);
waitForTransmitToComplete();
digitalWrite(_turnaroudpin,HIGH^_normaly_low);
};
void println(char in, int type = PRINT_BYTE) {
digitalWrite(_turnaroudpin,LOW^_normaly_low);
_thisSerial.println(in,type);
waitForTransmitToComplete();
digitalWrite(_turnaroudpin,HIGH^_normaly_low);
};
void println(unsigned char in, int type = PRINT_BYTE) {
digitalWrite(_turnaroudpin,LOW^_normaly_low);
_thisSerial.println(in,type);
waitForTransmitToComplete();
digitalWrite(_turnaroudpin,HIGH^_normaly_low);
};
void println(int in, int type = PRINT_DEC) {
digitalWrite(_turnaroudpin,LOW^_normaly_low);
_thisSerial.println(in,type);
waitForTransmitToComplete();
digitalWrite(_turnaroudpin,HIGH^_normaly_low);
};
void println(unsigned int in, int type = PRINT_DEC) {
digitalWrite(_turnaroudpin,LOW^_normaly_low);
_thisSerial.println(in,type);
waitForTransmitToComplete();
digitalWrite(_turnaroudpin,HIGH^_normaly_low);
};
void println(long in, int type = PRINT_DEC) {
digitalWrite(_turnaroudpin,LOW^_normaly_low);
_thisSerial.println(in,type);
waitForTransmitToComplete();
digitalWrite(_turnaroudpin,HIGH^_normaly_low);
};
void println(unsigned long in, int type = PRINT_DEC) {
digitalWrite(_turnaroudpin,LOW^_normaly_low);
_thisSerial.println(in,type);
waitForTransmitToComplete();
digitalWrite(_turnaroudpin,HIGH^_normaly_low);
};
void println(double in, int type = 2) {
digitalWrite(_turnaroudpin,LOW^_normaly_low);
_thisSerial.println(in,type);
waitForTransmitToComplete();
digitalWrite(_turnaroudpin,HIGH^_normaly_low);
};
void waitForTransmitToComplete()
{
_thisSerial.flush();
};
};