2 * Author: Henry Bruce <henry.bruce@intel.com>
3 * Copyright (c) 2015 Intel Corporation.
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:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
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.
49 virtual void onIioEvent(const IioEventData& eventData) = 0;
54 * @brief API to Industrial IO
56 * This file defines the C++ iio interface for libmraa
58 * @snippet iio_dummy_test.cpp Interesting
64 * Iio Constructor, takes a device number which will map directly to sysfs
65 * e.g. device 0 maps to /sys/bus/iio/devices/iio:device0
67 * @param device IIO device number
69 * @throws std::invalid_argument if initialization fails
73 m_iio = mraa_iio_init(device);
75 std::ostringstream oss;
76 oss << "IIO device " << device << " is not valid";
77 throw std::invalid_argument(oss.str());
84 * @param deviceName IIO device name
86 * @throws std::invalid_argument if initialization fails
88 Iio(const std::string& deviceName)
90 std::ostringstream oss;
91 int id = mraa_iio_get_device_num_by_name(deviceName.c_str());
93 oss << "IIO device name " << deviceName << " not found";
94 throw std::invalid_argument(oss.str());
96 m_iio = mraa_iio_init(id);
98 oss << "IIO device " << deviceName << " is not valid";
99 throw std::invalid_argument(oss.str());
108 mraa_iio_close(m_iio);
115 * @returns The device name
118 getDeviceName() const
120 return mraa_iio_get_device_name(m_iio);
124 * Read an int value from specified attribute.
126 * @param attributeName attribute mame
128 * @returns The int value
130 * @throws std::invalid_argument if read fails
133 readInt(const std::string& attributeName) const
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());
146 * Read a float value from specified attribute.
148 * @param attributeName attribute mame
150 * @returns The float value
152 * @throws std::invalid_argument if read fails
155 readFloat(const std::string& attributeName) const
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());
168 * Write an int value to specified attribute.
170 * @param attributeName attribute mame
171 * @param value int value
173 * @throws std::invalid_argument if write fails
176 writeInt(const std::string& attributeName, int value) const
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());
188 * Write a float value to specified attribute.
190 * @param attributeName attribute mame
191 * @param value float value
193 * @throws std::invalid_argument if write fails
196 writeFloat(const std::string& attributeName, float value) const
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());
208 * Register event handler.
210 * @param handler handler class that implements IioHandler
212 * @throws std::invalid_argument on failure
215 registerEventHandler(IioHandler* handler) const
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");
224 static void private_event_handler(iio_event_data* data, void *args)
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);
242 mraa_iio_context m_iio;