examples: Converted iio dummy driver test app to C++
Signed-off-by: Henry Bruce <henry.bruce@intel.com>
This commit is contained in:
committed by
Brendan Le Foll
parent
bd4f6998a7
commit
3429b5383d
@@ -13,7 +13,6 @@ add_executable (mraa-gpio mraa-gpio.c)
|
||||
add_executable (mraa-i2c mraa-i2c.c)
|
||||
add_executable (spi_max7219 spi_max7219.c)
|
||||
add_executable (iio_driver iio_driver.c)
|
||||
add_executable (iio_dummy_test iio_dummy_test.c)
|
||||
|
||||
include_directories(${PROJECT_SOURCE_DIR}/api)
|
||||
# FIXME Hack to access mraa internal types used by mraa-i2c
|
||||
@@ -35,7 +34,6 @@ target_link_libraries (mraa-gpio mraa)
|
||||
target_link_libraries (mraa-i2c mraa)
|
||||
target_link_libraries (spi_max7219 mraa)
|
||||
target_link_libraries (iio_driver mraa)
|
||||
target_link_libraries (iio_dummy_test mraa)
|
||||
|
||||
add_subdirectory (c++)
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ add_executable (I2c-compass I2c-compass.cpp)
|
||||
add_executable (Spi-pot Spi-pot.cpp)
|
||||
add_executable (Uart Uart-example.cpp)
|
||||
add_executable (Isr-pin6 Isr-pin6.cpp)
|
||||
add_executable (Iio-dummy Iio-dummy.cpp)
|
||||
|
||||
include_directories(${PROJECT_SOURCE_DIR}/api)
|
||||
|
||||
@@ -15,3 +16,4 @@ target_link_libraries (I2c-compass mraa stdc++ m)
|
||||
target_link_libraries (Spi-pot mraa stdc++)
|
||||
target_link_libraries (Uart mraa stdc++)
|
||||
target_link_libraries (Isr-pin6 mraa stdc++)
|
||||
target_link_libraries (Iio-dummy mraa stdc++)
|
||||
|
||||
98
examples/c++/Iio-dummy.cpp
Normal file
98
examples/c++/Iio-dummy.cpp
Normal file
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Author: Henry Bruce
|
||||
* Copyright (c) 2015 Intel Corporation.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
// #include <unistd.h>
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
#include <float.h>
|
||||
#include "mraa/iio.hpp"
|
||||
|
||||
#define EXPECT_FAILURE 0
|
||||
#define EXPECT_SUCCESS 1
|
||||
|
||||
#define IIO_RUN(func, attr, value, expect) \
|
||||
{ \
|
||||
std::string attr_name = attr; \
|
||||
bool success = true; \
|
||||
try { \
|
||||
iio_device->func(attr_name, value); \
|
||||
} catch (std::exception& e) { \
|
||||
success = false; \
|
||||
} \
|
||||
log_result(#func, attr_name, expect, success); \
|
||||
}
|
||||
|
||||
#define IIO_TEST(func, attr, value, expect) \
|
||||
{ \
|
||||
std::string attr_name = attr; \
|
||||
bool success = false; \
|
||||
try { \
|
||||
success = fabs(iio_device->func(attr_name) - value) < FLT_EPSILON; \
|
||||
} catch (std::exception& e) { \
|
||||
success = false; \
|
||||
} \
|
||||
log_result(#func, attr_name, expect, success); \
|
||||
}
|
||||
|
||||
mraa::Iio* iio_device;
|
||||
|
||||
void log_result(std::string test_name, std::string attr_name, bool expect_success, bool success)
|
||||
{
|
||||
std::string result;
|
||||
if (expect_success)
|
||||
result = success ? "PASS" : "FAIL";
|
||||
else
|
||||
result = success ? "FAIL" : "PASS";
|
||||
fprintf(stdout, "%s(%s): %s\n", test_name.c_str(), attr_name.c_str(), result.c_str());
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
try {
|
||||
iio_device = new mraa::Iio(0);
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << "IIO device 0 not found" << std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
try {
|
||||
mraa::Iio* bad_iio_device = new mraa::Iio(1);
|
||||
delete bad_iio_device;
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << "IIO device 1 not found" << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "Using IIO device0. Name is " << iio_device->getDeviceName() << std::endl;
|
||||
IIO_RUN(writeFloat, "in_accel_x_raw", 100, EXPECT_FAILURE);
|
||||
IIO_RUN(writeFloat, "in_voltage0_scale", 100, EXPECT_FAILURE);
|
||||
IIO_RUN(writeInt, "out_voltage0_raw", 100, EXPECT_SUCCESS);
|
||||
IIO_TEST(readInt, "in_accel_x_raw", 34, EXPECT_SUCCESS);
|
||||
IIO_TEST(readFloat, "in_voltage0_scale", 0.001333, EXPECT_SUCCESS);
|
||||
delete iio_device;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -1,85 +0,0 @@
|
||||
/*
|
||||
* Author: Henry Bruce
|
||||
* Copyright (c) 2015 Intel Corporation.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <unistd.h>
|
||||
#include <math.h>
|
||||
#include <float.h>
|
||||
#include "mraa/iio.h"
|
||||
|
||||
#define EXPECT_FAILURE 0
|
||||
#define EXPECT_SUCCESS 1
|
||||
|
||||
void log_result(const char* test_name, const char* attr_name, mraa_boolean_t expect_success, mraa_result_t test_result)
|
||||
{
|
||||
char* result;
|
||||
if (expect_success)
|
||||
result = test_result == MRAA_SUCCESS ? "PASS" : "FAIL";
|
||||
else
|
||||
result = test_result == MRAA_SUCCESS ? "FAIL" : "PASS";
|
||||
fprintf(stdout, "%s(%s): %s\n", test_name, attr_name, result);
|
||||
}
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
mraa_iio_context iio_device = mraa_iio_init(0);
|
||||
char* attr_name;
|
||||
float iio_float;
|
||||
int iio_integer;
|
||||
mraa_result_t ret;
|
||||
|
||||
if (iio_device == NULL) {
|
||||
fprintf(stderr, "IIO device %d not found\n", 0);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
fprintf(stderr, "Using IIO device %s\n", mraa_iio_get_device_name(iio_device));
|
||||
|
||||
attr_name = "in_accel_x_raw";
|
||||
ret = mraa_iio_write_float(iio_device, attr_name, 100);
|
||||
log_result("iio_write_float", attr_name, EXPECT_FAILURE, ret);
|
||||
|
||||
attr_name = "in_voltage0_scale";
|
||||
ret = mraa_iio_write_float(iio_device, attr_name, 100);
|
||||
log_result("iio_write_float", attr_name, EXPECT_FAILURE, ret);
|
||||
|
||||
attr_name = "out_voltage0_raw";
|
||||
ret = mraa_iio_write_integer(iio_device, attr_name, 100);
|
||||
log_result("iio_write_integer", attr_name, EXPECT_SUCCESS, ret);
|
||||
|
||||
attr_name = "in_accel_x_raw";
|
||||
ret = mraa_iio_read_integer(iio_device, attr_name, &iio_integer);
|
||||
ret = iio_integer == 34 ? MRAA_SUCCESS : MRAA_ERROR_UNSPECIFIED;
|
||||
log_result("iio_read_integer", attr_name, EXPECT_SUCCESS, ret);
|
||||
|
||||
attr_name = "in_voltage0_scale";
|
||||
ret = mraa_iio_read_float(iio_device, attr_name, &iio_float);
|
||||
ret = fabs(iio_float - 0.001333) < FLT_EPSILON ? MRAA_SUCCESS : MRAA_ERROR_UNSPECIFIED;
|
||||
log_result("iio_read_float", attr_name, EXPECT_SUCCESS, ret);
|
||||
|
||||
attr_name = "";
|
||||
ret = mraa_iio_stop(iio_device);
|
||||
log_result("iio_stop", attr_name, EXPECT_SUCCESS, ret);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user