Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Shield_Seeed_Bluetooth
Fork of BluetoothSerial by
BluetoothSerial.h@1:cf4d7779d9d6, 2014-07-25 (annotated)
- Committer:
- screamer
- Date:
- Fri Jul 25 10:25:45 2014 +0000
- Revision:
- 1:cf4d7779d9d6
- Parent:
- 0:f56002898ee8
Add param to setup for baud speed;; Add pin() method to change the pin;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| yihui | 0:f56002898ee8 | 1 | /** |
| yihui | 0:f56002898ee8 | 2 | * The library is for Bluetooth Shield from Seeed Studio |
| yihui | 0:f56002898ee8 | 3 | */ |
| yihui | 0:f56002898ee8 | 4 | |
| yihui | 0:f56002898ee8 | 5 | #ifndef __BLUETOOTH_SERIAL_H__ |
| yihui | 0:f56002898ee8 | 6 | #define __BLUETOOTH_SERIAL_H__ |
| yihui | 0:f56002898ee8 | 7 | |
| yihui | 0:f56002898ee8 | 8 | #include "mbed.h" |
| yihui | 0:f56002898ee8 | 9 | |
| yihui | 0:f56002898ee8 | 10 | #define BLUETOOTH_SERIAL_DEFAULT_BAUD 38400 |
| yihui | 0:f56002898ee8 | 11 | #define BLUETOOTH_SERIAL_TIMEOUT 10000 |
| yihui | 0:f56002898ee8 | 12 | #define BLUETOOTH_SERIAL_EOL "\r\n" |
| yihui | 0:f56002898ee8 | 13 | |
| screamer | 1:cf4d7779d9d6 | 14 | /** |
| screamer | 1:cf4d7779d9d6 | 15 | * The BluetoothSerial class |
| screamer | 1:cf4d7779d9d6 | 16 | */ |
| yihui | 0:f56002898ee8 | 17 | class BluetoothSerial : public Stream { |
| yihui | 0:f56002898ee8 | 18 | public: |
| yihui | 0:f56002898ee8 | 19 | BluetoothSerial(PinName tx, PinName rx); |
| yihui | 0:f56002898ee8 | 20 | |
| yihui | 0:f56002898ee8 | 21 | /** |
| yihui | 0:f56002898ee8 | 22 | * Setup bluetooth module(serial port baud rate) |
| yihui | 0:f56002898ee8 | 23 | */ |
| yihui | 0:f56002898ee8 | 24 | void setup(); |
| screamer | 1:cf4d7779d9d6 | 25 | |
| screamer | 1:cf4d7779d9d6 | 26 | /** |
| screamer | 1:cf4d7779d9d6 | 27 | * Setup bluetooth module(serial port baud rate) |
| screamer | 1:cf4d7779d9d6 | 28 | * \param baud baud rate |
| screamer | 1:cf4d7779d9d6 | 29 | */ |
| screamer | 1:cf4d7779d9d6 | 30 | void setup(int baud); |
| screamer | 1:cf4d7779d9d6 | 31 | |
| yihui | 0:f56002898ee8 | 32 | /** |
| yihui | 0:f56002898ee8 | 33 | * Set bluetooth module as a master |
| yihui | 0:f56002898ee8 | 34 | * \param name device name |
| yihui | 0:f56002898ee8 | 35 | * \param autoc 1: auto-connection, 0 not |
| yihui | 0:f56002898ee8 | 36 | */ |
| yihui | 0:f56002898ee8 | 37 | void master(const char *name, uint8_t autoc = 0); |
| yihui | 0:f56002898ee8 | 38 | |
| yihui | 0:f56002898ee8 | 39 | /** |
| yihui | 0:f56002898ee8 | 40 | * Set bluetooth module as a slave |
| yihui | 0:f56002898ee8 | 41 | * \param name device name |
| yihui | 0:f56002898ee8 | 42 | * \param autoc 1: auto-connection, 0 not |
| yihui | 0:f56002898ee8 | 43 | * \param oaut 1: permit paired device to connect, 0: not |
| yihui | 0:f56002898ee8 | 44 | */ |
| yihui | 0:f56002898ee8 | 45 | void slave(const char *name, uint8_t autoc = 0, uint8_t oaut = 1); |
| screamer | 1:cf4d7779d9d6 | 46 | |
| screamer | 1:cf4d7779d9d6 | 47 | /** |
| screamer | 1:cf4d7779d9d6 | 48 | * Set pin for the bluetooth connection |
| screamer | 1:cf4d7779d9d6 | 49 | * \param pin pin string |
| screamer | 1:cf4d7779d9d6 | 50 | */ |
| screamer | 1:cf4d7779d9d6 | 51 | void pin(int pin = 0); |
| screamer | 1:cf4d7779d9d6 | 52 | |
| screamer | 1:cf4d7779d9d6 | 53 | /** |
| screamer | 1:cf4d7779d9d6 | 54 | * Set pin for the bluetooth connection |
| screamer | 1:cf4d7779d9d6 | 55 | * \param pin pin string |
| screamer | 1:cf4d7779d9d6 | 56 | */ |
| screamer | 1:cf4d7779d9d6 | 57 | void pin(const char *pin); |
| screamer | 1:cf4d7779d9d6 | 58 | |
| yihui | 0:f56002898ee8 | 59 | |
| yihui | 0:f56002898ee8 | 60 | /** |
| yihui | 0:f56002898ee8 | 61 | * Inquire bluetooth devices and connect the specified device |
| yihui | 0:f56002898ee8 | 62 | */ |
| yihui | 0:f56002898ee8 | 63 | int connect(const char *name); |
| yihui | 0:f56002898ee8 | 64 | |
| yihui | 0:f56002898ee8 | 65 | /** |
| yihui | 0:f56002898ee8 | 66 | * Make the bluetooth module inquirable and available to connect, used in slave mode |
| yihui | 0:f56002898ee8 | 67 | */ |
| yihui | 0:f56002898ee8 | 68 | int connect(); |
| yihui | 0:f56002898ee8 | 69 | |
| yihui | 0:f56002898ee8 | 70 | int readable() { |
| yihui | 0:f56002898ee8 | 71 | return _serial.readable(); |
| yihui | 0:f56002898ee8 | 72 | } |
| yihui | 0:f56002898ee8 | 73 | |
| yihui | 0:f56002898ee8 | 74 | int writeable() { |
| yihui | 0:f56002898ee8 | 75 | return _serial.writeable(); |
| yihui | 0:f56002898ee8 | 76 | } |
| yihui | 0:f56002898ee8 | 77 | |
| yihui | 0:f56002898ee8 | 78 | |
| yihui | 0:f56002898ee8 | 79 | protected: |
| yihui | 0:f56002898ee8 | 80 | virtual int _getc(); |
| yihui | 0:f56002898ee8 | 81 | virtual int _putc(int c); |
| yihui | 0:f56002898ee8 | 82 | |
| yihui | 0:f56002898ee8 | 83 | void clear(); |
| yihui | 0:f56002898ee8 | 84 | int readline(uint8_t *buf, int len, uint32_t timeout = 0); |
| yihui | 0:f56002898ee8 | 85 | |
| yihui | 0:f56002898ee8 | 86 | Serial _serial; |
| yihui | 0:f56002898ee8 | 87 | uint8_t _buf[64]; |
| yihui | 0:f56002898ee8 | 88 | }; |
| yihui | 0:f56002898ee8 | 89 | |
| yihui | 0:f56002898ee8 | 90 | #endif // __BLUETOOTH_SERIAL_H__ |
