diff --git a/CMakeLists.txt b/CMakeLists.txt index 727010cd..2f5d95d3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ if (SWIG_FOUND) endif () find_package (PkgConfig REQUIRED) -pkg_check_modules (MAA maa>=0.2.1) +pkg_check_modules (MAA maa>=0.2.2) message (INFO " found libmaa version: ${MAA_VERSION}") set (CMAKE_SWIG_FLAGS "") diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index c06fc58a..bfbbb2c7 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,5 +1,8 @@ add_executable (compass compass.cxx) +add_executable (groveled groveled.cxx) include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l) +include_directories (${PROJECT_SOURCE_DIR}/src/grove) target_link_libraries (compass hmc5883l) +target_link_libraries (groveled grove) diff --git a/examples/compass.cxx b/examples/compass.cxx index c72337ae..45aaa5e4 100644 --- a/examples/compass.cxx +++ b/examples/compass.cxx @@ -27,7 +27,8 @@ int main(int argc, char **argv) { - upm::Hmc5883l* compass = new upm::Hmc5883l(); + // Use i2c device 0 all the time + upm::Hmc5883l* compass = new upm::Hmc5883l(0); fprintf(stdout, "heading: %f\n", compass->heading()); return 0; diff --git a/examples/groveled.cxx b/examples/groveled.cxx new file mode 100644 index 00000000..59de3051 --- /dev/null +++ b/examples/groveled.cxx @@ -0,0 +1,43 @@ +/* + * Author: Brendan Le Foll + * Copyright (c) 2014 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 +#include +#include "grove.h" + +int +main(int argc, char **argv) +{ + // Use i2c device 0 all the time + upm::GroveLed* led = new upm::GroveLed(2); + std::cout << led->name() << std::endl; + for (int i=0; i < 10; i++) { + led->on(); + sleep(1); + led->off(); + sleep(1); + } + + return 0; +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d665c158..f2fc1678 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1 +1,2 @@ add_subdirectory (hmc5883l) +add_subdirectory (grove) diff --git a/src/grove/CMakeLists.txt b/src/grove/CMakeLists.txt new file mode 100644 index 00000000..f783527f --- /dev/null +++ b/src/grove/CMakeLists.txt @@ -0,0 +1,35 @@ +set (libname "grove") +add_library (grove SHARED grove.cxx) +include_directories (${MAA_INCLUDE_DIR}) +target_link_libraries (grove ${MAA_LIBRARIES}) + +if (DOXYGEN_FOUND AND SWIG_FOUND) + find_package (PythonLibs) + + include_directories ( + ${PYTHON_INCLUDE_PATH} + ${PYTHON_INCLUDE_DIRS} + ${MAA_INCLUDE_DIR} + . + ) + + set_source_files_properties (pyupm_grove.i PROPERTIES CPLUSPLUS ON) + set_source_files_properties (jsupm_grove.i PROPERTIES CPLUSPLUS ON) + + swig_add_module (pyupm_grove python pyupm_grove.i grove.cxx) + swig_add_module (jsupm_grove python jsupm_grove.i grove.cxx) + swig_link_libraries (pyupm_grove ${PYTHON_LIBRARIES} ${MAA_LIBRARIES}) + swig_link_libraries (jsupm_grove ${PYTHON_LIBRARIES} ${MAA_LIBRARIES}) + + set (CMAKE_SWIG_FLAGS -DDOXYGEN=${DOXYGEN_FOUND}) + add_custom_command (OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${libname}_doc.i + COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/../doxy2swig.py -n + ${CMAKE_BINARY_DIR}/xml/${libname}_8h.xml + ${CMAKE_CURRENT_BINARY_DIR}/${libname}_doc.i + DEPENDS ${CMAKE_BINARY_DIR}/xml/${libname}_8h.xml + ) + add_custom_target (${libname}doc_i DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${libname}_doc.i) + add_dependencies (${libname}doc_i doc) + add_dependencies (${SWIG_MODULE_pyupm_grove_REAL_NAME} ${libname}doc_i) + +endif () diff --git a/src/grove/grove.cxx b/src/grove/grove.cxx new file mode 100644 index 00000000..13c71cc6 --- /dev/null +++ b/src/grove/grove.cxx @@ -0,0 +1,58 @@ +/* + * Author: Brendan Le Foll + * Copyright (c) 2014 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 "grove.h" + +using namespace upm; + +GroveLed::GroveLed(int pin) +{ + maa_init(); + m_gpio = maa_gpio_init(pin); + maa_gpio_dir(m_gpio, MAA_GPIO_OUT); + m_name = "LED Socket"; +} + +GroveLed::~GroveLed() +{ + maa_gpio_close(m_gpio); +} + +maa_result_t GroveLed::write(int value) +{ + if (value >= 1) { + return maa_gpio_write(m_gpio, 1); + } + return maa_gpio_write(m_gpio, 0); +} + +maa_result_t GroveLed::on() +{ + return write(1); +} + +maa_result_t GroveLed::off() +{ + return write(0); +} diff --git a/src/grove/grove.h b/src/grove/grove.h new file mode 100644 index 00000000..5df27b8c --- /dev/null +++ b/src/grove/grove.h @@ -0,0 +1,53 @@ +/* + * Author: Brendan Le Foll + * Copyright (c) 2014 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. + */ +#pragma once + +#include +#include + +namespace upm { + +class Grove { + public: + virtual ~Grove() {} + std::string name() + { + return m_name; + } + protected: + std::string m_name; +}; + +class GroveLed: public Grove { + public: + GroveLed(int pin); + ~GroveLed(); + maa_result_t write(int value); + maa_result_t off(); + maa_result_t on(); + private: + maa_gpio_context * m_gpio; +}; + +} diff --git a/src/grove/jsupm_grove.i b/src/grove/jsupm_grove.i new file mode 100644 index 00000000..97a17c25 --- /dev/null +++ b/src/grove/jsupm_grove.i @@ -0,0 +1,7 @@ +%module jsupm_grove + +%{ + #include "grove.h" +%} + +%include "grove.h" diff --git a/src/grove/pyupm_grove.i b/src/grove/pyupm_grove.i new file mode 100644 index 00000000..abb4c43c --- /dev/null +++ b/src/grove/pyupm_grove.i @@ -0,0 +1,12 @@ +%module pyupm_grove + +%feature("autodoc", "3"); + +#ifdef DOXYGEN +%include "grove_doc.i" +#endif + +%include "grove.h" +%{ + #include "grove.h" +%} diff --git a/src/hmc5883l/hmc5883l.cxx b/src/hmc5883l/hmc5883l.cxx index 0fdbad65..6049d827 100644 --- a/src/hmc5883l/hmc5883l.cxx +++ b/src/hmc5883l/hmc5883l.cxx @@ -77,9 +77,9 @@ using namespace upm; -Hmc5883l::Hmc5883l() +Hmc5883l::Hmc5883l(int bus) { - m_i2c = maa_i2c_init(); + m_i2c = maa_i2c_init(bus); maa_i2c_address(m_i2c, HMC5883L_I2C_ADDR); m_rx_tx_buf[0] = HMC5883L_CONF_REG_B; diff --git a/src/hmc5883l/hmc5883l.h b/src/hmc5883l/hmc5883l.h index 17fe1571..45df0fd7 100644 --- a/src/hmc5883l/hmc5883l.h +++ b/src/hmc5883l/hmc5883l.h @@ -32,7 +32,7 @@ namespace upm { class Hmc5883l { public: /// Creates a Hmc5883l object - Hmc5883l(); + Hmc5883l(int bus); /// Returns the direction float direction();