examples: Converted iio dummy driver test app to C++
[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_RUN(func, attr, value, expect) \
35 { \
36     std::string attr_name = attr; \
37     bool success = true;    \
38     try { \
39         iio_device->func(attr_name, value);  \
40     } catch (std::exception& e) { \
41         success = false; \
42     } \
43     log_result(#func, attr_name, expect, success); \
44 }
45
46 #define IIO_TEST(func, attr, value, expect) \
47 { \
48     std::string attr_name = attr; \
49     bool success = false;    \
50     try { \
51         success = fabs(iio_device->func(attr_name) - value) < FLT_EPSILON; \
52     } catch (std::exception& e) { \
53         success = false; \
54     } \
55     log_result(#func, attr_name, expect, success); \
56 }
57
58 mraa::Iio* iio_device;
59
60 void log_result(std::string test_name, std::string attr_name, bool expect_success, bool success)
61 {
62     std::string result;
63     if (expect_success)
64        result = success ? "PASS" : "FAIL";
65     else
66        result = success ? "FAIL" : "PASS";
67     fprintf(stdout, "%s(%s): %s\n", test_name.c_str(), attr_name.c_str(), result.c_str());
68 }
69
70
71 int
72 main()
73 {
74     try {
75         iio_device = new mraa::Iio(0);      
76     } catch (std::exception& e) {
77         std::cerr << "IIO device 0 not found" << std::endl;
78         return EXIT_FAILURE;
79     }
80
81     try {
82         mraa::Iio* bad_iio_device = new mraa::Iio(1);        
83         delete bad_iio_device;
84     } catch (std::exception& e) {
85         std::cerr << "IIO device 1 not found" << std::endl;
86     }
87
88     std::cout << "Using IIO device0. Name is " << iio_device->getDeviceName() << std::endl;          
89     IIO_RUN(writeFloat, "in_accel_x_raw", 100, EXPECT_FAILURE);
90     IIO_RUN(writeFloat, "in_voltage0_scale", 100, EXPECT_FAILURE);    
91     IIO_RUN(writeInt, "out_voltage0_raw", 100, EXPECT_SUCCESS);        
92     IIO_TEST(readInt, "in_accel_x_raw", 34, EXPECT_SUCCESS);
93     IIO_TEST(readFloat, "in_voltage0_scale", 0.001333, EXPECT_SUCCESS);    
94     delete iio_device;
95
96     return EXIT_SUCCESS;
97 }
98