Home | History | Annotate | Download | only in mraa
      1 /*
      2  * Author: Henry Bruce <henry.bruce (at) 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 struct IioEventData
     36 {
     37     int channelType;
     38     int modifier;
     39     int type;
     40     int direction;
     41     int channel;
     42     int channel2;
     43     int diff;
     44 };
     45 
     46 class IioHandler
     47 {
     48 public:
     49   virtual void onIioEvent(const IioEventData& eventData) = 0;
     50 };
     51 
     52 
     53 /**
     54  * @brief API to Industrial IO
     55  *
     56  * This file defines the C++ iio interface for libmraa
     57  *
     58  * @snippet iio_dummy_test.cpp Interesting
     59  */
     60 class Iio
     61 {
     62   public:
     63     /**
     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
     66      *
     67      * @param device IIO device number
     68      *
     69      * @throws std::invalid_argument if initialization fails
     70      */
     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 
     81     /**
     82      * Iio Constructor
     83      *
     84      * @param deviceName IIO device name
     85      *
     86      * @throws std::invalid_argument if initialization fails
     87      */
     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 
    103     /**
    104      * Iio destructor
    105      */
    106     ~Iio()
    107     {
    108         mraa_iio_close(m_iio);
    109     }
    110 
    111 
    112     /**
    113      * Get device name
    114      *
    115      * @returns The device name
    116      */
    117     std::string
    118     getDeviceName() const
    119     {
    120         return mraa_iio_get_device_name(m_iio);
    121     }
    122 
    123     /**
    124      * Read an int value from specified attribute.
    125      *
    126      * @param attributeName attribute mame
    127      *
    128      * @returns The int value
    129      *
    130      * @throws std::invalid_argument if read fails
    131      */
    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 
    145     /**
    146      * Read a float value from specified attribute.
    147      *
    148      * @param attributeName attribute mame
    149      *
    150      * @returns The float value
    151      *
    152      * @throws std::invalid_argument if read fails
    153      */
    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 
    167     /**
    168      * Write an int value to specified attribute.
    169      *
    170      * @param attributeName attribute mame
    171      * @param value int value
    172      *
    173      * @throws std::invalid_argument if write fails
    174      */
    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 
    187     /**
    188      * Write a float value to specified attribute.
    189      *
    190      * @param attributeName attribute mame
    191      * @param value float value
    192      *
    193      * @throws std::invalid_argument if write fails
    194      */
    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 
    207     /**
    208      * Register event handler.
    209      *
    210      * @param handler handler class that implements IioHandler
    211      *
    212      * @throws std::invalid_argument on failure
    213      */
    214     void
    215     registerEventHandler(IioHandler* handler) const
    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 }
    246