From b266f874500bbf93361199df0f1a8b0e983815c8 Mon Sep 17 00:00:00 2001 From: Sisinty Sasmita Patra Date: Mon, 12 Sep 2016 09:27:24 -0700 Subject: [PATCH] Relay: Added C source Signed-off-by: Sisinty Sasmita Patra --- doxy/samples.mapping.txt | 2 +- examples/c++/CMakeLists.txt | 2 +- examples/c++/{groverelay.cxx => relay.cxx} | 2 +- examples/java/CMakeLists.txt | 2 +- ...GroveRelaySample.java => RelaySample.java} | 4 +- .../javascript/{groverelay.js => relay.js} | 2 +- examples/python/{groverelay.py => relay.py} | 2 +- src/grove/CMakeLists.txt | 4 +- src/grove/grove.hpp | 2 +- src/grove/javaupm_grove.i | 4 +- src/grove/jsupm_grove.i | 4 +- src/grove/pyupm_grove.i | 4 +- src/relay/CMakeLists.txt | 8 ++ src/relay/javaupm_relay.i | 19 ++++ src/relay/jsupm_relay.i | 8 ++ src/relay/pyupm_relay.i | 11 +++ src/relay/relay.c | 83 ++++++++++++++++ src/{grove/groverelay.cxx => relay/relay.cxx} | 15 ++- src/relay/relay.h | 57 +++++++++++ src/{grove/groverelay.hpp => relay/relay.hpp} | 25 ++--- src/relay/relay_fti.c | 97 +++++++++++++++++++ 21 files changed, 320 insertions(+), 37 deletions(-) rename examples/c++/{groverelay.cxx => relay.cxx} (97%) rename examples/java/{GroveRelaySample.java => RelaySample.java} (94%) rename examples/javascript/{groverelay.js => relay.js} (97%) rename examples/python/{groverelay.py => relay.py} (98%) create mode 100644 src/relay/CMakeLists.txt create mode 100644 src/relay/javaupm_relay.i create mode 100644 src/relay/jsupm_relay.i create mode 100644 src/relay/pyupm_relay.i create mode 100644 src/relay/relay.c rename src/{grove/groverelay.cxx => relay/relay.cxx} (88%) create mode 100644 src/relay/relay.h rename src/{grove/groverelay.hpp => relay/relay.hpp} (87%) create mode 100644 src/relay/relay_fti.c diff --git a/doxy/samples.mapping.txt b/doxy/samples.mapping.txt index 39e55ccd..2114129d 100644 --- a/doxy/samples.mapping.txt +++ b/doxy/samples.mapping.txt @@ -16,7 +16,7 @@ groveled.cxx GroveLEDSample.java grovelinefinder.cxx GroveLineFinderSample.java light.cxx LightSample.java grovemoisture.cxx GroveMoistureSample.java -groverelay.cxx GroveRelaySample.java +relay.cxx RelaySample.java groverotary.cxx GroveRotarySample.java grovescam.cxx GROVESCAMSample.java slide.cxx SlideSample.java diff --git a/examples/c++/CMakeLists.txt b/examples/c++/CMakeLists.txt index 6a4e7673..22d02f93 100644 --- a/examples/c++/CMakeLists.txt +++ b/examples/c++/CMakeLists.txt @@ -88,7 +88,7 @@ link_directories (${MRAA_LIBDIR}) # grove* will use module grove add_example (hmc5883l) add_example (groveled) -add_example (groverelay) +add_example (relay) add_example (light) add_example (temperature) add_example (grovebutton) diff --git a/examples/c++/groverelay.cxx b/examples/c++/relay.cxx similarity index 97% rename from examples/c++/groverelay.cxx rename to examples/c++/relay.cxx index 92971399..2aadc8ac 100644 --- a/examples/c++/groverelay.cxx +++ b/examples/c++/relay.cxx @@ -33,7 +33,7 @@ main(int argc, char **argv) //! [Interesting] // Create the relay switch object using GPIO pin 0 - upm::GroveRelay* relay = new upm::GroveRelay(0); + upm::Relay* relay = new upm::Relay(0); // Close and then open the relay switch 3 times, // waiting one second each time. The LED on the relay switch diff --git a/examples/java/CMakeLists.txt b/examples/java/CMakeLists.txt index bf7ae9b0..ed532bcb 100644 --- a/examples/java/CMakeLists.txt +++ b/examples/java/CMakeLists.txt @@ -46,7 +46,7 @@ add_example(GroveMQ3 gas) add_example(GroveMQ9 gas) add_example(O2Example o2) add_example(GroveQTouch at42qt1070) -add_example(GroveRelaySample grove) +add_example(RelaySample grove) add_example(GroveRotarySample grove) add_example(GROVESCAMSample grovescam) add_example(SlideSample slide) diff --git a/examples/java/GroveRelaySample.java b/examples/java/RelaySample.java similarity index 94% rename from examples/java/GroveRelaySample.java rename to examples/java/RelaySample.java index 56a044b9..be2ee9c0 100644 --- a/examples/java/GroveRelaySample.java +++ b/examples/java/RelaySample.java @@ -22,11 +22,11 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -public class GroveRelaySample { +public class RelaySample { public static void main(String[] args) throws InterruptedException { // ! [Interesting] // Create the button object using UART - upm_grove.GroveRelay relay = new upm_grove.GroveRelay(5); + upm_grove.Relay relay = new upm_grove.Relay(5); for (int i = 0; i < 3; i++) { relay.on(); diff --git a/examples/javascript/groverelay.js b/examples/javascript/relay.js similarity index 97% rename from examples/javascript/groverelay.js rename to examples/javascript/relay.js index a8fefe7b..d9d8b4aa 100644 --- a/examples/javascript/groverelay.js +++ b/examples/javascript/relay.js @@ -26,7 +26,7 @@ var groveSensor = require('jsupm_grove'); // Create the relay switch object using GPIO pin 0 -var relay = new groveSensor.GroveRelay(0); +var relay = new groveSensor.Relay(0); // Close and then open the relay switch 3 times, // waiting one second each time. The LED on the relay switch diff --git a/examples/python/groverelay.py b/examples/python/relay.py similarity index 98% rename from examples/python/groverelay.py rename to examples/python/relay.py index 44f033fa..cf232956 100644 --- a/examples/python/groverelay.py +++ b/examples/python/relay.py @@ -24,7 +24,7 @@ import time import pyupm_grove as grove # Create the relay switch object using GPIO pin 0 -relay = grove.GroveRelay(0) +relay = grove.Relay(0) # Close and then open the relay switch 3 times, # waiting one second each time. The LED on the relay switch diff --git a/src/grove/CMakeLists.txt b/src/grove/CMakeLists.txt index 407c94f3..25751d1c 100644 --- a/src/grove/CMakeLists.txt +++ b/src/grove/CMakeLists.txt @@ -1,7 +1,7 @@ upm_mixed_module_init (NAME grove DESCRIPTION "upm grove module" - CPP_HDR grovebutton.hpp groveled.hpp groverelay.hpp groverotary.hpp + CPP_HDR grovebutton.hpp groveled.hpp groverotary.hpp grovetemp.hpp grovebase.hpp grove.hpp - CPP_SRC grovebutton.cxx groveled.cxx groverelay.cxx groverotary.cxx + CPP_SRC grovebutton.cxx groveled.cxx groverotary.cxx grovetemp.cxx REQUIRES mraa) diff --git a/src/grove/grove.hpp b/src/grove/grove.hpp index db003ee7..77fa796c 100644 --- a/src/grove/grove.hpp +++ b/src/grove/grove.hpp @@ -27,6 +27,6 @@ #include #include -#include +#include #include #include diff --git a/src/grove/javaupm_grove.i b/src/grove/javaupm_grove.i index adc4246f..eef5df66 100644 --- a/src/grove/javaupm_grove.i +++ b/src/grove/javaupm_grove.i @@ -24,9 +24,9 @@ #include "groveled.hpp" %} -%include "groverelay.hpp" +%include "relay.hpp" %{ - #include "groverelay.hpp" + #include "relay.hpp" %} %include "groverotary.hpp" diff --git a/src/grove/jsupm_grove.i b/src/grove/jsupm_grove.i index beb94128..5bc3f2d2 100644 --- a/src/grove/jsupm_grove.i +++ b/src/grove/jsupm_grove.i @@ -21,9 +21,9 @@ #include "groveled.hpp" %} -%include "groverelay.hpp" +%include "relay.hpp" %{ - #include "groverelay.hpp" + #include "relay.hpp" %} %include "groverotary.hpp" diff --git a/src/grove/pyupm_grove.i b/src/grove/pyupm_grove.i index 5d376ea4..5686f598 100644 --- a/src/grove/pyupm_grove.i +++ b/src/grove/pyupm_grove.i @@ -25,9 +25,9 @@ #include "groveled.hpp" %} -%include "groverelay.hpp" +%include "relay.hpp" %{ - #include "groverelay.hpp" + #include "relay.hpp" %} %include "groverotary.hpp" diff --git a/src/relay/CMakeLists.txt b/src/relay/CMakeLists.txt new file mode 100644 index 00000000..def4146c --- /dev/null +++ b/src/relay/CMakeLists.txt @@ -0,0 +1,8 @@ +upm_mixed_module_init (NAME relay + DESCRIPTION "UPM Relay Module: relay" + C_HDR relay.h + C_SRC relay.c + CPP_HDR relay.hpp + CPP_SRC relay.cxx + FTI_SRC relay_fti.c + REQUIRES mraa) diff --git a/src/relay/javaupm_relay.i b/src/relay/javaupm_relay.i new file mode 100644 index 00000000..4b8e7e8f --- /dev/null +++ b/src/relay/javaupm_relay.i @@ -0,0 +1,19 @@ +%module javaupm_relay +%include "../upm.i" + +%{ + #include "relay.hpp" +%} + +%include "relay.hpp" + +%pragma(java) jniclasscode=%{ + static { + try { + System.loadLibrary("javaupm_relay"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Native code library failed to load. \n" + e); + System.exit(1); + } + } +%} diff --git a/src/relay/jsupm_relay.i b/src/relay/jsupm_relay.i new file mode 100644 index 00000000..02276c9f --- /dev/null +++ b/src/relay/jsupm_relay.i @@ -0,0 +1,8 @@ +%module jsupm_relay +%include "../upm.i" + +%{ + #include "relay.hpp" +%} + +%include "relay.hpp" diff --git a/src/relay/pyupm_relay.i b/src/relay/pyupm_relay.i new file mode 100644 index 00000000..c8d3d536 --- /dev/null +++ b/src/relay/pyupm_relay.i @@ -0,0 +1,11 @@ +// Include doxygen-generated documentation +%include "pyupm_doxy2swig.i" +%module pyupm_relay +%include "../upm.i" + +%feature("autodoc", "3"); + +%include "relay.hpp" +%{ + #include "relay.hpp" +%} diff --git a/src/relay/relay.c b/src/relay/relay.c new file mode 100644 index 00000000..a45f0fe2 --- /dev/null +++ b/src/relay/relay.c @@ -0,0 +1,83 @@ +/* + * Author: Sisinty Sasmita Patra + * + * 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 "relay.h" + +relay_context relay_init(int pin) +{ + relay_context dev = (relay_context)malloc(sizeof(struct _relay_context)); + + if (dev == NULL) + return NULL; + + dev->gpio = mraa_gpio_init(pin); + + if (dev->gpio == NULL) + { + free(dev); + return NULL; + } + return dev; +} + +void relay_close(relay_context dev) +{ + mraa_gpio_close(dev->gpio); + + free(dev); +} + +upm_result_t relay_on(relay_context dev) +{ + mraa_gpio_write(dev->gpio, 1); + return UPM_SUCCESS; +} + +upm_result_t relay_off(relay_context dev) +{ + mraa_gpio_write(dev->gpio, 0); + return UPM_SUCCESS; +} + +bool relay_is_on(relay_context dev) +{ + int val; + val = mraa_gpio_read(dev->gpio); + if (val > 0) + return true; + else + return false; +} + +bool relay_is_off(relay_context dev) +{ + int val; + val = mraa_gpio_read(dev->gpio); + if (!val) + return true; + else + return false; +} + diff --git a/src/grove/groverelay.cxx b/src/relay/relay.cxx similarity index 88% rename from src/grove/groverelay.cxx rename to src/relay/relay.cxx index 53fba212..2b6165be 100644 --- a/src/grove/groverelay.cxx +++ b/src/relay/relay.cxx @@ -28,11 +28,11 @@ #include #include -#include "groverelay.hpp" +#include "relay.hpp" using namespace upm; -GroveRelay::GroveRelay(unsigned int pin) +Relay::Relay(unsigned int pin) { if ( !(m_gpio = mraa_gpio_init(pin)) ) { throw std::invalid_argument(std::string(__FUNCTION__) + @@ -40,30 +40,29 @@ GroveRelay::GroveRelay(unsigned int pin) return; } mraa_gpio_dir(m_gpio, MRAA_GPIO_OUT); - m_name = "Relay Switch"; } -GroveRelay::~GroveRelay() +Relay::~Relay() { mraa_gpio_close(m_gpio); } -mraa_result_t GroveRelay::on() +mraa_result_t Relay::on() { return mraa_gpio_write(m_gpio, 1); } -mraa_result_t GroveRelay::off() +mraa_result_t Relay::off() { return mraa_gpio_write(m_gpio, 0); } -bool GroveRelay::isOn() +bool Relay::isOn() { return mraa_gpio_read(m_gpio) == 1; } -bool GroveRelay::isOff() +bool Relay::isOff() { return mraa_gpio_read(m_gpio) == 0; } diff --git a/src/relay/relay.h b/src/relay/relay.h new file mode 100644 index 00000000..58a3cf09 --- /dev/null +++ b/src/relay/relay.h @@ -0,0 +1,57 @@ +/* + * Author: Sisinty Sasmita Patra + * + * 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 RELAY_H_ +#define RELAY_H_ + +#pragma once +#include +#include + +#include "upm.h" +#include + +/** + * device context + */ +typedef struct _relay_context{ + mraa_gpio_context gpio; +} *relay_context; + +typedef struct _relay_context *relay_context; + +relay_context relay_init(int pin); + +void relay_close(relay_context dev); + +upm_result_t relay_on(relay_context dev); + +upm_result_t relay_off(relay_context dev); + +bool relay_is_on(relay_context dev); + +bool relay_is_off(relay_context dev); + +#endif /* RELAY_H_ */ diff --git a/src/grove/groverelay.hpp b/src/relay/relay.hpp similarity index 87% rename from src/grove/groverelay.hpp rename to src/relay/relay.hpp index 325e1ac0..dc7e36a4 100644 --- a/src/grove/groverelay.hpp +++ b/src/relay/relay.hpp @@ -28,41 +28,41 @@ #include #include -#include "grovebase.hpp" namespace upm { /** - * @library grove - * @sensor groverelay + * @library relay + * @defgroup relay libupm-relay + * @sensor relay * @comname Grove Relay * @type relay * @man seeed * @con gpio * @kit gsk eak hak * - * @brief API for the Grove Relay + * @brief API for the Relay * - * UPM module for the Grove relay switch. The Grove relay is a + * UPM module for the relay switch. The 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. * - * @image html groverelay.jpg - * @snippet groverelay.cxx Interesting + * @image html relay.jpg + * @snippet relay.cxx Interesting */ -class GroveRelay: public Grove { +class Relay{ public: /** - * Grove relay constructor + * relay constructor * * @param gpio Pin to use */ - GroveRelay(unsigned int pin); + Relay(unsigned int pin); /** - * Grove relay destructor + * relay destructor */ - ~GroveRelay(); + ~Relay(); /** * Sets the relay switch to on (closed). This allows current * to flow and lights up the indicator LED. @@ -89,6 +89,7 @@ class GroveRelay: public Grove { * @return True if the switch is off (open), false otherwise */ bool isOff(); + std::string name(){ return "Relay Switch";} private: mraa_gpio_context m_gpio; }; diff --git a/src/relay/relay_fti.c b/src/relay/relay_fti.c new file mode 100644 index 00000000..4723f2db --- /dev/null +++ b/src/relay/relay_fti.c @@ -0,0 +1,97 @@ +/* + * Author: Sisinty Sasmita Patra + * + * 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 "relay.h" +#include "upm_fti.h" + +/** + * This file implements the Function Table Interface (FTI) for this sensor + */ + +const char upm_relay_name[] = "Grove Relay"; +const char upm_relay_description[] = "Grove relay Sensor"; +const upm_protocol_t upm_relay_protocol[] = {UPM_GPIO}; +const upm_sensor_t upm_relay_category[] = {UPM_SWITCH}; + +// forward declarations +const upm_sensor_descriptor_t upm_relay_get_descriptor(); +const void* upm_relay_get_ft(upm_sensor_t sensor_type); +void* upm_relay_init_name(); +void upm_relay_close(void* dev); +upm_result_t upm_relay_get_value(void* dev, bool* value, int num); + +const upm_sensor_descriptor_t upm_relay_get_descriptor() { + upm_sensor_descriptor_t usd; + usd.name = upm_relay_name; + usd.description = upm_relay_description; + usd.protocol_size = 1; + usd.protocol = upm_relay_protocol; + usd.category_size = 1; + usd.category = upm_relay_category; + return usd; +} + +static const upm_sensor_ft ft = +{ + .upm_sensor_init_name = &upm_relay_init_name, + .upm_sensor_close = &upm_relay_close, + .upm_sensor_get_descriptor = &upm_relay_get_descriptor +}; + +static const upm_switch_ft sft = +{ + .upm_switch_get_value = &upm_relay_get_value +}; + +const void* upm_relay_get_ft(upm_sensor_t sensor_type) { + if(sensor_type == UPM_SENSOR) { + return &ft; + } + if(sensor_type == UPM_SWITCH) { + return &sft; + } + return NULL; +} + +void* upm_relay_init_name(){ + return NULL; +} + +void upm_relay_close(void* dev) +{ + relay_close((relay_context)dev); +} + +upm_result_t upm_relay_get_value(void* dev, bool* value, int num) +{ + // num is unused + + if (relay_is_on((relay_context)dev)) + *value = true; + else + *value = false; + + return UPM_SUCCESS; +}