Bluetooth.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. char romoteControl = ' ';
  12. public int checkBt()
  13. {
  14. if (rxUart.byteAvailable())
  15. {
  16. tempChar = (char)rxUart.receiveByte();
  17. if (tempChar == '?')
  18. {
  19. int i = 0;
  20. clearRoute();
  21. while (true)
  22. {
  23. if (rxUart.byteAvailable())
  24. {
  25. tempChar = (char)rxUart.receiveByte();
  26. if(tempChar == '?')
  27. {
  28. return 2;
  29. }
  30. route[i] = tempChar;
  31. i ++;
  32. }
  33. }
  34. }
  35. }
  36. else if (tempChar == '!')
  37. {
  38. int i = 0;
  39. clearRoute();
  40. while (true)
  41. {
  42. if (rxUart.byteAvailable())
  43. {
  44. tempChar = (char)rxUart.receiveByte();
  45. if(tempChar == '!')
  46. {
  47. route[i] = tempChar;
  48. return 3;
  49. }
  50. route[i] = tempChar;
  51. i ++;
  52. }
  53. }
  54. }else if ((tempChar == 'a') || (tempChar =='v') || (tempChar == 'l') || (tempChar == 'r') || (tempChar == 's'))
  55. {
  56. romoteControl = tempChar;
  57. return 1;
  58. }
  59. return 0;
  60. }
  61. public char[] getRoute()
  62. {
  63. return route;
  64. }
  65. public int[] getCoordinates()
  66. {
  67. String[] dataString = new String[40];
  68. int[] dataInt = new int[7];
  69. String coordinate = "";
  70. int StringArrayCount = 0;
  71. int intArrayCount = 0;
  72. for (int i = 0; route.length > i; i ++)
  73. {
  74. if (route[i] != ',' && route[i] != '!')
  75. {
  76. coordinate += new String(new char[]{route[i]});
  77. }
  78. if (route[i] == ',')
  79. {
  80. dataString[StringArrayCount] = coordinate;
  81. coordinate = "";
  82. StringArrayCount ++;
  83. }
  84. if (route[i] == '!')
  85. {
  86. i = route.length;
  87. StringArrayCount = 0;
  88. }
  89. }
  90. for (int i = 0; dataString.length > i; i ++)
  91. {
  92. if (dataString[i] != null)
  93. {
  94. dataInt[intArrayCount] = Integer.parseInt(dataString[i]);
  95. intArrayCount ++;
  96. }
  97. if (dataString[i] == null)
  98. {
  99. intArrayCount = 0;
  100. return dataInt;
  101. }
  102. }
  103. return dataInt;
  104. }
  105. public int getRemoteControl()
  106. {
  107. int value = 4;
  108. switch (romoteControl)
  109. {
  110. case 'v':
  111. value = 1;
  112. break;
  113. case 'a':
  114. value = 7;
  115. break;
  116. case 's':
  117. value = 4;
  118. break;
  119. case 'r':
  120. value = 5;
  121. break;
  122. case 'l':
  123. value = 3;
  124. break;
  125. default:
  126. value = 4;
  127. break;
  128. }
  129. return value;
  130. }
  131. public void clearRoute()
  132. {
  133. for(int i = 0; i < 40; i++)
  134. {
  135. route[i] = ' ';
  136. }
  137. }
  138. }