-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBluetoothHC05.h
204 lines (179 loc) · 5.85 KB
/
BluetoothHC05.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
#pragma once
/*
template <typename DerivedType, typename _SerialType = HardwareSerial, byte PIN_POWER = 50, byte PIN_STATE = 51, byte PIN_34 = 34>
class BluetoothHC05 : public SerialActivity<BluetoothHC05<DerivedType, _SerialType, PIN_POWER, PIN_STATE, PIN_34>, _SerialType> {
protected:
enum class EInitState {
Failed,
AT,
Name,
Role,
Password,
Baud,
Done
};
EInitState InitState = EInitState::Failed;
public:
String Name;
String Password;
int Baud = 0;
bool OK = false;
void OnSerialReceived(const StringBuf& buf) {
switch (InitState) {
case EInitState::Failed:
return;
case EInitState::AT: {
if (buf == "OK") {
}
}
}
static_cast<DerivedType*>(this)->OnBluetoothReceived(buf);
}
template <typename... Types>
String BluetoothCommand(Types... args);
template <typename Type>
String BluetoothCommand(Type arg);
template <typename Type, typename... Types>
String BluetoothCommand(Type arg, Types... args) {
write(arg);
return BluetoothCommand(args...);
}
String BluetoothCommand(const char* Command) {
write(Command);
write("\r\n");
String Response = readStringUntil('\n');
Response.trim();
return Response;
}
template <typename ConfigType>
void OnSetup(const ConfigType& config) {
SerialActivity<BluetoothHC05<DerivedType, _SerialType, PIN_POWER, PIN_STATE, PIN_34>, _SerialType>::OnSetup(config);
pinMode(PIN_POWER, OUTPUT);
pinMode(PIN_STATE, INPUT_PULLUP);
pinMode(PIN_34, OUTPUT);
digitalWrite(PIN_POWER, LOW);
digitalWrite(PIN_34, LOW);
delay(500);
// set pin 34 HIGH
digitalWrite(PIN_34, HIGH);
delay(100);
// turn on the HC-05
digitalWrite(PIN_POWER, HIGH);
delay(1000);
write("AT\r\n");
InitState = EInitState::AT;
OK = BluetoothCommand("AT") == "OK";
if (OK) {
OK &= BluetoothCommand("AT+NAME=", config.Name) == "OK";
OK &= BluetoothCommand("AT+ROLE=0") == "OK";
OK &= BluetoothCommand("AT+PSWD=", config.Password) == "OK";
OK &= BluetoothCommand("AT+UART=", config.Baud, ",0,0") == "OK";
if (OK) {
digitalWrite(PIN_34, LOW);
digitalWrite(PIN_POWER, LOW);
delay(1000);
digitalWrite(PIN_POWER, HIGH);
}
}
}
void OnLoop(const ActivityContext& context) {
if (OK) {
SerialActivity<BluetoothHC05<DerivedType, _SerialType, PIN_POWER, PIN_STATE, PIN_34>, _SerialType>::OnLoop(context);
}
}
bool IsBluetoothConnected() const {
return digitalRead(PIN_STATE) != LOW;
}
};
*/
namespace AW {
template <byte PIN_POWER = 50, byte PIN_STATE = 51, byte PIN_34 = 34>
class TBluetoothHC05 {
protected:
enum class EState {
Error,
Pin34,
PinPower,
Init,
AT,
Name,
Role,
Password,
Baud,
Reset,
OK
};
EState State = EState::Error;
TActor* Owner = nullptr;
TActor* Serial = nullptr;
public:
TBluetoothHC05(TActor* owner, TActor* serial)
: Owner(owner)
, Serial(serial)
{}
void Init(const TActorContext& context) {
pinMode(PIN_POWER, OUTPUT);
pinMode(PIN_STATE, INPUT_PULLUP);
pinMode(PIN_34, OUTPUT);
digitalWrite(PIN_POWER, LOW);
digitalWrite(PIN_34, LOW);
State = EState::Pin34;
context.Send(Owner, Owner, new TEventReceive(context.Now + TTime::MilliSeconds(500)));
//context.Send(Owner, Serial, new TEventSerialData("AT"));
}
bool Receive(TUniquePtr<TEventSerialData>& event, const TActorContext& context) {
switch (State) {
case EState::OK:
return false;
case EState::Error:
break;
case EState::AT:
if (event->Data == "OK") {
//context.Send(Owner, Serial, new TEventSerialData("AT+NAME="));
/*OK &= BluetoothCommand("AT+NAME=", config.Name) == "OK";
OK &= BluetoothCommand("AT+ROLE=0") == "OK";
OK &= BluetoothCommand("AT+PSWD=", config.Password) == "OK";
OK &= BluetoothCommand("AT+UART=", config.Baud, ",0,0") == "OK";*/
digitalWrite(PIN_34, LOW);
digitalWrite(PIN_POWER, LOW);
State = EState::Reset;
context.Send(Owner, Owner, new TEventReceive(context.Now + TTime::MilliSeconds(1000)));
} else {
State = EState::Error;
}
}
return true;
}
void Receive(TUniquePtr<TEventReceive>& event, const TActorContext& context) {
switch (State) {
case EState::Pin34:
// set pin 34 HIGH
digitalWrite(PIN_34, HIGH);
State = EState::PinPower;
context.Send(Owner, Owner, new TEventReceive(context.Now + TTime::MilliSeconds(100)));
return;
case EState::PinPower:
// turn on the HC-05
digitalWrite(PIN_POWER, HIGH);
State = EState::Init;
context.Send(Owner, Owner, new TEventReceive(context.Now + TTime::MilliSeconds(1000)));
return;
case EState::Init:
State = EState::AT;
context.Send(Owner, Serial, new TEventSerialData("AT"));
return;
case EState::Reset:
digitalWrite(PIN_POWER, HIGH);
State = EState::OK;
return;
}
State = EState::Error;
}
bool IsOK() const {
return State == EState::OK;
}
bool IsConnected() const {
return digitalRead(PIN_STATE) != LOW;
}
};
}