Mavlink bridge for Mbed devices
uart_interface.h@0:28183cc7963f, 2015-11-24 (annotated)
- Committer:
- bhepp
- Date:
- Tue Nov 24 16:41:11 2015 +0000
- Revision:
- 0:28183cc7963f
- Child:
- 6:48b46bcdd5cb
Mavlink bridge for Mbed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bhepp | 0:28183cc7963f | 1 | #pragma once |
bhepp | 0:28183cc7963f | 2 | |
bhepp | 0:28183cc7963f | 3 | #ifdef __MBED__ |
bhepp | 0:28183cc7963f | 4 | #include "mbed.h" |
bhepp | 0:28183cc7963f | 5 | #endif |
bhepp | 0:28183cc7963f | 6 | |
bhepp | 0:28183cc7963f | 7 | namespace ait { |
bhepp | 0:28183cc7963f | 8 | |
bhepp | 0:28183cc7963f | 9 | class UART_Interface { |
bhepp | 0:28183cc7963f | 10 | public: |
bhepp | 0:28183cc7963f | 11 | virtual bool writeChar(uint8_t c) = 0; |
bhepp | 0:28183cc7963f | 12 | virtual bool isCharAvailable() = 0; |
bhepp | 0:28183cc7963f | 13 | virtual uint8_t readChar(bool* err_flag = NULL) = 0; |
bhepp | 0:28183cc7963f | 14 | }; |
bhepp | 0:28183cc7963f | 15 | |
bhepp | 0:28183cc7963f | 16 | #ifdef __MBED__ |
bhepp | 0:28183cc7963f | 17 | class UART_Mbed : public UART_Interface { |
bhepp | 0:28183cc7963f | 18 | Serial _serial; |
bhepp | 0:28183cc7963f | 19 | |
bhepp | 0:28183cc7963f | 20 | public: |
bhepp | 0:28183cc7963f | 21 | UART_Mbed(PinName tx, PinName rx, int baudrate = 115200) |
bhepp | 0:28183cc7963f | 22 | : _serial(tx, rx) { |
bhepp | 0:28183cc7963f | 23 | _serial.baud(baudrate); |
bhepp | 0:28183cc7963f | 24 | } |
bhepp | 0:28183cc7963f | 25 | |
bhepp | 0:28183cc7963f | 26 | virtual bool writeChar(uint8_t c) { |
bhepp | 0:28183cc7963f | 27 | int ret = _serial.putc(c); |
bhepp | 0:28183cc7963f | 28 | if (ret == -1) { |
bhepp | 0:28183cc7963f | 29 | return false; |
bhepp | 0:28183cc7963f | 30 | //throw std::exception("Unable to write on serial port"); |
bhepp | 0:28183cc7963f | 31 | } |
bhepp | 0:28183cc7963f | 32 | return true; |
bhepp | 0:28183cc7963f | 33 | } |
bhepp | 0:28183cc7963f | 34 | |
bhepp | 0:28183cc7963f | 35 | virtual bool isCharAvailable() { |
bhepp | 0:28183cc7963f | 36 | return _serial.readable(); |
bhepp | 0:28183cc7963f | 37 | } |
bhepp | 0:28183cc7963f | 38 | |
bhepp | 0:28183cc7963f | 39 | virtual uint8_t readChar(bool* err_flag = NULL) { |
bhepp | 0:28183cc7963f | 40 | int c = _serial.getc(); |
bhepp | 0:28183cc7963f | 41 | if (err_flag != NULL) { |
bhepp | 0:28183cc7963f | 42 | if (c == -1) |
bhepp | 0:28183cc7963f | 43 | *err_flag = true; |
bhepp | 0:28183cc7963f | 44 | else |
bhepp | 0:28183cc7963f | 45 | *err_flag = false; |
bhepp | 0:28183cc7963f | 46 | } |
bhepp | 0:28183cc7963f | 47 | return static_cast<uint8_t>(c); |
bhepp | 0:28183cc7963f | 48 | } |
bhepp | 0:28183cc7963f | 49 | }; |
bhepp | 0:28183cc7963f | 50 | #endif // __MBED__ |
bhepp | 0:28183cc7963f | 51 | |
bhepp | 0:28183cc7963f | 52 | } |