diff --git a/examples/c++/CMakeLists.txt b/examples/c++/CMakeLists.txt index 386e5258..e84dab2b 100644 --- a/examples/c++/CMakeLists.txt +++ b/examples/c++/CMakeLists.txt @@ -87,7 +87,7 @@ link_directories (${MRAA_LIBDIR}) # mq? will use module gas # grove* will use module grove add_example (hmc5883l) -add_example (groveled) +add_example (led) add_example (relay) add_example (light) add_example (temperature) diff --git a/examples/c++/groveled.cxx b/examples/c++/led.cxx similarity index 96% rename from examples/c++/groveled.cxx rename to examples/c++/led.cxx index 8f7e752d..300a0abd 100644 --- a/examples/c++/groveled.cxx +++ b/examples/c++/led.cxx @@ -25,7 +25,7 @@ #include #include -#include "grove.hpp" +#include "led.hpp" int main(int argc, char **argv) @@ -33,7 +33,7 @@ main(int argc, char **argv) //! [Interesting] // Create the Grove LED object using GPIO pin 2 - upm::GroveLed* led = new upm::GroveLed(2); + upm::Led* led = new upm::Led(2); // Print the name std::cout << led->name() << std::endl; diff --git a/examples/c/CMakeLists.txt b/examples/c/CMakeLists.txt index ffabafc5..eb46e18e 100644 --- a/examples/c/CMakeLists.txt +++ b/examples/c/CMakeLists.txt @@ -115,6 +115,7 @@ add_example (loudness) add_example (tsl2561) add_example (collision) add_example (moisture) +add_example (led) # Custom examples add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps) diff --git a/examples/c/led.c b/examples/c/led.c new file mode 100644 index 00000000..dbd4950b --- /dev/null +++ b/examples/c/led.c @@ -0,0 +1,24 @@ +//Modified: Abhishek Malik + +#include +#include +#include +#include +#include +#include "led.h" + +void main(void) +{ + led_context dev = led_init(2); + while(1){ + if(led_on(dev) != UPM_SUCCESS){ + printf("problem turning the LED on\n"); + } + upm_delay(1); + if(led_off(dev) != UPM_SUCCESS){ + printf("problem turning the LED off\n"); + } + upm_delay(1); + } + led_close(dev); +} diff --git a/examples/java/CMakeLists.txt b/examples/java/CMakeLists.txt index e5bfeb5d..1d71e1c5 100644 --- a/examples/java/CMakeLists.txt +++ b/examples/java/CMakeLists.txt @@ -37,7 +37,7 @@ add_example(GroveEHRSample groveehr) add_example(Emg emg) add_example(Gsr gsr) add_example(GroveLed_multiSample grove) -add_example(GroveLEDSample grove) +add_example(LEDSample led) add_example(LightSample light) add_example(GroveLineFinderSample grovelinefinder) add_example(GroveMDSample grovemd) diff --git a/examples/java/GroveLEDSample.java b/examples/java/LEDSample.java similarity index 94% rename from examples/java/GroveLEDSample.java rename to examples/java/LEDSample.java index b9964490..48e7b2e0 100644 --- a/examples/java/GroveLEDSample.java +++ b/examples/java/LEDSample.java @@ -22,10 +22,10 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -public class GroveLEDSample { +public class LEDSample { public static void main (String args[]) throws InterruptedException { //! [Interesting] - upm_grove.GroveLed led = new upm_grove.GroveLed(2); + upm_led.Led led = new upm_led.Led(2); for (int i = 0; i < 10; ++i) { led.on(); diff --git a/examples/javascript/groveled.js b/examples/javascript/led.js similarity index 95% rename from examples/javascript/groveled.js rename to examples/javascript/led.js index 7314d39f..24730b7f 100644 --- a/examples/javascript/groveled.js +++ b/examples/javascript/led.js @@ -23,10 +23,10 @@ */ // Load Grove module -var groveSensor = require('jsupm_grove'); +var ledSensor = require('jsupm_led'); // Create the Grove LED object using GPIO pin 2 -var led = new groveSensor.GroveLed(2); +var led = new ledSensor.Led(2); // Print the name console.log(led.name()); diff --git a/examples/python/groveled.py b/examples/python/led.py similarity index 96% rename from examples/python/groveled.py rename to examples/python/led.py index eb104bf0..afcff24d 100644 --- a/examples/python/groveled.py +++ b/examples/python/led.py @@ -21,10 +21,10 @@ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import time -import pyupm_grove as grove +import pyupm_led as led # Create the Grove LED object using GPIO pin 2 -led = grove.GroveLed(2) +led = led.Led(2) # Print the name print led.name() diff --git a/src/led/CMakeLists.txt b/src/led/CMakeLists.txt new file mode 100644 index 00000000..2b249b18 --- /dev/null +++ b/src/led/CMakeLists.txt @@ -0,0 +1,9 @@ +upm_mixed_module_init (NAME led + DESCRIPTION "upm led module" + C_HDR led.h + C_SRC led.c + CPP_HDR led.hpp + CPP_SRC led.cxx + FTI_SRC led_fti.c + CPP_WRAPS_C + REQUIRES mraa) diff --git a/src/led/javaupm_led.i b/src/led/javaupm_led.i new file mode 100644 index 00000000..2d31bd3f --- /dev/null +++ b/src/led/javaupm_led.i @@ -0,0 +1,20 @@ +%module javaupm_led +%include "../upm.i" + +%apply int {mraa::Edge} + +%include "led.hpp" +%{ + #include "led.hpp" +%} + +%pragma(java) jniclasscode=%{ + static { + try { + System.loadLibrary("javaupm_led"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Native code library failed to load. \n" + e); + System.exit(1); + } + } +%} diff --git a/src/led/jsupm_led.i b/src/led/jsupm_led.i new file mode 100644 index 00000000..ea991177 --- /dev/null +++ b/src/led/jsupm_led.i @@ -0,0 +1,7 @@ +%module jsupm_led +%include "../upm.i" + +%include "led.hpp" +%{ + #include "led.hpp" +%} diff --git a/src/led/led.c b/src/led/led.c new file mode 100644 index 00000000..c9649c71 --- /dev/null +++ b/src/led/led.c @@ -0,0 +1,62 @@ +/* + * Author: Brendan Le Foll + * Contributions: Mihai Tudor Panu + * Contributions: Sarah Knepper + * Abhishek Malik + * Copyright (c) 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 "led.h" +#include "upm_types.h" + +led_context led_init(uint8_t pin){ + led_context dev = + (led_context)malloc(sizeof(struct _led_context)); + + if (!dev) + return NULL; + + dev->led_pin = pin; + dev->gpio = mraa_gpio_init(dev->led_pin); + + if (mraa_gpio_dir(dev->gpio, MRAA_GPIO_OUT) != MRAA_SUCCESS) + return NULL; + + return dev; +} + +void led_close(led_context dev){ + free(dev); +} + +upm_result_t led_on(led_context dev){ + if (mraa_gpio_write(dev->gpio, 1) != MRAA_SUCCESS) + return UPM_ERROR_OPERATION_FAILED; + + return UPM_SUCCESS; +} + +upm_result_t led_off(led_context dev){ + if (mraa_gpio_write(dev->gpio, 0) != MRAA_SUCCESS) + return UPM_ERROR_OPERATION_FAILED; + + return UPM_SUCCESS; +} diff --git a/src/led/led.cxx b/src/led/led.cxx new file mode 100644 index 00000000..ee74f939 --- /dev/null +++ b/src/led/led.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 "led.hpp" + +using namespace std; +using namespace upm; + +Led::Led(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); +} + +Led::~Led() +{ + mraa_gpio_close(m_gpio); +} + +mraa_result_t Led::write(int value) +{ + if (value >= 1) { + return mraa_gpio_write(m_gpio, 1); + } + return mraa_gpio_write(m_gpio, 0); +} + +mraa_result_t Led::on() +{ + return write(1); +} + +mraa_result_t Led::off() +{ + return write(0); +} diff --git a/src/led/led.h b/src/led/led.h new file mode 100644 index 00000000..8f3a248c --- /dev/null +++ b/src/led/led.h @@ -0,0 +1,93 @@ +/* + * Author: Brendan Le Foll + * Contributions: Mihai Tudor Panu + * Contributions: Sarah Knepper + * Abhishek Malik + * Copyright (c) 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. + */ +#ifndef LED_LED_H_ +#define LED_LED_H_ + +#include +#include +#include +#include +#include "upm.h" +#include "mraa/gpio.h" + +/** + * @type led + * @con gpio + * @kit gsk + * + * @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. + * + * @image html groveled.jpg + */ + +/** + * device context + */ +typedef struct _led_context { + mraa_gpio_context gpio; + uint8_t led_pin; +} *led_context; + +/** + * LED Initialization function + * + * @param pin GPIO pin to use + * @return The sensor context + */ +led_context led_init(uint8_t pin); + +/** + * LED Initialization function + * + * @param The sensor context + */ +void led_close(led_context dev); + +/** + * Function to turn LED on + * + * @param The sensor context + * @return upm_result_t UPM success/error code + */ +upm_result_t led_on(led_context dev); + +/** + * Function to turn LED off + * + * @param The sensor context + * @return upm_result_t UPM success/error code + */ +upm_result_t led_off(led_context dev); + +#endif /* LED_LED_H_ */ diff --git a/src/led/led.hpp b/src/led/led.hpp new file mode 100644 index 00000000..f6fe85d1 --- /dev/null +++ b/src/led/led.hpp @@ -0,0 +1,101 @@ +/* + * 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 +#include +#include "led.hpp" + +namespace upm { + +/** + * @library led + * @sensor led + * @comname LED + * @altname Grove LED + * @type led + * @man seeed + * @con gpio + * @kit gsk + * + * @brief API for the LED + * + * UPM module for the 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. + * + * @image html groveled.jpg + * @snippet groveled.cxx Interesting + * @snippet groveled-multi.cxx Interesting + */ +class Led { + public: + /** + * LED constructor + * + * @param gpio Pin to use + */ + Led(int pin); + /** + * LED destructor + */ + ~Led(); + /** + * 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(); + std::string name() + { + return "LED Sensor"; + } + private: + mraa_gpio_context m_gpio; +}; +} diff --git a/src/led/led_fti.c b/src/led/led_fti.c new file mode 100644 index 00000000..2aab8977 --- /dev/null +++ b/src/led/led_fti.c @@ -0,0 +1,76 @@ +/* + * Author: Brendan Le Foll + * Contributions: Mihai Tudor Panu + * Contributions: Sarah Knepper + * Abhishek Malik + * Copyright (c) 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 "led.h" +#include "upm_fti.h" + +/** + * This file implements the Function Table Interface (FTI) for this sensor + */ + +const char upm_led_name[] = "LED"; +const char upm_led_description[] = "LED"; +const upm_protocol_t upm_led_protocol[] = {UPM_GPIO}; +const upm_sensor_t upm_led_category[] = {UPM_LED}; + +// forward declarations +const upm_sensor_descriptor_t upm_led_get_descriptor(); +const void* upm_led_get_ft(upm_sensor_t sensor_type); +void* upm_led_init_name(); +void upm_led_close(void* dev); + +static const upm_sensor_ft ft = +{ + .upm_sensor_init_name = &upm_led_init_name, + .upm_sensor_close = &upm_led_close, + .upm_sensor_get_descriptor = &upm_led_get_descriptor +}; + +const void* upm_led_get_ft(upm_sensor_t sensor_type){ + if(sensor_type == UPM_SENSOR){ + return &ft; + } + return NULL; +} + +const upm_sensor_descriptor_t upm_led_get_descriptor(){ + upm_sensor_descriptor_t usd; + usd.name = upm_led_name; + usd.description = upm_led_description; + usd.protocol_size = 1; + usd.protocol = upm_led_protocol; + usd.category_size = 1; + usd.category = upm_led_category; + return usd; +} + +void* upm_led_init_name(){ + return NULL; +} + +void upm_led_close(void* dev) { + led_close((led_context)dev); +} diff --git a/src/led/pyupm_led.i b/src/led/pyupm_led.i new file mode 100644 index 00000000..16c31e18 --- /dev/null +++ b/src/led/pyupm_led.i @@ -0,0 +1,11 @@ +// Include doxygen-generated documentation +%include "pyupm_doxy2swig.i" +%module pyupm_led +%include "../upm.i" + +%feature("autodoc", "3"); + +%include "led.hpp" +%{ + #include "led.hpp" +%}