-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBluetooth.java
More file actions
166 lines (148 loc) · 4.49 KB
/
Bluetooth.java
File metadata and controls
166 lines (148 loc) · 4.49 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
package boebot;
import stamp.core.*;
public class Bluetooth
{
final static int SERIAL_RX_PIN = CPU.pin2;
final static int SERIAL_TX_PIN = CPU.pin0;
static Uart rxUart = new Uart(Uart.dirReceive, SERIAL_RX_PIN, Uart.dontInvert, Uart.speed9600, Uart.stop1);
static Uart txUart = new Uart(Uart.dirTransmit, SERIAL_TX_PIN,Uart.dontInvert, Uart.speed9600, Uart.stop1);
char tempChar;
char[] route = new char[40];
public char romoteControl = ' ';
int speed;
public int checkBt(){
if (rxUart.byteAvailable()){
tempChar = (char)rxUart.receiveByte();
if (tempChar == '?'){
int i = 0;
clearRoute();
while (true){
if (rxUart.byteAvailable()){
tempChar = (char)rxUart.receiveByte();
if(tempChar == '?'){
return 2;
}
route[i] = tempChar;
i ++;
}
}
}else if (tempChar == '!'){
int i = 0;
clearRoute();
while (true){
if (rxUart.byteAvailable()){
tempChar = (char)rxUart.receiveByte();
if(tempChar == '!'){
route[i] = tempChar;
return 3;
}
route[i] = tempChar;
i ++;
}
}
}else if ((tempChar == 'a') || (tempChar =='v') || (tempChar == 'l') || (tempChar == 'r') || (tempChar == 's')){
romoteControl = tempChar;
return 1;
}else if ((tempChar == 'p') || (tempChar == 'h')){
romoteControl = tempChar;
return 5;
} else if (tempChar == 'f'){
while (true){
if (rxUart.byteAvailable()){
speed = Integer.parseInt(new String(new char[]{(char)rxUart.receiveByte()}));
return 4;
}
} }
}
return 0;
}
public char[] getRoute()
{
return route;
}
public int getSpeed(){
if(speed >= 0 && speed <= 9){
return (((speed + 1) * 10));
}
return 0;
}
public void sendChar(char c){
txUart.sendByte(c);
}
public int[] getCoordinates()
{
String[] dataString = new String[40];
int[] dataInt = new int[7];
String coordinate = "";
int StringArrayCount = 0;
int intArrayCount = 0;
for (int i = 0; route.length > i; i ++)
{
if (route[i] != ',' && route[i] != '!')
{
coordinate += new String(new char[]{route[i]});
}
if (route[i] == ',')
{
dataString[StringArrayCount] = coordinate;
coordinate = "";
StringArrayCount ++;
}
if (route[i] == '!')
{
i = route.length;
StringArrayCount = 0;
}
}
for (int i = 0; dataString.length > i; i ++)
{
if (dataString[i] != null)
{
dataInt[intArrayCount] = Integer.parseInt(dataString[i]);
intArrayCount ++;
}
if (dataString[i] == null)
{
intArrayCount = 0;
return dataInt;
}
}
return dataInt;
}
public char getChar(){
return romoteControl;
}
public int getRemoteControl()
{
int value = 4;
switch (romoteControl)
{
case 'v':
value = 1;
break;
case 'a':
value = 7;
break;
case 's':
value = 4;
break;
case 'r':
value = 5;
break;
case 'l':
value = 3;
break;
default:
value = 4;
break;
}
return value;
}
public void clearRoute()
{
for(int i = 0; i < 40; i++)
{
route[i] = ' ';
}
}
}