From 3ab6b5575c236b918f7e5f43352604a44d980ffc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergi=20Hern=C3=A0ndez=20Juan?= <shernand@iri.upc.edu> Date: Mon, 12 Jul 2010 13:56:12 +0000 Subject: [PATCH] added an example to test the basic features of the adapter --- src/examples/test_usb_i2c.cpp | 93 +++++++++++++++++++++++++++++++++++ src/usb_i2c.cpp | 1 + 2 files changed, 94 insertions(+) create mode 100644 src/examples/test_usb_i2c.cpp diff --git a/src/examples/test_usb_i2c.cpp b/src/examples/test_usb_i2c.cpp new file mode 100644 index 0000000..65586b5 --- /dev/null +++ b/src/examples/test_usb_i2c.cpp @@ -0,0 +1,93 @@ +#include "exceptions.h" +#include "usb_i2c.h" +#include <iostream> + +const std::string usb_port="/dev/ttyUSB0"; +const unsigned char dev_id=0x22; + +int main(int argc, char *argv[]) +{ + CUSBI2C adapter("adapter"); + int i=0; + + try{ + adapter.open(usb_port); + std::cout << "Firmware revision: " << (int)adapter.get_revision() << std::endl; + std::cout << "Turning the led off ..." << std::endl; + adapter.turn_led_off(); + sleep(1); + std::cout << "Turning the led on ..." << std::endl; + adapter.turn_led_on(); + std::cout << "Using the second general purose I/O as digital output ..." << std::endl; + adapter.config_gpio(gpio2,digital_output); + std::cout << " Outputing a high level ..." << std::endl; + adapter.set_gpio(gpio2,true); + sleep(2); + std::cout << " Outputing a low level ..." << std::endl; + adapter.set_gpio(gpio2,false); + sleep(2); + std::cout << "Using the second general purose I/O as digital input ..." << std::endl; + adapter.config_gpio(gpio2,digital_input); + std::cout << " Reading the current value ..." << std::endl; + for(i=0;i<10;i++) + { + if(adapter.get_gpio(gpio2)) + std::cout << " high level." << std::endl; + else + std::cout << " low level." << std::endl; + usleep(500000); + } + std::cout << "Using the second general purose I/O as analog input ..." << std::endl; + adapter.config_gpio(gpio2,analog_input); + std::cout << " Reading the current value ..." << std::endl; + for(i=0;i<10;i++) + { + std::cout << " " << adapter.get_analog(gpio2) << std::endl; + usleep(500000); + } + std::cout << "Using the second general purose I/O as I2C ..." << std::endl; + adapter.config_gpio(gpio2,i2c); + std::cout << "Using the third general purose I/O as digital output ..." << std::endl; + adapter.config_gpio(gpio1,digital_output); + std::cout << " Outputing a high level ..." << std::endl; + adapter.set_gpio(gpio3,true); + sleep(2); + std::cout << " Outputing a low level ..." << std::endl; + adapter.set_gpio(gpio3,false); + sleep(2); + std::cout << "Using the third general purose I/O as digital input ..." << std::endl; + adapter.config_gpio(gpio3,digital_input); + std::cout << " Reading the current value ..." << std::endl; + for(i=0;i<10;i++) + { + if(adapter.get_gpio(gpio3)) + std::cout << " high level." << std::endl; + else + std::cout << " low level." << std::endl; + usleep(500000); + } + std::cout << "Using the third general purose I/O as analog input ..." << std::endl; + adapter.config_gpio(gpio3,analog_input); + std::cout << " Reading the current value ..." << std::endl; + for(i=0;i<10;i++) + { + std::cout << " " << adapter.get_analog(gpio3) << std::endl; + usleep(500000); + } + std::cout << "Using the third general purose I/O as I2C ..." << std::endl; + adapter.config_gpio(gpio3,i2c); + std::cout << "Using the first general purose I/O as digital input ..." << std::endl; + adapter.config_gpio(gpio1,digital_input); + for(i=0;i<10;i++) + { + if(adapter.get_gpio(gpio1)) + std::cout << " high level." << std::endl; + else + std::cout << " low level." << std::endl; + usleep(500000); + } + }catch(CException &e) + { + std::cout << e.what() << std::endl; + } +} diff --git a/src/usb_i2c.cpp b/src/usb_i2c.cpp index 3a375e4..c6a511c 100644 --- a/src/usb_i2c.cpp +++ b/src/usb_i2c.cpp @@ -180,6 +180,7 @@ short int CUSBI2C::get_analog(gpio_pins pin) analog_value=answer[0]*256+answer[1]; else analog_value=answer[2]*256+answer[3]; + return analog_value; } else { -- GitLab