-
Notifications
You must be signed in to change notification settings - Fork 1
/
voice_command_android.h
56 lines (46 loc) · 1 KB
/
voice_command_android.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
#ifndef voice_command_android_H
#define voice_command_android_H
#include <Arduino.h>
#include <SoftwareSerial.h>
/*
Voice Command Android for Arduino.
*/
class VoiceCommandItem{
public :
String val;
void (*callback)();
};
class VoiceCommand{
VoiceCommandItem *item;
SoftwareSerial *bt;
int _pos = 0;
int _maxitem;
public :
VoiceCommand(int maxitem, int btRx, int bttx){
item = new VoiceCommandItem[maxitem];
_maxitem = maxitem;
bt = new SoftwareSerial(bttx,btRx);
}
void begin(int baudrate){
bt->begin(baudrate);
}
void regCommand(void (*callbackIn)(),String val){
item[_pos].callback = callbackIn;
item[_pos].val = val;
_pos++;
}
void call(){
String val = "";
while(bt->available()){
val += (char) bt->read();
}
val.trim();
if(val == "") return;
Serial.println(val);
for(int i=0;i < _maxitem;i++){
if(item[i].val == "") continue;
if(item[i].val == val) item[i].callback();
}
}
};
#endif