library for Bluetooth Shield from Seeed Studio
Fork of BluetoothSerial by
BluetoothSerial.h@0:f56002898ee8, 2014-02-20 (annotated)
- Committer:
- yihui
- Date:
- Thu Feb 20 03:30:43 2014 +0000
- Revision:
- 0:f56002898ee8
initial
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 | |
yihui | 0:f56002898ee8 | 14 | |
yihui | 0:f56002898ee8 | 15 | class BluetoothSerial : public Stream { |
yihui | 0:f56002898ee8 | 16 | public: |
yihui | 0:f56002898ee8 | 17 | BluetoothSerial(PinName tx, PinName rx); |
yihui | 0:f56002898ee8 | 18 | |
yihui | 0:f56002898ee8 | 19 | /** |
yihui | 0:f56002898ee8 | 20 | * Setup bluetooth module(serial port baud rate) |
yihui | 0:f56002898ee8 | 21 | */ |
yihui | 0:f56002898ee8 | 22 | void setup(); |
yihui | 0:f56002898ee8 | 23 | |
yihui | 0:f56002898ee8 | 24 | /** |
yihui | 0:f56002898ee8 | 25 | * Set bluetooth module as a master |
yihui | 0:f56002898ee8 | 26 | * \param name device name |
yihui | 0:f56002898ee8 | 27 | * \param autoc 1: auto-connection, 0 not |
yihui | 0:f56002898ee8 | 28 | */ |
yihui | 0:f56002898ee8 | 29 | void master(const char *name, uint8_t autoc = 0); |
yihui | 0:f56002898ee8 | 30 | |
yihui | 0:f56002898ee8 | 31 | /** |
yihui | 0:f56002898ee8 | 32 | * Set bluetooth module as a slave |
yihui | 0:f56002898ee8 | 33 | * \param name device name |
yihui | 0:f56002898ee8 | 34 | * \param autoc 1: auto-connection, 0 not |
yihui | 0:f56002898ee8 | 35 | * \param oaut 1: permit paired device to connect, 0: not |
yihui | 0:f56002898ee8 | 36 | */ |
yihui | 0:f56002898ee8 | 37 | void slave(const char *name, uint8_t autoc = 0, uint8_t oaut = 1); |
yihui | 0:f56002898ee8 | 38 | |
yihui | 0:f56002898ee8 | 39 | /** |
yihui | 0:f56002898ee8 | 40 | * Inquire bluetooth devices and connect the specified device |
yihui | 0:f56002898ee8 | 41 | */ |
yihui | 0:f56002898ee8 | 42 | int connect(const char *name); |
yihui | 0:f56002898ee8 | 43 | |
yihui | 0:f56002898ee8 | 44 | /** |
yihui | 0:f56002898ee8 | 45 | * Make the bluetooth module inquirable and available to connect, used in slave mode |
yihui | 0:f56002898ee8 | 46 | */ |
yihui | 0:f56002898ee8 | 47 | int connect(); |
yihui | 0:f56002898ee8 | 48 | |
yihui | 0:f56002898ee8 | 49 | int readable() { |
yihui | 0:f56002898ee8 | 50 | return _serial.readable(); |
yihui | 0:f56002898ee8 | 51 | } |
yihui | 0:f56002898ee8 | 52 | |
yihui | 0:f56002898ee8 | 53 | int writeable() { |
yihui | 0:f56002898ee8 | 54 | return _serial.writeable(); |
yihui | 0:f56002898ee8 | 55 | } |
yihui | 0:f56002898ee8 | 56 | |
yihui | 0:f56002898ee8 | 57 | |
yihui | 0:f56002898ee8 | 58 | protected: |
yihui | 0:f56002898ee8 | 59 | virtual int _getc(); |
yihui | 0:f56002898ee8 | 60 | virtual int _putc(int c); |
yihui | 0:f56002898ee8 | 61 | |
yihui | 0:f56002898ee8 | 62 | void clear(); |
yihui | 0:f56002898ee8 | 63 | int readline(uint8_t *buf, int len, uint32_t timeout = 0); |
yihui | 0:f56002898ee8 | 64 | |
yihui | 0:f56002898ee8 | 65 | Serial _serial; |
yihui | 0:f56002898ee8 | 66 | uint8_t _buf[64]; |
yihui | 0:f56002898ee8 | 67 | }; |
yihui | 0:f56002898ee8 | 68 | |
yihui | 0:f56002898ee8 | 69 | #endif // __BLUETOOTH_SERIAL_H__ |