70e11289bf6c3e9de9dadf4ac2ed33d164001d1d
[contrib/mraa.git] / examples / c++ / Iio-dummy.cpp
1 /*
2  * Author: Henry Bruce
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 #include <unistd.h>
26 #include <iostream> 
27 #include <math.h>
28 #include <float.h>
29 #include "mraa/iio.hpp"
30
31 #define EXPECT_FAILURE 0
32 #define EXPECT_SUCCESS 1
33
34 #define IIO_TRY(func) \
35 { \
36     bool success = true;    \
37     try { \
38         iio_device->func;  \
39     } catch (std::exception& e) { \
40         success = false; \
41     } \
42     log_result(#func, "", true, success); \
43 }
44
45 // Macro to run IIO method on attribute and log output
46 #define IIO_RUN(func, attr, value, expect) \
47 { \
48     std::string attr_name = attr; \
49     bool success = true;    \
50     try { \
51         iio_device->func(attr_name, value);  \
52     } catch (std::exception& e) { \
53         success = false; \
54     } \
55     log_result(#func, attr_name, expect, success); \
56 }
57
58 // Macro to run IIO method on attribute and check for expected result and log output
59 #define IIO_TEST(func, attr, value, expect) \
60 { \
61     std::string attr_name = attr; \
62     bool success = false;    \
63     try { \
64         success = fabs(iio_device->func(attr_name) - value) < FLT_EPSILON; \
65     } catch (std::exception& e) { \
66         success = false; \
67     } \
68     log_result(#func, attr_name, expect, success); \
69 }
70
71 mraa::Iio* iio_device;
72 int eventCount = 0;
73
74 // Log result of test. Note a "fail" (i.e. success is false) will be displayed as a pass if a fail was expected
75 void log_result(std::string test_name, std::string attr_name, bool expect_success, bool success)
76 {
77     std::string result;
78     if (expect_success)
79        result = success ? "PASS" : "FAIL";
80     else
81        result = success ? "FAIL" : "PASS";
82     if (attr_name.empty())
83         fprintf(stdout, "%s: %s\n", test_name.c_str(), result.c_str());   
84     else
85         fprintf(stdout, "%s(%s): %s\n", test_name.c_str(), attr_name.c_str(), result.c_str());
86 }
87
88 // Generate iio_dummy driver event by writing a string to a specific sysfs node
89 bool generate_event()
90 {
91     FILE *fp = fopen("/sys/bus/iio/devices/iio_evgen/poke_ev0", "w");
92     if (fp == NULL)
93         return false;
94     fprintf(fp, "1\n");
95     fclose(fp);
96     return true;
97 }
98
99
100 // IIO event handler that checks for event from dummy_iio_evgen driver
101 class IioTestHandler : public mraa::IioHandler
102 {
103 protected:
104     void onIioEvent(const mraa::IioEventData& eventData) {
105         if (eventData.channelType == IIO_VOLTAGE && eventData.direction == IIO_EV_DIR_RISING && eventData.type == IIO_EV_TYPE_THRESH)
106             eventCount++;
107     }
108 };
109
110 int
111 main()
112 {
113     IioTestHandler testHandler;
114     std::string deviceName;
115     try {
116         mraa::Iio* iio_device0 = new mraa::Iio(0);      
117         std::cout << "IIO device 0 found by id." << std::endl;
118         deviceName = iio_device0->getDeviceName();        
119         delete iio_device0;        
120     } catch (std::exception& e) {
121         std::cerr << "IIO device 0 not found." << std::endl;
122         return EXIT_FAILURE;
123     }
124
125     try {
126         mraa::Iio* iio_device1 = new mraa::Iio(1);        
127         delete iio_device1;
128     } catch (std::exception& e) {
129         std::cerr << "IIO device 1 not found. This is expected behavior." << std::endl;
130     }
131
132     try {
133         iio_device = new mraa::Iio(deviceName);      
134         std::cout << "IIO device 0 found by name." << std::endl;        
135     } catch (std::exception& e) {
136         std::cerr << "IIO device 0 not found." << std::endl;
137         return EXIT_FAILURE;
138     }
139
140
141     std::cout << "Using IIO device0. Name is " << iio_device->getDeviceName() << std::endl;          
142     IIO_RUN(writeFloat, "in_accel_x_raw", 100, EXPECT_FAILURE);
143     IIO_RUN(writeFloat, "in_voltage0_scale", 100, EXPECT_FAILURE);    
144     IIO_RUN(writeInt, "out_voltage0_raw", 100, EXPECT_SUCCESS);        
145     IIO_TEST(readInt, "in_accel_x_raw", 34, EXPECT_SUCCESS);
146     IIO_TEST(readFloat, "in_voltage0_scale", 0.001333, EXPECT_SUCCESS);
147     IIO_RUN(writeInt, "events/in_voltage0_thresh_rising_en", 1, EXPECT_SUCCESS);
148     IIO_TRY(registerEventHandler(&testHandler));    
149     eventCount = 0;
150     generate_event();
151     usleep(500000);
152     log_result("eventReceived", "", (eventCount == 1), true);
153
154     delete iio_device;
155     return EXIT_SUCCESS;
156 }
157