mraa  1.0.0
Low Level Skeleton Library for Communication on GNU/Linux platforms
 All Data Structures Namespaces Files Functions 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 
82  {
83  mraa_uart_stop(m_uart);
84  }
85 
92  std::string
94  {
95  std::string ret_val(mraa_uart_get_dev_path(m_uart));
96  return ret_val;
97  }
98 
106  int
107  read(char* data, int length)
108  {
109  return mraa_uart_read(m_uart, data, (size_t) length);
110  }
111 
119  int
120  write(const char* data, int length)
121  {
122  return mraa_uart_write(m_uart, data, (size_t) length);
123  }
124 
132  std::string
133  readStr(int length)
134  {
135  char* data = (char*) malloc(sizeof(char) * length);
136  if (data == NULL) {
137  throw std::bad_alloc();
138  }
139 
140  int v = mraa_uart_read(m_uart, data, (size_t) length);
141  std::string ret(data, v);
142  free(data);
143  return ret;
144  }
145 
152  int
153  writeStr(std::string data)
154  {
155  // this is data.length() not +1 because we want to avoid the '\0' char
156  return mraa_uart_write(m_uart, data.c_str(), (data.length()));
157  }
158 
165  bool
166  dataAvailable(unsigned int millis = 0)
167  {
168  if (mraa_uart_data_available(m_uart, millis))
169  return true;
170  else
171  return false;
172  }
173 
180  Result
182  {
183  return (Result) mraa_uart_flush(m_uart);
184  }
185 
194  Result
195  setBaudRate(unsigned int baud)
196  {
197  return (Result) mraa_uart_set_baudrate(m_uart, baud);
198  }
199 
210  Result
211  setMode(int bytesize, UartParity parity, int stopbits)
212  {
213  return (Result) mraa_uart_set_mode(m_uart, bytesize, (mraa_uart_parity_t) parity, stopbits);
214  }
215 
223  Result
224  setFlowcontrol(bool xonxoff, bool rtscts)
225  {
226  return (Result) mraa_uart_set_flowcontrol(m_uart, xonxoff, rtscts);
227  }
228 
238  Result
239  setTimeout(int read, int write, int interchar)
240  {
241  return (Result) mraa_uart_set_timeout(m_uart, read, write, interchar);
242  }
243 
251  Result
252  SetNonBlocking(bool nonblock)
253  {
254  return (Result) mraa_uart_set_non_blocking(m_uart, nonblock);
255  }
256 
257  private:
258  mraa_uart_context m_uart;
259 };
260 }
Result
Definition: types.hpp:194
Result setBaudRate(unsigned int baud)
Definition: uart.hpp:195
UART module.
std::string readStr(int length)
Definition: uart.hpp:133
Result SetNonBlocking(bool nonblock)
Definition: uart.hpp:252
int mraa_uart_write(mraa_uart_context dev, const char *buf, size_t length)
int read(char *data, int length)
Definition: uart.hpp:107
bool dataAvailable(unsigned int millis=0)
Definition: uart.hpp:166
Uart(int uart)
Definition: uart.hpp:54
Uart(std::string path)
Definition: uart.hpp:69
mraa_result_t mraa_uart_stop(mraa_uart_context dev)
Result flush()
Definition: uart.hpp:181
API to UART (enabling only)
Definition: uart.hpp:45
mraa_result_t mraa_uart_set_flowcontrol(mraa_uart_context dev, mraa_boolean_t xonxoff, mraa_boolean_t rtscts)
~Uart()
Definition: uart.hpp:81
mraa_result_t mraa_uart_flush(mraa_uart_context dev)
std::string getDevicePath()
Definition: uart.hpp:93
Result setFlowcontrol(bool xonxoff, bool rtscts)
Definition: uart.hpp:224
mraa_uart_context mraa_uart_init_raw(const char *path)
Result setMode(int bytesize, UartParity parity, int stopbits)
Definition: uart.hpp:211
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:153
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:239
int write(const char *data, int length)
Definition: uart.hpp:120
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)