Private
Public Access
2
0

iio: C API changes and C++ API enhancements

- C API read/write integer functions changed to int to match C types
- C API now has close function to release resources acquired during init
- iio internal type isr_event() function now has args param in signature
- C++ API now supports events with handler interface and new data structure
- C and C++ examples updated to use API changes

Signed-off-by: Henry Bruce <henry.bruce@intel.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Henry Bruce
2015-12-14 11:50:30 -08:00
committed by Brendan Le Foll
parent 5f01de1bf1
commit 8e4a809f12
6 changed files with 186 additions and 42 deletions

View File

@@ -22,7 +22,7 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// #include <unistd.h>
#include <unistd.h>
#include <iostream>
#include <math.h>
#include <float.h>
@@ -31,6 +31,18 @@
#define EXPECT_FAILURE 0
#define EXPECT_SUCCESS 1
#define IIO_TRY(func) \
{ \
bool success = true; \
try { \
iio_device->func; \
} catch (std::exception& e) { \
success = false; \
} \
log_result(#func, "", true, success); \
}
// Macro to run IIO method on attribute and log output
#define IIO_RUN(func, attr, value, expect) \
{ \
std::string attr_name = attr; \
@@ -43,6 +55,7 @@
log_result(#func, attr_name, expect, success); \
}
// Macro to run IIO method on attribute and check for expected result and log output
#define IIO_TEST(func, attr, value, expect) \
{ \
std::string attr_name = attr; \
@@ -56,7 +69,9 @@
}
mraa::Iio* iio_device;
int eventCount = 0;
// Log result of test. Note a "fail" (i.e. success is false) will be displayed as a pass if a fail was expected
void log_result(std::string test_name, std::string attr_name, bool expect_success, bool success)
{
std::string result;
@@ -64,35 +79,79 @@ void log_result(std::string test_name, std::string attr_name, bool expect_succes
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());
if (attr_name.empty())
fprintf(stdout, "%s: %s\n", test_name.c_str(), result.c_str());
else
fprintf(stdout, "%s(%s): %s\n", test_name.c_str(), attr_name.c_str(), result.c_str());
}
// Generate iio_dummy driver event by writing a string to a specific sysfs node
bool generate_event()
{
FILE *fp = fopen("/sys/bus/iio/devices/iio_evgen/poke_ev0", "w");
if (fp == NULL)
return false;
fprintf(fp, "1\n");
fclose(fp);
return true;
}
// IIO event handler that checks for event from dummy_iio_evgen driver
class IioTestHandler : public mraa::IioHandler
{
protected:
void onIioEvent(const mraa::IioEventData& eventData) {
if (eventData.channelType == IIO_VOLTAGE && eventData.direction == IIO_EV_DIR_RISING && eventData.type == IIO_EV_TYPE_THRESH)
eventCount++;
}
};
int
main()
{
IioTestHandler testHandler;
std::string deviceName;
try {
iio_device = new mraa::Iio(0);
mraa::Iio* iio_device0 = new mraa::Iio(0);
std::cout << "IIO device 0 found by id." << std::endl;
deviceName = iio_device0->getDeviceName();
delete iio_device0;
} catch (std::exception& e) {
std::cerr << "IIO device 0 not found" << std::endl;
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;
mraa::Iio* iio_device1 = new mraa::Iio(1);
delete iio_device1;
} catch (std::exception& e) {
std::cerr << "IIO device 1 not found" << std::endl;
std::cerr << "IIO device 1 not found. This is expected behavior." << std::endl;
}
try {
iio_device = new mraa::Iio(deviceName);
std::cout << "IIO device 0 found by name." << std::endl;
} catch (std::exception& e) {
std::cerr << "IIO device 0 not found." << std::endl;
return EXIT_FAILURE;
}
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;
IIO_TEST(readFloat, "in_voltage0_scale", 0.001333, EXPECT_SUCCESS);
IIO_RUN(writeInt, "events/in_voltage0_thresh_rising_en", 1, EXPECT_SUCCESS);
IIO_TRY(registerEventHandler(&testHandler));
eventCount = 0;
generate_event();
usleep(500000);
log_result("eventReceived", "", (eventCount == 1), true);
delete iio_device;
return EXIT_SUCCESS;
}

View File

@@ -93,7 +93,7 @@ main()
}
float iio_float;
int iio_integer;
int iio_int;
mraa_result_t ret;
ret = mraa_iio_write_float(iio_device0, "in_accel_scale", 0.019163);
@@ -106,14 +106,14 @@ main()
fprintf(stdout, "IIO read %f\n", iio_float);
}
ret = mraa_iio_write_integer(iio_device0, "scan_elements/in_accel_x_en", 1);
ret = mraa_iio_write_int(iio_device0, "scan_elements/in_accel_x_en", 1);
if (ret == MRAA_SUCCESS) {
fprintf(stdout, "IIO write success\n", iio_integer);
fprintf(stdout, "IIO write success\n");
}
ret = mraa_iio_read_integer(iio_device0, "scan_elements/in_accel_x_en", &iio_integer);
ret = mraa_iio_read_int(iio_device0, "scan_elements/in_accel_x_en", &iio_int);
if (ret == MRAA_SUCCESS) {
fprintf(stdout, "IIO read %d\n", iio_integer);
fprintf(stdout, "IIO read %d\n", iio_int);
}
if (mraa_iio_trigger_buffer(iio_device0, interrupt, NULL) == MRAA_SUCCESS) {
@@ -127,7 +127,7 @@ main()
if (iio_device6 == NULL) {
return EXIT_FAILURE;
}
mraa_iio_write_integer(iio_device6, "events/in_proximity2_thresh_either_en", 1);
mraa_iio_write_int(iio_device6, "events/in_proximity2_thresh_either_en", 1);
// Blocking until event fired