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
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 };
51 
52 
60 class Iio
61 {
62  public:
71  Iio(int device)
72  {
73  m_iio = mraa_iio_init(device);
74  if (m_iio == NULL) {
75  std::ostringstream oss;
76  oss << "IIO device " << device << " is not valid";
77  throw std::invalid_argument(oss.str());
78  }
79  }
80 
88  Iio(const std::string& deviceName)
89  {
90  std::ostringstream oss;
91  int id = mraa_iio_get_device_num_by_name(deviceName.c_str());
92  if (id == -1) {
93  oss << "IIO device name " << deviceName << " not found";
94  throw std::invalid_argument(oss.str());
95  }
96  m_iio = mraa_iio_init(id);
97  if (m_iio == NULL) {
98  oss << "IIO device " << deviceName << " is not valid";
99  throw std::invalid_argument(oss.str());
100  }
101  }
102 
107  {
108  mraa_iio_close(m_iio);
109  }
110 
111 
117  std::string
119  {
120  return mraa_iio_get_device_name(m_iio);
121  }
122 
132  int
133  readInt(const std::string& attributeName) const
134  {
135  int value;
136  mraa_result_t res = mraa_iio_read_int(m_iio, attributeName.c_str(), &value);
137  if (res != MRAA_SUCCESS) {
138  std::ostringstream oss;
139  oss << "IIO readInt for attibute " << attributeName << " failed";
140  throw std::runtime_error(oss.str());
141  }
142  return value;
143  }
144 
154  float
155  readFloat(const std::string& attributeName) const
156  {
157  float value;
158  mraa_result_t res = mraa_iio_read_float(m_iio, attributeName.c_str(), &value);
159  if (res != MRAA_SUCCESS) {
160  std::ostringstream oss;
161  oss << "IIO readFloat for attibute " << attributeName << " failed";
162  throw std::runtime_error(oss.str());
163  }
164  return value;
165  }
166 
175  void
176  writeInt(const std::string& attributeName, int value) const
177  {
178  mraa_result_t res = mraa_iio_write_int(m_iio, attributeName.c_str(), value);
179  if (res != MRAA_SUCCESS) {
180  std::ostringstream oss;
181  oss << "IIO writeInt for attibute " << attributeName << " failed";
182  throw std::runtime_error(oss.str());
183  }
184 
185  }
186 
195  void
196  writeFloat(const std::string& attributeName, float value) const
197  {
198  mraa_result_t res = mraa_iio_write_float(m_iio, attributeName.c_str(), value);
199  if (res != MRAA_SUCCESS) {
200  std::ostringstream oss;
201  oss << "IIO writeFloat for attibute " << attributeName << " failed";
202  throw std::runtime_error(oss.str());
203  }
204 
205  }
206 
214  void
216  {
217  mraa_result_t res = mraa_iio_event_setup_callback(m_iio, private_event_handler, handler);
218  if (res != MRAA_SUCCESS) {
219  throw std::runtime_error("registerEventHandler failed");
220  }
221  }
222 
223  private:
224  static void private_event_handler(iio_event_data* data, void *args)
225  {
226  if (args != NULL) {
227  IioHandler* handler = (IioHandler*)args;
228  IioEventData eventData;
229  int chan_type, modifier, type, direction, channel, channel2, different;
230  mraa_iio_event_extract_event(data, &chan_type, &modifier, &type, &direction, &channel, &channel2, &different);
231  eventData.channelType = chan_type;
232  eventData.modifier = modifier;
233  eventData.type = type;
234  eventData.direction = direction;
235  eventData.channel = channel;
236  eventData.channel2 = channel2;
237  eventData.diff = different;
238  handler->onIioEvent(eventData);
239  }
240  }
241 
242  mraa_iio_context m_iio;
243 };
244 
245 }
~Iio()
Definition: iio.hpp:106
Definition: iio.hpp:46
Iio(int device)
Definition: iio.hpp:71
mraa_result_t mraa_iio_close(mraa_iio_context dev)
Definition: types.h:194
std::string getDeviceName() const
Definition: iio.hpp:118
int readInt(const std::string &attributeName) const
Definition: iio.hpp:133
API to Industrial IO.
Definition: iio.hpp:60
Iio(const std::string &deviceName)
Definition: iio.hpp:88
void registerEventHandler(IioHandler *handler) const
Definition: iio.hpp:215
float readFloat(const std::string &attributeName) const
Definition: iio.hpp:155
mraa_iio_context mraa_iio_init(int device)
void writeFloat(const std::string &attributeName, float value) const
Definition: iio.hpp:196
iio
Definition: iio.hpp:35
struct _iio * mraa_iio_context
Definition: iio.h:72
mraa_result_t
Definition: types.h:193
void writeInt(const std::string &attributeName, int value) const
Definition: iio.hpp:176
Definition: iio_kernel_headers.h:117