-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsulBT.cpp
270 lines (230 loc) · 5.11 KB
/
AsulBT.cpp
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
266
267
268
269
270
#include <Arduino.h>
#include "SoftwareSerial.h"
//#define _USE_DEBUG
#include "AsulBT.h"
//#define btSerial Serial
SoftwareSerial btSerial(3, 2);
//SoftwareSerial *pBtSerial;
AsulBT::AsulBT(uint8_t index)
{
Cmd_State = RSP_CMD_STATE_WAIT_STX;
switch(index)
{
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
}
}
AsulBT::AsulBT(uint8_t rxPin, uint8_t txPin)
{
Cmd_State = RSP_CMD_STATE_WAIT_STX;
// pBtSerial = new SoftwareSerial(rxPin, txPin);
}
void AsulBT::begin(uint32_t baud)
{
btSerial.begin(baud);
}
int AsulBT::update(void)
{
int Ret = -1;
uint8_t ch;
//-- 명령어 수신
//
if( btSerial.available() )
{
ch = btSerial.read();
#ifdef _USE_DEBUG
Serial.print(ch, HEX);
Serial.print(", ");
#endif
}
else
{
return -1;
}
//-- 바이트간 타임아웃 설정(50ms)
//
CurrentTime = millis();
if( (CurrentTime - PreviousTime) > 1000 )
{
Cmd_State = RSP_CMD_STATE_WAIT_STX;
PreviousTime = CurrentTime;
}
//-- 명령어 상태
//
switch( Cmd_State )
{
//-- STX 문자 기다리는 상태
//
case RSP_CMD_STATE_WAIT_STX:
// 시작 문자를 기다림
if( ch == RSP_CMD_STX )
{
Cmd_State = RSP_CMD_STATE_WAIT_CMD;
Cmd.CheckSum = 0x00;
Cmd.Length = 0;
//Serial.println("STX");
}
break;
//-- 명령어 기다리는 상태
//
case RSP_CMD_STATE_WAIT_CMD:
Cmd.Cmd = ch;
Cmd.CheckSum ^= ch;
Cmd_State = RSP_CMD_STATE_WAIT_SIZE;
//Serial.println("CMD");
break;
//-- 데이터 사이즈 기다리는 상태(128까지)
//
case RSP_CMD_STATE_WAIT_SIZE:
//Serial.println("SIZE");
if( ch <= RSP_CMD_MAX_LENGTH )
{
Cmd.Length = ch;
Index_Data = 0;
Cmd.CheckSum ^= ch;
if( Cmd.Length > 0 )
{
Cmd_State = RSP_CMD_STATE_WAIT_DATA;
}
else
{
Cmd_State = RSP_CMD_STATE_WAIT_CHECKSUM;
}
}
else
{
Cmd_State = RSP_CMD_STATE_WAIT_STX;
}
break;
//-- 데이터를 기다리는 상태
//
case RSP_CMD_STATE_WAIT_DATA:
//Serial.println("DATA");
Cmd.CheckSum ^= ch;
Cmd.Data[ Index_Data ] = ch;
Index_Data++;
if( Index_Data >= Cmd.Length )
{
Cmd_State = RSP_CMD_STATE_WAIT_CHECKSUM;
}
break;
//-- 체크섬을 기다리는 상태
//
case RSP_CMD_STATE_WAIT_CHECKSUM:
//Serial.println("CHECKSUM");
Cmd.CheckSumRecv = ch;
Cmd_State = RSP_CMD_STATE_WAIT_ETX;
break;
//-- ETX 기다리는 상태
//
case RSP_CMD_STATE_WAIT_ETX:
//Serial.println("ETX");
#ifdef _USE_DEBUG
Serial.print(Cmd.CheckSumRecv);
Serial.print(" ");
Serial.println(Cmd.CheckSum);
#endif
if( ch == RSP_CMD_ETX )
{
if( Cmd.CheckSumRecv == Cmd.CheckSum )
{
Ret = Cmd.Cmd;
}
}
Cmd_State = RSP_CMD_STATE_WAIT_STX;
break;
}
return Ret;
}
void AsulBT::SendCmd( RSP_CMD_OBJ *pCmd )
{
uint8_t i;
uint8_t CheckSum = 0;
#ifdef _USE_DEBUG
Serial.print("STX: ");
Serial.println(RSP_CMD_STX, HEX);
Serial.print("CMD: ");
Serial.println(pCmd->Cmd, HEX);
Serial.print("LEN: ");
Serial.println(pCmd->Length, DEC);
#endif
btSerial.write( RSP_CMD_STX );
btSerial.write( pCmd->Cmd ); CheckSum ^= pCmd->Cmd;
btSerial.write( pCmd->Length ); CheckSum ^= pCmd->Length;
//Serial.println("DATA:");
for( i=0; i<pCmd->Length; i++ )
{
btSerial.write( pCmd->Data[i] );
CheckSum ^= pCmd->Data[i];
#ifdef _USE_DEBUG
Serial.println(pCmd->Data[i], DEC);
#endif
}
btSerial.write( CheckSum );
btSerial.write( RSP_CMD_ETX );
#ifdef _USE_DEBUG
Serial.print("CHK: ");
Serial.println(CheckSum, HEX);
Serial.print("ETX: ");
Serial.println(RSP_CMD_ETX, HEX);
#endif
}
void AsulBT::writeJoystick(JOYSTICK_DATA joyData)
{
Cmd.Cmd = CMD_JOYSTICK;
Cmd.Length = sizeof(JOYSTICK_DATA);
memcpy(Cmd.Data, (uint8_t*)&joyData, sizeof(joyData));
SendCmd(&Cmd);
}
int AsulBT::readJoystick(JOYSTICK_DATA* joyData)
{
if(Cmd.Cmd == CMD_JOYSTICK)
{
memcpy((uint8_t*)joyData, Cmd.Data, sizeof(JOYSTICK_DATA));
return 1;
}
return -1;
}
uint8_t AsulBT::write(uint8_t val)
{
Cmd.Cmd = CMD_RAWDATA;
Cmd.Length = 1;
Cmd.Data[0] = val;
SendCmd(&Cmd);
return 1;
}
uint8_t AsulBT::write(String str)
{
Cmd.Cmd = CMD_RAWDATA;
Cmd.Length = str.length();
if(RSP_CMD_MAX_LENGTH < Cmd.Length)
return 0;
str.getBytes((unsigned char*)Cmd.Data, Cmd.Length);
SendCmd(&Cmd);
return Cmd.Length;
}
uint8_t AsulBT::write(uint8_t *buf, uint8_t len)
{
Cmd.Cmd = CMD_RAWDATA;
Cmd.Length = len;
if(RSP_CMD_MAX_LENGTH < Cmd.Length)
return 0;
memcpy(Cmd.Data, buf, Cmd.Length);
SendCmd(&Cmd);
return Cmd.Length;
}
uint8_t AsulBT::read(uint8_t *buf)
{
memcpy(buf, Cmd.Data, Cmd.Length);
return Cmd.Length;
}
String AsulBT::getVersion()
{
return Version;
}