From af17df2714cf30005b7a51fb591668d9d4ce346e 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:25:20 +0000 Subject: [PATCH] Initial commit --- CMakeLists.txt | 66 +++++ Findusb_i2c_adapter.cmake | 21 ++ ReadMe.txt | 19 ++ doc/doxygen.conf | 251 +++++++++++++++++++ doc/doxygen_project_name.conf | 1 + doc/main.dox | 107 ++++++++ src/CMakeLists.txt | 28 +++ src/examples/CMakeLists.txt | 6 + src/usb_i2c.cpp | 456 ++++++++++++++++++++++++++++++++++ src/usb_i2c.h | 37 +++ 10 files changed, 992 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 Findusb_i2c_adapter.cmake create mode 100644 ReadMe.txt create mode 100644 doc/doxygen.conf create mode 100644 doc/doxygen_project_name.conf create mode 100644 doc/main.dox create mode 100644 src/CMakeLists.txt create mode 100644 src/examples/CMakeLists.txt create mode 100644 src/usb_i2c.cpp create mode 100644 src/usb_i2c.h diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..d6afd49 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,66 @@ +# Pre-requisites about cmake itself +CMAKE_MINIMUM_REQUIRED(VERSION 2.4) + +if(COMMAND cmake_policy) + cmake_policy(SET CMP0005 NEW) + cmake_policy(SET CMP0003 NEW) + +endif(COMMAND cmake_policy) + +# The project name and the type of project +PROJECT(library_name) + +SET(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/bin) +SET(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/lib) +SET(CMAKE_INSTALL_PREFIX /usr/local) + +IF (NOT CMAKE_BUILD_TYPE) + SET(CMAKE_BUILD_TYPE "DEBUG") +ENDIF (NOT CMAKE_BUILD_TYPE) + +SET(CMAKE_C_FLAGS_DEBUG "-g -Wall -D_REENTRANT") +SET(CMAKE_C_FLAGS_RELEASE "-O3 -D_REENTRANT") + +ADD_SUBDIRECTORY(src) + +FIND_PACKAGE(Doxygen) + +FIND_PATH(IRI_DOC_DIR doxygen.conf ${CMAKE_SOURCE_DIR}/doc/iri_doc/) +IF (IRI_DOC_DIR) + ADD_CUSTOM_TARGET (doc ${DOXYGEN_EXECUTABLE} ${CMAKE_SOURCE_DIR}/doc/iri_doc/doxygen.conf) +ELSE (IRI_DOC_DIR) + ADD_CUSTOM_TARGET (doc ${DOXYGEN_EXECUTABLE} ${CMAKE_SOURCE_DIR}/doc/doxygen.conf) +ENDIF (IRI_DOC_DIR) + +ADD_CUSTOM_TARGET (distclean @echo cleaning cmake files) + +IF (UNIX) + ADD_CUSTOM_COMMAND( + COMMENT "distribution clean" + COMMAND make ARGS clean + COMMAND rm ARGS -rf ${CMAKE_SOURCE_DIR}/build/* + + TARGET distclean + ) +ELSE(UNIX) + ADD_CUSTOM_COMMAND( + COMMENT "distclean only implemented in unix" + TARGET distclean + ) +ENDIF(UNIX) + +ADD_CUSTOM_TARGET (uninstall @echo uninstall package) + +IF (UNIX) + ADD_CUSTOM_COMMAND( + COMMENT "uninstall package" + COMMAND xargs ARGS rm < install_manifest.txt + + TARGET uninstall + ) +ELSE(UNIX) + ADD_CUSTOM_COMMAND( + COMMENT "uninstall only implemented in unix" + TARGET uninstall + ) +ENDIF(UNIX) diff --git a/Findusb_i2c_adapter.cmake b/Findusb_i2c_adapter.cmake new file mode 100644 index 0000000..6a0a047 --- /dev/null +++ b/Findusb_i2c_adapter.cmake @@ -0,0 +1,21 @@ +#edit the following line to add the librarie's header files +FIND_PATH(usb_i2c_adapter_INCLUDE_DIR <your own header files> /usr/include/iridrivers /usr/local/include/iridrivers) + +FIND_LIBRARY(usb_i2c_adapter_LIBRARY + NAMES usb_i2c_adapter + PATHS /usr/lib /usr/local/lib /usr/local/lib/iridrivers) + +IF (usb_i2c_adapter_INCLUDE_DIR AND usb_i2c_adapter_LIBRARY) + SET(usb_i2c_adapter_FOUND TRUE) +ENDIF (usb_i2c_adapter_INCLUDE_DIR AND usb_i2c_adapter_LIBRARY) + +IF (usb_i2c_adapter_FOUND) + IF (NOT usb_i2c_adapter_FIND_QUIETLY) + MESSAGE(STATUS "Found usb_i2c_adapter: ${usb_i2c_adapter_LIBRARY}") + ENDIF (NOT usb_i2c_adapter_FIND_QUIETLY) +ELSE (usb_i2c_adapter_FOUND) + IF (usb_i2c_adapter_FIND_REQUIRED) + MESSAGE(FATAL_ERROR "Could not find usb_i2c_adapter") + ENDIF (usb_i2c_adapter_FIND_REQUIRED) +ENDIF (usb_i2c_adapter_FOUND) + diff --git a/ReadMe.txt b/ReadMe.txt new file mode 100644 index 0000000..5694836 --- /dev/null +++ b/ReadMe.txt @@ -0,0 +1,19 @@ +Copyright (C) 2009-2010 Institut de Robòtica i Informà tica Industrial, CSIC-UPC. +Author shernand (shernand@iri.upc.edu) +All rights reserved. + +This file is part of USB to I2C adapter library +USB to I2C adapter library is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with this program. If not, see <http://www.gnu.org/licenses/> + + diff --git a/doc/doxygen.conf b/doc/doxygen.conf new file mode 100644 index 0000000..347af2e --- /dev/null +++ b/doc/doxygen.conf @@ -0,0 +1,251 @@ +# Doxyfile 1.5.5 + +@INCLUDE_PATH = ../doc/ +@INCLUDE = doxygen_project_name.conf + +#--------------------------------------------------------------------------- +# Project related configuration options +#--------------------------------------------------------------------------- +DOXYFILE_ENCODING = UTF-8 +PROJECT_NUMBER = +OUTPUT_DIRECTORY = ../doc +CREATE_SUBDIRS = NO +OUTPUT_LANGUAGE = English +BRIEF_MEMBER_DESC = YES +REPEAT_BRIEF = NO +ABBREVIATE_BRIEF = +ALWAYS_DETAILED_SEC = NO +INLINE_INHERITED_MEMB = NO +FULL_PATH_NAMES = YES +STRIP_FROM_PATH = +STRIP_FROM_INC_PATH = +SHORT_NAMES = NO +JAVADOC_AUTOBRIEF = NO +QT_AUTOBRIEF = NO +MULTILINE_CPP_IS_BRIEF = NO +DETAILS_AT_TOP = NO +INHERIT_DOCS = YES +SEPARATE_MEMBER_PAGES = NO +TAB_SIZE = 8 +ALIASES = +OPTIMIZE_OUTPUT_FOR_C = YES +OPTIMIZE_OUTPUT_JAVA = NO +OPTIMIZE_FOR_FORTRAN = NO +OPTIMIZE_OUTPUT_VHDL = NO +BUILTIN_STL_SUPPORT = NO +CPP_CLI_SUPPORT = NO +SIP_SUPPORT = NO +DISTRIBUTE_GROUP_DOC = NO +SUBGROUPING = YES +TYPEDEF_HIDES_STRUCT = NO +#--------------------------------------------------------------------------- +# Build related configuration options +#--------------------------------------------------------------------------- +EXTRACT_ALL = NO +EXTRACT_PRIVATE = NO +EXTRACT_STATIC = NO +EXTRACT_LOCAL_CLASSES = YES +EXTRACT_LOCAL_METHODS = NO +EXTRACT_ANON_NSPACES = NO +HIDE_UNDOC_MEMBERS = NO +HIDE_UNDOC_CLASSES = NO +HIDE_FRIEND_COMPOUNDS = NO +HIDE_IN_BODY_DOCS = NO +INTERNAL_DOCS = NO +CASE_SENSE_NAMES = YES +HIDE_SCOPE_NAMES = NO +SHOW_INCLUDE_FILES = YES +INLINE_INFO = YES +SORT_MEMBER_DOCS = NO +SORT_BRIEF_DOCS = NO +SORT_GROUP_NAMES = NO +SORT_BY_SCOPE_NAME = NO +GENERATE_TODOLIST = YES +GENERATE_TESTLIST = YES +GENERATE_BUGLIST = YES +GENERATE_DEPRECATEDLIST= YES +ENABLED_SECTIONS = +MAX_INITIALIZER_LINES = 30 +SHOW_USED_FILES = YES +SHOW_DIRECTORIES = YES +FILE_VERSION_FILTER = +#--------------------------------------------------------------------------- +# configuration options related to warning and progress messages +#--------------------------------------------------------------------------- +QUIET = YES +WARNINGS = YES +WARN_IF_UNDOCUMENTED = YES +WARN_IF_DOC_ERROR = YES +WARN_NO_PARAMDOC = NO +WARN_FORMAT = "$file:$line: $text" +WARN_LOGFILE = +#--------------------------------------------------------------------------- +# configuration options related to the input files +#--------------------------------------------------------------------------- +INPUT = ../src \ + ../doc/main.dox +INPUT_ENCODING = UTF-8 +FILE_PATTERNS = *.c \ + *.h \ + *.cpp +RECURSIVE = YES +EXCLUDE = +EXCLUDE_SYMLINKS = NO +EXCLUDE_PATTERNS = *.tab.c \ + *.tab.h \ + lex* \ + *glr.h \ + *llr.h \ + *glr.c \ + *llr.c \ + *general.h +EXCLUDE_SYMBOLS = +EXAMPLE_PATH = ../src/examples +EXAMPLE_PATTERNS = +EXAMPLE_RECURSIVE = NO +IMAGE_PATH = ../doc/images +INPUT_FILTER = +FILTER_PATTERNS = +FILTER_SOURCE_FILES = NO +#--------------------------------------------------------------------------- +# configuration options related to source browsing +#--------------------------------------------------------------------------- +SOURCE_BROWSER = YES +INLINE_SOURCES = NO +STRIP_CODE_COMMENTS = YES +REFERENCED_BY_RELATION = YES +REFERENCES_RELATION = YES +REFERENCES_LINK_SOURCE = YES +USE_HTAGS = NO +VERBATIM_HEADERS = YES +#--------------------------------------------------------------------------- +# configuration options related to the alphabetical class index +#--------------------------------------------------------------------------- +ALPHABETICAL_INDEX = YES +COLS_IN_ALPHA_INDEX = 5 +IGNORE_PREFIX = +#--------------------------------------------------------------------------- +# configuration options related to the HTML output +#--------------------------------------------------------------------------- +GENERATE_HTML = YES +HTML_OUTPUT = html +HTML_FILE_EXTENSION = .html +HTML_HEADER = +HTML_FOOTER = +HTML_STYLESHEET = +HTML_ALIGN_MEMBERS = YES +GENERATE_HTMLHELP = NO +GENERATE_DOCSET = NO +DOCSET_FEEDNAME = "Doxygen generated docs" +DOCSET_BUNDLE_ID = org.doxygen.Project +HTML_DYNAMIC_SECTIONS = NO +CHM_FILE = +HHC_LOCATION = +GENERATE_CHI = NO +BINARY_TOC = NO +TOC_EXPAND = NO +DISABLE_INDEX = NO +ENUM_VALUES_PER_LINE = 4 +GENERATE_TREEVIEW = NO +TREEVIEW_WIDTH = 250 +#--------------------------------------------------------------------------- +# configuration options related to the LaTeX output +#--------------------------------------------------------------------------- +GENERATE_LATEX = NO +LATEX_OUTPUT = latex +LATEX_CMD_NAME = latex +MAKEINDEX_CMD_NAME = makeindex +COMPACT_LATEX = NO +PAPER_TYPE = a4 +EXTRA_PACKAGES = +LATEX_HEADER = +PDF_HYPERLINKS = YES +USE_PDFLATEX = NO +LATEX_BATCHMODE = NO +LATEX_HIDE_INDICES = NO +#--------------------------------------------------------------------------- +# configuration options related to the RTF output +#--------------------------------------------------------------------------- +GENERATE_RTF = NO +RTF_OUTPUT = rtf +COMPACT_RTF = NO +RTF_HYPERLINKS = NO +RTF_STYLESHEET_FILE = +RTF_EXTENSIONS_FILE = +#--------------------------------------------------------------------------- +# configuration options related to the man page output +#--------------------------------------------------------------------------- +GENERATE_MAN = NO +MAN_OUTPUT = man +MAN_EXTENSION = .3 +MAN_LINKS = NO +#--------------------------------------------------------------------------- +# configuration options related to the XML output +#--------------------------------------------------------------------------- +GENERATE_XML = NO +XML_OUTPUT = xml +XML_SCHEMA = +XML_DTD = +XML_PROGRAMLISTING = YES +#--------------------------------------------------------------------------- +# configuration options for the AutoGen Definitions output +#--------------------------------------------------------------------------- +GENERATE_AUTOGEN_DEF = NO +#--------------------------------------------------------------------------- +# configuration options related to the Perl module output +#--------------------------------------------------------------------------- +GENERATE_PERLMOD = NO +PERLMOD_LATEX = NO +PERLMOD_PRETTY = YES +PERLMOD_MAKEVAR_PREFIX = +#--------------------------------------------------------------------------- +# Configuration options related to the preprocessor +#--------------------------------------------------------------------------- +ENABLE_PREPROCESSING = YES +MACRO_EXPANSION = NO +EXPAND_ONLY_PREDEF = NO +SEARCH_INCLUDES = YES +INCLUDE_PATH = +INCLUDE_FILE_PATTERNS = +PREDEFINED = _USE_MPI=1 +EXPAND_AS_DEFINED = +SKIP_FUNCTION_MACROS = YES +#--------------------------------------------------------------------------- +# Configuration::additions related to external references +#--------------------------------------------------------------------------- +TAGFILES = +GENERATE_TAGFILE = +ALLEXTERNALS = NO +EXTERNAL_GROUPS = YES +PERL_PATH = /usr/bin/perl +#--------------------------------------------------------------------------- +# Configuration options related to the dot tool +#--------------------------------------------------------------------------- +CLASS_DIAGRAMS = YES +MSCGEN_PATH = +HIDE_UNDOC_RELATIONS = YES +HAVE_DOT = YES +CLASS_GRAPH = YES +COLLABORATION_GRAPH = YES +GROUP_GRAPHS = YES +UML_LOOK = NO +TEMPLATE_RELATIONS = NO +INCLUDE_GRAPH = NO +INCLUDED_BY_GRAPH = NO +CALL_GRAPH = YES +CALLER_GRAPH = YES +GRAPHICAL_HIERARCHY = YES +DIRECTORY_GRAPH = NO +DOT_IMAGE_FORMAT = png +DOT_PATH = +DOTFILE_DIRS = +DOT_GRAPH_MAX_NODES = 50 +MAX_DOT_GRAPH_DEPTH = 2 +DOT_TRANSPARENT = YES +DOT_MULTI_TARGETS = NO +GENERATE_LEGEND = YES +DOT_CLEANUP = YES +#--------------------------------------------------------------------------- +# Configuration::additions related to the search engine +#--------------------------------------------------------------------------- +SEARCHENGINE = NO diff --git a/doc/doxygen_project_name.conf b/doc/doxygen_project_name.conf new file mode 100644 index 0000000..939319f --- /dev/null +++ b/doc/doxygen_project_name.conf @@ -0,0 +1 @@ +PROJECT_NAME = "USB to I2C adapter" diff --git a/doc/main.dox b/doc/main.dox new file mode 100644 index 0000000..8fb2d65 --- /dev/null +++ b/doc/main.dox @@ -0,0 +1,107 @@ +/*! \mainpage USB to I2C adapter + + \section Introduction + + \subsection Pre-Requisites + + This package requires of the following libraries and packages + - <A href="http://www.cmake.org">cmake</A>, a cross-platform build system. + - <A href="http://www.doxygen.org">doxygen</a> and + <A href="http://www.graphviz.org">graphviz</a> to generate the documentation. + - stdc++, + . + + Under linux all of these utilities are available in ready-to-use packages. + + Under MacOS most of the packages are available via <a href="http://www.finkproject.org/">fink</a>. <br> + + \subsection Compilation + + Just download this package, uncompress it, and execute + - cd build + - cmake .. + . + to generate the makefile and then + - make + . + to obtain the shared library (in this case called <em>iriutils.so</em>) and + also all the example programs. + + The <em>cmake</em> only need to be executed once (make will automatically call + <em>cmake</em> if you modify one of the <em>CMakeList.txt</em> files). + + To generate this documentation type + - make doc + . + + The files in the <em>build</em> directory are genetated by <em>cmake</em> + and <em>make</em> and can be safely removed. + After doing so you will need to call cmake manually again. + + \subsection Configuration + + The default build mode is DEBUG. That is, objects and executables + include debug information. + + The RELEASE build mode optimizes for speed. To build in this mode + execute + - cmake .. -DCMAKE_BUILD_TYPE=RELEASE + . + The release mode will be kept until next time cmake is executed. + + \subsection Installation + + In order to be able to use the library, it it necessary to copy it into the system. + To do that, execute + + - make install + . + + as root and the shared libraries will be copied to <em>/usr/local/lib/iriutils</em> directory + and the header files will be copied to <em>/usr/local/include/iriutils</em> dierctory. At + this point, the library may be used by any user. + + To remove the library from the system, exceute + - make uninstall + . + as root, and all the associated files will be removed from the system. + + \section Customization + + To build a new application using these library, first it is necessary to locate if the library + has been installed or not using the following command + + - FIND_PACKAGE(usb_i2c_adapter REQUIRED) + + In the case that the package is present, it is necessary to add the header files directory to + the include directory path by using + + - INCLUDE_DIRECTORIES(${usb_i2c_adapter_INCLUDE_DIR}) + + Finally, it is also nevessary to link with the desired libraries by using the following command + + - TARGET_LINK_LIBRARIES(<executable name> ${usb_i2c_adapter_LIBRARY}) + . + + \section License + + This package is licensed under a + <a href="http://www.gnu.org/licenses/lgpl.html"> + LGPL 3.0 License</a>. + + \section Disclaimer + + This is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + + */ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..088139b --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,28 @@ +# edit the following line to add all the source code files of the library +SET(sources usb_i2c.cpp) +# edit the following line to add all the header files of the library +SET(headers usb_i2c.h) + +# edit the following line to find the necessary packages +FIND_PACKAGE(iriutils REQUIRED) +FIND_PACKAGE(comm REQUIRED) + +# edit the following line to add the necessary include directories +INCLUDE_DIRECTORIES(. ${iriutils_INCLUDE_DIR} ${comm_INCLUDE_DIR}) + +ADD_LIBRARY(usb_i2c_adapter SHARED ${sources}) + +#edit the following line to add the necessary system libraries (if any) +TARGET_LINK_LIBRARIES(usb_i2c_adapter ${iriutils_LIBRARY} ${comm_LIBRARY}) + +INSTALL(TARGETS usb_i2c_adapter + RUNTIME DESTINATION bin + LIBRARY DESTINATION lib/iridrivers + ARCHIVE DESTINATION lib/iridrivers +) + +INSTALL(FILES ${headers} DESTINATION include/iridrivers) + +INSTALL(FILES ../Findusb_i2c_adapter.cmake DESTINATION ${CMAKE_ROOT}/Modules/) + +ADD_SUBDIRECTORY(examples) diff --git a/src/examples/CMakeLists.txt b/src/examples/CMakeLists.txt new file mode 100644 index 0000000..4e01fdd --- /dev/null +++ b/src/examples/CMakeLists.txt @@ -0,0 +1,6 @@ +# edit the following line to add the source code for the example and the name of the executable +ADD_EXECUTABLE(test_usb_i2c test_usb_i2c.cpp) + +# edit the following line to add the necessary libraries +TARGET_LINK_LIBRARIES(test_usb_i2c usb_i2c_adapter ${iriutils_LIBRARY} ${comm_LIBRARY}) + diff --git a/src/usb_i2c.cpp b/src/usb_i2c.cpp new file mode 100644 index 0000000..3a375e4 --- /dev/null +++ b/src/usb_i2c.cpp @@ -0,0 +1,456 @@ +#include "usb_i2c.h" +#include "eventexceptions.h" + +CUSBI2C::CUSBI2C(const std::string &adapter_id) +{ + std::string comm_dev_id=adapter_id+"_comm_dev"; + + if(adapter_id.size()==0) + { + /* handle exceptions */ + } + else + { + this->adapter_id=adapter_id; + this->comm_dev=new CRS232(comm_dev_id); + this->gpio_config[gpio1]=digital_input; + this->gpio_config[gpio2]=i2c; + this->gpio_config[gpio3]=i2c; + this->rx_event_id.clear(); + this->event_server=CEventServer::instance(); + } +} + +void CUSBI2C::open(const std::string &port_id) +{ + TRS232_config config; + + if(this->comm_dev!=NULL) + this->comm_dev->open((void *)&port_id); + config.baud=19200; + config.num_bits=8; + config.parity=none; + config.stop_bits=2; + this->comm_dev->config(&config); + this->rx_event_id=this->comm_dev->get_rx_event_id(); +} + +unsigned char CUSBI2C::get_revision(void) +{ + unsigned char cmd[4]={0x5A,0x01,0x00,0x00}; + std::list<std::string> events; + unsigned char revision; + int written=0,read=0; + + events.push_back(this->rx_event_id); + if((written=this->comm_dev->write(cmd,4))!=4) + { + /* handle exceptions */ + } + else + { + this->event_server->wait_first(events); + read=this->comm_dev->read(&revision,1); + } + + return revision; +} + +void CUSBI2C::config_gpio(gpio_pins pin, gpio_types type) +{ + switch(pin) + { + case gpio1: if(type!=digital_input) + { + /* handle exceptions */ + } + else + { + this->gpio_config[pin]=digital_output; + this->set_gpio(pin,true); + this->gpio_config[pin]=type; + } + break; + case gpio2: this->gpio_config[pin]=digital_output; + if(type==digital_input || type==analog_input) + this->set_gpio(pin,true); + else + this->set_gpio(pin,false); + this->gpio_config[pin]=type; + break; + case gpio3: this->gpio_config[pin]=digital_output; + if(type==digital_input || type==analog_input) + this->set_gpio(pin,true); + else + this->set_gpio(pin,false); + this->gpio_config[pin]=type; + break; + } +} + +void CUSBI2C::set_gpio(gpio_pins pin, bool value) +{ + unsigned char cmd[4]={0x5A,0x11,0x00,0x00}; + unsigned char gpio_state=0x00; + std::list<std::string> events; + int written=0,read=0; + + if(this->gpio_config[pin]==digital_output) + { + events.push_back(this->rx_event_id); + if((written=this->comm_dev->write(cmd,4))!=4) + { + /* handle exceptions */ + } + else + { + this->event_server->wait_first(events); + read=this->comm_dev->read(&gpio_state,1); + if(value==true) + gpio_state|=(0x01<<(pin+1)); + else + gpio_state&=~(0x01<<(pin+1)); + cmd[1]=0x10; + cmd[2]=gpio_state; + if((written=this->comm_dev->write(cmd,4))!=4) + { + /* handle exceptions */ + } + else + { + this->event_server->wait_first(events); + read=this->comm_dev->read(&gpio_state,1); + } + } + } + else + { + /* handle exceptions */ + } +} + +bool CUSBI2C::get_gpio(gpio_pins pin) +{ + unsigned char cmd[4]={0x5A,0x11,0x00,0x00}; + unsigned char gpio_state=0x00; + std::list<std::string> events; + int written=0,read=0; + + if(this->gpio_config[pin]==digital_input) + { + events.push_back(this->rx_event_id); + if((written=this->comm_dev->write(cmd,4))!=4) + { + /* handle exceptions */ + } + else + { + this->event_server->wait_first(events); + read=this->comm_dev->read(&gpio_state,1); + if(gpio_state&(0x01<<(pin+1))) + return true; + else + return false; + } + } + else + { + /* handle exceptions */ + } +} + +short int CUSBI2C::get_analog(gpio_pins pin) +{ + unsigned char cmd[4]={0x5A,0x12,0x00,0x00}; + int total_read=0,read=0,written=0; + std::list<std::string> events; + short int analog_value=0; + unsigned char answer[4]; + + if(this->gpio_config[pin]==analog_input) + { + events.push_back(this->rx_event_id); + written=this->comm_dev->write(cmd,4); + do{ + this->event_server->wait_first(events); + read=this->comm_dev->read(&answer[total_read],4-total_read); + total_read+=read; + }while(total_read<4); + if(pin==gpio2) + analog_value=answer[0]*256+answer[1]; + else + analog_value=answer[2]*256+answer[3]; + } + else + { + /* handle exceptions */ + } +} + +void CUSBI2C::turn_led_on(void) +{ + unsigned char cmd[4]={0x5A,0x11,0x00,0x00}; + unsigned char gpio_state=0x00; + std::list<std::string> events; + int written=0,read=0; + + events.push_back(this->rx_event_id); + if((written=this->comm_dev->write(cmd,4))!=4) + { + /* handle exceptions */ + } + else + { + this->event_server->wait_first(events); + read=this->comm_dev->read(&gpio_state,1); + gpio_state|=0x01; + cmd[1]=0x10; + cmd[2]=gpio_state; + if((written=this->comm_dev->write(cmd,4))!=4) + { + /* handle exceptions */ + } + else + { + this->event_server->wait_first(events); + read=this->comm_dev->read(&gpio_state,1); + } + } +} + +void CUSBI2C::turn_led_off(void) +{ + unsigned char cmd[4]={0x5A,0x11,0x00,0x00}; + unsigned char gpio_state=0x00; + std::list<std::string> events; + int written=0,read=0; + + events.push_back(this->rx_event_id); + if((written=this->comm_dev->write(cmd,4))!=4) + { + /* handle exceptions */ + } + else + { + this->event_server->wait_first(events); + read=this->comm_dev->read(&gpio_state,1); + gpio_state&=0xFE; + cmd[1]=0x10; + cmd[2]=gpio_state; + if((written=this->comm_dev->write(cmd,4))!=4) + { + /* handle exceptions */ + } + else + { + this->event_server->wait_first(events); + read=this->comm_dev->read(&gpio_state,1); + } + } +} + +int CUSBI2C::write(unsigned char dev_id, unsigned char *data, int len) +{ + unsigned char cmd[3]={0x53,0x00,0x00}; + std::list<std::string> events; + unsigned char status; + int written=0,read=0; + + if(len>1) + { + /* handle exceptions */ + } + else + { + if(data==NULL) + { + /* handle exceptions */ + } + else + { + cmd[1]=dev_id<<1; + cmd[2]=data[0]; + events.push_back(this->rx_event_id); + if((written=this->comm_dev->write(cmd,3))!=3) + { + /* handle exceptions */ + } + else + { + try{ + this->event_server->wait_first(events,500); + read=this->comm_dev->read(&status,1); + if(status==0x00) + { + /* handle exceptions */ + } + }catch(CEventTimeoutException &e){ + /* handle exceptions */ + } + } + } + } +} + +int CUSBI2C::read(unsigned char dev_id, unsigned char *data, int len) +{ + int written=0,read=0,total_read=0; + unsigned char cmd[3]={0x00,0x00,0x00}; + std::list<std::string> events; + unsigned char status; + + if(data==NULL) + { + /* handle exceptions */ + } + else + { + cmd[1]=(dev_id<<1)+1; + events.push_back(this->rx_event_id); + if(len==1) + { + cmd[0]=0x53; + if((written=this->comm_dev->write(cmd,2))!=2) + { + /* handle exceptions */ + } + } + else + { + cmd[0]=0x54; + cmd[2]=len; + if((written=this->comm_dev->write(cmd,3))!=3) + { + /* handle exceptions */ + } + } + try{ + do{ + this->event_server->wait_first(events,500); + read=this->comm_dev->read(&data[total_read],len-total_read); + total_read+=read; + }while(total_read<len); + }catch(CEventTimeoutException &e){ + /* handle exceptions */ + } + } +} + +int CUSBI2C::write_reg(unsigned char dev_id, short int address, unsigned char *data, int len) +{ + int written=0,read=0,total_read=0,num=0,total_written=0; + std::list<std::string> events; + unsigned char status; + unsigned char *cmd; + + do{ + if((len-total_written)>64) num=64; + else num=len-total_written; + if(address<256) + { + cmd=new unsigned char[num+4]; + cmd[0]=0x55; + cmd[1]=dev_id<<1; + cmd[2]=address; + cmd[3]=num; + memcpy(&data[total_written],&cmd[4],num); + num+=4; + } + else + { + cmd=new unsigned char[num+5]; + cmd[0]=0x56; + cmd[1]=dev_id<<1; + cmd[2]=address/256; + cmd[3]=address%256; + cmd[4]=num; + memcpy(&data[total_written],&cmd[5],num); + num+=5; + } + if((written=this->comm_dev->write(cmd,num))!=num) + { + /* handle exceptions */ + } + else + { + try{ + this->event_server->wait_first(events,500); + read=this->comm_dev->read(&status,1); + if(status==0x00) + { + /* handle exceptions */ + } + }catch(CEventTimeoutException &e){ + /* handle exceptions */ + } + } + delete[] cmd; + if(address<256) total_written+=num-4; + else total_written+=num-5; + }while(total_written<len); +} + +int CUSBI2C::read_reg(unsigned char dev_id, short int address, unsigned char *data, int len) +{ + int written=0,read=0,total_read=0,num=0,total_num=0; + std::list<std::string> events; + unsigned char cmd[5]; + unsigned char status; + + do{ + if((len-total_read)>60) num=60; + else num=len-total_read; + if(address<256) + { + cmd[0]=0x55; + cmd[1]=dev_id<<1; + cmd[2]=address; + cmd[3]=num; + written=this->comm_dev->write(cmd,4)!=4; + } + else + { + cmd[0]=0x56; + cmd[1]=dev_id<<1; + cmd[2]=address/256; + cmd[3]=address%256; + cmd[4]=num; + written=this->comm_dev->write(cmd,5)!=5; + } + if(written!=num) + { + /* handle exceptions */ + } + else + { + try{ + do{ + this->event_server->wait_first(events,500); + read=this->comm_dev->read(&data[total_read+total_num],num-total_num); + total_num+=read; + }while(total_num<num); + }catch(CEventTimeoutException &e){ + /* handle exceptions */ + } + } + total_read+=num; + }while(total_read<len); +} + +void CUSBI2C::close(void) +{ + if(this->comm_dev!=NULL) + this->comm_dev->close(); + this->rx_event_id.clear(); +} + +CUSBI2C::~CUSBI2C() +{ + this->close(); + if(this->comm_dev!=NULL) + delete this->comm_dev; + this->adapter_id.clear(); + this->gpio_config[gpio1]=0x00; + this->gpio_config[gpio2]=0x00; + this->gpio_config[gpio3]=0x00; +} diff --git a/src/usb_i2c.h b/src/usb_i2c.h new file mode 100644 index 0000000..74045a6 --- /dev/null +++ b/src/usb_i2c.h @@ -0,0 +1,37 @@ +#ifndef _USB_I2C_H +#define _USB_I2C_H + +#include "rs232.h" +#include "eventserver.h" + +typedef enum {gpio1=0,gpio2=1,gpio3=2} gpio_pins; + +typedef enum {digital_input=0x01,digital_output=0x02,analog_input=0x04,i2c=0x08} gpio_types; + +class CUSBI2C +{ + private: + CRS232 *comm_dev; + std::string adapter_id; + std::string rx_event_id; + unsigned char gpio_config[3]; + CEventServer *event_server; + public: + CUSBI2C(const std::string &adapter_id); + void open(const std::string &port_id); + unsigned char get_revision(void); + void config_gpio(gpio_pins pin, gpio_types type); + void set_gpio(gpio_pins pin, bool value); + bool get_gpio(gpio_pins pin); + short int get_analog(gpio_pins pin); + void turn_led_on(void); + void turn_led_off(void); + int write(unsigned char dev_id, unsigned char *data, int len); + int read(unsigned char dev_id, unsigned char *data, int len); + int write_reg(unsigned char dev_id, short int address, unsigned char *data, int len); + int read_reg(unsigned char dev_id, short int address, unsigned char *data, int len); + void close(void); + ~CUSBI2C(); +}; + +#endif -- GitLab