mraa  1.9.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 "uart_ow.h"
28 #include "types.hpp"
29 #include <stdexcept>
30 #include <cstring>
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 
81  {
82  mraa_uart_ow_stop(m_uart);
83  }
84 
91  std::string
93  {
94  std::string ret_val(mraa_uart_ow_get_dev_path(m_uart));
95  return ret_val;
96  }
97 
104  uint8_t
106  {
107  int res = mraa_uart_ow_read_byte(m_uart);
108  if (res == -1) {
109  throw std::invalid_argument("Unknown UART_OW error");
110  }
111  return (uint8_t) res;
112  }
113 
122  uint8_t
123  writeByte(uint8_t byte)
124  {
125  int res = mraa_uart_ow_write_byte(m_uart, byte);
126  if (res == -1) {
127  throw std::invalid_argument("Unknown UART_OW error");
128  }
129  return (uint8_t) res;
130  }
131 
141  bool
142  writeBit(bool bit)
143  {
144  int res = mraa_uart_ow_bit(m_uart, (bit) ? 1 : 0);
145  if (res == -1) {
146  throw std::invalid_argument("Unknown UART_OW error");
147  }
148  return ((res) ? true : false);
149  }
150 
158  {
159  return (mraa::Result) mraa_uart_ow_reset(m_uart);
160  }
161 
175  search(bool start, uint8_t* id)
176  {
177  return (mraa::Result) mraa_uart_ow_rom_search(m_uart, (start) ? 1 : 0, id);
178  }
179 
191  std::string
192  search(bool start)
193  {
194  uint8_t id[MRAA_UART_OW_ROMCODE_SIZE];
195  mraa_result_t rv;
196 
197  rv = mraa_uart_ow_rom_search(m_uart, (start) ? 1 : 0, id);
198 
199  if (rv == MRAA_SUCCESS) {
200  // we found one
201  std::string idStr((char*) id, MRAA_UART_OW_ROMCODE_SIZE);
202  return idStr;
203  } else {
204  // failure, or end of search
205  return "";
206  }
207  }
208 
218  command(uint8_t command, uint8_t* id)
219  {
220  return (mraa::Result) mraa_uart_ow_command(m_uart, command, id);
221  }
222 
234  command(uint8_t command, std::string id)
235  {
236  if (id.empty())
237  return (mraa::Result) mraa_uart_ow_command(m_uart, command, NULL);
238  else {
239  if (id.size() != 8) {
240  // Only 8 byte romcodes are legal.
241  throw std::invalid_argument(std::string(__FUNCTION__) +
242  ": id must be 8 bytes only");
243  }
244  return (mraa::Result) mraa_uart_ow_command(m_uart, command, (uint8_t*) id.data());
245  }
246  }
247 
255  uint8_t
256  crc8(uint8_t* buffer, uint16_t length)
257  {
258  return mraa_uart_ow_crc8(buffer, length);
259  }
260 
268  uint8_t
269  crc8(std::string buffer)
270  {
271  return mraa_uart_ow_crc8((uint8_t*) buffer.data(), buffer.size());
272  }
273 
274  private:
275  mraa_uart_ow_context m_uart;
276 };
277 }
uint8_t crc8(std::string buffer)
Definition: uart_ow.hpp:269
mraa::Result reset()
Definition: uart_ow.hpp:157
mraa::Result search(bool start, uint8_t *id)
Definition: uart_ow.hpp:175
Result
Definition: types.hpp:200
uint8_t crc8(uint8_t *buffer, uint16_t length)
Definition: uart_ow.hpp:256
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:234
API for UART One Wire.
Definition: uart_ow.hpp:42
Definition: types.h:209
std::string search(bool start)
Definition: uart_ow.hpp:192
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)
uint8_t writeByte(uint8_t byte)
Definition: uart_ow.hpp:123
Definition: uart_ow.h:71
UartOW(int uart)
Definition: uart_ow.hpp:52
uint8_t readByte()
Definition: uart_ow.hpp:105
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:218
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:208
#define MRAA_UART_OW_ROMCODE_SIZE
Definition: uart_ow.h:68
UART OW module.
std::string getDevicePath()
Definition: uart_ow.hpp:92
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:80
bool writeBit(bool bit)
Definition: uart_ow.hpp:142