diff --git a/docs/images/groveslide.jpeg b/docs/images/groveslide.jpeg new file mode 100644 index 00000000..70fb82d8 Binary files /dev/null and b/docs/images/groveslide.jpeg differ diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 185c6e3e..f021c46b 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,6 +1,8 @@ add_executable (hmc5883l-example hmc5883l.cxx) add_executable (groveled-example groveled.cxx) add_executable (grovetemp-example grovetemp.cxx) +add_executable (groverotary-example groverotary.cxx) +add_executable (groveslide-example groveslide.cxx) add_executable (lcm-lcd-example lcm-lcd.cxx) add_executable (rgb-lcd-example rgb-lcd.cxx) add_executable (buzzer-sound-example buzzer-sound.cxx) @@ -77,6 +79,8 @@ include_directories (${PROJECT_SOURCE_DIR}/src/tsl2561) 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 (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}) target_link_libraries (rgb-lcd-example i2clcd ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (buzzer-sound-example buzzer ${CMAKE_THREAD_LIBS_INIT}) diff --git a/examples/groveslide.cxx b/examples/groveslide.cxx new file mode 100644 index 00000000..acc959ee --- /dev/null +++ b/examples/groveslide.cxx @@ -0,0 +1,50 @@ +/* + * Author: Mihai Tudor Panu + * 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 +#include "grove.h" + +using namespace std; + +int main () +{ +//! [Interesting] + upm::GroveSlide* slide = new upm::GroveSlide(0); // Instantiate new grove slide potentiometer on analog pin A0 + + cout << slide->name() << endl; + + while(true) { + float adc_value = slide->raw_value(); // Read raw value + float volts = slide->voltage_value(); // Read voltage, board reference set at 5.0V + fprintf(stdout, "%4d = %.2f V\n", (uint16_t)adc_value, volts); + + usleep(2500000); // Sleep for 2.5s + } +//! [Interesting] + delete slide; + return 0; +} diff --git a/examples/javascript/groveslide.js b/examples/javascript/groveslide.js new file mode 100644 index 00000000..562bfc4b --- /dev/null +++ b/examples/javascript/groveslide.js @@ -0,0 +1,42 @@ +/* + * Author: Mihai Tudor Panu + * 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. + */ + +var upm_grove = require('jsupm_grove'); + +//setup access analog input Analog pin #0 (A0) +var groveSlide = new upm_grove.GroveSlide(0); + +loop(); + +function loop() +{ + var raw = groveSlide.raw_value(); + var volts = groveSlide.voltage_value(); + + //write the slider values to the console + console.log("Slider Value: " + raw + " = " + volts.toFixed(2) + " V"); + + //wait 2 s then call function again + setTimeout(loop, 2000); +} diff --git a/examples/python/groveslide.py b/examples/python/groveslide.py new file mode 100644 index 00000000..69a1f0a7 --- /dev/null +++ b/examples/python/groveslide.py @@ -0,0 +1,39 @@ +# Author: Mihai Tudor Panu +# 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. + +from time import sleep +import pyupm_grove as grove + +# New Grove Slider on AIO pin 0 +slider = grove.GroveSlide(0) + +# Loop indefinitely +while True: + + # Read values + raw = slider.raw_value() + volts = slider.voltage_value() + + print "Slider value: ", raw , " = %.2f" % volts , " V" + + # Sleep for 2.5 s + sleep(2.5) diff --git a/src/grove/grove.cxx b/src/grove/grove.cxx index 199aa6b6..e48ba1ba 100644 --- a/src/grove/grove.cxx +++ b/src/grove/grove.cxx @@ -160,3 +160,36 @@ float GroveRotary::rel_rad() { return GroveRotary::rel_deg() * M_PI / 180.0; } + +//// GroveSlide //// + +GroveSlide::GroveSlide(unsigned int pin, float ref_voltage) +{ + mraa_init(); + m_aio = mraa_aio_init(pin); + m_ref_voltage = ref_voltage; + m_name = "Slide Potentiometer"; +} + +GroveSlide::~GroveSlide() +{ + mraa_aio_close(m_aio); +} + +float GroveSlide::raw_value() +{ + return (float) mraa_aio_read(m_aio); +} + +float GroveSlide::voltage_value() +{ + // conversion to Volts + float a = GroveSlide::raw_value(); + a = m_ref_voltage * a / 1023.0 ; + return a; +} + +float GroveSlide::ref_voltage() +{ + return m_ref_voltage; +} diff --git a/src/grove/grove.h b/src/grove/grove.h index 2996b360..cbaa5ef8 100644 --- a/src/grove/grove.h +++ b/src/grove/grove.h @@ -200,4 +200,51 @@ class GroveRotary: public Grove { static const int m_max_angle = 300; }; +/** + * @brief C++ API for Grove Slide Potentiometer + * + * Very basic UPM module for Grove slide potentiometer on analog, + * returns either raw value or scaled voltage value. + * + * @ingroup grove analog + * @snippet groveslide.cxx Interesting + * @image html groveslide.jpeg + */ +class GroveSlide: public Grove { + public: + /** + * Grove analog slide potentiometer constructor + * + * @param pin number of analog pin to use + * + * @param ref_voltage the reference voltage the board is set to as float, e.g. 3.3 or 5.0 (default) + */ + GroveSlide(unsigned int pin, float ref_voltage = 5.0); + /** + * GroveSlide Destructor + */ + ~GroveSlide(); + /** + * Get raw value from AIO pin + * + * @return the raw value from the ADC + */ + float raw_value(); + /** + * Get the voltage value from the pin + * + * @return the voltage reading based on the reference voltage + */ + float voltage_value(); + /** + * Get the board's reference voltage passed on object initialization + * + * @return the reference voltage the class was set for + */ + float ref_voltage(); + private: + mraa_aio_context m_aio; + float m_ref_voltage; +}; + }