mraa  1.6.0
Low Level Skeleton Library for Communication on GNU/Linux platforms
 All Data Structures Namespaces Files Functions Typedefs Enumerations Enumerator Macros Pages
iio.hpp
1 /*
2  * Author: Henry Bruce <henry.bruce@intel.com>
3  * Copyright (c) 2015 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 <stdexcept>
28  #include <sstream>
29 #include "iio.h"
30 #include "types.hpp"
31 
32 namespace mraa
33 {
34 
36 {
37  int channelType;
38  int modifier;
39  int type;
40  int direction;
41  int channel;
42  int channel2;
43  int diff;
44 };
45 
47 {
48 public:
49  virtual void onIioEvent(const IioEventData& eventData) = 0;
50  virtual ~IioHandler() {}; // add an empty destructor to get rid of warning
51 };
52 
53 
61 class Iio
62 {
63  public:
72  Iio(int device)
73  {
74  m_iio = mraa_iio_init(device);
75  if (m_iio == NULL) {
76  std::ostringstream oss;
77  oss << "IIO device " << device << " is not valid";
78  throw std::invalid_argument(oss.str());
79  }
80  }
81 
89  Iio(const std::string& deviceName)
90  {
91  std::ostringstream oss;
92  int id = mraa_iio_get_device_num_by_name(deviceName.c_str());
93  if (id == -1) {
94  oss << "IIO device name " << deviceName << " not found";
95  throw std::invalid_argument(oss.str());
96  }
97  m_iio = mraa_iio_init(id);
98  if (m_iio == NULL) {
99  oss << "IIO device " << deviceName << " is not valid";
100  throw std::invalid_argument(oss.str());
101  }
102  }
103 
108  {
109  mraa_iio_close(m_iio);
110  }
111 
112 
118  std::string
120  {
121  return mraa_iio_get_device_name(m_iio);
122  }
123 
133  int
134  readInt(const std::string& attributeName) const
135  {
136  int value;
137  mraa_result_t res = mraa_iio_read_int(m_iio, attributeName.c_str(), &value);
138  if (res != MRAA_SUCCESS) {
139  std::ostringstream oss;
140  oss << "IIO readInt for attibute " << attributeName << " failed";
141  throw std::runtime_error(oss.str());
142  }
143  return value;
144  }
145 
155  float
156  readFloat(const std::string& attributeName) const
157  {
158  float value;
159  mraa_result_t res = mraa_iio_read_float(m_iio, attributeName.c_str(), &value);
160  if (res != MRAA_SUCCESS) {
161  std::ostringstream oss;
162  oss << "IIO readFloat for attibute " << attributeName << " failed";
163  throw std::runtime_error(oss.str());
164  }
165  return value;
166  }
167 
176  void
177  writeInt(const std::string& attributeName, int value) const
178  {
179  mraa_result_t res = mraa_iio_write_int(m_iio, attributeName.c_str(), value);
180  if (res != MRAA_SUCCESS) {
181  std::ostringstream oss;
182  oss << "IIO writeInt for attibute " << attributeName << " failed";
183  throw std::runtime_error(oss.str());
184  }
185 
186  }
187 
196  void
197  writeFloat(const std::string& attributeName, float value) const
198  {
199  mraa_result_t res = mraa_iio_write_float(m_iio, attributeName.c_str(), value);
200  if (res != MRAA_SUCCESS) {
201  std::ostringstream oss;
202  oss << "IIO writeFloat for attibute " << attributeName << " failed";
203  throw std::runtime_error(oss.str());
204  }
205 
206  }
207 
215  void
217  {
218  mraa_result_t res = mraa_iio_event_setup_callback(m_iio, private_event_handler, handler);
219  if (res != MRAA_SUCCESS) {
220  throw std::runtime_error("registerEventHandler failed");
221  }
222  }
223 
224  private:
225  static void private_event_handler(iio_event_data* data, void *args)
226  {
227  if (args != NULL) {
228  IioHandler* handler = (IioHandler*)args;
229  IioEventData eventData;
230  int chan_type, modifier, type, direction, channel, channel2, different;
231  mraa_iio_event_extract_event(data, &chan_type, &modifier, &type, &direction, &channel, &channel2, &different);
232  eventData.channelType = chan_type;
233  eventData.modifier = modifier;
234  eventData.type = type;
235  eventData.direction = direction;
236  eventData.channel = channel;
237  eventData.channel2 = channel2;
238  eventData.diff = different;
239  handler->onIioEvent(eventData);
240  }
241  }
242 
243  mraa_iio_context m_iio;
244 };
245 
246 }
~Iio()
Definition: iio.hpp:107
Definition: iio.hpp:46
Iio(int device)
Definition: iio.hpp:72
mraa_result_t mraa_iio_close(mraa_iio_context dev)
Definition: types.h:205
std::string getDeviceName() const
Definition: iio.hpp:119
int readInt(const std::string &attributeName) const
Definition: iio.hpp:134
API to Industrial IO.
Definition: iio.hpp:61
Iio(const std::string &deviceName)
Definition: iio.hpp:89
void registerEventHandler(IioHandler *handler) const
Definition: iio.hpp:216
float readFloat(const std::string &attributeName) const
Definition: iio.hpp:156
mraa_iio_context mraa_iio_init(int device)
void writeFloat(const std::string &attributeName, float value) const
Definition: iio.hpp:197
iio
Definition: iio.hpp:35
struct _iio * mraa_iio_context
Definition: iio.h:72
mraa_result_t
Definition: types.h:204
void writeInt(const std::string &attributeName, int value) const
Definition: iio.hpp:177
Definition: iio_kernel_headers.h:122