-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleVR.cpp
More file actions
506 lines (459 loc) · 10.1 KB
/
SimpleVR.cpp
File metadata and controls
506 lines (459 loc) · 10.1 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/**
******************************************************************************
* @file SimpleVR.cpp
* @author Elechouse Team
* @version V1.0
* @date 2014-01-08
* @brief This file provides all the SimpleVR firmware functions.
******************************************************************************
@note
This driver is for elechouse SimpleVR Module(LINKS here)
******************************************************************************
* @section HISTORY
V1.0 Initial version.
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, ELECHOUSE SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2013 ELECHOUSE</center></h2>
******************************************************************************
*/
#include "SimpleVR.h"
#include <string.h>
VR* VR::instance;
/** temp data buffer */
uint8_t vr_buf[32];
uint8_t hextab[17]="0123456789ABCDEF";
/**
@brief VR class constructor.
@param receivePin --> software serial RX
transmitPin --> software serial TX
*/
VR::VR(uint8_t receivePin, uint8_t transmitPin) : SoftwareSerial(receivePin, transmitPin)
{
instance = this;
SoftwareSerial::begin(38400);
}
/**
@brief read recognise result
@param buf --> return data .
buf[0] --> high byte of sentence index value
buf[1] --> low byte of sentence index value
buf[2] --> sentences group number
buf[3] --> score of recognition.
timeout --> wait time for receiving packet.
@retval length of valid data in buf. 0 means no data received.
*/
int VR :: recognize(uint8_t *buf, int timeout)
{
int ret, i;
ret = receive_pkt(vr_buf, timeout);
if(vr_buf[2] != FRAME_CMD_VR){
return -1;
}
if(ret > 0){
for(i = 0; i < (vr_buf[1] - 3); i++){
buf[i] = vr_buf[4+i];
}
return i;
}
return 0;
}
/**
@brief reset system setting to default
@retval 0 --> success
-1 --> failed
*/
int VR :: restoreSystemSettings()
{
int len;
send_pkt(FRAME_CMD_SYS_RESET, 0, 0);
len = receive_pkt(vr_buf);
if(len<=0){
return -1;
}
if(vr_buf[2] != FRAME_CMD_SYS_RESET){
return -1;
}
return 0;
}
/**
@brief check system settings
@param buf --> return value
buf[0] --> work state
buf[1] --> group selected
buf[2] --> score
@retval '>0' --> buf length
-1 --> failed
*/
int VR :: checkSystemSettings(uint8_t* buf)
{
int len;
if(buf == 0){
return -1;
}
send_pkt(FRAME_CMD_CHECK_SYS, 0, 0);
len = receive_pkt(vr_buf);
if(len<=0){
return -1;
}
if(vr_buf[2] != FRAME_CMD_CHECK_SYS){
return -1;
}
memcpy(buf, vr_buf+4, vr_buf[1]-3);
return vr_buf[1]-3;
}
/**
@brief check simplevr version
@param buf --> return value
buf[0] --> software version major
buf[1] --> software version minor
buf[2] --> software version patch
buf[3] --> hardware version major
buf[4] --> hardware version minor
@retval '>0' --> buf length
-1 --> failed
*/
int VR :: checkVersion(uint8_t* buf)
{
int len;
if(buf == 0){
return -1;
}
send_pkt(FRAME_CMD_CHECK_VERSION, 0, 0);
len = receive_pkt(vr_buf);
if(len<=0){
return -1;
}
if(vr_buf[2] != FRAME_CMD_CHECK_VERSION){
return -1;
}
memcpy(buf, vr_buf+4, vr_buf[1]-3);
return vr_buf[1]-3;
}
/**
@brief select and enable a group
@param grp --> group number
@retval 0 --> success
-1 --> failed
*/
int VR :: setGroup(uint8_t grp)
{
int len;
if(grp == 0 || grp > 64){
return -1;
}
send_pkt(FRAME_CMD_GRP, &grp, 1);
len = receive_pkt(vr_buf);
if(len<=0){
return -1;
}
if(vr_buf[2] != FRAME_CMD_GRP){
return -1;
}
return 0;
}
/**
@brief set threshold value
@param val --> threshold value
@retval 0 --> success
-1 --> failed
*/
int VR :: setThreshold(uint8_t val)
{
int len;
send_pkt(FRAME_CMD_THRESHOLD, &val, 1);
len = receive_pkt(vr_buf);
if(len<=0){
return -1;
}
if(vr_buf[2] != FRAME_CMD_THRESHOLD){
return -1;
}
return 0;
}
/**
@brief enable SimpleVR
@param sta --> new state
true --> enable
false --> disable
@retval 0 --> success
-1 --> failed
*/
int VR :: setEnable(bool sta)
{
int len;
uint8_t cmd;
if(sta){
cmd = FRAME_CMD_ENABLE_VR;
}else{
cmd = FRAME_CMD_DISABLE_VR;
}
send_pkt((uint8_t)cmd, 0, 0);
len = receive_pkt(vr_buf);
if(len<=0){
return -1;
}
if(vr_buf[2] != cmd){
return -1;
}
return 0;
}
/**
@brief disable SimpleVR
@param sta --> new state
true --> disable
false --> enable
@retval 0 --> success
-1 --> failed
*/
int VR :: setDisable(bool sta)
{
return setEnable(!sta);
}
/**
@brief enable startup information
@param sta --> new state
true --> enalbe
false --> disable
@retval 0 --> success
-1 --> failed
*/
int VR :: setStartInfoEnable(uint8_t sta)
{
int len;
uint8_t cmd;
if(sta){
cmd = 1;
}else{
cmd = 0;
}
send_pkt(FRAME_CMD_SYS_INFO_CTL, &cmd, 1);
len = receive_pkt(vr_buf);
if(len<=0){
return -1;
}
if(vr_buf[2] != cmd){
return -1;
}
return 0;
}
/**
@brief disable startup information
@param sta --> new state
true --> disable
false --> enable
@retval 0 --> success
-1 --> failed
*/
int VR :: setStartInfoDisable(uint8_t sta)
{
return setStartInfoEnable(!sta);
}
/**flash operation function (strlen)*/
int VR :: len(uint8_t *buf)
{
int i=0;
while(pgm_read_byte_near(buf++)){
i++;
}
return i;
}
/**flash operation function (strcmp)*/
int VR :: cmp(uint8_t *buf, uint8_t *bufcmp, int len )
{
int i;
for(i=0; i<len; i++){
if(buf[i] != pgm_read_byte_near(bufcmp+i)){
return -1;
}
}
return 0;
}
/**flash operation function (strcpy)*/
void VR :: cpy(char *buf, char * pbuf)
{
int i=0;
while(pgm_read_byte_near(pbuf)){
buf[i] = pgm_read_byte_near(pbuf++);
i++;
}
}
/** ascending sort */
void VR :: sort(uint8_t *buf, int len)
{
int i, j;
uint8_t tmp;
for(i=0; i<len; i++){
for(j=i+1; j<len; j++){
if(buf[j] < buf[i]){
tmp = buf[i];
buf[i] = buf[j];
buf[j] = tmp;
}
}
}
}
/** remove duplicates */
int VR :: cleanDup(uint8_t *des, uint8_t *buf, int len)
{
if(len<1){
return -1;
}
int i, j, k=0;
for(i=0; i<len; i++){
for(j=0; j<k; j++){
if(buf[i] == des[j]){
break;
}
}
if(j==k){
des[k] = buf[i];
k++;
}
}
return k;
#if 0
int i=0, j=1, k=0;
sort(buf, len);
for(; j<len; ){
des[k] = buf[i];
k++;
while(buf[i]==buf[j]){
j++;
if(j==len){
return k;
}
}
i=j;
}
return k;
#endif
}
/** Serial print in HEX format */
int VR :: writehex(uint8_t *buf, uint8_t len)
{
int i;
for(i=0; i<len; i++){
DBGCHAR(hextab[(buf[i]&0xF0)>>4]);
DBGCHAR(hextab[(buf[i]&0x0F)]);
DBGCHAR(' ');
}
return len;
}
/**
@brief send data packet in Voice Recognition module protocol format.
@param cmd --> command
subcmd --> subcommand
buf --> data area
len --> length of buf
*/
void VR :: send_pkt(uint8_t cmd, uint8_t subcmd, uint8_t *buf, uint8_t len)
{
while(available()){
flush();
}
write(FRAME_HEAD);
write(len+3);
write(cmd);
write(subcmd);
write(buf, len);
write(FRAME_END);
}
/**
@brief send data packet in Voice Recognition module protocol format.
@param cmd --> command
buf --> data area
len --> length of buf
*/
void VR :: send_pkt(uint8_t cmd, uint8_t *buf, uint8_t len)
{
while(available()){
flush();
}
write(FRAME_HEAD);
write(len+2);
write(cmd);
write(buf, len);
write(FRAME_END);
}
/**
@brief send data packet in Voice Recognition module protocol format.
@param buf --> data area
len --> length of buf
*/
void VR :: send_pkt(uint8_t *buf, uint8_t len)
{
while(available()){
flush();
}
write(FRAME_HEAD);
write(len+1);
write(buf, len);
write(FRAME_END);
}
/**
@brief receive a valid data packet in Voice Recognition module protocol format.
@param buf --> return value buffer.
timeout --> time of reveiving
@retval '>0' --> success, packet lenght(length of all data in buf)
'<0' --> failed
*/
int VR :: receive_pkt(uint8_t *buf, uint16_t timeout)
{
int ret;
ret = receive(buf, 2, timeout);
if(ret != 2){
return -1;
}
if(buf[0] != FRAME_HEAD){
return -2;
}
if(buf[1] < 2){
return -3;
}
ret = receive(buf+2, buf[1], timeout);
if(buf[buf[1]+1] != FRAME_END){
return -4;
}
// DBGBUF(buf, buf[1]+2);
return buf[1]+2;
}
/**
@brief receive data .
@param buf --> return value buffer.
len --> length expect to receive.
timeout --> time of reveiving
@retval number of received bytes, 0 means no data received.
*/
int VR::receive(uint8_t *buf, int len, uint16_t timeout)
{
int read_bytes = 0;
int ret;
unsigned long start_millis;
while (read_bytes < len) {
start_millis = millis();
do {
ret = read();
if (ret >= 0) {
#if 0
uint8_t tmp = ret;
writehex(&tmp, 1);
#endif
break;
}
} while( (millis()- start_millis ) < timeout);
if (ret < 0) {
return read_bytes;
}
buf[read_bytes] = (char)ret;
read_bytes++;
}
return read_bytes;
}