mraa  2.0.0
Low Level Skeleton Library for Communication on GNU/Linux platforms
uart_ow.hpp
1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2016 Intel Corporation
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #pragma once
26 
27 #include "types.hpp"
28 #include "uart_ow.h"
29 #include <cstring>
30 #include <stdexcept>
31 
32 namespace mraa
33 {
34 
42 class UartOW
43 {
44  public:
52  UartOW(int uart)
53  {
54  m_uart = mraa_uart_ow_init(uart);
55 
56  if (m_uart == NULL) {
57  throw std::invalid_argument("Error initialising UART_OW");
58  }
59  }
60 
68  UartOW(std::string path)
69  {
70  m_uart = mraa_uart_ow_init_raw(path.c_str());
71 
72  if (m_uart == NULL) {
73  throw std::invalid_argument("Error initialising UART");
74  }
75  }
76 
83  UartOW(void* uart_ow_context)
84  {
85  m_uart = (mraa_uart_ow_context) uart_ow_context;
86 
87  if (m_uart == NULL) {
88  throw std::invalid_argument("Invalid UART_OW context");
89  }
90  }
91 
96  {
97  mraa_uart_ow_stop(m_uart);
98  }
99 
106  std::string
108  {
109  std::string ret_val(mraa_uart_ow_get_dev_path(m_uart));
110  return ret_val;
111  }
112 
119  uint8_t
121  {
122  int res = mraa_uart_ow_read_byte(m_uart);
123  if (res == -1) {
124  throw std::invalid_argument("Unknown UART_OW error");
125  }
126  return (uint8_t) res;
127  }
128 
137  uint8_t
138  writeByte(uint8_t byte)
139  {
140  int res = mraa_uart_ow_write_byte(m_uart, byte);
141  if (res == -1) {
142  throw std::invalid_argument("Unknown UART_OW error");
143  }
144  return (uint8_t) res;
145  }
146 
156  bool
157  writeBit(bool bit)
158  {
159  int res = mraa_uart_ow_bit(m_uart, (bit) ? 1 : 0);
160  if (res == -1) {
161  throw std::invalid_argument("Unknown UART_OW error");
162  }
163  return ((res) ? true : false);
164  }
165 
173  {
174  return (mraa::Result) mraa_uart_ow_reset(m_uart);
175  }
176 
190  search(bool start, uint8_t* id)
191  {
192  return (mraa::Result) mraa_uart_ow_rom_search(m_uart, (start) ? 1 : 0, id);
193  }
194 
206  std::string
207  search(bool start)
208  {
209  uint8_t id[MRAA_UART_OW_ROMCODE_SIZE];
210  mraa_result_t rv;
211 
212  rv = mraa_uart_ow_rom_search(m_uart, (start) ? 1 : 0, id);
213 
214  if (rv == MRAA_SUCCESS) {
215  // we found one
216  std::string idStr((char*) id, MRAA_UART_OW_ROMCODE_SIZE);
217  return idStr;
218  } else {
219  // failure, or end of search
220  return "";
221  }
222  }
223 
233  command(uint8_t command, uint8_t* id)
234  {
235  return (mraa::Result) mraa_uart_ow_command(m_uart, command, id);
236  }
237 
249  command(uint8_t command, std::string id)
250  {
251  if (id.empty())
252  return (mraa::Result) mraa_uart_ow_command(m_uart, command, NULL);
253  else {
254  if (id.size() != 8) {
255  // Only 8 byte romcodes are legal.
256  throw std::invalid_argument(std::string(__FUNCTION__) +
257  ": id must be 8 bytes only");
258  }
259  return (mraa::Result) mraa_uart_ow_command(m_uart, command, (uint8_t*) id.data());
260  }
261  }
262 
270  uint8_t
271  crc8(uint8_t* buffer, uint16_t length)
272  {
273  return mraa_uart_ow_crc8(buffer, length);
274  }
275 
283  uint8_t
284  crc8(std::string buffer)
285  {
286  return mraa_uart_ow_crc8((uint8_t*) buffer.data(), buffer.size());
287  }
288 
289  private:
290  mraa_uart_ow_context m_uart;
291 };
292 }
uint8_t crc8(std::string buffer)
Definition: uart_ow.hpp:284
mraa::Result reset()
Definition: uart_ow.hpp:172
mraa::Result search(bool start, uint8_t *id)
Definition: uart_ow.hpp:190
Result
Definition: types.hpp:204
uint8_t crc8(uint8_t *buffer, uint16_t length)
Definition: uart_ow.hpp:271
mraa_uart_ow_context mraa_uart_ow_init_raw(const char *path)
mraa::Result command(uint8_t command, std::string id)
Definition: uart_ow.hpp:249
API for UART One Wire.
Definition: uart_ow.hpp:42
Definition: types.h:210
std::string search(bool start)
Definition: uart_ow.hpp:207
UartOW(std::string path)
Definition: uart_ow.hpp:68
mraa_result_t mraa_uart_ow_rom_search(mraa_uart_ow_context dev, mraa_boolean_t start, uint8_t *id)
int mraa_uart_ow_read_byte(mraa_uart_ow_context dev)
mraa_uart_ow_context mraa_uart_ow_init(int uart)
const char * mraa_uart_ow_get_dev_path(mraa_uart_ow_context dev)
struct _mraa_uart_ow * mraa_uart_ow_context
uint8_t writeByte(uint8_t byte)
Definition: uart_ow.hpp:138
Definition: uart_ow.h:71
UartOW(int uart)
Definition: uart_ow.hpp:52
uint8_t readByte()
Definition: uart_ow.hpp:120
uint8_t mraa_uart_ow_crc8(uint8_t *buffer, uint16_t length)
mraa::Result command(uint8_t command, uint8_t *id)
Definition: uart_ow.hpp:233
int mraa_uart_ow_write_byte(mraa_uart_ow_context dev, uint8_t byte)
Definition: aio.hpp:31
int mraa_uart_ow_bit(mraa_uart_ow_context dev, uint8_t bit)
mraa_result_t mraa_uart_ow_command(mraa_uart_ow_context dev, uint8_t command, uint8_t *id)
mraa_result_t
Definition: types.h:209
#define MRAA_UART_OW_ROMCODE_SIZE
Definition: uart_ow.h:68
UART OW module.
std::string getDevicePath()
Definition: uart_ow.hpp:107
UartOW(void *uart_ow_context)
Definition: uart_ow.hpp:83
mraa_result_t mraa_uart_ow_reset(mraa_uart_ow_context dev)
mraa_result_t mraa_uart_ow_stop(mraa_uart_ow_context dev)
~UartOW()
Definition: uart_ow.hpp:95
bool writeBit(bool bit)
Definition: uart_ow.hpp:157