mraa  1.8.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 <stdexcept>
28 #include <sstream>
29 #include "iio.h"
30 #include "types.hpp"
31 
32 namespace mraa
33 {
34 
37 {
41  int modifier;
43  int type;
45  int direction;
47  int channel;
49  int channel2;
51  int diff;
52 };
53 
56 {
57 public:
59  virtual void onIioEvent(const IioEventData& eventData) = 0;
61  virtual ~IioHandler() {}; // add an empty destructor to get rid of warning
62 };
63 
64 
72 class Iio
73 {
74  public:
83  Iio(int device)
84  {
85  m_iio = mraa_iio_init(device);
86  if (m_iio == NULL) {
87  std::ostringstream oss;
88  oss << "IIO device " << device << " is not valid";
89  throw std::invalid_argument(oss.str());
90  }
91  }
92 
100  Iio(const std::string& deviceName)
101  {
102  std::ostringstream oss;
103  int id = mraa_iio_get_device_num_by_name(deviceName.c_str());
104  if (id == -1) {
105  oss << "IIO device name " << deviceName << " not found";
106  throw std::invalid_argument(oss.str());
107  }
108  m_iio = mraa_iio_init(id);
109  if (m_iio == NULL) {
110  oss << "IIO device " << deviceName << " is not valid";
111  throw std::invalid_argument(oss.str());
112  }
113  }
114 
119  {
120  mraa_iio_close(m_iio);
121  }
122 
123 
129  std::string
131  {
132  return mraa_iio_get_device_name(m_iio);
133  }
134 
144  int
145  readInt(const std::string& attributeName) const
146  {
147  int value;
148  mraa_result_t res = mraa_iio_read_int(m_iio, attributeName.c_str(), &value);
149  if (res != MRAA_SUCCESS) {
150  std::ostringstream oss;
151  oss << "IIO readInt for attibute " << attributeName << " failed";
152  throw std::runtime_error(oss.str());
153  }
154  return value;
155  }
156 
166  float
167  readFloat(const std::string& attributeName) const
168  {
169  float value;
170  mraa_result_t res = mraa_iio_read_float(m_iio, attributeName.c_str(), &value);
171  if (res != MRAA_SUCCESS) {
172  std::ostringstream oss;
173  oss << "IIO readFloat for attibute " << attributeName << " failed";
174  throw std::runtime_error(oss.str());
175  }
176  return value;
177  }
178 
187  void
188  writeInt(const std::string& attributeName, int value) const
189  {
190  mraa_result_t res = mraa_iio_write_int(m_iio, attributeName.c_str(), value);
191  if (res != MRAA_SUCCESS) {
192  std::ostringstream oss;
193  oss << "IIO writeInt for attibute " << attributeName << " failed";
194  throw std::runtime_error(oss.str());
195  }
196 
197  }
198 
207  void
208  writeFloat(const std::string& attributeName, float value) const
209  {
210  mraa_result_t res = mraa_iio_write_float(m_iio, attributeName.c_str(), value);
211  if (res != MRAA_SUCCESS) {
212  std::ostringstream oss;
213  oss << "IIO writeFloat for attibute " << attributeName << " failed";
214  throw std::runtime_error(oss.str());
215  }
216 
217  }
218 
226  void
228  {
229  mraa_result_t res = mraa_iio_event_setup_callback(m_iio, private_event_handler, handler);
230  if (res != MRAA_SUCCESS) {
231  throw std::runtime_error("registerEventHandler failed");
232  }
233  }
234 
235  private:
236  static void private_event_handler(iio_event_data* data, void *args)
237  {
238  if (args != NULL) {
239  IioHandler* handler = (IioHandler*)args;
240  IioEventData eventData;
241  int chan_type, modifier, type, direction, channel, channel2, different;
242  mraa_iio_event_extract_event(data, &chan_type, &modifier, &type, &direction, &channel, &channel2, &different);
243  eventData.channelType = chan_type;
244  eventData.modifier = modifier;
245  eventData.type = type;
246  eventData.direction = direction;
247  eventData.channel = channel;
248  eventData.channel2 = channel2;
249  eventData.diff = different;
250  handler->onIioEvent(eventData);
251  }
252  }
253 
254  mraa_iio_context m_iio;
255 };
256 
257 }
~Iio()
Definition: iio.hpp:118
mraa_result_t mraa_iio_read_int(mraa_iio_context dev, const char *filename, int *data)
Definition: iio.hpp:55
Iio(int device)
Definition: iio.hpp:83
int mraa_iio_get_device_num_by_name(const char *name)
virtual void onIioEvent(const IioEventData &eventData)=0
int type
Definition: iio.hpp:43
mraa_result_t mraa_iio_close(mraa_iio_context dev)
int channelType
Definition: iio.hpp:39
Definition: types.h:209
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:130
int readInt(const std::string &attributeName) const
Definition: iio.hpp:145
API to Industrial IO.
Definition: iio.hpp:72
Iio(const std::string &deviceName)
Definition: iio.hpp:100
const char * mraa_iio_get_device_name(mraa_iio_context dev)
void registerEventHandler(IioHandler *handler) const
Definition: iio.hpp:227
float readFloat(const std::string &attributeName) const
Definition: iio.hpp:167
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:41
mraa_iio_context mraa_iio_init(int device)
void writeFloat(const std::string &attributeName, float value) const
Definition: iio.hpp:208
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:47
int channel2
Definition: iio.hpp:49
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:51
mraa_result_t
Definition: types.h:208
void writeInt(const std::string &attributeName, int value) const
Definition: iio.hpp:188
Definition: iio_kernel_headers.h:119
virtual ~IioHandler()
Definition: iio.hpp:61
int direction
Definition: iio.hpp:45