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.
25 // #include <unistd.h>
29 #include "mraa/iio.hpp"
31 #define EXPECT_FAILURE 0
32 #define EXPECT_SUCCESS 1
34 #define IIO_RUN(func, attr, value, expect) \
36 std::string attr_name = attr; \
37 bool success = true; \
39 iio_device->func(attr_name, value); \
40 } catch (std::exception& e) { \
43 log_result(#func, attr_name, expect, success); \
46 #define IIO_TEST(func, attr, value, expect) \
48 std::string attr_name = attr; \
49 bool success = false; \
51 success = fabs(iio_device->func(attr_name) - value) < FLT_EPSILON; \
52 } catch (std::exception& e) { \
55 log_result(#func, attr_name, expect, success); \
58 mraa::Iio* iio_device;
60 void log_result(std::string test_name, std::string attr_name, bool expect_success, bool success)
64 result = success ? "PASS" : "FAIL";
66 result = success ? "FAIL" : "PASS";
67 fprintf(stdout, "%s(%s): %s\n", test_name.c_str(), attr_name.c_str(), result.c_str());
75 iio_device = new mraa::Iio(0);
76 } catch (std::exception& e) {
77 std::cerr << "IIO device 0 not found" << std::endl;
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;
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);