mraa  0.9.6
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 
131  std::string
132  readStr(int length)
133  {
134  char* data = (char*) malloc(sizeof(char) * length);
135  int v = mraa_uart_read(m_uart, data, (size_t) length);
136  std::string ret(data, v);
137  free(data);
138  return ret;
139  }
140 
147  int
148  writeStr(std::string data)
149  {
150  // this is data.length() not +1 because we want to avoid the '\0' char
151  return mraa_uart_write(m_uart, data.c_str(), (data.length()));
152  }
153 
160  bool
161  dataAvailable(unsigned int millis = 0)
162  {
163  if (mraa_uart_data_available(m_uart, millis))
164  return true;
165  else
166  return false;
167  }
168 
175  Result
177  {
178  return (Result) mraa_uart_flush(m_uart);
179  }
180 
189  Result
190  setBaudRate(unsigned int baud)
191  {
192  return (Result) mraa_uart_set_baudrate(m_uart, baud);
193  }
194 
205  Result
206  setMode(int bytesize, UartParity parity, int stopbits)
207  {
208  return (Result) mraa_uart_set_mode(m_uart, bytesize, (mraa_uart_parity_t) parity, stopbits);
209  }
210 
218  Result
219  setFlowcontrol(bool xonxoff, bool rtscts)
220  {
221  return (Result) mraa_uart_set_flowcontrol(m_uart, xonxoff, rtscts);
222  }
223 
233  Result
234  setTimeout(int read, int write, int interchar)
235  {
236  return (Result) mraa_uart_set_timeout(m_uart, read, write, interchar);
237  }
238 
239  private:
240  mraa_uart_context m_uart;
241 };
242 }
Result
Definition: types.hpp:185
Result setBaudRate(unsigned int baud)
Definition: uart.hpp:190
UART module.
std::string readStr(int length)
Definition: uart.hpp:132
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:161
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:176
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:219
mraa_uart_context mraa_uart_init_raw(const char *path)
Result setMode(int bytesize, UartParity parity, int stopbits)
Definition: uart.hpp:206
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:148
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:234
int write(const char *data, int length)
Definition: uart.hpp:120
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)