mraa  0.7.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 <stdexcept>
31 #include <cstring>
32 
33 namespace mraa
34 {
35 
41 class Uart
42 {
43  public:
50  Uart(int uart)
51  {
52  m_uart = mraa_uart_init(uart);
53 
54  if (m_uart == NULL) {
55  throw std::invalid_argument("Error initialising UART");
56  }
57  }
58 
65  Uart(std::string path)
66  {
67  char *p = new char[path.length() + 1];
68  std::strcpy(p, path.c_str());
69  m_uart = mraa_uart_init_raw(p);
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  std::string
105  read(int length)
106  {
107  char* data = (char*) malloc(sizeof(char) * length);
108  int v = mraa_uart_read(m_uart, data, (size_t) length);
109  char* out = (char*) malloc(sizeof(char) * v);
110  strncpy(out, data, v);
111  std::string ret(out);
112  free(data);
113  free(out);
114  return ret;
115  }
116 
124  int
125  write(std::string data)
126  {
127  char *d = new char[data.length() + 1];
128  std::strcpy(d, data.c_str());
129  return mraa_uart_write(m_uart, d, (data.length() + 1));
130  }
131 
138  bool
139  dataAvailable(unsigned int millis = 0)
140  {
141  if (mraa_uart_data_available(m_uart, millis))
142  return true;
143  else
144  return false;
145  }
146 
155  {
156  return mraa_uart_flush(m_uart);
157  }
158 
168  setBaudRate(unsigned int baud)
169  {
170  return mraa_uart_set_baudrate(m_uart, baud);
171  }
172 
184  setMode(int bytesize, mraa_uart_parity_t parity, int stopbits)
185  {
186  return mraa_uart_set_mode(m_uart, bytesize, parity, stopbits);
187  }
188 
197  setFlowcontrol(bool xonxoff, bool rtscts)
198  {
199  return mraa_uart_set_flowcontrol(m_uart, xonxoff, rtscts);
200  }
201 
212  setTimeout(int read, int write, int interchar)
213  {
214  return mraa_uart_set_timeout(m_uart, read, write, interchar);
215  }
216 
217  private:
218  mraa_uart_context m_uart;
219 };
220 }
UART module.
mraa_result_t setFlowcontrol(bool xonxoff, bool rtscts)
Definition: uart.hpp:197
bool dataAvailable(unsigned int millis=0)
Definition: uart.hpp:139
Uart(int uart)
Definition: uart.hpp:50
mraa_uart_context mraa_uart_init_raw(char *path)
Uart(std::string path)
Definition: uart.hpp:65
mraa_result_t mraa_uart_stop(mraa_uart_context dev)
API to UART (enabling only)
Definition: uart.hpp:41
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:168
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:184
std::string getDevicePath()
Definition: uart.hpp:91
mraa_result_t setTimeout(int read, int write, int interchar)
Definition: uart.hpp:212
mraa_result_t flush()
Definition: uart.hpp:154
char * mraa_uart_get_dev_path(mraa_uart_context dev)
mraa_boolean_t mraa_uart_data_available(mraa_uart_context dev, unsigned int millis)
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
mraa_result_t mraa_uart_set_timeout(mraa_uart_context dev, int read, int write, int interchar)
std::string read(int length)
Definition: uart.hpp:105
mraa_result_t mraa_uart_set_baudrate(mraa_uart_context dev, unsigned int baud)
int mraa_uart_write(mraa_uart_context dev, char *buf, size_t length)
int write(std::string data)
Definition: uart.hpp:125