mraa  0.7.1
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 <stdexcept>
31 #include <cstring>
32 
33 namespace mraa
34 {
35 
43 class Uart
44 {
45  public:
52  Uart(int uart)
53  {
54  m_uart = mraa_uart_init(uart);
55 
56  if (m_uart == NULL) {
57  throw std::invalid_argument("Error initialising UART");
58  }
59  }
60 
67  Uart(std::string path)
68  {
69  m_uart = mraa_uart_init_raw(path.c_str());
70 
71  if (m_uart == NULL) {
72  throw std::invalid_argument("Error initialising UART");
73  }
74  }
75 
80  {
81  mraa_uart_stop(m_uart);
82  }
83 
90  std::string
92  {
93  std::string ret_val(mraa_uart_get_dev_path(m_uart));
94  return ret_val;
95  }
96 
104  int
105  read(char* data, int length)
106  {
107  return mraa_uart_read(m_uart, data, (size_t) length);
108  }
109 
117  int
118  write(const char* data, int length)
119  {
120  return mraa_uart_write(m_uart, data, (size_t) length);
121  }
122 
129  std::string
130  readStr(int length)
131  {
132  char* data = (char*) malloc(sizeof(char) * length);
133  int v = mraa_uart_read(m_uart, data, (size_t) length);
134  std::string ret(data, v);
135  free(data);
136  return ret;
137  }
138 
145  int
146  writeStr(std::string data)
147  {
148  // this is data.length() not +1 because we want to avoid the '\0' char
149  return mraa_uart_write(m_uart, data.c_str(), (data.length()));
150  }
151 
158  bool
159  dataAvailable(unsigned int millis = 0)
160  {
161  if (mraa_uart_data_available(m_uart, millis))
162  return true;
163  else
164  return false;
165  }
166 
175  {
176  return mraa_uart_flush(m_uart);
177  }
178 
188  setBaudRate(unsigned int baud)
189  {
190  return mraa_uart_set_baudrate(m_uart, baud);
191  }
192 
204  setMode(int bytesize, mraa_uart_parity_t parity, int stopbits)
205  {
206  return mraa_uart_set_mode(m_uart, bytesize, parity, stopbits);
207  }
208 
217  setFlowcontrol(bool xonxoff, bool rtscts)
218  {
219  return mraa_uart_set_flowcontrol(m_uart, xonxoff, rtscts);
220  }
221 
232  setTimeout(int read, int write, int interchar)
233  {
234  return mraa_uart_set_timeout(m_uart, read, write, interchar);
235  }
236 
237  private:
238  mraa_uart_context m_uart;
239 };
240 }
UART module.
std::string readStr(int length)
Definition: uart.hpp:130
mraa_result_t setFlowcontrol(bool xonxoff, bool rtscts)
Definition: uart.hpp:217
int mraa_uart_write(mraa_uart_context dev, const char *buf, size_t length)
int read(char *data, int length)
Definition: uart.hpp:105
bool dataAvailable(unsigned int millis=0)
Definition: uart.hpp:159
Uart(int uart)
Definition: uart.hpp:52
Uart(std::string path)
Definition: uart.hpp:67
mraa_result_t mraa_uart_stop(mraa_uart_context dev)
API to UART (enabling only)
Definition: uart.hpp:43
mraa_result_t mraa_uart_set_flowcontrol(mraa_uart_context dev, mraa_boolean_t xonxoff, mraa_boolean_t rtscts)
~Uart()
Definition: uart.hpp:79
mraa_result_t setBaudRate(unsigned int baud)
Definition: uart.hpp:188
mraa_result_t mraa_uart_flush(mraa_uart_context dev)
mraa_result_t setMode(int bytesize, mraa_uart_parity_t parity, int stopbits)
Definition: uart.hpp:204
std::string getDevicePath()
Definition: uart.hpp:91
mraa_result_t setTimeout(int read, int write, int interchar)
Definition: uart.hpp:232
mraa_result_t flush()
Definition: uart.hpp:174
mraa_uart_context mraa_uart_init_raw(const char *path)
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:146
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)
Definition: aio.hpp:30
mraa_result_t
Definition: types.h:184
int write(const char *data, int length)
Definition: uart.hpp:118
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)