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

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;
}

68
src/relay/relay.cxx Normal file
View 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 "relay.hpp"
using namespace upm;
Relay::Relay(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);
}
Relay::~Relay()
{
mraa_gpio_close(m_gpio);
}
mraa_result_t Relay::on()
{
return mraa_gpio_write(m_gpio, 1);
}
mraa_result_t Relay::off()
{
return mraa_gpio_write(m_gpio, 0);
}
bool Relay::isOn()
{
return mraa_gpio_read(m_gpio) == 1;
}
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_ */

96
src/relay/relay.hpp Normal file
View File

@ -0,0 +1,96 @@
/*
* 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>
#include <mraa/gpio.hpp>
namespace upm {
/**
* @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 Relay
*
* 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 relay.jpg
* @snippet relay.cxx Interesting
*/
class Relay{
public:
/**
* relay constructor
*
* @param gpio Pin to use
*/
Relay(unsigned int pin);
/**
* relay destructor
*/
~Relay();
/**
* 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();
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;
}