-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSV_BT.cpp
More file actions
108 lines (96 loc) · 1.76 KB
/
SV_BT.cpp
File metadata and controls
108 lines (96 loc) · 1.76 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
#include <Arduino.h>
#include <SoftwareSerial.h>
SoftwareSerial BTlink(10, 11); // (10 pin RX board -> TX BT), (11 pin TX board -> RX BT)
int waitingForSend = 99;
String toSend = "";
bool slotOpen = false;
void BT_init()
{
BTlink.begin(9600);
}
int get_waitingForSend()
{
return waitingForSend;
}
bool is_slotOpen()
{
return slotOpen;
}
int BT_prepareToSend(String message)
{
if ((message[1]-'0')<waitingForSend)
{
waitingForSend = message[1]-'0';
toSend = message;
return 0;
}
else
{
return -1;
}
}
int BT_Send()
{
if(true == slotOpen)
{
toSend = String('$') + toSend;
toSend = toSend + String('#');
BTlink.println(toSend);
toSend = "";
waitingForSend = 99;
slotOpen = false;
}
}
int BT_Receive(String &message)
{
String inData = "";
String BluetoothData = "";
int ret = -1;
while(BTlink.available()>0)
{
char recieved = BTlink.read();
if( (inData == "" && recieved == '$') || (inData != "") )
{
inData += recieved;
// Process message when new line character is recieved
if (recieved == '#')
{
BluetoothData = inData;
inData = "";
break;
}
}
else if(recieved >= '0' && recieved <= '9')
{
BluetoothData = String(recieved);
break;
}
}
if(BluetoothData[0] == '0')
{
slotOpen = true;
ret = 0;
}if(BluetoothData[0] == '9')
{
ret = 9;
}else if(BluetoothData[0] == '2')
{
ret = 2;
}else if(BluetoothData[0] == '3')
{
ret = 3;
}else if(BluetoothData[1] == '4')
{
message = BluetoothData;
ret = 4;
}else if(BluetoothData[0] == '5')
{
ret = 5;
}else if(BluetoothData[1] == '6')
{
ret = 6;
}
inData = "";
BluetoothData = "";
return ret;
}