Emulate 「Serial」of Arduino library for mbed. We can easily port arduino's project into mbed by this library.
Dependents: MPU6050 MPU9150 MPU6050 MPU6050 ... more
Revision 1:e5a32ea3587b, committed 2016-01-31
- Comitter:
- syundo0730
- Date:
- Sun Jan 31 13:57:42 2016 +0000
- Parent:
- 0:35db472ea9e6
- Commit message:
- release
Changed in this revision
ArduinoSerial.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 35db472ea9e6 -r e5a32ea3587b ArduinoSerial.h --- a/ArduinoSerial.h Sun Jan 31 09:11:43 2016 +0000 +++ b/ArduinoSerial.h Sun Jan 31 13:57:42 2016 +0000 @@ -3,39 +3,71 @@ #include "mbed.h" -enum Format { BIN, OCT, DEC, HEX }; +enum Format { BIN, OCT, DEC, HEX, NUMBER }; class ArduinoSerial { public: ArduinoSerial() : serial(USBTX, USBRX) {} - ArduinoSerial(PinName tx, PinName rx) : serial(tx, rx) {} + ArduinoSerial(int baudrate) : serial(USBTX, USBRX) { + serial.baud(baudrate); + } + ArduinoSerial(PinName tx, PinName rx, int baudrate = 9600) : serial(tx, rx) { + serial.baud(baudrate); + } private: Serial serial; public: + void begin(int baudrate) { + serial.baud(baudrate); + } + + void inline print(const char* x) { + serial.printf("%s", x); + } + template <typename T> - void inline print(T x, Format fmt = BIN) { - if(fmt == OCT) { + void inline print(T x) { + serial.printf("%f", (float)x); + } + + template <typename T> + void inline print(T x, Format fmt) { + if (fmt == BIN) { + serial.printf("We aren't supporting this format: %d", x); + } else if(fmt == OCT) { serial.printf("%o", x); } else if (fmt == DEC) { serial.printf("%d", x); } else if (fmt == HEX) { serial.printf("%x", x); } else { - serial.printf("We aren't supporting this format: %d", x); + serial.printf("%g", x); } } + + template <typename T> + void inline println(T x) { + ArduinoSerial::print(x); + serial.printf("\r\n"); + } + + template <typename T> + void inline println(T x, Format fmt) { + ArduinoSerial::print(x, fmt); + serial.printf("\r\n"); + } - void inline print(const char* x) { - serial.printf("%s", x); + void inline write(const uint8_t packet) { + serial.putc(packet); } - template <typename T> - void inline println(T x, Format fmt = BIN) { - ArduinoSerial::print(x, fmt); - serial.printf("\r\n"); + void inline write(const uint8_t* packet, uint8_t length) { + for (int i = 0; i < length; ++i) { + serial.putc(packet[i]); + } } };