You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HC05.h 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * HC05.h - interface definitions for the HC05 library
  3. *
  4. * Select hardware or software serial port:
  5. * Define HC05_SOFTWARE_SERIAL to select a SoftwareSerial port, then
  6. * initialize the HC05 class with either two arguments, for a hardware port,
  7. * or four arguments for a software serial port:
  8. *
  9. * HC05(cmdPin, statePin)
  10. * or
  11. * HC05(cmdPin, statePin, rxPin, txPin)
  12. *
  13. * Specify an alternate hardware serial port by changing the
  14. * HC05_HW_SERIAL_PORT define.
  15. *
  16. * Define DEBUG_HC05 to enable debugging messages and use the
  17. * DEBUG_BEGIN() macro in the sketch setup() function.
  18. * By default, debugging messages go to the harware serial port, Serial.
  19. * Change that by defining DEBUG_SW_PORT.
  20. *
  21. */
  22. #ifndef HC05_h
  23. #define HC05_h
  24. #include <inttypes.h>
  25. #include <Stream.h>
  26. #include <SoftwareSerial.h>
  27. /*
  28. * Comment the following define line if you aren't using the State pin or
  29. * if your HC05 does not have such a pin. You still must specify a
  30. * statePin to initialize the HC05 class, but the pin will not be used.
  31. */
  32. #define HC05_STATE_PIN
  33. /*
  34. * This macro must be defined even if you are using a software serial
  35. * port. You can change this to any serial port supported by your
  36. * Arduino (i.e, Serial1, Serial2, etc.)
  37. */
  38. #define HC05_HW_SERIAL_PORT Serial
  39. /*
  40. * Optional macros, define as needed
  41. */
  42. #define HC05_SOFTWARE_SERIAL
  43. #define DEBUG_HC05
  44. //#define DEBUG_SW_PORT swserial(4,5)
  45. #ifdef DEBUG_HC05
  46. #ifdef DEBUG_SW_PORT
  47. #define DEBUG_PORT swserial
  48. #else
  49. #define DEBUG_PORT Serial
  50. #endif
  51. #define DEBUG_BEGIN(baud) DEBUG_PORT.begin(baud)
  52. #define DEBUG_WRITE(...) DEBUG_PORT.write(__VA_ARGS__)
  53. #define DEBUG_PRINT(...) DEBUG_PORT.print(__VA_ARGS__)
  54. #define DEBUG_PRINTLN(...) DEBUG_PORT.println(__VA_ARGS__)
  55. #else
  56. #define DEBUG_BEGIN(baud)
  57. #define DEBUG_WRITE(...)
  58. #define DEBUG_PRINT(...)
  59. #define DEBUG_PRINTLN(...)
  60. #endif // DEBUG_HC05
  61. class HC05 : public Stream
  62. {
  63. public:
  64. HC05(int cmdPin, int statePin);
  65. HC05(int cmdPin, int statePin, uint8_t rx, uint8_t tx);
  66. unsigned long findBaud();
  67. void setBaud(unsigned long baud); // always no parity, one stop bit
  68. void setBaud(unsigned long baud, unsigned long stopbits, unsigned long parity);
  69. // cmd(): 100ms default timeout covers simple commands, but commands
  70. // that manage the connection are likely to take much longer.
  71. int cmd(String cmd, unsigned long timeout=100);
  72. int cmd(const char* cmd, unsigned long timeout=100);
  73. // HC05 cmdMode2 forces 38400, no parity, one stop bit
  74. // Entering cmdMode2 uses a pin to turn the HC05 power on and off
  75. // with the cmdPin high. cmdPin must remain high to stay in
  76. // cmdMode2, so use cmdMode2End() to exit.
  77. void cmdMode2Start(int pwrPin);
  78. void cmdMode2End(void);
  79. #ifdef HC05_STATE_PIN
  80. bool connected(void);
  81. #endif
  82. virtual int available(void);
  83. virtual void begin(unsigned long);
  84. virtual int peek(void);
  85. virtual int read(void);
  86. virtual void flush(void);
  87. virtual size_t write(uint8_t);
  88. using Print::write;
  89. #ifdef HC05_SOFTWARE_SERIAL
  90. SoftwareSerial _btSerial;
  91. #endif
  92. private:
  93. bool cmdMode;
  94. int _cmdPin;
  95. #ifdef HC05_STATE_PIN
  96. int _statePin;
  97. #endif
  98. int _bufsize;
  99. char _buffer[32];
  100. void setCmdPin(bool state);
  101. };
  102. extern HC05 btSerial;
  103. #endif // HC05_h