diff --git a/src/grove/CMakeLists.txt b/src/grove/CMakeLists.txt index e76ac5dd..ccf2f2d8 100644 --- a/src/grove/CMakeLists.txt +++ b/src/grove/CMakeLists.txt @@ -1,5 +1,5 @@ set (libname "grove") set (libdescription "upm grove module") -set (module_src ${libname}.cxx) -set (module_hpp ${libname}.hpp) +set (module_src grovebutton.cxx groveled.cxx grovelight.cxx groverelay.cxx groverotary.cxx groveslide.cxx grovetemp.cxx) +set (module_hpp grovebutton.hpp groveled.hpp grovelight.hpp groverelay.hpp groverotary.hpp groveslide.hpp grovetemp.hpp grovebase.hpp grove.hpp) upm_module_init() diff --git a/src/grove/grove.cxx b/src/grove/grove.cxx index b0be12d8..8b137891 100644 --- a/src/grove/grove.cxx +++ b/src/grove/grove.cxx @@ -1,307 +1 @@ -/* - * Author: Brendan Le Foll - * Contributions: Mihai Tudor Panu - * Contributions: 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 - -#include "grove.hpp" -#include "math.h" - -using namespace upm; - -//// GroveLed //// - -GroveLed::GroveLed(int pin) -{ - if ( !(m_gpio = mraa_gpio_init(pin)) ) { - throw std::invalid_argument(std::string(__FUNCTION__) + - ": mraa_gpio_init() failed, invalid pin?"); - return; - } - mraa_gpio_dir(m_gpio, MRAA_GPIO_OUT); - m_name = "LED Socket"; -} - -GroveLed::~GroveLed() -{ - mraa_gpio_close(m_gpio); -} - -mraa_result_t GroveLed::write(int value) -{ - if (value >= 1) { - return mraa_gpio_write(m_gpio, 1); - } - return mraa_gpio_write(m_gpio, 0); -} - -mraa_result_t GroveLed::on() -{ - return write(1); -} - -mraa_result_t GroveLed::off() -{ - return write(0); -} - -//// GroveRelay //// - -GroveRelay::GroveRelay(unsigned int pin) -{ - if ( !(m_gpio = mraa_gpio_init(pin)) ) { - throw std::invalid_argument(std::string(__FUNCTION__) + - ": mraa_gpio_init() failed, invalid pin?"); - return; - } - mraa_gpio_dir(m_gpio, MRAA_GPIO_OUT); - m_name = "Relay Switch"; -} - -GroveRelay::~GroveRelay() -{ - mraa_gpio_close(m_gpio); -} - -mraa_result_t GroveRelay::on() -{ - return mraa_gpio_write(m_gpio, 1); -} - -mraa_result_t GroveRelay::off() -{ - return mraa_gpio_write(m_gpio, 0); -} - -bool GroveRelay::isOn() -{ - return mraa_gpio_read(m_gpio) == 1; -} - -bool GroveRelay::isOff() -{ - return mraa_gpio_read(m_gpio) == 0; -} - -//// GroveTemp //// - -GroveTemp::GroveTemp(unsigned int pin, float scale) -{ - if ( !(m_aio = mraa_aio_init(pin)) ) { - throw std::invalid_argument(std::string(__FUNCTION__) + - ": mraa_aio_init() failed, invalid pin?"); - return; - } - m_name = "Temperature Sensor"; - m_scale = scale; -} - -GroveTemp::~GroveTemp() -{ - mraa_aio_close(m_aio); -} - -int GroveTemp::value () -{ - float a = (float) mraa_aio_read(m_aio); - if (a == -1.0) return -1; - // Apply scale factor after error check - a *= m_scale; - float r = (float)(1023.0-a)*10000.0/a; - float t = 1.0/(log(r/10000.0)/3975.0 + 1.0/298.15)-273.15; - return (int) round(t); -} - -float GroveTemp::raw_value() -{ - return (float) mraa_aio_read(m_aio); -} - -//// GroveLight //// - -GroveLight::GroveLight(unsigned int pin) -{ - if ( !(m_aio = mraa_aio_init(pin)) ) { - throw std::invalid_argument(std::string(__FUNCTION__) + - ": mraa_aio_init() failed, invalid pin?"); - return; - } - m_name = "Light Sensor"; -} - -GroveLight::~GroveLight() -{ - mraa_aio_close(m_aio); -} - -int GroveLight::value() -{ - // rough conversion to lux, using formula from Grove Starter Kit booklet - float a = (float) mraa_aio_read(m_aio); - if (a == -1.0) return -1; - a = 10000.0/pow(((1023.0-a)*10.0/a)*15.0,4.0/3.0); - return (int) round(a); -} - -float GroveLight::raw_value() -{ - return (float) mraa_aio_read(m_aio); -} - -//// GroveRotary //// - -GroveRotary::GroveRotary(unsigned int pin) -{ - if ( !(m_aio = mraa_aio_init(pin)) ) { - throw std::invalid_argument(std::string(__FUNCTION__) + - ": mraa_aio_init() failed, invalid pin?"); - return; - } - m_name = "Rotary Angle Sensor"; -} - -GroveRotary::~GroveRotary() -{ - mraa_aio_close(m_aio); -} - -float GroveRotary::abs_value() -{ - return (float) mraa_aio_read(m_aio); -} - -float GroveRotary::abs_deg() -{ - return GroveRotary::abs_value() * (float) m_max_angle / 1023.0; -} - -float GroveRotary::abs_rad() -{ - return GroveRotary::abs_deg() * M_PI / 180.0; -} - -float GroveRotary::rel_value() -{ - return GroveRotary::abs_value() - 512.0; -} - -float GroveRotary::rel_deg() -{ - return GroveRotary::rel_value() * (float) m_max_angle / 1023.0; -} - -float GroveRotary::rel_rad() -{ - return GroveRotary::rel_deg() * M_PI / 180.0; -} - -//// GroveSlide //// - -GroveSlide::GroveSlide(unsigned int pin, float ref_voltage) -{ - if ( !(m_aio = mraa_aio_init(pin)) ) { - throw std::invalid_argument(std::string(__FUNCTION__) + - ": mraa_aio_init() failed, invalid pin?"); - return; - } - 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; -} - -//// GroveButton //// - -GroveButton::GroveButton(unsigned int pin) -{ - if ( !(m_gpio = mraa_gpio_init(pin)) ) { - throw std::invalid_argument(std::string(__FUNCTION__) + - ": mraa_gpio_init() failed, invalid pin?"); - return; - } - 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); -} - -#ifdef JAVACALLBACK -void GroveButton::installISR(mraa::Edge level, jobject runnable) -{ - installISR(level, mraa_java_isr_callback, runnable); -} -#endif - -void GroveButton::installISR(mraa::Edge level, void (*isr)(void *), void *arg) -{ - if (m_isrInstalled) - uninstallISR(); - - // install our interrupt handler - mraa_gpio_isr(m_gpio, (mraa_gpio_edge_t) level, isr, arg); - m_isrInstalled = true; -} - -void GroveButton::uninstallISR() -{ - mraa_gpio_isr_exit(m_gpio); - m_isrInstalled = false; -} diff --git a/src/grove/grove.hpp b/src/grove/grove.hpp index a3e13c68..a7da8758 100644 --- a/src/grove/grove.hpp +++ b/src/grove/grove.hpp @@ -1,8 +1,8 @@ /* - * Author: Brendan Le Foll - * Contributions: Mihai Tudor Panu - * Contributions: Sarah Knepper - * Copyright (c) 2014 Intel Corporation. + * Authors: Brendan Le Foll + * Mihai Tudor Panu + * Sarah Knepper + * Copyright (c) 2014 - 2016 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -25,389 +25,10 @@ */ #pragma once -#include -#include -#include - -namespace upm { - -/** - * @brief Generic library for basic Grove sensors - * @defgroup grove libupm-grove - * @ingroup seeed gpio pwm ainput button led light relay temp touch gsk eak hak - */ -class Grove { - public: - virtual ~Grove() {} - std::string name() - { - return m_name; - } - protected: - std::string m_name; -}; - -/** - * @brief API for the Grove LED - * - * UPM module for the Grove LED (or other similar light-emitting diodes). - * An LED is a small lightbulb that emits light in - * response to a small current. The longer wire of an LED connects - * to the positive seat (anode); the shorter wire connects to the - * negative seat (cathode). The flat side of the bulb corresponds - * to the cathode, while the rounded side corresponds to the anode. - * - * @ingroup grove gpio - * @snippet groveled.cxx Interesting - * @snippet groveled-multi.cxx Interesting - * @image html groveled.jpg - */ -class GroveLed: public Grove { - public: - /** - * Grove LED constructor - * - * @param gpio Pin to use - */ - GroveLed(int pin); - /** - * Grove LED destructor - */ - ~GroveLed(); - /** - * Turns the LED on or off, depending on the value. - * If the value is positive (greater than or equal - * to 1), the LED is turned on. Otherwise, for 0 - * or negative values, the LED is turned off. - * - * @param value Tells the LED to turn on (for values >=1) - * or off (for values <1) - * - * @return 0 if successful, non-zero otherwise - */ - mraa_result_t write(int value); - /** - * Turns the LED off - * - * @return 0 if successful, non-zero otherwise - */ - mraa_result_t off(); - /** - * Turns the LED on - * - * @return 0 if successful, non-zero otherwise - */ - mraa_result_t on(); - private: - mraa_gpio_context m_gpio; -}; - -/** - * @brief API for the Grove Relay - * - * UPM module for the Grove relay switch. Grove relay is a - * digital normally-open switch that uses low voltage or current to - * control a higher voltage and/or higher current. When closed, - * the indicator LED lights up and current is allowed to flow. - * - * @ingroup grove gpio - * @snippet groverelay.cxx Interesting - * @image html groverelay.jpg - */ -class GroveRelay: public Grove { - public: - /** - * Grove relay constructor - * - * @param gpio Pin to use - */ - GroveRelay(unsigned int pin); - /** - * Grove relay destructor - */ - ~GroveRelay(); - /** - * Sets the relay switch to on (closed). This allows current - * to flow and lights up the indicator LED. - * - * @return 0 if successful, non-zero otherwise - */ - mraa_result_t on(); - /** - * Sets the relay switch to off (open). This stops current - * from flowing and the indicator LED is not lit. - * - * @return 0 if successful, non-zero otherwise - */ - mraa_result_t off(); - /** - * Defines whether the relay switch is closed. - * - * @return True if the switch is on (closed), false otherwise - */ - bool isOn(); - /** - * Defines whether the relay switch is open. - * - * @return True if the switch is off (open), false otherwise - */ - bool isOff(); - private: - mraa_gpio_context m_gpio; -}; - -/** - * @brief API for the Grove Temperature Sensor - * - * Basic UPM module for the Grove temperature sensor on analog - * - * @ingroup grove analog - * @snippet grovetemp.cxx Interesting - * @image html grovetemp.jpg - */ -class GroveTemp: public Grove { - public: - /** - * Grove analog temperature sensor constructor - * - * @param pin Analog pin to use - * @param scale Scaling factor for raw analog value from the ADC, - * useful for mixed 3.3V/5V boards - */ - GroveTemp(unsigned int pin, float scale = 1.0); - /** - * GroveTemp destructor - */ - ~GroveTemp(); - /** - * Gets the raw value from the AIO pin - * - * @return Raw value from the ADC - */ - float raw_value(); - /** - * Gets the temperature in Celsius from the sensor - * - * @return Normalized temperature in Celsius - */ - int value(); - private: - mraa_aio_context m_aio; - float m_scale; -}; - -/** - * @brief API for the Grove Light Sensor - * - * The Grove light sensor detects the intensity of the ambient light. - * As the light intensity of the environment increases, the resistance - * of the sensor decreases. This means the raw value from the - * analog pin is greater in bright light and smaller in the dark. - * An approximate lux value can also be returned. - * - * @ingroup grove analog - * @snippet grovelight.cxx Interesting - * @image html grovelight.jpg - */ -class GroveLight: public Grove { - public: - /** - * Grove analog light sensor constructor - * - * @param pin Analog pin to use - */ - GroveLight(unsigned int pin); - /** - * GroveLight destructor - */ - ~GroveLight(); - /** - * Gets the raw value from the AIO pin - * - * @return Raw value from the ADC - */ - float raw_value(); - /** - * Gets an approximate light value, in lux, from the sensor - * - * @return Normalized light reading in lux - */ - int value(); - private: - mraa_aio_context m_aio; -}; - -/** - * @brief API for the Grove Rotary Angle Sensor (Knob) - * - * Basic UPM module for the Grove rotary angle sensor (knob) on analog. Provides - * a set of functions to read the absolute pin value, degrees or radians, and another set - * to do the same relative to the center of the knob range. - * - * @ingroup grove analog - * @snippet groverotary.cxx Interesting - * @image html groverotary.jpeg - */ -class GroveRotary: public Grove { - public: - /** - * Grove rotary angle sensor constructor - * - * @param pin Number of the analog pin to use - */ - GroveRotary(unsigned int pin); - /** - * GroveRotary destructor - */ - ~GroveRotary(); - /** - * Gets the absolute raw value from the AIO pin - * - * @return Unsigned value from the ADC - */ - float abs_value(); - /** - * Gets absolute raw degrees from the AIO pin - * - * @return Unsigned degrees from the ADC - */ - float abs_deg(); - /** - * Gets absolute raw radians from the AIO pin - * - * @return Unsigned radians from the ADC - */ - float abs_rad(); - /** - * Gets the relative value from the AIO pin - * - * @return Signed value from the ADC - */ - float rel_value(); - /** - * Gets relative degrees from the AIO pin - * - * @return Signed degrees from the ADC - */ - float rel_deg(); - /** - * Gets relative radians from the AIO pin - * - * @return Signed radians from the ADC - */ - float rel_rad(); - private: - mraa_aio_context m_aio; - static const int m_max_angle = 300; -}; - -/** - * @brief API for the Grove Slide Potentiometer - * - * Basic UPM module for the Grove slide potentiometer on analog that - * returns either a raw value or a 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 the analog pin to use - * - * @param ref_voltage Reference voltage the board is set to, as a floating-point value; default is 5.0V - */ - GroveSlide(unsigned int pin, float ref_voltage = 5.0); - /** - * GroveSlide destructor - */ - ~GroveSlide(); - /** - * Gets the raw value from the AIO pin - * - * @return Raw value from the ADC - */ - float raw_value(); - /** - * Gets the voltage value from the pin - * - * @return Voltage reading based on the reference voltage - */ - float voltage_value(); - /** - * Gets the board's reference voltage passed on object initialization - * - * @return Reference voltage the class was set for - */ - float ref_voltage(); - private: - mraa_aio_context m_aio; - float m_ref_voltage; -}; - -/** - * @brief API for the Grove Button - * - * Basic UPM module for the Grove button - * - * @ingroup grove gpio - * @snippet grovebutton.cxx Interesting - * @image html grovebutton.jpg - */ -class GroveButton: public Grove { - public: - /** - * Grove button constructor - * - * @param gpio Pin to use - */ - GroveButton(unsigned int pin); - /** - * Grove button destructor - */ - ~GroveButton(); - /** - * Gets the name of the sensor - * - * @return Name of this sensor - */ - std::string name(); - /** - * Gets the value from the GPIO pin - * - * @return Value from the GPIO pin - */ - int value(); - - /** - * Installs an interrupt service routine (ISR) to be called when - * the button is activated or deactivated. - * - * @param fptr Pointer to a function to be called on interrupt - * @param arg Pointer to an object to be supplied as an - * argument to the ISR. - */ -#if defined(SWIGJAVA) || defined(JAVACALLBACK) - void installISR(mraa::Edge level, jobject runnable); -#else - void installISR(mraa::Edge level, void (*isr)(void *), void *arg); -#endif - /** - * Uninstalls the previously installed ISR - * - */ - void uninstallISR(); - - private: -#if defined(SWIGJAVA) || defined(JAVACALLBACK) - void installISR(mraa::Edge level, void (*isr)(void *), void *arg); -#endif - bool m_isrInstalled; - std::string m_name; - mraa_gpio_context m_gpio; -}; - -} +#include +#include +#include +#include +#include +#include +#include diff --git a/src/grove/grovebase.hpp b/src/grove/grovebase.hpp new file mode 100644 index 00000000..67fe6318 --- /dev/null +++ b/src/grove/grovebase.hpp @@ -0,0 +1,47 @@ +/* + * Authors: Brendan Le Foll + * Mihai Tudor Panu + * Sarah Knepper + * Copyright (c) 2014 - 2016 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 + +namespace upm { + +/** + * @brief Generic library for basic Grove sensors + * @defgroup grove libupm-grove + * @ingroup seeed gpio pwm ainput button led light relay temp touch gsk eak hak + */ +class Grove { + public: + virtual ~Grove() {} + std::string name() + { + return m_name; + } + protected: + std::string m_name; +}; +} diff --git a/src/grove/grovebutton.cxx b/src/grove/grovebutton.cxx new file mode 100644 index 00000000..a4926285 --- /dev/null +++ b/src/grove/grovebutton.cxx @@ -0,0 +1,82 @@ +/* + * Authors: Brendan Le Foll + * Mihai Tudor Panu + * Sarah Knepper + * Copyright (c) 2014 - 2016 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 "grovebutton.hpp" + +using namespace upm; + +GroveButton::GroveButton(unsigned int pin) +{ + if ( !(m_gpio = mraa_gpio_init(pin)) ) { + throw std::invalid_argument(std::string(__FUNCTION__) + + ": mraa_gpio_init() failed, invalid pin?"); + return; + } + 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); +} + +#ifdef JAVACALLBACK +void GroveButton::installISR(mraa::Edge level, jobject runnable) +{ + installISR(level, mraa_java_isr_callback, runnable); +} +#endif + +void GroveButton::installISR(mraa::Edge level, void (*isr)(void *), void *arg) +{ + if (m_isrInstalled) + uninstallISR(); + + // install our interrupt handler + mraa_gpio_isr(m_gpio, (mraa_gpio_edge_t) level, isr, arg); + m_isrInstalled = true; +} + +void GroveButton::uninstallISR() +{ + mraa_gpio_isr_exit(m_gpio); + m_isrInstalled = false; +} diff --git a/src/grove/grovebutton.hpp b/src/grove/grovebutton.hpp index 6f746ccb..72eb4734 100644 --- a/src/grove/grovebutton.hpp +++ b/src/grove/grovebutton.hpp @@ -1,7 +1,8 @@ /* - * Place-holder Header for Documentation and future API change - * - * Copyright (c) 2014 Intel Corporation. + * Authors: Brendan Le Foll + * Mihai Tudor Panu + * Sarah Knepper + * Copyright (c) 2014 - 2016 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -22,7 +23,15 @@ * 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 +#include "grovebase.hpp" + +namespace upm { + /** * @library grove * @sensor grovebutton @@ -40,3 +49,56 @@ * @image html grovebutton.jpg * @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(); + /** + * Gets the name of the sensor + * + * @return Name of this sensor + */ + std::string name(); + /** + * Gets the value from the GPIO pin + * + * @return Value from the GPIO pin + */ + int value(); + + /** + * Installs an interrupt service routine (ISR) to be called when + * the button is activated or deactivated. + * + * @param fptr Pointer to a function to be called on interrupt + * @param arg Pointer to an object to be supplied as an + * argument to the ISR. + */ +#if defined(SWIGJAVA) || defined(JAVACALLBACK) + void installISR(mraa::Edge level, jobject runnable); +#else + void installISR(mraa::Edge level, void (*isr)(void *), void *arg); +#endif + /** + * Uninstalls the previously installed ISR + * + */ + void uninstallISR(); + + private: +#if defined(SWIGJAVA) || defined(JAVACALLBACK) + void installISR(mraa::Edge level, void (*isr)(void *), void *arg); +#endif + bool m_isrInstalled; + std::string m_name; + mraa_gpio_context m_gpio; +}; +} diff --git a/src/grove/groveled.cxx b/src/grove/groveled.cxx new file mode 100644 index 00000000..68f107da --- /dev/null +++ b/src/grove/groveled.cxx @@ -0,0 +1,67 @@ +/* + * Authors: Brendan Le Foll + * Mihai Tudor Panu + * Sarah Knepper + * Copyright (c) 2014 - 2016 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 "groveled.hpp" + +using namespace upm; + +GroveLed::GroveLed(int pin) +{ + if ( !(m_gpio = mraa_gpio_init(pin)) ) { + throw std::invalid_argument(std::string(__FUNCTION__) + + ": mraa_gpio_init() failed, invalid pin?"); + return; + } + mraa_gpio_dir(m_gpio, MRAA_GPIO_OUT); + m_name = "LED Socket"; +} + +GroveLed::~GroveLed() +{ + mraa_gpio_close(m_gpio); +} + +mraa_result_t GroveLed::write(int value) +{ + if (value >= 1) { + return mraa_gpio_write(m_gpio, 1); + } + return mraa_gpio_write(m_gpio, 0); +} + +mraa_result_t GroveLed::on() +{ + return write(1); +} + +mraa_result_t GroveLed::off() +{ + return write(0); +} diff --git a/src/grove/groveled.hpp b/src/grove/groveled.hpp index c35c653e..83d32eec 100644 --- a/src/grove/groveled.hpp +++ b/src/grove/groveled.hpp @@ -1,7 +1,8 @@ /* - * Place-holder Header for Documentation and future API change - * - * Copyright (c) 2014 Intel Corporation. + * Authors: Brendan Le Foll + * Mihai Tudor Panu + * Sarah Knepper + * Copyright (c) 2014 - 2016 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -22,7 +23,15 @@ * 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 +#include "grovebase.hpp" + +namespace upm { + /** * @library grove * @sensor groveled @@ -35,7 +44,7 @@ * @brief API for the Grove LED * * UPM module for the Grove LED (or other similar light-emitting diodes). - * An LED is a small lightbulb that emits light in + * An LED is a small lightbulb that emits light in * response to a small current. The longer wire of an LED connects * to the positive seat (anode); the shorter wire connects to the * negative seat (cathode). The flat side of the bulb corresponds @@ -45,4 +54,43 @@ * @snippet groveled.cxx Interesting * @snippet groveled-multi.cxx Interesting */ - +class GroveLed: public Grove { + public: + /** + * Grove LED constructor + * + * @param gpio Pin to use + */ + GroveLed(int pin); + /** + * Grove LED destructor + */ + ~GroveLed(); + /** + * Turns the LED on or off, depending on the value. + * If the value is positive (greater than or equal + * to 1), the LED is turned on. Otherwise, for 0 + * or negative values, the LED is turned off. + * + * @param value Tells the LED to turn on (for values >=1) + * or off (for values <1) + * + * @return 0 if successful, non-zero otherwise + */ + mraa_result_t write(int value); + /** + * Turns the LED off + * + * @return 0 if successful, non-zero otherwise + */ + mraa_result_t off(); + /** + * Turns the LED on + * + * @return 0 if successful, non-zero otherwise + */ + mraa_result_t on(); + private: + mraa_gpio_context m_gpio; +}; +} diff --git a/src/grove/grovelight.cxx b/src/grove/grovelight.cxx new file mode 100644 index 00000000..39313782 --- /dev/null +++ b/src/grove/grovelight.cxx @@ -0,0 +1,63 @@ +/* + * Authors: Brendan Le Foll + * Mihai Tudor Panu + * Sarah Knepper + * Copyright (c) 2014 - 2016 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 "grovelight.hpp" +#include "math.h" + +using namespace upm; + +GroveLight::GroveLight(unsigned int pin) +{ + if ( !(m_aio = mraa_aio_init(pin)) ) { + throw std::invalid_argument(std::string(__FUNCTION__) + + ": mraa_aio_init() failed, invalid pin?"); + return; + } + m_name = "Light Sensor"; +} + +GroveLight::~GroveLight() +{ + mraa_aio_close(m_aio); +} + +int GroveLight::value() +{ + // rough conversion to lux, using formula from Grove Starter Kit booklet + float a = (float) mraa_aio_read(m_aio); + if (a == -1.0) return -1; + a = 10000.0/pow(((1023.0-a)*10.0/a)*15.0,4.0/3.0); + return (int) round(a); +} + +float GroveLight::raw_value() +{ + return (float) mraa_aio_read(m_aio); +} diff --git a/src/grove/grovelight.hpp b/src/grove/grovelight.hpp index b71d8adc..10e45f72 100644 --- a/src/grove/grovelight.hpp +++ b/src/grove/grovelight.hpp @@ -1,7 +1,8 @@ /* - * Place-holder Header for Documentation and future API change - * - * Copyright (c) 2014 Intel Corporation. + * Authors: Brendan Le Foll + * Mihai Tudor Panu + * Sarah Knepper + * Copyright (c) 2014 - 2016 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -22,7 +23,15 @@ * 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 +#include "grovebase.hpp" + +namespace upm { + /** * @library grove * @sensor grovelight @@ -43,4 +52,31 @@ * @image html grovelight.jpg * @snippet grovelight.cxx Interesting */ - +class GroveLight: public Grove { + public: + /** + * Grove analog light sensor constructor + * + * @param pin Analog pin to use + */ + GroveLight(unsigned int pin); + /** + * GroveLight destructor + */ + ~GroveLight(); + /** + * Gets the raw value from the AIO pin + * + * @return Raw value from the ADC + */ + float raw_value(); + /** + * Gets an approximate light value, in lux, from the sensor + * + * @return Normalized light reading in lux + */ + int value(); + private: + mraa_aio_context m_aio; +}; +} diff --git a/src/grove/groverelay.cxx b/src/grove/groverelay.cxx new file mode 100644 index 00000000..53fba212 --- /dev/null +++ b/src/grove/groverelay.cxx @@ -0,0 +1,69 @@ +/* + * Authors: Brendan Le Foll + * Mihai Tudor Panu + * Sarah Knepper + * Copyright (c) 2014 - 2016 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 "groverelay.hpp" + +using namespace upm; + +GroveRelay::GroveRelay(unsigned int pin) +{ + if ( !(m_gpio = mraa_gpio_init(pin)) ) { + throw std::invalid_argument(std::string(__FUNCTION__) + + ": mraa_gpio_init() failed, invalid pin?"); + return; + } + mraa_gpio_dir(m_gpio, MRAA_GPIO_OUT); + m_name = "Relay Switch"; +} + +GroveRelay::~GroveRelay() +{ + mraa_gpio_close(m_gpio); +} + +mraa_result_t GroveRelay::on() +{ + return mraa_gpio_write(m_gpio, 1); +} + +mraa_result_t GroveRelay::off() +{ + return mraa_gpio_write(m_gpio, 0); +} + +bool GroveRelay::isOn() +{ + return mraa_gpio_read(m_gpio) == 1; +} + +bool GroveRelay::isOff() +{ + return mraa_gpio_read(m_gpio) == 0; +} diff --git a/src/grove/groverelay.hpp b/src/grove/groverelay.hpp index 6edc77dc..325e1ac0 100644 --- a/src/grove/groverelay.hpp +++ b/src/grove/groverelay.hpp @@ -1,7 +1,8 @@ /* - * Place-holder Header for Documentation and future API change - * - * Copyright (c) 2014 Intel Corporation. + * Authors: Brendan Le Foll + * Mihai Tudor Panu + * Sarah Knepper + * Copyright (c) 2014 - 2016 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -22,7 +23,15 @@ * 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 +#include "grovebase.hpp" + +namespace upm { + /** * @library grove * @sensor groverelay @@ -35,11 +44,52 @@ * @brief API for the Grove Relay * * UPM module for the Grove relay switch. The Grove relay is a - * digital normally-open switch that uses low voltage or current to - * control a higher voltage and/or higher current. When closed, + * digital normally-open switch that uses low voltage or current to + * control a higher voltage and/or higher current. When closed, * the indicator LED lights up and current is allowed to flow. * * @image html groverelay.jpg * @snippet groverelay.cxx Interesting */ - +class GroveRelay: public Grove { + public: + /** + * Grove relay constructor + * + * @param gpio Pin to use + */ + GroveRelay(unsigned int pin); + /** + * Grove relay destructor + */ + ~GroveRelay(); + /** + * Sets the relay switch to on (closed). This allows current + * to flow and lights up the indicator LED. + * + * @return 0 if successful, non-zero otherwise + */ + mraa_result_t on(); + /** + * Sets the relay switch to off (open). This stops current + * from flowing and the indicator LED is not lit. + * + * @return 0 if successful, non-zero otherwise + */ + mraa_result_t off(); + /** + * Defines whether the relay switch is closed. + * + * @return True if the switch is on (closed), false otherwise + */ + bool isOn(); + /** + * Defines whether the relay switch is open. + * + * @return True if the switch is off (open), false otherwise + */ + bool isOff(); + private: + mraa_gpio_context m_gpio; +}; +} diff --git a/src/grove/groverotary.cxx b/src/grove/groverotary.cxx new file mode 100644 index 00000000..925c97fd --- /dev/null +++ b/src/grove/groverotary.cxx @@ -0,0 +1,79 @@ +/* + * Authors: Brendan Le Foll + * Mihai Tudor Panu + * Sarah Knepper + * Copyright (c) 2014 - 2016 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 "groverotary.hpp" +#include "math.h" + +using namespace upm; + +GroveRotary::GroveRotary(unsigned int pin) +{ + if ( !(m_aio = mraa_aio_init(pin)) ) { + throw std::invalid_argument(std::string(__FUNCTION__) + + ": mraa_aio_init() failed, invalid pin?"); + return; + } + m_name = "Rotary Angle Sensor"; +} + +GroveRotary::~GroveRotary() +{ + mraa_aio_close(m_aio); +} + +float GroveRotary::abs_value() +{ + return (float) mraa_aio_read(m_aio); +} + +float GroveRotary::abs_deg() +{ + return GroveRotary::abs_value() * (float) m_max_angle / 1023.0; +} + +float GroveRotary::abs_rad() +{ + return GroveRotary::abs_deg() * M_PI / 180.0; +} + +float GroveRotary::rel_value() +{ + return GroveRotary::abs_value() - 512.0; +} + +float GroveRotary::rel_deg() +{ + return GroveRotary::rel_value() * (float) m_max_angle / 1023.0; +} + +float GroveRotary::rel_rad() +{ + return GroveRotary::rel_deg() * M_PI / 180.0; +} diff --git a/src/grove/groverotary.hpp b/src/grove/groverotary.hpp index ee989781..86c8ad6c 100644 --- a/src/grove/groverotary.hpp +++ b/src/grove/groverotary.hpp @@ -1,7 +1,8 @@ /* - * Place-holder Header for Documentation and future API change - * - * Copyright (c) 2014 Intel Corporation. + * Authors: Brendan Le Foll + * Mihai Tudor Panu + * Sarah Knepper + * Copyright (c) 2014 - 2016 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -22,7 +23,15 @@ * 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 +#include "grovebase.hpp" + +namespace upm { + /** * @library grove * @sensor groverotary @@ -42,4 +51,56 @@ * @image html groverotary.jpeg * @snippet groverotary.cxx Interesting */ - \ No newline at end of file +class GroveRotary: public Grove { + public: + /** + * Grove rotary angle sensor constructor + * + * @param pin Number of the analog pin to use + */ + GroveRotary(unsigned int pin); + /** + * GroveRotary destructor + */ + ~GroveRotary(); + /** + * Gets the absolute raw value from the AIO pin + * + * @return Unsigned value from the ADC + */ + float abs_value(); + /** + * Gets absolute raw degrees from the AIO pin + * + * @return Unsigned degrees from the ADC + */ + float abs_deg(); + /** + * Gets absolute raw radians from the AIO pin + * + * @return Unsigned radians from the ADC + */ + float abs_rad(); + /** + * Gets the relative value from the AIO pin + * + * @return Signed value from the ADC + */ + float rel_value(); + /** + * Gets relative degrees from the AIO pin + * + * @return Signed degrees from the ADC + */ + float rel_deg(); + /** + * Gets relative radians from the AIO pin + * + * @return Signed radians from the ADC + */ + float rel_rad(); + private: + mraa_aio_context m_aio; + static const int m_max_angle = 300; +}; +} diff --git a/src/grove/groveslide.cxx b/src/grove/groveslide.cxx new file mode 100644 index 00000000..d3c511fb --- /dev/null +++ b/src/grove/groveslide.cxx @@ -0,0 +1,68 @@ +/* + * Authors: Brendan Le Foll + * Mihai Tudor Panu + * Sarah Knepper + * Copyright (c) 2014 - 2016 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 "groveslide.hpp" +#include "math.h" + +using namespace upm; + +GroveSlide::GroveSlide(unsigned int pin, float ref_voltage) +{ + if ( !(m_aio = mraa_aio_init(pin)) ) { + throw std::invalid_argument(std::string(__FUNCTION__) + + ": mraa_aio_init() failed, invalid pin?"); + return; + } + 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/groveslide.hpp b/src/grove/groveslide.hpp index e3cbba19..7fa35f61 100644 --- a/src/grove/groveslide.hpp +++ b/src/grove/groveslide.hpp @@ -1,7 +1,8 @@ /* - * Place-holder Header for Documentation and future API change - * - * Copyright (c) 2014 Intel Corporation. + * Authors: Brendan Le Foll + * Mihai Tudor Panu + * Sarah Knepper + * Copyright (c) 2014 - 2016 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -22,7 +23,15 @@ * 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 +#include "grovebase.hpp" + +namespace upm { + /** * @library grove * @sensor groveslide @@ -39,4 +48,40 @@ * @image html groveslide.jpeg * @snippet groveslide.cxx Interesting */ - \ No newline at end of file +class GroveSlide: public Grove { + public: + /** + * Grove analog slide potentiometer constructor + * + * @param pin Number of the analog pin to use + * + * @param ref_voltage Reference voltage the board is set to, as a floating-point value; default is 5.0V + */ + GroveSlide(unsigned int pin, float ref_voltage = 5.0); + /** + * GroveSlide destructor + */ + ~GroveSlide(); + /** + * Gets the raw value from the AIO pin + * + * @return Raw value from the ADC + */ + float raw_value(); + /** + * Gets the voltage value from the pin + * + * @return Voltage reading based on the reference voltage + */ + float voltage_value(); + /** + * Gets the board's reference voltage passed on object initialization + * + * @return Reference voltage the class was set for + */ + float ref_voltage(); + private: + mraa_aio_context m_aio; + float m_ref_voltage; +}; +} diff --git a/src/grove/grovetemp.cxx b/src/grove/grovetemp.cxx new file mode 100644 index 00000000..8e55914c --- /dev/null +++ b/src/grove/grovetemp.cxx @@ -0,0 +1,66 @@ +/* + * Authors: Brendan Le Foll + * Mihai Tudor Panu + * Sarah Knepper + * Copyright (c) 2014 - 2016 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 "grovetemp.hpp" +#include "math.h" + +using namespace upm; + +GroveTemp::GroveTemp(unsigned int pin, float scale) +{ + if ( !(m_aio = mraa_aio_init(pin)) ) { + throw std::invalid_argument(std::string(__FUNCTION__) + + ": mraa_aio_init() failed, invalid pin?"); + return; + } + m_name = "Temperature Sensor"; + m_scale = scale; +} + +GroveTemp::~GroveTemp() +{ + mraa_aio_close(m_aio); +} + +int GroveTemp::value () +{ + float a = (float) mraa_aio_read(m_aio); + if (a == -1.0) return -1; + // Apply scale factor after error check + a *= m_scale; + float r = (float)(1023.0-a)*10000.0/a; + float t = 1.0/(log(r/10000.0)/3975.0 + 1.0/298.15)-273.15; + return (int) round(t); +} + +float GroveTemp::raw_value() +{ + return (float) mraa_aio_read(m_aio); +} diff --git a/src/grove/grovetemp.hpp b/src/grove/grovetemp.hpp index d85ac3e8..19e4d045 100644 --- a/src/grove/grovetemp.hpp +++ b/src/grove/grovetemp.hpp @@ -1,7 +1,8 @@ /* - * Place-holder Header for Documentation and future API change - * - * Copyright (c) 2014 Intel Corporation. + * Authors: Brendan Le Foll + * Mihai Tudor Panu + * Sarah Knepper + * Copyright (c) 2014 - 2016 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -22,7 +23,15 @@ * 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 +#include "grovebase.hpp" + +namespace upm { + /** * @library grove * @sensor grovetemp @@ -39,4 +48,34 @@ * @image html grovetemp.jpg * @snippet grovetemp.cxx Interesting */ - +class GroveTemp: public Grove { + public: + /** + * Grove analog temperature sensor constructor + * + * @param pin Analog pin to use + * @param scale Scaling factor for raw analog value from the ADC, + * useful for mixed 3.3V/5V boards + */ + GroveTemp(unsigned int pin, float scale = 1.0); + /** + * GroveTemp destructor + */ + ~GroveTemp(); + /** + * Gets the raw value from the AIO pin + * + * @return Raw value from the ADC + */ + float raw_value(); + /** + * Gets the temperature in Celsius from the sensor + * + * @return Normalized temperature in Celsius + */ + int value(); + private: + mraa_aio_context m_aio; + float m_scale; +}; +} diff --git a/src/grove/javaupm_grove.i b/src/grove/javaupm_grove.i index 72cc8ad7..a70cdea1 100644 --- a/src/grove/javaupm_grove.i +++ b/src/grove/javaupm_grove.i @@ -4,11 +4,50 @@ %apply int {mraa::Edge} +%include "grove.hpp" %{ #include "grove.hpp" %} -%include "grove.hpp" +%include "grovebase.hpp" +%{ + #include "grovebase.hpp" +%} + +%include "grovebutton.hpp" +%{ + #include "grovebutton.hpp" +%} + +%include "groveled.hpp" +%{ + #include "groveled.hpp" +%} + +%include "grovelight.hpp" +%{ + #include "grovelight.hpp" +%} + +%include "groverelay.hpp" +%{ + #include "groverelay.hpp" +%} + +%include "groverotary.hpp" +%{ + #include "groverotary.hpp" +%} + +%include "groveslide.hpp" +%{ + #include "groveslide.hpp" +%} + +%include "grovetemp.hpp" +%{ + #include "grovetemp.hpp" +%} %pragma(java) jniclasscode=%{ static { @@ -20,4 +59,3 @@ } } %} - diff --git a/src/grove/jsupm_grove.i b/src/grove/jsupm_grove.i index 59269d99..e3390235 100644 --- a/src/grove/jsupm_grove.i +++ b/src/grove/jsupm_grove.i @@ -1,8 +1,47 @@ %module jsupm_grove %include "../upm.i" +%include "grove.hpp" %{ #include "grove.hpp" %} -%include "grove.hpp" +%include "grovebase.hpp" +%{ + #include "grovebase.hpp" +%} + +%include "grovebutton.hpp" +%{ + #include "grovebutton.hpp" +%} + +%include "groveled.hpp" +%{ + #include "groveled.hpp" +%} + +%include "grovelight.hpp" +%{ + #include "grovelight.hpp" +%} + +%include "groverelay.hpp" +%{ + #include "groverelay.hpp" +%} + +%include "groverotary.hpp" +%{ + #include "groverotary.hpp" +%} + +%include "groveslide.hpp" +%{ + #include "groveslide.hpp" +%} + +%include "grovetemp.hpp" +%{ + #include "grovetemp.hpp" +%} diff --git a/src/grove/pyupm_grove.i b/src/grove/pyupm_grove.i index 1edd8028..31412d02 100644 --- a/src/grove/pyupm_grove.i +++ b/src/grove/pyupm_grove.i @@ -5,11 +5,47 @@ %feature("autodoc", "3"); -#ifdef DOXYGEN -%include "grove_doc.i" -#endif - %include "grove.hpp" %{ #include "grove.hpp" %} + +%include "grovebase.hpp" +%{ + #include "grovebase.hpp" +%} + +%include "grovebutton.hpp" +%{ + #include "grovebutton.hpp" +%} + +%include "groveled.hpp" +%{ + #include "groveled.hpp" +%} + +%include "grovelight.hpp" +%{ + #include "grovelight.hpp" +%} + +%include "groverelay.hpp" +%{ + #include "groverelay.hpp" +%} + +%include "groverotary.hpp" +%{ + #include "groverotary.hpp" +%} + +%include "groveslide.hpp" +%{ + #include "groveslide.hpp" +%} + +%include "grovetemp.hpp" +%{ + #include "grovetemp.hpp" +%}