doubleSerial.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "doubleSerial.h"
  2. // Constructor
  3. doubleSerial::doubleSerial() {
  4. this->useBT = false;
  5. this->BTName = "ActuBoard BLE";
  6. }
  7. doubleSerial::doubleSerial(const char *BT_Name) {
  8. this->useBT = false;
  9. this->BTName = BT_Name;
  10. }
  11. /**
  12. * select which serial is used to communicate
  13. *
  14. */
  15. void doubleSerial::selectSerial(boolean useBluetooth) {
  16. if (useBluetooth) {
  17. Serial.end();
  18. SerialBT.begin(this->BTName); //Bluetooth device name
  19. SerialBT.flush();
  20. } else {
  21. SerialBT.end();
  22. Serial.begin(115200, SERIAL_8N1, -1, -1, false, 1100UL);
  23. Serial.flush();
  24. }
  25. this->useBT = useBluetooth;
  26. }
  27. /**
  28. * read from selected Serial if available
  29. * will return true if a character was read
  30. */
  31. boolean doubleSerial::readSerial(uint8_t *data) {
  32. //check if we have something to read
  33. if (this->useBT) {
  34. if (SerialBT.available()){
  35. *data = SerialBT.read();
  36. return true;
  37. }
  38. } else {
  39. if (Serial.available()){
  40. *data = Serial.read();
  41. return true;
  42. }
  43. }
  44. return false;
  45. }
  46. /**
  47. * write one char to the selected Serial
  48. */
  49. void doubleSerial::writeSerial(const char c) {
  50. if (this->useBT) {
  51. SerialBT.write(c);
  52. } else {
  53. Serial.write(c);
  54. }
  55. }
  56. /**
  57. * write string char to the selected Serial
  58. */
  59. void doubleSerial::writeSerial(const char *c) {
  60. while(*c != 0) {
  61. this->writeSerial(*c);
  62. c++;
  63. }
  64. }
  65. /**
  66. * return of there is a client on the other end of the serial console
  67. * in case of uart serial it is always true, bluetooth depends.
  68. */
  69. boolean doubleSerial::hasClient(void) {
  70. if (this->useBT) {
  71. return SerialBT.hasClient();
  72. } else {
  73. return true;
  74. }
  75. }
  76. /**
  77. * return true if currently bluetooth is used for communication
  78. */
  79. boolean doubleSerial::isBT(void) {
  80. return this->useBT;
  81. }