-
Notifications
You must be signed in to change notification settings - Fork 0
/
Serial.h
148 lines (124 loc) · 4.11 KB
/
Serial.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
#pragma once
#include <HardwareSerial.h>
#include <SoftwareSerial.h>
namespace AW {
template <HardwareSerial& Port, long Baud>
class THardwareSerial {
public:
void Begin() {
Port.begin(Baud);
}
int AvailableForRead() const {
return Port.available();
}
int AvailableForWrite() const {
return Port.availableForWrite();
}
int Write(const char* buffer, int length) {
return Port.write(buffer, length);
}
int Read(char* buffer, int length) {
return Port.readBytes(buffer, length);
}
};
template <int RxPin, int TxPin, long Baud>
class TSoftwareSerial {
protected:
SoftwareSerial Port;
public:
TSoftwareSerial()
: Port(RxPin, TxPin)
{}
void Begin() {
Port.begin(Baud);
}
int AvailableForRead() const {
return const_cast<SoftwareSerial&>(Port).available();
}
int AvailableForWrite() const {
return 32;
}
int Write(const char* buffer, int length) {
return Port.write(buffer, length);
}
int Read(char* buffer, int length) {
return Port.readBytes(buffer, length);
}
};
struct TEventSerialData : TBasicEvent<TEventSerialData> {
constexpr static TEventID EventID = 2; // TODO
String Data;
TEventSerialData(String data)
: Data(data) {}
};
template <typename SerialType>
class TSerialActor : public TActor {
static constexpr unsigned int MaxBufferSize = 256;
public:
TSerialActor(TActor* owner)
: Owner(owner)
, EOL("\n")
{}
protected:
SerialType Port;
TActor* Owner;
String Buffer;
StringBuf EOL;
void OnEvent(TEventPtr event, const TActorContext& context) override {
switch (event->EventID) {
case TEventBootstrap::EventID:
return OnBootstrap(static_cast<TEventBootstrap*>(event.Release()), context);
case TEventSerialData::EventID:
return OnSerialData(static_cast<TEventSerialData*>(event.Release()), context);
case TEventReceive::EventID:
return OnReceive(static_cast<TEventReceive*>(event.Release()), context);
}
}
void OnBootstrap(TUniquePtr<TEventBootstrap>, const TActorContext& context) {
Port.Begin();
context.Send(this, this, new TEventReceive);
}
void OnSerialData(TUniquePtr<TEventSerialData> event, const TActorContext& context) {
int availableForWrite = Port.AvailableForWrite();
int len = event->Data.length();
int size = len > availableForWrite ? availableForWrite : len;
if (size > 0) {
Port.Write(event->Data.begin(), size);
if (size < len) {
event->Data.erase(0, size);
context.ResendImmediate(this, event.Release());
} else {
// TODO: could block
Port.Write(EOL.data(), EOL.size());
}
} else {
context.ResendImmediate(this, event.Release());
}
}
void OnReceive(TUniquePtr<TEventReceive> event, const TActorContext& context) {
auto size = min((unsigned int)Port.AvailableForRead(), MaxBufferSize - Buffer.size());
if (size > 0) {
//::Serial.println(size);
int strStart = 0;
auto bufferPos = Buffer.size();
Buffer.reserve(Buffer.size() + size);
size = Port.Read(Buffer.data() + Buffer.size(), size);
Buffer.resize(Buffer.size() + size);
while (bufferPos < Buffer.size()) {
if (Buffer[bufferPos] == '\n') {
int strSize = bufferPos;
while (strSize > 0 && Buffer[strSize - 1] == '\r') {
--strSize;
}
//::Serial.println(Buffer.substr(strStart, strSize - strStart).data());
context.Send(this, Owner, new TEventSerialData(Buffer.substr(strStart, strSize - strStart)));
strStart = bufferPos + 1;
}
++bufferPos;
}
Buffer = Buffer.substr(strStart, bufferPos - strStart);
}
context.Resend(this, event.Release());
}
};
}