-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbt-serial.cpp
148 lines (139 loc) · 3.92 KB
/
bt-serial.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
/********************************************************
* #######################################################
*
* Bluetooth Serial - bt-serial.cpp
* dekitarpg@gmail.com
*
* #######################################################
********************************************************/
/**
* Included Libraries:..
*/
#include "bt-serial.h"
/**
* BTSerial::begin(baud_rate);
* @param baud_rate {int}
* Setup the Bluetooth serial module initializing
* communication rate to 'baud_rate'.
*/
void BTSerial::begin(int brate, bool iprint) {
baud_rate = brate;
buffer_index = 0;
Serial.begin(baud_rate);
bts.begin(baud_rate);
if (iprint) {
Serial.println("BTSerial Started!");
bts.println("BTS says hi!");
}
}
/**
* BTSerial::update();
* @return hasCommand() {bool}
* Updates to read the serial data for the bluetooth connection.
* Should be called at least once before using getCommand();
* after each command has been read, make sure to call update();
* again before calling getCommand(); again.
*/
bool BTSerial::update() {
char this_char;
//if (Serial.available()) {
// this_char = Serial.read();
// bts.print(this_char);
//}
if (bts.available()) {
while (bts.available() > 0){
// manage overflow:..
if (buffer_index+1 > DEBUFFER_LIMIT) {
resetDebuffer("OVERFLOW");
break;
}
// no overflow, lets continue:..
// get next character of data
this_char = bts.read();
//Serial.println(this_char);
// if char is terminator, then flag command as ready and break.
if (this_char == ';') {
//char msg[] = "message received: ";
buffer_index = 0;
comready = true;
break;
} else { // add new char to buffer string.
debuffer[buffer_index] = this_char;
debuffer[++buffer_index] = '\0';
}
}
}
// return bool for if we have command or not
return hasCommand();
}
/**
* BTSerial::resetDebuffer()
* @return {bool} based on if there is a command ready.
*/
void BTSerial::resetDebuffer(char *reason) {
char msg[] = "BUFFER RESET: ";
buffer_index = 0; debuffer[0] = '\0';
//Serial.print(msg); Serial.println(reason);
//bts.print(msg); bts.println(reason);
}
/**
* BTSerial::hasCommand()
* @return {bool} based on if there is a command ready.
*/
bool BTSerial::hasCommand() {
return comready;
}
/**
* BTSerial::flush()
* Flushes the BTSerial buffer
*/
void BTSerial::flush() {
while(bts.available() > 0) { bts.read(); }
//Serial.println("Flushed!");
}
/**
* BTSerial::getCommand()
* @return {BTSCommand} containing n and v for command.
* This function splits the buffer string to seperate
* the command n(name) and v(value).
* It then resets the command ready flag to allow for
* the next command to be processed.
*/
BTSCommand BTSerial::getCommand() {
int valStart = 0;
for (int i=0; debuffer[i] != '\0'; i++) {
if (debuffer[i] == ':') {
valStart = i+1;
}
if (valStart > 0) {
command.cv[i-valStart] = debuffer[i];
command.cv[i-valStart+1] = '\0';
} else {
command.cn[i] = debuffer[i];
command.cn[i+1] = '\0';
}
}
resetDebuffer("has command!");
//Serial.print(debuffer);
comready = false;
return command;
}
/**
* BTSerial::print()
* BTSerial::println()
* Similar to Serial.print/println functions,
* but for the BTSerial communication line.
*/
void BTSerial::print(char* str) {
bts.print(str);
}
void BTSerial::println(char* str) {
bts.println(str);
}
/********************************************************
* #######################################################
*
* End of Code
*
* #######################################################
********************************************************/