diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index f26414b2..85d8929e 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,6 +1,7 @@ add_executable (hmc5883l-example hmc5883l.cxx) add_executable (groveled-example groveled.cxx) add_executable (grovetemp-example grovetemp.cxx) +add_executable (grovebutton-example grovebutton.cxx) add_executable (groverotary-example groverotary.cxx) add_executable (groveslide-example groveslide.cxx) add_executable (lcm-lcd-example lcm-lcd.cxx) @@ -85,6 +86,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/ldt0028) target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (grovetemp-example grove ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries (grovebutton-example grove ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (groverotary-example grove ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (groveslide-example grove ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (lcm-lcd-example i2clcd ${CMAKE_THREAD_LIBS_INIT}) diff --git a/examples/grovebutton.cxx b/examples/grovebutton.cxx new file mode 100644 index 00000000..7d2d69a9 --- /dev/null +++ b/examples/grovebutton.cxx @@ -0,0 +1,49 @@ +/* + * Author: Sarah Knepper + * 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) +{ + // This example uses GPIO 0 +//! [Interesting] + + // Create the button object using GPIO pin 0 + upm::GroveButton* button = new upm::GroveButton(0); + + // Read the input and print, waiting one second between readings + while( 1 ) { + std::cout << button->name() << " value is " << button->value() << std::endl; + sleep(1); + } + + // Delete the button object + delete button; +//! [Interesting] + + return 0; +} diff --git a/examples/javascript/grovebutton.js b/examples/javascript/grovebutton.js new file mode 100644 index 00000000..428396ad --- /dev/null +++ b/examples/javascript/grovebutton.js @@ -0,0 +1,35 @@ +/* + * Author: Sarah Knepper + * 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. + */ + +// Load Grove module +var groveSensor = require('jsupm_grove'); + +// Create the button object using GPIO pin 0 +var button = new groveSensor.GroveButton(0); + +// Read the input and print, waiting one second between readings +function readButtonValue() { + console.log(button.name() + " value is " + button.value()); +} +setInterval(readButtonValue, 1000); diff --git a/examples/python/grovebutton.py b/examples/python/grovebutton.py new file mode 100644 index 00000000..c62c399a --- /dev/null +++ b/examples/python/grovebutton.py @@ -0,0 +1,35 @@ +# Author: Sarah Knepper +# 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. + +import time +import pyupm_grove as grove + +# Create the button object using GPIO pin 0 +button = grove.GroveButton(0) + +# Read the input and print, waiting one second between readings +while 1: + print button.name(), ' value is ', button.value() + time.sleep(1) + +# Delete the button object +del button diff --git a/src/grove/grove.cxx b/src/grove/grove.cxx index e48ba1ba..fd4ffbae 100644 --- a/src/grove/grove.cxx +++ b/src/grove/grove.cxx @@ -193,3 +193,28 @@ float GroveSlide::ref_voltage() { return m_ref_voltage; } + +//// GroveButton //// + +GroveButton::GroveButton(unsigned int pin) +{ + mraa_init(); + m_gpio = mraa_gpio_init(pin); + mraa_gpio_dir(m_gpio, MRAA_GPIO_IN); + m_name = "Button Sensor"; +} + +GroveButton::~GroveButton() +{ + mraa_gpio_close(m_gpio); +} + +std::string GroveButton::name() +{ + return m_name; +} + +int GroveButton::value() +{ + return mraa_gpio_read(m_gpio); +} diff --git a/src/grove/grove.h b/src/grove/grove.h index cbaa5ef8..3440a184 100644 --- a/src/grove/grove.h +++ b/src/grove/grove.h @@ -247,4 +247,41 @@ class GroveSlide: public Grove { float m_ref_voltage; }; +/** + * @brief C++ API for Grove button + * + * Very basic UPM module for Grove button + * + * @ingroup grove gpio + * @snippet grovebutton.cxx Interesting + */ +class GroveButton: public Grove { + public: + /** + * Grove button constructor + * + * @param gpio pin to use + */ + GroveButton(unsigned int pin); + /** + * Grove button destructor + */ + ~GroveButton(); + /** + * Get name of sensor + * + * @return the name of this sensor + */ + std::string name(); + /** + * Get value from GPIO pin + * + * @return the value from the GPIO pin + */ + int value(); + private: + std::string m_name; + mraa_gpio_context m_gpio; +}; + }