light: Added light sensor C source

Added the C source for the light sensor with necessary changes to cmake,
examples, docs.

    * Renamed all files with grovelight to light
    * Replaced all instances of grovelight with light
    * Added C source for light sensor
    * Updated all cmake files
    * Added C example for light sensor
    * Split out light sensor from grove library

Signed-off-by: Noel Eck <noel.eck@intel.com>
This commit is contained in:
Noel Eck 2016-09-09 14:10:30 -07:00
parent 0e52ea619b
commit 571f9c9498
25 changed files with 564 additions and 51 deletions

View File

Before

Width:  |  Height:  |  Size: 22 KiB

After

Width:  |  Height:  |  Size: 22 KiB

View File

@ -14,7 +14,7 @@ grovebutton.cxx GroveButtonSample.java
groveehr.cxx GroveEHRSample.java
groveled.cxx GroveLEDSample.java
grovelinefinder.cxx GroveLineFinderSample.java
grovelight.cxx GroveLightSample.java
light.cxx LightSample.java
grovemoisture.cxx GroveMoistureSample.java
groverelay.cxx GroveRelaySample.java
groverotary.cxx GroveRotarySample.java

View File

@ -89,7 +89,7 @@ link_directories (${MRAA_LIBDIR})
add_example (hmc5883l)
add_example (groveled)
add_example (groverelay)
add_example (grovelight)
add_example (light)
add_example (grovetemp)
add_example (grovebutton)
add_example (groverotary)

View File

@ -25,14 +25,14 @@
#include <unistd.h>
#include <iostream>
#include "grove.hpp"
#include "light.hpp"
int
main(int argc, char **argv)
{
//! [Interesting]
// Create the light sensor object using AIO pin 0
upm::GroveLight* light = new upm::GroveLight(0);
upm::Light* light = new upm::Light(0);
// Read the input and print both the raw value and a rough lux value,
// waiting one second between readings

View File

@ -99,6 +99,7 @@ add_example (mqx)
add_example (o2)
add_example (emg)
add_example (gsr)
add_example (light)
# Custom examples
add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps)

86
examples/c/light.c Normal file
View File

@ -0,0 +1,86 @@
/*
* Author: Jon Trulson <jtrulson@ics.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 <unistd.h>
#include <signal.h>
#include "light.h"
bool shouldRun = true;
void sig_handler(int signo)
{
if (signo == SIGINT)
shouldRun = false;
}
int main()
{
signal(SIGINT, sig_handler);
//! [Interesting]
// Instantiate a light sensor on analog pin A0
light_context sensor = light_init(0);
if (!sensor)
{
printf("light_init() failed.\n");
return -1;
}
// Set the aref, scale, and offset
light_set_aref(sensor, 5.0);
light_set_scale(sensor, 1.0);
light_set_offset(sensor, -.1);
printf("aRef: %0.03f scale: %0.03f offset: %0.03f\n\n",
light_get_aref(sensor),
light_get_scale(sensor),
light_get_offset(sensor));
// Every half a second, sample the sensor output
while (shouldRun)
{
float normalized = 0.0;
float raw_volts = 0.0;
float lux = 0.0;
light_get_normalized(sensor, &normalized);
light_get_raw_volts(sensor, &raw_volts);
light_get_lux(sensor, &lux);
printf("Normalized output: %0.03f, raw light sensor output: %0.03f v "
"light output: %0.03f lux\n", normalized, raw_volts, lux);
usleep(500000);
}
//! [Interesting]
printf("Exiting\n");
light_close(sensor);
return 0;
}

View File

@ -38,7 +38,7 @@ add_example(Emg emg)
add_example(Gsr gsr)
add_example(GroveLed_multiSample grove)
add_example(GroveLEDSample grove)
add_example(GroveLightSample grove)
add_example(LightSample grove)
add_example(GroveLineFinderSample grovelinefinder)
add_example(GroveMDSample grovemd)
add_example(GroveMoistureSample grovemoisture)

View File

@ -22,10 +22,10 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
public class GroveLightSample {
public class LightSample {
public static void main(String args[]) throws InterruptedException {
// ! [Interesting]
upm_grove.GroveLight gl = new upm_grove.GroveLight(2);
upm_grove.Light gl = new upm_grove.Light(2);
while (true) {
float raw_value = gl.raw_value();

View File

@ -26,7 +26,7 @@
var groveSensor = require('jsupm_grove');
// Create the light sensor object using AIO pin 0
var light = new groveSensor.GroveLight(0);
var light = new groveSensor.Light(0);
// Read the input and print both the raw value and a rough lux value,
// waiting one second between readings

View File

@ -24,7 +24,7 @@ import time
import pyupm_grove as grove
# Create the light sensor object using AIO pin 0
light = grove.GroveLight(0)
light = grove.Light(0)
# Read the input and print both the raw value and a rough lux value,
# waiting one second between readings

View File

@ -309,7 +309,7 @@ macro(upm_module_init)
endif (IS_C_LIBRARY)
link_directories (${MRAA_LIBDIR})
add_library (${libname} SHARED ${module_src})
add_library (${libname} SHARED ${module_src} ${module_hpp})
foreach (linkflag ${ARGN})
target_link_libraries (${libname} ${linkflag})
endforeach ()

View File

@ -1,5 +1,8 @@
set (libname "grove")
set (libdescription "upm grove module")
set (module_src grovebutton.cxx groveled.cxx grovelight.cxx groverelay.cxx groverotary.cxx groveslide.cxx grovetemp.cxx)
set (module_hpp grovebutton.hpp groveled.hpp grovelight.hpp groverelay.hpp groverotary.hpp groveslide.hpp grovetemp.hpp grovebase.hpp grove.hpp)
upm_module_init()
upm_mixed_module_init (NAME grove
DESCRIPTION "upm grove module"
CPP_HDR grovebutton.hpp groveled.hpp groverelay.hpp groverotary.hpp
groveslide.hpp grovetemp.hpp grovebase.hpp
grove.hpp
CPP_SRC grovebutton.cxx groveled.cxx groverelay.cxx groverotary.cxx
groveslide.cxx grovetemp.cxx
REQUIRES mraa)

View File

@ -27,7 +27,6 @@
#include <grovebutton.hpp>
#include <groveled.hpp>
#include <grovelight.hpp>
#include <groverelay.hpp>
#include <groverotary.hpp>
#include <groveslide.hpp>

View File

@ -24,11 +24,6 @@
#include "groveled.hpp"
%}
%include "grovelight.hpp"
%{
#include "grovelight.hpp"
%}
%include "groverelay.hpp"
%{
#include "groverelay.hpp"

View File

@ -21,11 +21,6 @@
#include "groveled.hpp"
%}
%include "grovelight.hpp"
%{
#include "grovelight.hpp"
%}
%include "groverelay.hpp"
%{
#include "groverelay.hpp"

View File

@ -25,11 +25,6 @@
#include "groveled.hpp"
%}
%include "grovelight.hpp"
%{
#include "grovelight.hpp"
%}
%include "groverelay.hpp"
%{
#include "groverelay.hpp"

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

@ -0,0 +1,9 @@
upm_mixed_module_init (NAME light
DESCRIPTION "upm light sensor module"
C_HDR light.h
C_SRC light.c
CPP_HDR light.hpp
CPP_SRC light.cxx
FTI_SRC light_fti.c
REQUIRES mraa grove m)
target_link_libraries(light-c m)

19
src/light/javaupm_light.i Normal file
View File

@ -0,0 +1,19 @@
%module javaupm_light
%include "../upm.i"
%{
#include "light.hpp"
%}
%include "light.hpp"
%pragma(java) jniclasscode=%{
static {
try {
System.loadLibrary("javaupm_light");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load. \n" + e);
System.exit(1);
}
}
%}

8
src/light/jsupm_light.i Normal file
View File

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

131
src/light/light.c Normal file
View File

@ -0,0 +1,131 @@
/*
* Author:
* Copyright (c) 2015 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 <string.h>
#include <stdlib.h>
#include <math.h>
#include "light.h"
light_context light_init(int16_t pin)
{
light_context dev = (light_context) malloc(sizeof(struct _light_context));
if (dev == NULL)
return NULL;
/* Init aio pin */
dev->aio = mraa_aio_init(pin);
if (dev->aio == NULL) {
free(dev);
return NULL;
}
/* Set the ADC ref, scale, and offset defaults */
dev->m_aRef = 5.0;
dev->m_scale = 1.0;
dev->m_offset = 0.0;
return dev;
}
void light_close(light_context dev)
{
mraa_aio_close(dev->aio);
free(dev);
}
upm_result_t light_set_aref(const light_context dev, float aref)
{
dev->m_aRef = aref;
return UPM_SUCCESS;
}
upm_result_t light_set_scale(const light_context dev, float scale)
{
dev->m_scale = scale;
return UPM_SUCCESS;
}
upm_result_t light_set_offset(const light_context dev, float offset)
{
dev->m_offset = offset;
return UPM_SUCCESS;
}
float light_get_aref(const light_context dev)
{
return dev->m_aRef;
}
float light_get_scale(const light_context dev)
{
return dev->m_scale;
}
float light_get_offset(const light_context dev)
{
return dev->m_offset;
}
upm_result_t light_get_normalized(const light_context dev, float *value)
{
*value = mraa_aio_read_float(dev->aio);
if (*value < 0)
return UPM_ERROR_OPERATION_FAILED;
}
upm_result_t light_get_raw_volts(const light_context dev, float *value)
{
*value = mraa_aio_read_float(dev->aio);
if (*value < 0)
return UPM_ERROR_OPERATION_FAILED;
/* Scale by the ADC reference voltage */
*value *= dev->m_aRef;
return UPM_SUCCESS;
}
upm_result_t light_get_lux(const light_context dev, float *value)
{
*value = mraa_aio_read(dev->aio);
if (*value < 0)
return UPM_ERROR_OPERATION_FAILED;
/* Get max adc value range 1023, 2047, 4095, etc... */
float max_adc = (1 << mraa_aio_get_bit(dev->aio)) - 1;
/* Convert the value to lux */
*value = 10000.0/pow(((max_adc - *value) * 10.0 / *value)*15.0,4.0/3.0);
/* Apply scale */
*value *= dev->m_scale;
/* Apply the offset in lux */
*value += dev->m_offset;
return UPM_SUCCESS;
}

View File

@ -28,36 +28,35 @@
#include <string>
#include <stdexcept>
#include "grovelight.hpp"
#include "light.hpp"
#include "math.h"
using namespace upm;
GroveLight::GroveLight(unsigned int pin)
Light::Light(unsigned int pin)
{
if ( !(m_aio = mraa_aio_init(pin)) ) {
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_aio_init() failed, invalid pin?");
return;
}
m_name = "Light Sensor";
}
GroveLight::~GroveLight()
Light::~Light()
{
mraa_aio_close(m_aio);
}
int GroveLight::value()
int Light::value()
{
// rough conversion to lux, using formula from Grove Starter Kit booklet
// rough conversion to lux, using formula from Starter Kit booklet
float a = (float) mraa_aio_read(m_aio);
if (a == -1.0) return -1;
a = 10000.0/pow(((1023.0-a)*10.0/a)*15.0,4.0/3.0);
return (int) round(a);
}
float GroveLight::raw_value()
float Light::raw_value()
{
return (float) mraa_aio_read(m_aio);
}

133
src/light/light.h Normal file
View File

@ -0,0 +1,133 @@
/*
* Author:
* Copyright (c) 2015 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 "upm.h"
#include "mraa/aio.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* driver context
*/
typedef struct _light_context {
/* mraa aio pin context */
mraa_aio_context aio;
/* Analog voltage reference */
float m_aRef;
/* Scale */
float m_scale;
/* Offset in sensor units */
float m_offset;
} *light_context;
/**
* Initialize analog sensor
* @param pin Analog pin
* @return sensor context
*/
light_context light_init(int16_t pin);
/**
* Analog sensor destructor
* @param sensor context pointer
*/
void light_close(light_context dev);
/**
* Set ADC reference voltage
* @param dev sensor context pointer
* @param aref ADC reference voltage
* @return Function result code
*/
upm_result_t light_set_aref(const light_context dev, float aref);
/**
* Set sensor scale. This scale is applied to the return value:
* counts = counts * scale
* @param dev sensor context pointer
* @param scale count scale value used
* @return Function result code
*/
upm_result_t light_set_scale(const light_context dev, float scale);
/**
* Set sensor offset. This offset is applied to the return value:
* value = value + offset
* @param dev sensor context pointer
* @param offset count offset value used
* @return Function result code
*/
upm_result_t light_set_offset(const light_context dev, float offset);
/**
* Get sensor aref
* @param dev sensor context pointer
* @return Sensor ADC reference voltage
*/
float light_get_aref(const light_context dev);
/**
* Get sensor scale
* @param dev sensor context pointer
* @return Sensor scale
*/
float light_get_scale(const light_context dev);
/**
* Get sensor offset
* @param dev sensor context pointer
* @return Sensor offset
*/
float light_get_offset(const light_context dev);
/**
* Read normalized value for sensor
* @param dev sensor context pointer
* @param *value Normalized value (0.0 -> 1.0)
* @return Function result code
*/
upm_result_t light_get_normalized(const light_context dev, float *value);
/**
* Read raw voltage from the sensor
* @param dev sensor context pointer
* @param *value Raw sensor voltage
* @return Function result code
*/
upm_result_t light_get_raw_volts(const light_context dev, float *value);
/**
* Read scaled/offset light value in lux
* @param dev sensor context pointer
* @param *value Adjusted sensor value in lux
* @return Function result code
*/
upm_result_t light_get_lux(const light_context dev, float *value);
#ifdef __cplusplus
}
#endif

View File

@ -28,42 +28,41 @@
#include <string>
#include <mraa/aio.hpp>
#include "grovebase.hpp"
namespace upm {
/**
* @library grove
* @sensor grovelight
* @comname Grove Light Sensor
* @library light
* @sensor light
* @comname Light Sensor
* @type light
* @man seeed
* @con analog
* @kit gsk
*
* @brief API for the Grove Light Sensor
* @brief API for the Light Sensor
*
* The Grove light sensor detects the intensity of the ambient light.
* The light sensor detects the intensity of the ambient light.
* As the light intensity of the environment increases, the resistance
* of the sensor decreases. This means the raw value from the
* analog pin is larger in bright light and smaller in the dark.
* An approximate lux value can also be returned.
*
* @image html grovelight.jpg
* @snippet grovelight.cxx Interesting
* @image html light.jpg
* @snippet light.cxx Interesting
*/
class GroveLight: public Grove {
class Light {
public:
/**
* Grove analog light sensor constructor
* analog light sensor constructor
*
* @param pin Analog pin to use
*/
GroveLight(unsigned int pin);
Light(unsigned int pin);
/**
* GroveLight destructor
* Light destructor
*/
~GroveLight();
~Light();
/**
* Gets the raw value from the AIO pin
*
@ -76,7 +75,18 @@ class GroveLight: public Grove {
* @return Normalized light reading in lux
*/
int value();
/* Sensor name
*
* @return Sensor name
*/
std::string name()
{
return m_name;
}
private:
mraa_aio_context m_aio;
const std::string m_name = "Light Sensor";
};
}

119
src/light/light_fti.c Normal file
View File

@ -0,0 +1,119 @@
/*
* Author:
* Copyright (c) 2015 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 <string.h>
#include <stdlib.h>
#include "light.h"
#include "upm_fti.h"
#include "fti/upm_sensor.h"
#include "fti/upm_raw.h"
/**
* This file implements the Function Table Interface (FTI) for this sensor
*/
const char upm_light_name[] = "LIGHT";
const char upm_light_description[] = "Analog light sensor";
const upm_protocol_t upm_light_protocol[] = {UPM_ANALOG};
const upm_sensor_t upm_light_category[] = {UPM_RAW};
// forward declarations
const void* upm_light_get_ft(upm_sensor_t sensor_type);
void* upm_light_init_str(const char* protocol, const char* params);
void upm_light_close(void* dev);
const upm_sensor_descriptor_t upm_light_get_descriptor();
upm_result_t upm_light_set_offset(const void* dev, float offset);
upm_result_t upm_light_set_scale(const void* dev, float scale);
upm_result_t upm_light_get_value(const void* dev, float *value);
/* This sensor implementes 2 function tables */
/* 1. Generic base function table */
static const upm_sensor_ft ft_gen =
{
.upm_sensor_init_name = &upm_light_init_str,
.upm_sensor_close = &upm_light_close,
.upm_sensor_get_descriptor = &upm_light_get_descriptor
};
/* 2. RAW function table */
static const upm_raw_ft ft_raw =
{
.upm_raw_set_offset = &upm_light_set_offset,
.upm_raw_set_scale = &upm_light_set_scale,
.upm_raw_get_value = &upm_light_get_value
};
const void* upm_light_get_ft(upm_sensor_t sensor_type)
{
switch(sensor_type)
{
case UPM_SENSOR:
return &ft_gen;
case UPM_RAW:
return &ft_raw;
default:
return NULL;
}
}
void* upm_light_init_str(const char* protocol, const char* params)
{
fprintf(stderr, "String initialization - not implemented, using ain0: %s\n", __FILENAME__);
return light_init(0);
}
void upm_light_close(void* dev)
{
light_close((light_context)dev);
}
const upm_sensor_descriptor_t upm_light_get_descriptor()
{
/* Fill in the descriptor */
upm_sensor_descriptor_t usd;
usd.name = upm_light_name;
usd.description = upm_light_description;
usd.protocol_size = 1;
usd.protocol = upm_light_protocol;
usd.category_size = 1;
usd.category = upm_light_category;
return usd;
}
upm_result_t upm_light_set_offset(const void* dev, float offset)
{
return light_set_offset((light_context)dev, offset);
}
upm_result_t upm_light_set_scale(const void* dev, float scale)
{
return light_set_scale((light_context)dev, scale);
}
upm_result_t upm_light_get_value(const void* dev, float *value)
{
return light_get_lux((light_context)dev, value);
}

11
src/light/pyupm_light.i Normal file
View File

@ -0,0 +1,11 @@
// Include doxygen-generated documentation
%include "pyupm_doxy2swig.i"
%module pyupm_light
%include "../upm.i"
%feature("autodoc", "3");
%include "light.hpp"
%{
#include "light.hpp"
%}