serialport.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Freie Universität Berlin
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
19 #ifndef ARDUINO_SERIAL_H
20 #define ARDUINO_SERIAL_H
21 
22 extern "C" {
23 #include "ringbuffer.h"
24 #include "periph/uart.h"
25 }
26 
30 #define SERIAL_RX_BUFSIZE (64)
31 
36  BIN,
37  OCT,
38  DEC,
39  HEX
40 };
41 
45 class SerialPort {
46 
47 private:
48  uart_t dev;
49  char rx_mem[SERIAL_RX_BUFSIZE];
50  ringbuffer_t rx_buf;
51  template<typename T> size_t _println(T val);
52  template<typename T> size_t _println(T val, SerialFormat format);
53 
54 public:
60  explicit SerialPort(uart_t dev);
61 
74  int available(void);
75 
89  void begin(long speed);
90 
99  void end(void);
100 
116  size_t print(int val);
117 
128  size_t print(int val, SerialFormat format);
129 
145  size_t print(unsigned int val);
146 
157  size_t print(unsigned int val, SerialFormat format);
158 
174  size_t print(long val);
175 
186  size_t print(long val, SerialFormat format);
187 
203  size_t print(unsigned long val);
204 
215  size_t print(unsigned long val, SerialFormat format);
216 
224  size_t print(float val);
225 
236  size_t print(float val, int format);
237 
247  size_t print(char val);
248 
258  size_t print(const char *val);
259 
273  size_t println(int val);
274 
287  size_t println(int val, SerialFormat format);
288 
302  size_t println(unsigned int val);
303 
316  size_t println(unsigned int val, SerialFormat format);
317 
331  size_t println(long val);
332 
345  size_t println(long val, SerialFormat format);
346 
360  size_t println(unsigned long val);
361 
374  size_t println(unsigned long val, SerialFormat format);
375 
387  size_t println(float val);
388 
401  size_t println(float val, int format);
402 
414  size_t println(char val);
415 
427  size_t println(const char *val);
428 
437  size_t println(void);
438 
447  int read(void);
448 
461  int write(int val);
462 
472  int write(const char *str);
473 
483  int write(char *buf, int len);
484 };
485 
486 #endif /* ARDUINO_SERIAL_H */
487 
BIN
@ BIN
format to binary representation
Definition: serialport.hpp:36
SerialPort::available
int available(void)
Get the number of bytes (characters) available for reading from the serial port.
ringbuffer.h
A utility for storing and retrieving byte data using a ring buffer.
SerialPort::write
int write(int val)
Writes binary data to the serial port.
SerialPort::print
size_t print(int val)
Prints data to the serial port as human-readable ASCII text.
SerialFormat
SerialFormat
Formatting options for Serial.print(int, format)
Definition: serialport.hpp:35
SerialPort::println
size_t println(void)
Prints a carriage return character (ASCII 13, or "\r") and a newline character (ASCII 10,...
SerialPort::end
void end(void)
Disables serial communication, allowing the RX and TX pins to be used for general input and output.
uart_t
unsigned int uart_t
Define default UART type identifier.
Definition: uart.h:76
uart.h
Low-level UART peripheral driver interface definition.
OCT
@ OCT
format to octal representation
Definition: serialport.hpp:37
ringbuffer_t
Ringbuffer.
Definition: ringbuffer.h:36
SerialPort
Arduino Serial Interface.
Definition: serialport.hpp:45
HEX
@ HEX
format to hex representation
Definition: serialport.hpp:39
SERIAL_RX_BUFSIZE
#define SERIAL_RX_BUFSIZE
Default RX buffer size - same as the original Arduino...
Definition: serialport.hpp:30
SerialPort::SerialPort
SerialPort(uart_t dev)
Constructor maps the serial port to a RIOT UART device.
DEC
@ DEC
format to decimal representation
Definition: serialport.hpp:38
SerialPort::read
int read(void)
Reads incoming serial data.
SerialPort::begin
void begin(long speed)
Sets the data rate in bits per second (baud) for serial data transmission.