mraa  2.0.0
Low Level Skeleton Library for Communication on GNU/Linux platforms
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 "iio.h"
28 #include "types.hpp"
29 #include <sstream>
30 #include <stdexcept>
31 
32 namespace mraa
33 {
34 
36 struct IioEventData {
40  int modifier;
42  int type;
44  int direction;
46  int channel;
48  int channel2;
50  int diff;
51 };
52 
55 {
56  public:
58  virtual void onIioEvent(const IioEventData& eventData) = 0;
60  virtual ~IioHandler(){}; // add an empty destructor to get rid of warning
61 };
62 
63 
71 class Iio
72 {
73  public:
82  Iio(int device)
83  {
84  m_iio = mraa_iio_init(device);
85  if (m_iio == NULL) {
86  std::ostringstream oss;
87  oss << "IIO device " << device << " is not valid";
88  throw std::invalid_argument(oss.str());
89  }
90  }
91 
99  Iio(const std::string& deviceName)
100  {
101  std::ostringstream oss;
102  int id = mraa_iio_get_device_num_by_name(deviceName.c_str());
103  if (id == -1) {
104  oss << "IIO device name " << deviceName << " not found";
105  throw std::invalid_argument(oss.str());
106  }
107  m_iio = mraa_iio_init(id);
108  if (m_iio == NULL) {
109  oss << "IIO device " << deviceName << " is not valid";
110  throw std::invalid_argument(oss.str());
111  }
112  }
113 
119  Iio(void* iio_context)
120  {
121  m_iio = (mraa_iio_context) iio_context;
122  if (m_iio == NULL) {
123  throw std::invalid_argument("Invalid IIO context");
124  }
125  }
126 
131  {
132  mraa_iio_close(m_iio);
133  }
134 
135 
141  std::string
143  {
144  return mraa_iio_get_device_name(m_iio);
145  }
146 
156  int
157  readInt(const std::string& attributeName) const
158  {
159  int value;
160  mraa_result_t res = mraa_iio_read_int(m_iio, attributeName.c_str(), &value);
161  if (res != MRAA_SUCCESS) {
162  std::ostringstream oss;
163  oss << "IIO readInt for attibute " << attributeName << " failed";
164  throw std::runtime_error(oss.str());
165  }
166  return value;
167  }
168 
178  float
179  readFloat(const std::string& attributeName) const
180  {
181  float value;
182  mraa_result_t res = mraa_iio_read_float(m_iio, attributeName.c_str(), &value);
183  if (res != MRAA_SUCCESS) {
184  std::ostringstream oss;
185  oss << "IIO readFloat for attibute " << attributeName << " failed";
186  throw std::runtime_error(oss.str());
187  }
188  return value;
189  }
190 
199  void
200  writeInt(const std::string& attributeName, int value) const
201  {
202  mraa_result_t res = mraa_iio_write_int(m_iio, attributeName.c_str(), value);
203  if (res != MRAA_SUCCESS) {
204  std::ostringstream oss;
205  oss << "IIO writeInt for attibute " << attributeName << " failed";
206  throw std::runtime_error(oss.str());
207  }
208  }
209 
218  void
219  writeFloat(const std::string& attributeName, float value) const
220  {
221  mraa_result_t res = mraa_iio_write_float(m_iio, attributeName.c_str(), value);
222  if (res != MRAA_SUCCESS) {
223  std::ostringstream oss;
224  oss << "IIO writeFloat for attibute " << attributeName << " failed";
225  throw std::runtime_error(oss.str());
226  }
227  }
228 
236  void
238  {
239  mraa_result_t res = mraa_iio_event_setup_callback(m_iio, private_event_handler, handler);
240  if (res != MRAA_SUCCESS) {
241  throw std::runtime_error("registerEventHandler failed");
242  }
243  }
244 
245  private:
246  static void
247  private_event_handler(iio_event_data* data, void* args)
248  {
249  if (args != NULL) {
250  IioHandler* handler = (IioHandler*) args;
251  IioEventData eventData;
252  int chan_type, modifier, type, direction, channel, channel2, different;
253  mraa_iio_event_extract_event(data, &chan_type, &modifier, &type, &direction, &channel,
254  &channel2, &different);
255  eventData.channelType = chan_type;
256  eventData.modifier = modifier;
257  eventData.type = type;
258  eventData.direction = direction;
259  eventData.channel = channel;
260  eventData.channel2 = channel2;
261  eventData.diff = different;
262  handler->onIioEvent(eventData);
263  }
264  }
265 
266  mraa_iio_context m_iio;
267 };
268 }
~Iio()
Definition: iio.hpp:130
mraa_result_t mraa_iio_read_int(mraa_iio_context dev, const char *filename, int *data)
Definition: iio.hpp:54
Iio(int device)
Definition: iio.hpp:82
int mraa_iio_get_device_num_by_name(const char *name)
virtual void onIioEvent(const IioEventData &eventData)=0
int type
Definition: iio.hpp:42
mraa_result_t mraa_iio_close(mraa_iio_context dev)
int channelType
Definition: iio.hpp:38
Definition: types.h:210
mraa_result_t mraa_iio_write_float(mraa_iio_context dev, const char *attr_chan, const float data)
std::string getDeviceName() const
Definition: iio.hpp:142
int readInt(const std::string &attributeName) const
Definition: iio.hpp:157
API to Industrial IO.
Definition: iio.hpp:71
Iio(const std::string &deviceName)
Definition: iio.hpp:99
const char * mraa_iio_get_device_name(mraa_iio_context dev)
void registerEventHandler(IioHandler *handler) const
Definition: iio.hpp:237
float readFloat(const std::string &attributeName) const
Definition: iio.hpp:179
mraa_result_t mraa_iio_read_float(mraa_iio_context dev, const char *filename, float *data)
mraa_result_t mraa_iio_event_setup_callback(mraa_iio_context dev, void(*fptr)(struct iio_event_data *data, void *args), void *args)
int modifier
Definition: iio.hpp:40
Iio(void *iio_context)
Definition: iio.hpp:119
mraa_iio_context mraa_iio_init(int device)
void writeFloat(const std::string &attributeName, float value) const
Definition: iio.hpp:219
iio
mraa_result_t mraa_iio_event_extract_event(struct iio_event_data *event, int *chan_type, int *modifier, int *type, int *direction, int *channel, int *channel2, int *different)
Definition: iio.hpp:36
int channel
Definition: iio.hpp:46
int channel2
Definition: iio.hpp:48
struct _iio * mraa_iio_context
Definition: iio.h:87
Definition: aio.hpp:31
mraa_result_t mraa_iio_write_int(mraa_iio_context dev, const char *attr_chan, const int data)
int diff
Definition: iio.hpp:50
mraa_result_t
Definition: types.h:209
void writeInt(const std::string &attributeName, int value) const
Definition: iio.hpp:200
Definition: iio_kernel_headers.h:119
virtual ~IioHandler()
Definition: iio.hpp:60
int direction
Definition: iio.hpp:44