mraa  0.9.0
Low Level Skeleton Library for Communication on GNU/Linux platforms
uart.hpp
1 /*
2  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3  * Contributions: Jon Trulson <jtrulson@ics.com>
4  * Contributions: Thomas Ingleby <thomas.c.ingleby@intel.com>
5  * Copyright (c) 2014 - 2015 Intel Corporation.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  */
26 
27 #pragma once
28 
29 #include "uart.h"
30 #include "types.hpp"
31 #include <stdexcept>
32 #include <cstring>
33 
34 namespace mraa
35 {
36 
44 class Uart
45 {
46  public:
53  Uart(int uart)
54  {
55  m_uart = mraa_uart_init(uart);
56 
57  if (m_uart == NULL) {
58  throw std::invalid_argument("Error initialising UART");
59  }
60  }
61 
68  Uart(std::string path)
69  {
70  m_uart = mraa_uart_init_raw(path.c_str());
71 
72  if (m_uart == NULL) {
73  throw std::invalid_argument("Error initialising UART");
74  }
75  }
76 
81  {
82  mraa_uart_stop(m_uart);
83  }
84 
91  std::string
93  {
94  std::string ret_val(mraa_uart_get_dev_path(m_uart));
95  return ret_val;
96  }
97 
105  int
106  read(char* data, int length)
107  {
108  return mraa_uart_read(m_uart, data, (size_t) length);
109  }
110 
118  int
119  write(const char* data, int length)
120  {
121  return mraa_uart_write(m_uart, data, (size_t) length);
122  }
123 
130  std::string
131  readStr(int length)
132  {
133  char* data = (char*) malloc(sizeof(char) * length);
134  int v = mraa_uart_read(m_uart, data, (size_t) length);
135  std::string ret(data, v);
136  free(data);
137  return ret;
138  }
139 
146  int
147  writeStr(std::string data)
148  {
149  // this is data.length() not +1 because we want to avoid the '\0' char
150  return mraa_uart_write(m_uart, data.c_str(), (data.length()));
151  }
152 
159  bool
160  dataAvailable(unsigned int millis = 0)
161  {
162  if (mraa_uart_data_available(m_uart, millis))
163  return true;
164  else
165  return false;
166  }
167 
174  Result
176  {
177  return (Result) mraa_uart_flush(m_uart);
178  }
179 
188  Result
189  setBaudRate(unsigned int baud)
190  {
191  return (Result) mraa_uart_set_baudrate(m_uart, baud);
192  }
193 
204  Result
205  setMode(int bytesize, UartParity parity, int stopbits)
206  {
207  return (Result) mraa_uart_set_mode(m_uart, bytesize, (mraa_uart_parity_t) parity, stopbits);
208  }
209 
217  Result
218  setFlowcontrol(bool xonxoff, bool rtscts)
219  {
220  return (Result) mraa_uart_set_flowcontrol(m_uart, xonxoff, rtscts);
221  }
222 
232  Result
233  setTimeout(int read, int write, int interchar)
234  {
235  return (Result) mraa_uart_set_timeout(m_uart, read, write, interchar);
236  }
237 
238  private:
239  mraa_uart_context m_uart;
240 };
241 }
Result
Definition: types.hpp:185
Result setBaudRate(unsigned int baud)
Definition: uart.hpp:189
UART module.
std::string readStr(int length)
Definition: uart.hpp:131
int mraa_uart_write(mraa_uart_context dev, const char *buf, size_t length)
int read(char *data, int length)
Definition: uart.hpp:106
bool dataAvailable(unsigned int millis=0)
Definition: uart.hpp:160
Uart(int uart)
Definition: uart.hpp:53
Uart(std::string path)
Definition: uart.hpp:68
mraa_result_t mraa_uart_stop(mraa_uart_context dev)
Result flush()
Definition: uart.hpp:175
API to UART (enabling only)
Definition: uart.hpp:44
mraa_result_t mraa_uart_set_flowcontrol(mraa_uart_context dev, mraa_boolean_t xonxoff, mraa_boolean_t rtscts)
~Uart()
Definition: uart.hpp:80
mraa_result_t mraa_uart_flush(mraa_uart_context dev)
std::string getDevicePath()
Definition: uart.hpp:92
Result setFlowcontrol(bool xonxoff, bool rtscts)
Definition: uart.hpp:218
mraa_uart_context mraa_uart_init_raw(const char *path)
Result setMode(int bytesize, UartParity parity, int stopbits)
Definition: uart.hpp:205
mraa_boolean_t mraa_uart_data_available(mraa_uart_context dev, unsigned int millis)
const char * mraa_uart_get_dev_path(mraa_uart_context dev)
int writeStr(std::string data)
Definition: uart.hpp:147
mraa_result_t mraa_uart_set_mode(mraa_uart_context dev, int bytesize, mraa_uart_parity_t parity, int stopbits)
int mraa_uart_read(mraa_uart_context dev, char *buf, size_t length)
mraa_uart_context mraa_uart_init(int uart)
Result setTimeout(int read, int write, int interchar)
Definition: uart.hpp:233
Definition: aio.hpp:31
int write(const char *data, int length)
Definition: uart.hpp:119
mraa_result_t mraa_uart_set_timeout(mraa_uart_context dev, int read, int write, int interchar)
mraa_result_t mraa_uart_set_baudrate(mraa_uart_context dev, unsigned int baud)