-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathespSoftwareSerial.h
265 lines (239 loc) · 5.74 KB
/
espSoftwareSerial.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*example:
#include "espSoftwareSerial.h"
using namespace eSSEnv;
void setup() {
Serial.begin(115200);
eSS1.uartInit(2, 3);
eSS2.uartInit(4, 5);
}
void loop() {
returnData RTN = receiveDataTask();
//Serial.println("LOOP OK!");
if (RTN.whichUart != 0) {
Serial.print(RTN.whichUart);
Serial.print(" ");
Serial.println(RTN.data);
}
sendData(UART1, "RTN.data")
}
*/
#ifndef espSoftwareSerial_H
#define espSoftwareSerial_H
#include <Arduino.h>
namespace eSSC{
typedef unsigned int CountType;
typedef uint8_t pinType;
#define Baud 10000
const uint32_t BaudMicroSecend = 1000000/Baud;
const uint8_t UART1 = 1;
const uint8_t UART2 = 2;
}
namespace eSSVar{
String receiveStr;
uint8_t waitflag = 0;
uint8_t uartNum = 0;
}
namespace eSSStruct{
struct returnData{
uint8_t whichUart;
String data;
};
}
namespace eSSNamespace{
using namespace eSSC;
using namespace eSSVar;
using namespace eSSStruct;
void sendDataTask1(void *);
returnData reUart1(void);
returnData reUart2(void);
class eSSModel{
public:
pinType TXD, RXD;
String sendStr;
void uartInit(pinType RXDt, pinType TXDt){
TXD = TXDt;
RXD = RXDt;
pinMode(RXD, INPUT_PULLUP);
pinMode(TXD, OUTPUT);
digitalWrite(TXD, HIGH);
//xTaskCreatePinnedToCore(receiveDataTask1, "receiveDataTask1", 4096, NULL, 3, &xHandle, 0);
}
void sendData(uint8_t whichUart, String str){
sendStr = str;
switch (whichUart){
case 1:
//Serial.println("Task OK!");
xTaskCreatePinnedToCore(sendDataTask1, "sendDataTask1", 4096, NULL, 3, &xHandle, 0);
break;
case 2:
/*xTaskCreatePinnedToCore(sendDataTask2, "sendDataTask2", 4096, NULL, 3, &xHandle, 1);*/
break;
}
}
/*void receiveData(){
//String strTemp;
static uint8_t i = 0;
if (digitalRead(RXD) == LOW) {
sendFlag = 0;
ets_delay_us(BaudMicroSecend); // 延迟一个位的时间
char data = 0;
// 接收每个比特位
for (int i = 0; i < 8; i++) {
data |= digitalRead(RXD) << i; // 读取当前位并设置到相应的位置
ets_delay_us(BaudMicroSecend); // 延迟一个位的时间
}
receiveStr.concat((char)data);
ets_delay_us(BaudMicroSecend);
//Serial.println("receiveDataTask1 OK!");
i+=3;
}else{
if(i>0){
i--;
if(i == 0){
reflag = 1;
sendFlag = 1;
//Serial.println("receiveDataTask1 reflag OK!");
}
ets_delay_us(BaudMicroSecend/2);
}
}
}*/
private:
TaskHandle_t xHandle;
};
eSSModel eSS1, eSS2;
void sendDataTask1(void *arg){
CountType length = eSS1.sendStr.length() + 1;
char data[length];
eSS1.sendStr.toCharArray(data, sizeof(data));
/*Serial.println(length);
Serial.println(data);*/
for(int j = 0; j<length - 1; j++){
digitalWrite(eSS1.TXD, LOW); // 发送起始位(低电平)
ets_delay_us(BaudMicroSecend); // 延迟一个位的时间
// 发送每个比特位
for (int i = 0; i < 8; i++) {
digitalWrite(eSS1.TXD, data[j] & 1); // 发送当前位
ets_delay_us(BaudMicroSecend); // 延迟一个位的时间
data[j] >>= 1; // 移位获取下一位
}
digitalWrite(eSS1.TXD, HIGH); // 发送停止位(高电平)
ets_delay_us(BaudMicroSecend); // 延迟一个位的时间
}
Serial.println("sendDataTask1 OK!");
vTaskDelete(NULL);
}
returnData receiveDataTask(){
returnData RTN;
switch(uartNum){
case UART1:
RTN = reUart1();
return RTN;
break;
case UART2:
RTN = reUart2();
return RTN;
break;
default:
if(digitalRead(eSS1.RXD) == LOW){
uartNum = UART1;
RTN = reUart1();
return RTN;
}
if(digitalRead(eSS2.RXD) == LOW){
uartNum = UART2;
RTN = reUart2();
return RTN;
}
break;
}
RTN.whichUart = 0;
return RTN;
}
returnData reUart1(){
returnData RTN;
if(digitalRead(eSS1.RXD) == LOW) {
ets_delay_us(BaudMicroSecend);
char data = 0;
for (int i = 0; i < 8; i++) {
data |= digitalRead(eSS1.RXD) << i;
ets_delay_us(BaudMicroSecend);
}
receiveStr.concat((char)data);
ets_delay_us(BaudMicroSecend);
waitflag+=3;
RTN.whichUart = 0;
}else{
if(waitflag > 0) {
waitflag--;
if(waitflag == 0){
uartNum = 0;
RTN.whichUart = 1;
RTN.data = receiveStr;
receiveStr = "";
}
ets_delay_us(BaudMicroSecend/2);
}
}
return RTN;
}
returnData reUart2(){
returnData RTN;
if(digitalRead(eSS2.RXD) == LOW) {
ets_delay_us(BaudMicroSecend);
char data = 0;
for (int i = 0; i < 8; i++) {
data |= digitalRead(eSS2.RXD) << i;
ets_delay_us(BaudMicroSecend);
}
receiveStr.concat((char)data);
ets_delay_us(BaudMicroSecend);
waitflag+=3;
RTN.whichUart = 0;
}else{
if(waitflag > 0) {
waitflag--;
if(waitflag == 0){
uartNum = 0;
RTN.whichUart = 2;
RTN.data = receiveStr;
receiveStr = "";
}
ets_delay_us(BaudMicroSecend/2);
}
}
return RTN;
}
}
namespace eSSEnv{
using namespace eSSStruct;
using namespace eSSC;
using eSSNamespace::receiveDataTask;
using eSSNamespace::eSS1;
using eSSNamespace::eSS2;
}
#endif
/*if (digitalRead(eSS1.RXD) == LOW) {
sendFlag = 0;
ets_delay_us(BaudMicroSecend); // 延迟一个位的时间
char data = 0;
// 接收每个比特位
for (int i = 0; i < 8; i++) {
data |= digitalRead(eSS1.RXD) << i; // 读取当前位并设置到相应的位置
ets_delay_us(BaudMicroSecend); // 延迟一个位的时间
}
receiveStr.concat((char)data);
ets_delay_us(BaudMicroSecend);
//Serial.println("receiveDataTask OK!");
i+=3;
}else{
if(i>0){
i--;
if(i == 0){
reflag = 1;
sendFlag = 1;
//Serial.println("receiveDataTask reflag OK!");
}
ets_delay_us(BaudMicroSecend/2);
}
*/