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

View File

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

View File

@ -25,7 +25,7 @@
#include <unistd.h>
#include <iostream>
#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;

View File

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

24
examples/c/led.c Normal file
View File

@ -0,0 +1,24 @@
//Modified: Abhishek Malik <abhishek.malik@intel.com>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#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);
}

View File

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

View File

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

View File

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

View File

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

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