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

Committer:
syundo0730
Date:
Sun Jan 31 09:11:43 2016 +0000
Revision:
0:35db472ea9e6
Child:
1:e5a32ea3587b
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
syundo0730 0:35db472ea9e6 1 #ifndef _ARDUINO_SERIAL_H_
syundo0730 0:35db472ea9e6 2 #define _ARDUINO_SERIAL_H_
syundo0730 0:35db472ea9e6 3
syundo0730 0:35db472ea9e6 4 #include "mbed.h"
syundo0730 0:35db472ea9e6 5
syundo0730 0:35db472ea9e6 6 enum Format { BIN, OCT, DEC, HEX };
syundo0730 0:35db472ea9e6 7
syundo0730 0:35db472ea9e6 8 class ArduinoSerial
syundo0730 0:35db472ea9e6 9 {
syundo0730 0:35db472ea9e6 10 public:
syundo0730 0:35db472ea9e6 11 ArduinoSerial() : serial(USBTX, USBRX) {}
syundo0730 0:35db472ea9e6 12 ArduinoSerial(PinName tx, PinName rx) : serial(tx, rx) {}
syundo0730 0:35db472ea9e6 13
syundo0730 0:35db472ea9e6 14 private:
syundo0730 0:35db472ea9e6 15 Serial serial;
syundo0730 0:35db472ea9e6 16
syundo0730 0:35db472ea9e6 17 public:
syundo0730 0:35db472ea9e6 18 template <typename T>
syundo0730 0:35db472ea9e6 19 void inline print(T x, Format fmt = BIN) {
syundo0730 0:35db472ea9e6 20 if(fmt == OCT) {
syundo0730 0:35db472ea9e6 21 serial.printf("%o", x);
syundo0730 0:35db472ea9e6 22 } else if (fmt == DEC) {
syundo0730 0:35db472ea9e6 23 serial.printf("%d", x);
syundo0730 0:35db472ea9e6 24 } else if (fmt == HEX) {
syundo0730 0:35db472ea9e6 25 serial.printf("%x", x);
syundo0730 0:35db472ea9e6 26 } else {
syundo0730 0:35db472ea9e6 27 serial.printf("We aren't supporting this format: %d", x);
syundo0730 0:35db472ea9e6 28 }
syundo0730 0:35db472ea9e6 29 }
syundo0730 0:35db472ea9e6 30
syundo0730 0:35db472ea9e6 31 void inline print(const char* x) {
syundo0730 0:35db472ea9e6 32 serial.printf("%s", x);
syundo0730 0:35db472ea9e6 33 }
syundo0730 0:35db472ea9e6 34
syundo0730 0:35db472ea9e6 35 template <typename T>
syundo0730 0:35db472ea9e6 36 void inline println(T x, Format fmt = BIN) {
syundo0730 0:35db472ea9e6 37 ArduinoSerial::print(x, fmt);
syundo0730 0:35db472ea9e6 38 serial.printf("\r\n");
syundo0730 0:35db472ea9e6 39 }
syundo0730 0:35db472ea9e6 40 };
syundo0730 0:35db472ea9e6 41
syundo0730 0:35db472ea9e6 42 #endif /* _ARDUINO_SERIAL_H_ */