mirror of
				https://github.com/eclipse/upm.git
				synced 2025-10-30 22:54:44 +03:00 
			
		
		
		
	grove: split code base for grove sensors into individual files
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
		| @@ -1,5 +1,5 @@ | |||||||
| set (libname "grove") | set (libname "grove") | ||||||
| set (libdescription "upm grove module") | set (libdescription "upm grove module") | ||||||
| set (module_src ${libname}.cxx) | set (module_src grovebutton.cxx groveled.cxx grovelight.cxx groverelay.cxx groverotary.cxx groveslide.cxx grovetemp.cxx) | ||||||
| set (module_hpp ${libname}.hpp) | set (module_hpp grovebutton.hpp groveled.hpp grovelight.hpp groverelay.hpp groverotary.hpp groveslide.hpp grovetemp.hpp grovebase.hpp grove.hpp) | ||||||
| upm_module_init() | upm_module_init() | ||||||
|   | |||||||
| @@ -1,307 +1 @@ | |||||||
| /* |  | ||||||
|  * Author: Brendan Le Foll <brendan.le.foll@intel.com> |  | ||||||
|  * Contributions: Mihai Tudor Panu <mihai.tudor.panu@intel.com> |  | ||||||
|  * Contributions: Sarah Knepper <sarah.knepper@intel.com> |  | ||||||
|  * 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 <iostream> |  | ||||||
| #include <string> |  | ||||||
| #include <stdexcept> |  | ||||||
|  |  | ||||||
| #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; |  | ||||||
| } |  | ||||||
|   | |||||||
| @@ -1,8 +1,8 @@ | |||||||
| /* | /* | ||||||
|  * Author: Brendan Le Foll <brendan.le.foll@intel.com> |  * Authors: Brendan Le Foll <brendan.le.foll@intel.com> | ||||||
|  * Contributions: Mihai Tudor Panu <mihai.tudor.panu@intel.com> |  *          Mihai Tudor Panu <mihai.tudor.panu@intel.com> | ||||||
|  * Contributions: Sarah Knepper <sarah.knepper@intel.com> |  *          Sarah Knepper <sarah.knepper@intel.com> | ||||||
|  * Copyright (c) 2014 Intel Corporation. |  * Copyright (c) 2014 - 2016 Intel Corporation. | ||||||
|  * |  * | ||||||
|  * Permission is hereby granted, free of charge, to any person obtaining |  * Permission is hereby granted, free of charge, to any person obtaining | ||||||
|  * a copy of this software and associated documentation files (the |  * a copy of this software and associated documentation files (the | ||||||
| @@ -25,389 +25,10 @@ | |||||||
|  */ |  */ | ||||||
| #pragma once | #pragma once | ||||||
|  |  | ||||||
| #include <string> | #include <grovebutton.hpp> | ||||||
| #include <mraa/aio.hpp> | #include <groveled.hpp> | ||||||
| #include <mraa/gpio.hpp> | #include <grovelight.hpp> | ||||||
|  | #include <groverelay.hpp> | ||||||
| namespace upm { | #include <groverotary.hpp> | ||||||
|  | #include <groveslide.hpp> | ||||||
| /** | #include <grovetemp.hpp> | ||||||
|  * @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; |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| } |  | ||||||
|   | |||||||
							
								
								
									
										47
									
								
								src/grove/grovebase.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								src/grove/grovebase.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,47 @@ | |||||||
|  | /* | ||||||
|  |  * Authors: Brendan Le Foll <brendan.le.foll@intel.com> | ||||||
|  |  *          Mihai Tudor Panu <mihai.tudor.panu@intel.com> | ||||||
|  |  *          Sarah Knepper <sarah.knepper@intel.com> | ||||||
|  |  * 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 <string> | ||||||
|  |  | ||||||
|  | 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; | ||||||
|  | }; | ||||||
|  | } | ||||||
							
								
								
									
										82
									
								
								src/grove/grovebutton.cxx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										82
									
								
								src/grove/grovebutton.cxx
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,82 @@ | |||||||
|  | /* | ||||||
|  |  * Authors: Brendan Le Foll <brendan.le.foll@intel.com> | ||||||
|  |  *          Mihai Tudor Panu <mihai.tudor.panu@intel.com> | ||||||
|  |  *          Sarah Knepper <sarah.knepper@intel.com> | ||||||
|  |  * 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 <iostream> | ||||||
|  | #include <string> | ||||||
|  | #include <stdexcept> | ||||||
|  |  | ||||||
|  | #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; | ||||||
|  | } | ||||||
| @@ -1,7 +1,8 @@ | |||||||
| /* | /* | ||||||
|  * Place-holder Header for Documentation and future API change |  * Authors: Brendan Le Foll <brendan.le.foll@intel.com> | ||||||
|  * |  *          Mihai Tudor Panu <mihai.tudor.panu@intel.com> | ||||||
|  * Copyright (c) 2014 Intel Corporation. |  *          Sarah Knepper <sarah.knepper@intel.com> | ||||||
|  |  * Copyright (c) 2014 - 2016 Intel Corporation. | ||||||
|  * |  * | ||||||
|  * Permission is hereby granted, free of charge, to any person obtaining |  * Permission is hereby granted, free of charge, to any person obtaining | ||||||
|  * a copy of this software and associated documentation files (the |  * a copy of this software and associated documentation files (the | ||||||
| @@ -23,6 +24,14 @@ | |||||||
|  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include <string> | ||||||
|  | #include <mraa/gpio.hpp> | ||||||
|  | #include "grovebase.hpp" | ||||||
|  |  | ||||||
|  | namespace upm { | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * @library grove |  * @library grove | ||||||
|  * @sensor grovebutton |  * @sensor grovebutton | ||||||
| @@ -40,3 +49,56 @@ | |||||||
|  * @image html grovebutton.jpg |  * @image html grovebutton.jpg | ||||||
|  * @snippet grovebutton.cxx Interesting |  * @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; | ||||||
|  | }; | ||||||
|  | } | ||||||
|   | |||||||
							
								
								
									
										67
									
								
								src/grove/groveled.cxx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								src/grove/groveled.cxx
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,67 @@ | |||||||
|  | /* | ||||||
|  |  * Authors: Brendan Le Foll <brendan.le.foll@intel.com> | ||||||
|  |  *          Mihai Tudor Panu <mihai.tudor.panu@intel.com> | ||||||
|  |  *          Sarah Knepper <sarah.knepper@intel.com> | ||||||
|  |  * 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 <iostream> | ||||||
|  | #include <string> | ||||||
|  | #include <stdexcept> | ||||||
|  |  | ||||||
|  | #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); | ||||||
|  | } | ||||||
| @@ -1,7 +1,8 @@ | |||||||
| /* | /* | ||||||
|  * Place-holder Header for Documentation and future API change |  * Authors: Brendan Le Foll <brendan.le.foll@intel.com> | ||||||
|  * |  *          Mihai Tudor Panu <mihai.tudor.panu@intel.com> | ||||||
|  * Copyright (c) 2014 Intel Corporation. |  *          Sarah Knepper <sarah.knepper@intel.com> | ||||||
|  |  * Copyright (c) 2014 - 2016 Intel Corporation. | ||||||
|  * |  * | ||||||
|  * Permission is hereby granted, free of charge, to any person obtaining |  * Permission is hereby granted, free of charge, to any person obtaining | ||||||
|  * a copy of this software and associated documentation files (the |  * a copy of this software and associated documentation files (the | ||||||
| @@ -23,6 +24,14 @@ | |||||||
|  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include <string> | ||||||
|  | #include <mraa/gpio.hpp> | ||||||
|  | #include "grovebase.hpp" | ||||||
|  |  | ||||||
|  | namespace upm { | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * @library grove |  * @library grove | ||||||
|  * @sensor groveled |  * @sensor groveled | ||||||
| @@ -45,4 +54,43 @@ | |||||||
|  * @snippet groveled.cxx Interesting |  * @snippet groveled.cxx Interesting | ||||||
|  * @snippet groveled-multi.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; | ||||||
|  | }; | ||||||
|  | } | ||||||
|   | |||||||
							
								
								
									
										63
									
								
								src/grove/grovelight.cxx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								src/grove/grovelight.cxx
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,63 @@ | |||||||
|  | /* | ||||||
|  |  * Authors: Brendan Le Foll <brendan.le.foll@intel.com> | ||||||
|  |  *          Mihai Tudor Panu <mihai.tudor.panu@intel.com> | ||||||
|  |  *          Sarah Knepper <sarah.knepper@intel.com> | ||||||
|  |  * 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 <iostream> | ||||||
|  | #include <string> | ||||||
|  | #include <stdexcept> | ||||||
|  |  | ||||||
|  | #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); | ||||||
|  | } | ||||||
| @@ -1,7 +1,8 @@ | |||||||
| /* | /* | ||||||
|  * Place-holder Header for Documentation and future API change |  * Authors: Brendan Le Foll <brendan.le.foll@intel.com> | ||||||
|  * |  *          Mihai Tudor Panu <mihai.tudor.panu@intel.com> | ||||||
|  * Copyright (c) 2014 Intel Corporation. |  *          Sarah Knepper <sarah.knepper@intel.com> | ||||||
|  |  * Copyright (c) 2014 - 2016 Intel Corporation. | ||||||
|  * |  * | ||||||
|  * Permission is hereby granted, free of charge, to any person obtaining |  * Permission is hereby granted, free of charge, to any person obtaining | ||||||
|  * a copy of this software and associated documentation files (the |  * a copy of this software and associated documentation files (the | ||||||
| @@ -23,6 +24,14 @@ | |||||||
|  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include <string> | ||||||
|  | #include <mraa/aio.hpp> | ||||||
|  | #include "grovebase.hpp" | ||||||
|  |  | ||||||
|  | namespace upm { | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * @library grove |  * @library grove | ||||||
|  * @sensor grovelight |  * @sensor grovelight | ||||||
| @@ -43,4 +52,31 @@ | |||||||
|  * @image html grovelight.jpg |  * @image html grovelight.jpg | ||||||
|  * @snippet grovelight.cxx Interesting |  * @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; | ||||||
|  | }; | ||||||
|  | } | ||||||
|   | |||||||
							
								
								
									
										69
									
								
								src/grove/groverelay.cxx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										69
									
								
								src/grove/groverelay.cxx
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,69 @@ | |||||||
|  | /* | ||||||
|  |  * Authors: Brendan Le Foll <brendan.le.foll@intel.com> | ||||||
|  |  *          Mihai Tudor Panu <mihai.tudor.panu@intel.com> | ||||||
|  |  *          Sarah Knepper <sarah.knepper@intel.com> | ||||||
|  |  * 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 <iostream> | ||||||
|  | #include <string> | ||||||
|  | #include <stdexcept> | ||||||
|  |  | ||||||
|  | #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; | ||||||
|  | } | ||||||
| @@ -1,7 +1,8 @@ | |||||||
| /* | /* | ||||||
|  * Place-holder Header for Documentation and future API change |  * Authors: Brendan Le Foll <brendan.le.foll@intel.com> | ||||||
|  * |  *          Mihai Tudor Panu <mihai.tudor.panu@intel.com> | ||||||
|  * Copyright (c) 2014 Intel Corporation. |  *          Sarah Knepper <sarah.knepper@intel.com> | ||||||
|  |  * Copyright (c) 2014 - 2016 Intel Corporation. | ||||||
|  * |  * | ||||||
|  * Permission is hereby granted, free of charge, to any person obtaining |  * Permission is hereby granted, free of charge, to any person obtaining | ||||||
|  * a copy of this software and associated documentation files (the |  * a copy of this software and associated documentation files (the | ||||||
| @@ -23,6 +24,14 @@ | |||||||
|  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include <string> | ||||||
|  | #include <mraa/gpio.hpp> | ||||||
|  | #include "grovebase.hpp" | ||||||
|  |  | ||||||
|  | namespace upm { | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * @library grove |  * @library grove | ||||||
|  * @sensor groverelay |  * @sensor groverelay | ||||||
| @@ -42,4 +51,45 @@ | |||||||
|  * @image html groverelay.jpg |  * @image html groverelay.jpg | ||||||
|  * @snippet groverelay.cxx Interesting |  * @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; | ||||||
|  | }; | ||||||
|  | } | ||||||
|   | |||||||
							
								
								
									
										79
									
								
								src/grove/groverotary.cxx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								src/grove/groverotary.cxx
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,79 @@ | |||||||
|  | /* | ||||||
|  |  * Authors: Brendan Le Foll <brendan.le.foll@intel.com> | ||||||
|  |  *          Mihai Tudor Panu <mihai.tudor.panu@intel.com> | ||||||
|  |  *          Sarah Knepper <sarah.knepper@intel.com> | ||||||
|  |  * 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 <iostream> | ||||||
|  | #include <string> | ||||||
|  | #include <stdexcept> | ||||||
|  |  | ||||||
|  | #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; | ||||||
|  | } | ||||||
| @@ -1,7 +1,8 @@ | |||||||
| /* | /* | ||||||
|  * Place-holder Header for Documentation and future API change |  * Authors: Brendan Le Foll <brendan.le.foll@intel.com> | ||||||
|  * |  *          Mihai Tudor Panu <mihai.tudor.panu@intel.com> | ||||||
|  * Copyright (c) 2014 Intel Corporation. |  *          Sarah Knepper <sarah.knepper@intel.com> | ||||||
|  |  * Copyright (c) 2014 - 2016 Intel Corporation. | ||||||
|  * |  * | ||||||
|  * Permission is hereby granted, free of charge, to any person obtaining |  * Permission is hereby granted, free of charge, to any person obtaining | ||||||
|  * a copy of this software and associated documentation files (the |  * a copy of this software and associated documentation files (the | ||||||
| @@ -23,6 +24,14 @@ | |||||||
|  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include <string> | ||||||
|  | #include <mraa/aio.hpp> | ||||||
|  | #include "grovebase.hpp" | ||||||
|  |  | ||||||
|  | namespace upm { | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * @library grove |  * @library grove | ||||||
|  * @sensor groverotary |  * @sensor groverotary | ||||||
| @@ -42,4 +51,56 @@ | |||||||
|  * @image html groverotary.jpeg |  * @image html groverotary.jpeg | ||||||
|  * @snippet groverotary.cxx Interesting |  * @snippet groverotary.cxx Interesting | ||||||
|  */ |  */ | ||||||
|   | 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; | ||||||
|  | }; | ||||||
|  | } | ||||||
|   | |||||||
							
								
								
									
										68
									
								
								src/grove/groveslide.cxx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								src/grove/groveslide.cxx
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,68 @@ | |||||||
|  | /* | ||||||
|  |  * Authors: Brendan Le Foll <brendan.le.foll@intel.com> | ||||||
|  |  *          Mihai Tudor Panu <mihai.tudor.panu@intel.com> | ||||||
|  |  *          Sarah Knepper <sarah.knepper@intel.com> | ||||||
|  |  * 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 <iostream> | ||||||
|  | #include <string> | ||||||
|  | #include <stdexcept> | ||||||
|  |  | ||||||
|  | #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; | ||||||
|  | } | ||||||
| @@ -1,7 +1,8 @@ | |||||||
| /* | /* | ||||||
|  * Place-holder Header for Documentation and future API change |  * Authors: Brendan Le Foll <brendan.le.foll@intel.com> | ||||||
|  * |  *          Mihai Tudor Panu <mihai.tudor.panu@intel.com> | ||||||
|  * Copyright (c) 2014 Intel Corporation. |  *          Sarah Knepper <sarah.knepper@intel.com> | ||||||
|  |  * Copyright (c) 2014 - 2016 Intel Corporation. | ||||||
|  * |  * | ||||||
|  * Permission is hereby granted, free of charge, to any person obtaining |  * Permission is hereby granted, free of charge, to any person obtaining | ||||||
|  * a copy of this software and associated documentation files (the |  * a copy of this software and associated documentation files (the | ||||||
| @@ -23,6 +24,14 @@ | |||||||
|  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include <string> | ||||||
|  | #include <mraa/aio.hpp> | ||||||
|  | #include "grovebase.hpp" | ||||||
|  |  | ||||||
|  | namespace upm { | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * @library grove |  * @library grove | ||||||
|  * @sensor groveslide |  * @sensor groveslide | ||||||
| @@ -39,4 +48,40 @@ | |||||||
|  * @image html groveslide.jpeg |  * @image html groveslide.jpeg | ||||||
|  * @snippet groveslide.cxx Interesting |  * @snippet groveslide.cxx Interesting | ||||||
|  */ |  */ | ||||||
|   | 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; | ||||||
|  | }; | ||||||
|  | } | ||||||
|   | |||||||
							
								
								
									
										66
									
								
								src/grove/grovetemp.cxx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								src/grove/grovetemp.cxx
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,66 @@ | |||||||
|  | /* | ||||||
|  |  * Authors: Brendan Le Foll <brendan.le.foll@intel.com> | ||||||
|  |  *          Mihai Tudor Panu <mihai.tudor.panu@intel.com> | ||||||
|  |  *          Sarah Knepper <sarah.knepper@intel.com> | ||||||
|  |  * 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 <iostream> | ||||||
|  | #include <string> | ||||||
|  | #include <stdexcept> | ||||||
|  |  | ||||||
|  | #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); | ||||||
|  | } | ||||||
| @@ -1,7 +1,8 @@ | |||||||
| /* | /* | ||||||
|  * Place-holder Header for Documentation and future API change |  * Authors: Brendan Le Foll <brendan.le.foll@intel.com> | ||||||
|  * |  *          Mihai Tudor Panu <mihai.tudor.panu@intel.com> | ||||||
|  * Copyright (c) 2014 Intel Corporation. |  *          Sarah Knepper <sarah.knepper@intel.com> | ||||||
|  |  * Copyright (c) 2014 - 2016 Intel Corporation. | ||||||
|  * |  * | ||||||
|  * Permission is hereby granted, free of charge, to any person obtaining |  * Permission is hereby granted, free of charge, to any person obtaining | ||||||
|  * a copy of this software and associated documentation files (the |  * a copy of this software and associated documentation files (the | ||||||
| @@ -23,6 +24,14 @@ | |||||||
|  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include <string> | ||||||
|  | #include <mraa/aio.hpp> | ||||||
|  | #include "grovebase.hpp" | ||||||
|  |  | ||||||
|  | namespace upm { | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * @library grove |  * @library grove | ||||||
|  * @sensor grovetemp |  * @sensor grovetemp | ||||||
| @@ -39,4 +48,34 @@ | |||||||
|  * @image html grovetemp.jpg |  * @image html grovetemp.jpg | ||||||
|  * @snippet grovetemp.cxx Interesting |  * @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; | ||||||
|  | }; | ||||||
|  | } | ||||||
|   | |||||||
| @@ -4,11 +4,50 @@ | |||||||
|  |  | ||||||
| %apply int {mraa::Edge} | %apply int {mraa::Edge} | ||||||
|  |  | ||||||
|  | %include "grove.hpp" | ||||||
| %{ | %{ | ||||||
|     #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=%{ | %pragma(java) jniclasscode=%{ | ||||||
|     static { |     static { | ||||||
| @@ -20,4 +59,3 @@ | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| %} | %} | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,8 +1,47 @@ | |||||||
| %module jsupm_grove | %module jsupm_grove | ||||||
| %include "../upm.i" | %include "../upm.i" | ||||||
|  |  | ||||||
|  | %include "grove.hpp" | ||||||
| %{ | %{ | ||||||
|     #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" | ||||||
|  | %} | ||||||
|   | |||||||
| @@ -5,11 +5,47 @@ | |||||||
|  |  | ||||||
| %feature("autodoc", "3"); | %feature("autodoc", "3"); | ||||||
|  |  | ||||||
| #ifdef DOXYGEN |  | ||||||
| %include "grove_doc.i" |  | ||||||
| #endif |  | ||||||
|  |  | ||||||
| %include "grove.hpp" | %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" | ||||||
|  | %} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Mihai Tudor Panu
					Mihai Tudor Panu