api: Added C++ API for IIO
[contrib/mraa.git] / api / mraa / 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
35 /**
36  * @brief API to Industrial IO
37  *
38  * This file defines the C++ iio interface for libmraa
39  *
40  * @snippet iio_dummy_test.cpp Interesting
41  */
42 class Iio
43 {
44   public:
45     /**
46      * Iio Constructor, takes a device number which will map directly to sysfs
47      * e.g. device 0 mAps to /sys/bus/iio/devices/iio:device0
48      *
49      * @param device IIO device number
50      *
51      * @throws std::invalid_argument if initialization fails
52      */
53     Iio(int device)
54     {
55         m_iio = mraa_iio_init(device);
56         if (m_iio == NULL) {
57             std::ostringstream oss;
58             oss << "IIO device " << device << " is not valid";
59             throw std::invalid_argument(oss.str());
60         }
61     }
62
63     /**
64      * Iio destructor
65      */
66     ~Iio()
67     {
68         mraa_iio_stop(m_iio);
69     }
70
71     
72     /**
73      * Get device name 
74      *
75      * @returns The device name
76      */
77     std::string
78     getDeviceName()
79     {
80         return mraa_iio_get_device_name(m_iio);
81     }
82
83     /**
84      * Read an integer value from specified attribute.
85      *
86      * @returns The integer value
87      *
88      * @throws std::invalid_argument if read fails
89      */
90     int
91     readInt(const std::string& attributeName)
92     {
93         int value;
94         mraa_result_t res = mraa_iio_read_integer(m_iio, attributeName.c_str(), &value);
95         if (res != MRAA_SUCCESS) {
96             std::ostringstream oss;
97             oss << "IIO readInt for attibute " << attributeName << " failed";
98             throw std::runtime_error(oss.str());
99         }
100         return value;
101     }
102
103     /**
104      * Read a float value from specified attribute. 
105      *
106      * @returns The float value
107      *
108      * @throws std::invalid_argument if read fails
109      */
110     float
111     readFloat(const std::string& attributeName)
112     {
113         float value;
114         mraa_result_t res = mraa_iio_read_float(m_iio, attributeName.c_str(), &value);
115         if (res != MRAA_SUCCESS) {
116             std::ostringstream oss;
117             oss << "IIO readFloat for attibute " << attributeName << " failed";
118             throw std::runtime_error(oss.str());
119         }
120         return value;
121     }
122
123     /**
124      * Write an integer value to specified attribute. 
125      *
126      * @throws std::invalid_argument if write fails
127      */
128     void
129     writeInt(const std::string& attributeName, int value)
130     {
131         mraa_result_t res = mraa_iio_write_integer(m_iio, attributeName.c_str(), value);
132         if (res != MRAA_SUCCESS) {
133             std::ostringstream oss;
134             oss << "IIO writeInt for attibute " << attributeName << " failed";
135             throw std::runtime_error(oss.str());
136         }
137
138     }
139
140     /**
141      * Write a float value to specified attribute. 
142      *
143      * @throws std::invalid_argument if write fails
144      */
145     void
146     writeFloat(const std::string& attributeName, float value)
147     {
148         mraa_result_t res = mraa_iio_write_float(m_iio, attributeName.c_str(), value);
149         if (res != MRAA_SUCCESS) {
150             std::ostringstream oss;
151             oss << "IIO writeFloat for attibute " << attributeName << " failed";
152             throw std::runtime_error(oss.str());
153         }
154
155     }
156
157
158   private:
159     mraa_iio_context m_iio;
160 };
161 }