From ce6cd0a19f32019e3115b77bdd25614ab1be9623 Mon Sep 17 00:00:00 2001 From: Zion Orent Date: Wed, 25 Feb 2015 17:25:30 -0500 Subject: [PATCH] groveelectromagnet: Initial implementation This module implements support for the Grove Electromagnet. Signed-off-by: Zion Orent Signed-off-by: Jon Trulson Signed-off-by: John Van Drasek --- examples/c++/CMakeLists.txt | 3 + examples/c++/groveelectromagnet.cxx | 79 +++++++++++++++++++ examples/javascript/groveelectromagnet.js | 57 +++++++++++++ src/groveelectromagnet/CMakeLists.txt | 5 ++ src/groveelectromagnet/groveelectromagnet.cxx | 54 +++++++++++++ src/groveelectromagnet/groveelectromagnet.h | 68 ++++++++++++++++ .../jsupm_groveelectromagnet.i | 8 ++ .../pyupm_groveelectromagnet.i | 9 +++ 8 files changed, 283 insertions(+) create mode 100644 examples/c++/groveelectromagnet.cxx create mode 100644 examples/javascript/groveelectromagnet.js create mode 100644 src/groveelectromagnet/CMakeLists.txt create mode 100644 src/groveelectromagnet/groveelectromagnet.cxx create mode 100644 src/groveelectromagnet/groveelectromagnet.h create mode 100644 src/groveelectromagnet/jsupm_groveelectromagnet.i create mode 100644 src/groveelectromagnet/pyupm_groveelectromagnet.i diff --git a/examples/c++/CMakeLists.txt b/examples/c++/CMakeLists.txt index eaf7cf94..0bb94cc7 100644 --- a/examples/c++/CMakeLists.txt +++ b/examples/c++/CMakeLists.txt @@ -89,6 +89,7 @@ add_executable (hmtrp-example hmtrp.cxx) add_executable (nunchuck-example nunchuck.cxx) add_executable (otp538u-example otp538u.cxx) add_executable (grovecollision-example grovecollision.cxx) +add_executable (groveelectromagnet-example groveelectromagnet.cxx) include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l) include_directories (${PROJECT_SOURCE_DIR}/src/grove) @@ -160,6 +161,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/hmtrp) include_directories (${PROJECT_SOURCE_DIR}/src/nunchuck) include_directories (${PROJECT_SOURCE_DIR}/src/otp538u) include_directories (${PROJECT_SOURCE_DIR}/src/grovecollision) +include_directories (${PROJECT_SOURCE_DIR}/src/groveelectromagnet) target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT}) @@ -250,3 +252,4 @@ target_link_libraries (hmtrp-example hmtrp ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (nunchuck-example nunchuck ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (otp538u-example otp538u ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (grovecollision-example grovecollision ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries (groveelectromagnet-example groveelectromagnet ${CMAKE_THREAD_LIBS_INIT}) diff --git a/examples/c++/groveelectromagnet.cxx b/examples/c++/groveelectromagnet.cxx new file mode 100644 index 00000000..386f4def --- /dev/null +++ b/examples/c++/groveelectromagnet.cxx @@ -0,0 +1,79 @@ +/* +* Author: Zion Orent +* 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 +#include +#include +#include "groveelectromagnet.h" + +using namespace std; + +int shouldRun = true; + +void sig_handler(int signo) +{ + if (signo == SIGINT) + shouldRun = false; +} + +float get_time() +{ + return ((float)(clock()))/CLOCKS_PER_SEC; +} + +int main(int argc, char **argv) +{ + signal(SIGINT, sig_handler); + +//! [Interesting] + // The was tested with the Grove Electromagnetic Module + // Instantiate a Grove Electromagnet on digital pin D2 + upm::GroveElectromagnet* magnet = new upm::GroveElectromagnet(2); + cout << "Starting up magnet...." << endl; + magnet->off(); + + bool magnetState = false; + float time_passed = get_time(); + + // Turn magnet on and off every 5 seconds + while (shouldRun) + { + if ((get_time() - time_passed) > 5.0) + { + magnetState = !magnetState; + if (magnetState) + magnet->on(); + else + magnet->off(); + cout << "Turning magnet " << ((magnetState) ? "on" : "off") << endl; + time_passed = get_time(); + } + } + +//! [Interesting] + magnet->off(); + cout << "Exiting" << endl; + + delete magnet; + return 0; +} diff --git a/examples/javascript/groveelectromagnet.js b/examples/javascript/groveelectromagnet.js new file mode 100644 index 00000000..390fa854 --- /dev/null +++ b/examples/javascript/groveelectromagnet.js @@ -0,0 +1,57 @@ +/*jslint node:true, vars:true, bitwise:true, unparam:true */ +/*jshint unused:true */ +/* +* Author: Zion Orent +* 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. +*/ + +var electromagnet_lib = require("jsupm_groveelectromagnet"); +// The was tested with the Grove Electromagnetic Module +// Instantiate a Grove Electromagnet on digital pin D2 +var electromagnet_obj = new electromagnet_lib.GroveElectromagnet(2); +console.log("Starting up magnet...."); +electromagnet_obj.off(); + +var magnetState = false; + +// Turn magnet on and off every 5 seconds +var myInterval = setInterval(function() +{ + magnetState = !magnetState; + if (magnetState) + electromagnet_obj.on(); + else + electromagnet_obj.off(); + console.log("Turning magnet " + ((magnetState) ? "on" : "off")); +}, 5000); + +// When exiting: clear interval, turn off magnet, run memory cleanup, and print message +process.on('SIGINT', function() +{ + clearInterval(myInterval); + electromagnet_obj.off(); + electromagnet_obj = null; + electromagnet_lib.cleanUp(); + electromagnet_lib = null; + console.log("Exiting..."); + process.exit(0); +}); diff --git a/src/groveelectromagnet/CMakeLists.txt b/src/groveelectromagnet/CMakeLists.txt new file mode 100644 index 00000000..69b1d88f --- /dev/null +++ b/src/groveelectromagnet/CMakeLists.txt @@ -0,0 +1,5 @@ +set (libname "groveelectromagnet") +set (libdescription "upm groveelectromagnet sensor module") +set (module_src ${libname}.cxx) +set (module_h ${libname}.h) +upm_module_init("-lrt") diff --git a/src/groveelectromagnet/groveelectromagnet.cxx b/src/groveelectromagnet/groveelectromagnet.cxx new file mode 100644 index 00000000..9ff82a89 --- /dev/null +++ b/src/groveelectromagnet/groveelectromagnet.cxx @@ -0,0 +1,54 @@ +/* + * Author: Zion Orent + * 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 "groveelectromagnet.h" + +using namespace upm; + +GroveElectromagnet::GroveElectromagnet(int pin) +{ + m_gpio = mraa_gpio_init(pin); + mraa_gpio_dir(m_gpio, MRAA_GPIO_OUT); +} + +GroveElectromagnet::~GroveElectromagnet() +{ + mraa_gpio_close(m_gpio); +} + +void GroveElectromagnet::on() +{ + mraa_result_t error = MRAA_SUCCESS; + error = mraa_gpio_write (m_gpio, HIGH); + if (error != MRAA_SUCCESS) + mraa_result_print(error); +} + +void GroveElectromagnet::off() +{ + mraa_result_t error = MRAA_SUCCESS; + error = mraa_gpio_write (m_gpio, LOW); + if (error != MRAA_SUCCESS) + mraa_result_print(error); +} diff --git a/src/groveelectromagnet/groveelectromagnet.h b/src/groveelectromagnet/groveelectromagnet.h new file mode 100644 index 00000000..6eaabb6b --- /dev/null +++ b/src/groveelectromagnet/groveelectromagnet.h @@ -0,0 +1,68 @@ +/* + * Author: Zion Orent + * 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. + */ +#pragma once + +#include + +#define HIGH 1 +#define LOW 0 + +namespace upm { + /** + * @brief UPM module for the Grove Electromagnet + * @defgroup groveelectromagnet libupm-groveelectromagnet + */ + /** + * @brief C++ API for the Grove Electromagnet + * + * The Grove Electromagnet can hold up to 1 KG + * + * @ingroup gpio groveelectromagnet + * @snippet groveelectromagnet.cxx Interesting + */ + class GroveElectromagnet { + public: + /** + * Grove Electromagnet Constructor + * + * @param pin digital pin to use + */ + GroveElectromagnet(int pin); + /** + * Grove Electromagnet Destructor + */ + ~GroveElectromagnet(); + /** + * Turn magnet on + */ + void on(); + /** + * Turn magnet off + */ + void off(); + + private: + mraa_gpio_context m_gpio; + }; +} diff --git a/src/groveelectromagnet/jsupm_groveelectromagnet.i b/src/groveelectromagnet/jsupm_groveelectromagnet.i new file mode 100644 index 00000000..323a8fb7 --- /dev/null +++ b/src/groveelectromagnet/jsupm_groveelectromagnet.i @@ -0,0 +1,8 @@ +%module jsupm_groveelectromagnet +%include "../upm.i" + +%{ + #include "groveelectromagnet.h" +%} + +%include "groveelectromagnet.h" diff --git a/src/groveelectromagnet/pyupm_groveelectromagnet.i b/src/groveelectromagnet/pyupm_groveelectromagnet.i new file mode 100644 index 00000000..17a88dfb --- /dev/null +++ b/src/groveelectromagnet/pyupm_groveelectromagnet.i @@ -0,0 +1,9 @@ +%module pyupm_groveelectromagnet +%include "../upm.i" + +%feature("autodoc", "3"); + +%include "groveelectromagnet.h" +%{ + #include "groveelectromagnet.h" +%}