Relay: Added C source

Signed-off-by: Sisinty Sasmita Patra <sisinty.s.patra@intel.com>
This commit is contained in:
Sisinty Sasmita Patra 2016-09-12 09:27:24 -07:00 committed by Noel Eck
parent af417d3ae1
commit b266f87450
21 changed files with 320 additions and 37 deletions

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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();

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -27,6 +27,6 @@
#include <grovebutton.hpp>
#include <groveled.hpp>
#include <groverelay.hpp>
#include <relay.hpp>
#include <groverotary.hpp>
#include <grovetemp.hpp>

View File

@ -24,9 +24,9 @@
#include "groveled.hpp"
%}
%include "groverelay.hpp"
%include "relay.hpp"
%{
#include "groverelay.hpp"
#include "relay.hpp"
%}
%include "groverotary.hpp"

View File

@ -21,9 +21,9 @@
#include "groveled.hpp"
%}
%include "groverelay.hpp"
%include "relay.hpp"
%{
#include "groverelay.hpp"
#include "relay.hpp"
%}
%include "groverotary.hpp"

View File

@ -25,9 +25,9 @@
#include "groveled.hpp"
%}
%include "groverelay.hpp"
%include "relay.hpp"
%{
#include "groverelay.hpp"
#include "relay.hpp"
%}
%include "groverotary.hpp"

8
src/relay/CMakeLists.txt Normal file
View File

@ -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)

19
src/relay/javaupm_relay.i Normal file
View File

@ -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);
}
}
%}

8
src/relay/jsupm_relay.i Normal file
View File

@ -0,0 +1,8 @@
%module jsupm_relay
%include "../upm.i"
%{
#include "relay.hpp"
%}
%include "relay.hpp"

11
src/relay/pyupm_relay.i Normal file
View File

@ -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"
%}

83
src/relay/relay.c Normal file
View File

@ -0,0 +1,83 @@
/*
* Author: Sisinty Sasmita Patra <sisinty.s.patra@intel.com>
*
* 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;
}

View File

@ -28,11 +28,11 @@
#include <string>
#include <stdexcept>
#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;
}

57
src/relay/relay.h Normal file
View File

@ -0,0 +1,57 @@
/*
* Author: Sisinty Sasmita Patra <sisinty.s.patra@intel.com>
*
* 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 <stdlib.h>
#include <unistd.h>
#include "upm.h"
#include <mraa/gpio.h>
/**
* 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_ */

View File

@ -28,41 +28,41 @@
#include <string>
#include <mraa/gpio.hpp>
#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;
};

97
src/relay/relay_fti.c Normal file
View File

@ -0,0 +1,97 @@
/*
* Author: Sisinty Sasmita Patra <sisinty.s.patra@intel.com>
*
* 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;
}