Bluetooth.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package boebot;
  2. import stamp.core.*;
  3. public class Bluetooth
  4. {
  5. final static int SERIAL_RX_PIN = CPU.pin2;
  6. final static int SERIAL_TX_PIN = CPU.pin0;
  7. static Uart rxUart = new Uart(Uart.dirReceive, SERIAL_RX_PIN, Uart.dontInvert, Uart.speed9600, Uart.stop1);
  8. static Uart txUart = new Uart(Uart.dirTransmit, SERIAL_TX_PIN,Uart.dontInvert, Uart.speed9600, Uart.stop1);
  9. char tempChar;
  10. char[] route = new char[40];
  11. public char romoteControl = ' ';
  12. int speed;
  13. public int checkBt(){
  14. if (rxUart.byteAvailable()){
  15. tempChar = (char)rxUart.receiveByte();
  16. if (tempChar == '?'){
  17. int i = 0;
  18. clearRoute();
  19. while (true){
  20. if (rxUart.byteAvailable()){
  21. tempChar = (char)rxUart.receiveByte();
  22. if(tempChar == '?'){
  23. return 2;
  24. }
  25. route[i] = tempChar;
  26. i ++;
  27. }
  28. }
  29. }else if (tempChar == '!'){
  30. int i = 0;
  31. clearRoute();
  32. while (true){
  33. if (rxUart.byteAvailable()){
  34. tempChar = (char)rxUart.receiveByte();
  35. if(tempChar == '!'){
  36. route[i] = tempChar;
  37. return 3;
  38. }
  39. route[i] = tempChar;
  40. i ++;
  41. }
  42. }
  43. }else if ((tempChar == 'a') || (tempChar =='v') || (tempChar == 'l') || (tempChar == 'r') || (tempChar == 's')){
  44. romoteControl = tempChar;
  45. return 1;
  46. }else if ((tempChar == 'p') || (tempChar == 'h')){
  47. romoteControl = tempChar;
  48. return 5;
  49. } else if (tempChar == 'f'){
  50. while (true){
  51. if (rxUart.byteAvailable()){
  52. speed = Integer.parseInt(new String(new char[]{(char)rxUart.receiveByte()}));
  53. return 4;
  54. }
  55. } }
  56. }
  57. return 0;
  58. }
  59. public char[] getRoute()
  60. {
  61. return route;
  62. }
  63. public int getSpeed(){
  64. if(speed >= 0 && speed <= 9){
  65. return (((speed + 1) * 10));
  66. }
  67. return 0;
  68. }
  69. public void sendChar(char c){
  70. txUart.sendByte(c);
  71. }
  72. public int[] getCoordinates()
  73. {
  74. String[] dataString = new String[40];
  75. int[] dataInt = new int[7];
  76. String coordinate = "";
  77. int StringArrayCount = 0;
  78. int intArrayCount = 0;
  79. for (int i = 0; route.length > i; i ++)
  80. {
  81. if (route[i] != ',' && route[i] != '!')
  82. {
  83. coordinate += new String(new char[]{route[i]});
  84. }
  85. if (route[i] == ',')
  86. {
  87. dataString[StringArrayCount] = coordinate;
  88. coordinate = "";
  89. StringArrayCount ++;
  90. }
  91. if (route[i] == '!')
  92. {
  93. i = route.length;
  94. StringArrayCount = 0;
  95. }
  96. }
  97. for (int i = 0; dataString.length > i; i ++)
  98. {
  99. if (dataString[i] != null)
  100. {
  101. dataInt[intArrayCount] = Integer.parseInt(dataString[i]);
  102. intArrayCount ++;
  103. }
  104. if (dataString[i] == null)
  105. {
  106. intArrayCount = 0;
  107. return dataInt;
  108. }
  109. }
  110. return dataInt;
  111. }
  112. public char getChar(){
  113. return romoteControl;
  114. }
  115. public int getRemoteControl()
  116. {
  117. int value = 4;
  118. switch (romoteControl)
  119. {
  120. case 'v':
  121. value = 1;
  122. break;
  123. case 'a':
  124. value = 7;
  125. break;
  126. case 's':
  127. value = 4;
  128. break;
  129. case 'r':
  130. value = 5;
  131. break;
  132. case 'l':
  133. value = 3;
  134. break;
  135. default:
  136. value = 4;
  137. break;
  138. }
  139. return value;
  140. }
  141. public void clearRoute()
  142. {
  143. for(int i = 0; i < 40; i++)
  144. {
  145. route[i] = ' ';
  146. }
  147. }
  148. }