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 13:57:42 2016 +0000
Revision:
1:e5a32ea3587b
Parent:
0:35db472ea9e6
release

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 1:e5a32ea3587b 6 enum Format { BIN, OCT, DEC, HEX, NUMBER };
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 1:e5a32ea3587b 12 ArduinoSerial(int baudrate) : serial(USBTX, USBRX) {
syundo0730 1:e5a32ea3587b 13 serial.baud(baudrate);
syundo0730 1:e5a32ea3587b 14 }
syundo0730 1:e5a32ea3587b 15 ArduinoSerial(PinName tx, PinName rx, int baudrate = 9600) : serial(tx, rx) {
syundo0730 1:e5a32ea3587b 16 serial.baud(baudrate);
syundo0730 1:e5a32ea3587b 17 }
syundo0730 0:35db472ea9e6 18
syundo0730 0:35db472ea9e6 19 private:
syundo0730 0:35db472ea9e6 20 Serial serial;
syundo0730 0:35db472ea9e6 21
syundo0730 0:35db472ea9e6 22 public:
syundo0730 1:e5a32ea3587b 23 void begin(int baudrate) {
syundo0730 1:e5a32ea3587b 24 serial.baud(baudrate);
syundo0730 1:e5a32ea3587b 25 }
syundo0730 1:e5a32ea3587b 26
syundo0730 1:e5a32ea3587b 27 void inline print(const char* x) {
syundo0730 1:e5a32ea3587b 28 serial.printf("%s", x);
syundo0730 1:e5a32ea3587b 29 }
syundo0730 1:e5a32ea3587b 30
syundo0730 0:35db472ea9e6 31 template <typename T>
syundo0730 1:e5a32ea3587b 32 void inline print(T x) {
syundo0730 1:e5a32ea3587b 33 serial.printf("%f", (float)x);
syundo0730 1:e5a32ea3587b 34 }
syundo0730 1:e5a32ea3587b 35
syundo0730 1:e5a32ea3587b 36 template <typename T>
syundo0730 1:e5a32ea3587b 37 void inline print(T x, Format fmt) {
syundo0730 1:e5a32ea3587b 38 if (fmt == BIN) {
syundo0730 1:e5a32ea3587b 39 serial.printf("We aren't supporting this format: %d", x);
syundo0730 1:e5a32ea3587b 40 } else if(fmt == OCT) {
syundo0730 0:35db472ea9e6 41 serial.printf("%o", x);
syundo0730 0:35db472ea9e6 42 } else if (fmt == DEC) {
syundo0730 0:35db472ea9e6 43 serial.printf("%d", x);
syundo0730 0:35db472ea9e6 44 } else if (fmt == HEX) {
syundo0730 0:35db472ea9e6 45 serial.printf("%x", x);
syundo0730 0:35db472ea9e6 46 } else {
syundo0730 1:e5a32ea3587b 47 serial.printf("%g", x);
syundo0730 0:35db472ea9e6 48 }
syundo0730 0:35db472ea9e6 49 }
syundo0730 1:e5a32ea3587b 50
syundo0730 1:e5a32ea3587b 51 template <typename T>
syundo0730 1:e5a32ea3587b 52 void inline println(T x) {
syundo0730 1:e5a32ea3587b 53 ArduinoSerial::print(x);
syundo0730 1:e5a32ea3587b 54 serial.printf("\r\n");
syundo0730 1:e5a32ea3587b 55 }
syundo0730 1:e5a32ea3587b 56
syundo0730 1:e5a32ea3587b 57 template <typename T>
syundo0730 1:e5a32ea3587b 58 void inline println(T x, Format fmt) {
syundo0730 1:e5a32ea3587b 59 ArduinoSerial::print(x, fmt);
syundo0730 1:e5a32ea3587b 60 serial.printf("\r\n");
syundo0730 1:e5a32ea3587b 61 }
syundo0730 0:35db472ea9e6 62
syundo0730 1:e5a32ea3587b 63 void inline write(const uint8_t packet) {
syundo0730 1:e5a32ea3587b 64 serial.putc(packet);
syundo0730 0:35db472ea9e6 65 }
syundo0730 0:35db472ea9e6 66
syundo0730 1:e5a32ea3587b 67 void inline write(const uint8_t* packet, uint8_t length) {
syundo0730 1:e5a32ea3587b 68 for (int i = 0; i < length; ++i) {
syundo0730 1:e5a32ea3587b 69 serial.putc(packet[i]);
syundo0730 1:e5a32ea3587b 70 }
syundo0730 0:35db472ea9e6 71 }
syundo0730 0:35db472ea9e6 72 };
syundo0730 0:35db472ea9e6 73
syundo0730 0:35db472ea9e6 74 #endif /* _ARDUINO_SERIAL_H_ */