-
Notifications
You must be signed in to change notification settings - Fork 0
/
BluetoothZS040.h
176 lines (154 loc) · 4.53 KB
/
BluetoothZS040.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
#pragma once
/*
template <typename DerivedType, typename _SerialType>
class BluetoothZS040 : public SerialActivity<BluetoothZS040<DerivedType, _SerialType>, _SerialType> {
public:
BluetoothZS040<DerivedType, _SerialType>& Bluetooth;
bool OK;
bool Connected;
BluetoothZS040()
: Bluetooth(*this)
, OK(false)
, Connected(false)
{}
void OnBluetoothCommand(const StringBuf& buf) {}
void OnBluetoothConnected() {}
void OnBluetoothDisconnected() {}
void OnSerialCommand(const StringBuf& buf) {
if (!Connected) {
if (buf == "CONNECTED") {
Connected = true;
static_cast<DerivedType*>(this)->OnBluetoothConnected();
return;
}
if (buf.starts_with("+")) {
return;
}
} else {
if (buf == "+DISC:SUCCESS") {
Connected = false;
static_cast<DerivedType*>(this)->OnBluetoothDisconnected();
return;
}
}
static_cast<DerivedType*>(this)->OnBluetoothCommand(buf);
}
template <typename... Types>
String BluetoothCommand(Types... args);
template <typename... Types>
String BluetoothCommand(const String& arg, Types... args) {
Port.write(arg.c_str());
return BluetoothCommand(args...);
}
template <typename... Types>
String BluetoothCommand(const StringSumHelper& arg, Types... args) {
Port.write(arg.c_str());
return BluetoothCommand(args...);
}
template <typename Type, typename... Types>
String BluetoothCommand(Type arg, Types... args) {
Port.write(arg);
return BluetoothCommand(args...);
}
String BluetoothCommand() {
String Response;
Port.write("\r\n");
do {
Response = Port.readStringUntil('\n');
Response.trim();
} while (Response.length() != 0 && Response[0] == '+');
return Response;
}
static unsigned long GetBaudCode(unsigned long baud) {
switch (baud) {
case 9600:
return 4;
case 38400:
return 6;
default:
return 4;
}
}
void Setup(int baud) {
SerialActivity<BluetoothZS040<DerivedType, _SerialType>, _SerialType>::Setup(baud);
for (int i = 0; i < 3; ++i) {
OK |= BluetoothCommand("AT") == "OK";
if (OK)
break;
}
}
void Setup(int baud, const String& name, const String& password) {
Setup(baud);
if (OK) {
//OK &= BluetoothCommand(String("AT+BAUD") + GetBaudCode(config.Baud)) == "OK" + config.Baud;
OK &= BluetoothCommand(String("AT+NAME") + name) == "OK";
OK &= BluetoothCommand(String("AT+PIN") + password) == "OK";
}
}
void OnLoop(const ActivityContext& context) {
if (OK) {
SerialActivity<BluetoothZS040<DerivedType, _SerialType>, _SerialType>::OnLoop(context);
}
}
bool IsBluetoothConnected() const {
// TODO
return OK && true;
}
};
*/
namespace AW {
class TBluetoothZS040 {
protected:
enum class EState {
Error,
AT,
OK
};
EState State = EState::Error;
TActor* Owner;
TActor* Serial;
bool Connected = false;
public:
TBluetoothZS040(TActor* owner, TActor* serial)
: Owner(owner)
, Serial(serial) {}
void Init(const TActorContext&) {
//State = EState::AT;
//context.Send(Owner, Serial, new TEventSerialData("AT"));
State = EState::OK;
}
bool Receive(TUniquePtr<TEventSerialData>& event, const TActorContext&) {
switch (State) {
case EState::Error:
break;
case EState::OK:
if (Connected) {
if (event->Data.starts_with("+")) {
if (event->Data == "+DISC:SUCCESS") {
Connected = false;
}
}
} else
if (event->Data == "CONNECTED") {
Connected = true;
}
return false;
case EState::AT:
if (event->Data == "OK") {
State = EState::OK;
} else {
State = EState::Error;
}
}
return true;
}
void Receive(TUniquePtr<TEventReceive>&, const TActorContext&) {
}
bool IsOK() const {
return State == EState::OK;
}
bool IsConnected() const {
return Connected;
}
};
}