mraa  1.8.0
Low Level Skeleton Library for Communication on GNU/Linux platforms
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 <stdlib.h>
32 #include <stdexcept>
33 #include <cstring>
34 
35 namespace mraa
36 {
37 
45 class Uart
46 {
47  public:
54  Uart(int uart)
55  {
56  m_uart = mraa_uart_init(uart);
57 
58  if (m_uart == NULL) {
59  throw std::invalid_argument("Error initialising UART");
60  }
61  }
62 
69  Uart(std::string path)
70  {
71  m_uart = mraa_uart_init_raw(path.c_str());
72 
73  if (m_uart == NULL) {
74  throw std::invalid_argument("Error initialising UART");
75  }
76  }
77 
84  Uart(void* uart_context)
85  {
86  m_uart = (mraa_uart_context) uart_context;
87 
88  if (m_uart == NULL) {
89  throw std::invalid_argument("Invalid UART context");
90  }
91  }
96  {
97  mraa_uart_stop(m_uart);
98  }
99 
106  std::string
108  {
109  std::string ret_val(mraa_uart_get_dev_path(m_uart));
110  return ret_val;
111  }
112 
120  int
121  read(char* data, int length)
122  {
123  return mraa_uart_read(m_uart, data, (size_t) length);
124  }
125 
133  int
134  write(const char* data, int length)
135  {
136  return mraa_uart_write(m_uart, data, (size_t) length);
137  }
138 
146  std::string
147  readStr(int length)
148  {
149  char* data = (char*) malloc(sizeof(char) * length);
150  if (data == NULL) {
151  throw std::bad_alloc();
152  }
153 
154  int v = mraa_uart_read(m_uart, data, (size_t) length);
155  std::string ret(data, v);
156  free(data);
157  return ret;
158  }
159 
166  int
167  writeStr(std::string data)
168  {
169  // this is data.length() not +1 because we want to avoid the '\0' char
170  return mraa_uart_write(m_uart, data.c_str(), (data.length()));
171  }
172 
179  bool
180  dataAvailable(unsigned int millis = 0)
181  {
182  if (mraa_uart_data_available(m_uart, millis))
183  return true;
184  else
185  return false;
186  }
187 
194  Result
196  {
197  return (Result) mraa_uart_flush(m_uart);
198  }
199 
209  Result
210  sendBreak(int duration)
211  {
212  return (Result) mraa_uart_sendbreak(m_uart, duration);
213  }
214 
223  Result
224  setBaudRate(unsigned int baud)
225  {
226  return (Result) mraa_uart_set_baudrate(m_uart, baud);
227  }
228 
239  Result
240  setMode(int bytesize, UartParity parity, int stopbits)
241  {
242  return (Result) mraa_uart_set_mode(m_uart, bytesize, (mraa_uart_parity_t) parity, stopbits);
243  }
244 
252  Result
253  setFlowcontrol(bool xonxoff, bool rtscts)
254  {
255  return (Result) mraa_uart_set_flowcontrol(m_uart, xonxoff, rtscts);
256  }
257 
267  Result
268  setTimeout(int read, int write, int interchar)
269  {
270  return (Result) mraa_uart_set_timeout(m_uart, read, write, interchar);
271  }
272 
279  Result
280  setNonBlocking(bool nonblock)
281  {
282  return (Result) mraa_uart_set_non_blocking(m_uart, nonblock);
283  }
284 
285  private:
286  mraa_uart_context m_uart;
287 };
288 }
Result
Definition: types.hpp:200
Result setBaudRate(unsigned int baud)
Definition: uart.hpp:224
UART module.
std::string readStr(int length)
Definition: uart.hpp:147
mraa_uart_parity_t
Definition: types.h:252
Uart(void *uart_context)
Definition: uart.hpp:84
UartParity
Definition: types.hpp:246
int mraa_uart_write(mraa_uart_context dev, const char *buf, size_t length)
int read(char *data, int length)
Definition: uart.hpp:121
bool dataAvailable(unsigned int millis=0)
Definition: uart.hpp:180
Uart(int uart)
Definition: uart.hpp:54
struct _uart * mraa_uart_context
Definition: uart.h:49
Uart(std::string path)
Definition: uart.hpp:69
mraa_result_t mraa_uart_stop(mraa_uart_context dev)
mraa_result_t mraa_uart_sendbreak(mraa_uart_context dev, int duration)
Result flush()
Definition: uart.hpp:195
API to UART (enabling only)
Definition: uart.hpp:45
Result setNonBlocking(bool nonblock)
Definition: uart.hpp:280
mraa_result_t mraa_uart_set_flowcontrol(mraa_uart_context dev, mraa_boolean_t xonxoff, mraa_boolean_t rtscts)
~Uart()
Definition: uart.hpp:95
mraa_result_t mraa_uart_flush(mraa_uart_context dev)
std::string getDevicePath()
Definition: uart.hpp:107
Result setFlowcontrol(bool xonxoff, bool rtscts)
Definition: uart.hpp:253
mraa_uart_context mraa_uart_init_raw(const char *path)
Result setMode(int bytesize, UartParity parity, int stopbits)
Definition: uart.hpp:240
mraa_boolean_t mraa_uart_data_available(mraa_uart_context dev, unsigned int millis)
Result sendBreak(int duration)
Definition: uart.hpp:210
const char * mraa_uart_get_dev_path(mraa_uart_context dev)
int writeStr(std::string data)
Definition: uart.hpp:167
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:268
int write(const char *data, int length)
Definition: uart.hpp:134
mraa_result_t mraa_uart_set_non_blocking(mraa_uart_context dev, mraa_boolean_t nonblock)
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)