Bluetooth.java 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package boebot;
  2. import stamp.core.*;
  3. public class Bluetooth extends Aansturing
  4. {
  5. final static int SERIAL_RX_PIN = CPU.pin14;
  6. final static int SERIAL_TX_PIN = CPU.pin15;
  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. private char[] route = new char[40];
  10. private int counter = 0;
  11. public void getRoute()
  12. {
  13. while (true)
  14. {
  15. if (rxUart.byteAvailable())
  16. {
  17. route[counter] = (char)rxUart.receiveByte();
  18. counter ++;
  19. }
  20. if (route[counter] == '¿')
  21. {
  22. counter = 0;
  23. givenRoute();
  24. break;
  25. }
  26. if (route[counter] == '¡')
  27. {
  28. counter = 0;
  29. calculateRoute();
  30. break;
  31. }
  32. CPU.delay(100);
  33. }
  34. }
  35. private char[] givenRoute()
  36. {
  37. char[] sendRoute = route;
  38. return sendRoute;
  39. }
  40. private char[] calculateRoute()
  41. {
  42. char[] sendRoute = new char[40];
  43. int charTest = 0;
  44. int addValue = 0;
  45. while (true)
  46. {
  47. if (route[charTest] != ',' && route[charTest] != '!' && route[charTest] != '¡')
  48. {
  49. sendRoute[addValue] = route[charTest];
  50. addValue ++;
  51. }
  52. if (route[charTest] == '¡')
  53. {
  54. break;
  55. }
  56. charTest ++;
  57. }
  58. return sendRoute;
  59. }
  60. public void remoteControl(int pin)
  61. {
  62. while (true)
  63. {
  64. if (rxUart.byteAvailable())
  65. {
  66. char command = (char)rxUart.receiveByte();
  67. boolean IR = false;
  68. switch (command)
  69. {
  70. case 'v':
  71. super.vooruit();
  72. break;
  73. case 'a':
  74. super.achteruit();
  75. break;
  76. case 'l':
  77. super.turnleft();
  78. break;
  79. case 'r':
  80. super.turnrechts();
  81. break;
  82. case 's':
  83. super.stop();
  84. break;
  85. case 'i':
  86. CPU.writePin(CPU.pins[pin], !IR);
  87. break;
  88. }
  89. }
  90. }
  91. }
  92. }