Bluetooth.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. if (tempChar == '!')
  35. {
  36. int i = 0;
  37. clearRoute();
  38. while (true)
  39. {
  40. if (rxUart.byteAvailable())
  41. {
  42. tempChar = (char)rxUart.receiveByte();
  43. if(tempChar == '!')
  44. {
  45. route[i] = tempChar;
  46. return 3;
  47. }
  48. route[i] = tempChar;
  49. i ++;
  50. }
  51. }
  52. }
  53. else if ((tempChar == 'a') || (tempChar =='v') || (tempChar == 'l') || (tempChar == 'r') || (tempChar == 's'))
  54. {
  55. romoteControl = tempChar;
  56. return 1;
  57. }
  58. return 0;
  59. }
  60. return 0;
  61. }
  62. public char[] getRoute()
  63. {
  64. return route;
  65. }
  66. public int[] getCoordinates()
  67. {
  68. String[] dataString = new String[40];
  69. int[] dataInt = new int[7];
  70. String coordinate = "";
  71. int StringArrayCount = 0;
  72. int intArrayCount = 0;
  73. for (int i = 0; route.length > i; i ++)
  74. {
  75. if (route[i] != ',' && route[i] != '!')
  76. {
  77. coordinate += String.valueOf(route[i]);
  78. }
  79. if (route[i] == ',')
  80. {
  81. dataString[StringArrayCount] = coordinate;
  82. coordinate = "";
  83. StringArrayCount ++;
  84. }
  85. if (route[i] == '!')
  86. {
  87. i = route.length;
  88. StringArrayCount = 0;
  89. }
  90. }
  91. for (int i = 0; dataString.length > i; i ++)
  92. {
  93. if (dataString[i] != null)
  94. {
  95. dataInt[intArrayCount] = Integer.parseInt(dataString[i]);
  96. intArrayCount ++;
  97. }
  98. if (dataString[i] == null)
  99. {
  100. intArrayCount = 0;
  101. return dataInt;
  102. }
  103. }
  104. }
  105. public int getromoteControl()
  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. }
  126. return value;
  127. }
  128. public void clearRoute()
  129. {
  130. for(int i = 0; i < 40; i++)
  131. {
  132. route[i] = ' ';
  133. }
  134. }
  135. }