LED: Added C Src and Example

Changed from Grove LED to LED.

Signed-off-by: Abhishek Malik <abhishek.malik@intel.com>
This commit is contained in:
Abhishek Malik
2016-09-12 16:16:30 -07:00
committed by Noel Eck
parent 1caf805d2b
commit f9a36314fb
17 changed files with 481 additions and 10 deletions

9
src/led/CMakeLists.txt Normal file
View File

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

20
src/led/javaupm_led.i Normal file
View File

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

7
src/led/jsupm_led.i Normal file
View File

@ -0,0 +1,7 @@
%module jsupm_led
%include "../upm.i"
%include "led.hpp"
%{
#include "led.hpp"
%}

62
src/led/led.c Normal file
View File

@ -0,0 +1,62 @@
/*
* 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>
* Abhishek Malik <abhishek.malik@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 "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;
}

67
src/led/led.cxx Normal file
View 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 "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);
}

93
src/led/led.h Normal file
View File

@ -0,0 +1,93 @@
/*
* 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>
* Abhishek Malik <abhishek.malik@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 LED_LED_H_
#define LED_LED_H_
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#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_ */

101
src/led/led.hpp Normal file
View File

@ -0,0 +1,101 @@
/*
* 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>
#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;
};
}

76
src/led/led_fti.c Normal file
View File

@ -0,0 +1,76 @@
/*
* 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>
* Abhishek Malik <abhishek.malik@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 "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);
}

11
src/led/pyupm_led.i Normal file
View File

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