From 571f9c949866f78247f6d53b67e2af7760cad935 Mon Sep 17 00:00:00 2001 From: Noel Eck Date: Fri, 9 Sep 2016 14:10:30 -0700 Subject: [PATCH] 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 --- docs/images/{grovelight.jpg => light.jpg} | Bin doxy/samples.mapping.txt | 2 +- examples/c++/CMakeLists.txt | 2 +- examples/c++/{grovelight.cxx => light.cxx} | 4 +- examples/c/CMakeLists.txt | 1 + examples/c/light.c | 86 +++++++++++ examples/java/CMakeLists.txt | 2 +- ...GroveLightSample.java => LightSample.java} | 4 +- .../javascript/{grovelight.js => light.js} | 2 +- examples/python/{grovelight.py => light.py} | 2 +- src/CMakeLists.txt | 2 +- src/grove/CMakeLists.txt | 13 +- src/grove/grove.hpp | 1 - src/grove/javaupm_grove.i | 5 - src/grove/jsupm_grove.i | 5 - src/grove/pyupm_grove.i | 5 - src/light/CMakeLists.txt | 9 ++ src/light/javaupm_light.i | 19 +++ src/light/jsupm_light.i | 8 ++ src/light/light.c | 131 +++++++++++++++++ src/{grove/grovelight.cxx => light/light.cxx} | 13 +- src/light/light.h | 133 ++++++++++++++++++ src/{grove/grovelight.hpp => light/light.hpp} | 36 +++-- src/light/light_fti.c | 119 ++++++++++++++++ src/light/pyupm_light.i | 11 ++ 25 files changed, 564 insertions(+), 51 deletions(-) rename docs/images/{grovelight.jpg => light.jpg} (100%) rename examples/c++/{grovelight.cxx => light.cxx} (96%) create mode 100644 examples/c/light.c rename examples/java/{GroveLightSample.java => LightSample.java} (94%) rename examples/javascript/{grovelight.js => light.js} (97%) rename examples/python/{grovelight.py => light.py} (98%) create mode 100644 src/light/CMakeLists.txt create mode 100644 src/light/javaupm_light.i create mode 100644 src/light/jsupm_light.i create mode 100644 src/light/light.c rename src/{grove/grovelight.cxx => light/light.cxx} (87%) create mode 100644 src/light/light.h rename src/{grove/grovelight.hpp => light/light.hpp} (79%) create mode 100644 src/light/light_fti.c create mode 100644 src/light/pyupm_light.i diff --git a/docs/images/grovelight.jpg b/docs/images/light.jpg similarity index 100% rename from docs/images/grovelight.jpg rename to docs/images/light.jpg diff --git a/doxy/samples.mapping.txt b/doxy/samples.mapping.txt index 72e66724..9ecf0ebf 100644 --- a/doxy/samples.mapping.txt +++ b/doxy/samples.mapping.txt @@ -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 diff --git a/examples/c++/CMakeLists.txt b/examples/c++/CMakeLists.txt index 6f4291fe..ca56d7d5 100644 --- a/examples/c++/CMakeLists.txt +++ b/examples/c++/CMakeLists.txt @@ -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) diff --git a/examples/c++/grovelight.cxx b/examples/c++/light.cxx similarity index 96% rename from examples/c++/grovelight.cxx rename to examples/c++/light.cxx index 18f8f6c0..e5c079b4 100644 --- a/examples/c++/grovelight.cxx +++ b/examples/c++/light.cxx @@ -25,14 +25,14 @@ #include #include -#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 diff --git a/examples/c/CMakeLists.txt b/examples/c/CMakeLists.txt index 6dc10a13..e59bf40a 100644 --- a/examples/c/CMakeLists.txt +++ b/examples/c/CMakeLists.txt @@ -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) diff --git a/examples/c/light.c b/examples/c/light.c new file mode 100644 index 00000000..2828dbf4 --- /dev/null +++ b/examples/c/light.c @@ -0,0 +1,86 @@ +/* + * Author: Jon Trulson + * 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 +#include + +#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; +} diff --git a/examples/java/CMakeLists.txt b/examples/java/CMakeLists.txt index ce3b6832..830f9bcf 100644 --- a/examples/java/CMakeLists.txt +++ b/examples/java/CMakeLists.txt @@ -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) diff --git a/examples/java/GroveLightSample.java b/examples/java/LightSample.java similarity index 94% rename from examples/java/GroveLightSample.java rename to examples/java/LightSample.java index 611b744b..40c2b7a2 100644 --- a/examples/java/GroveLightSample.java +++ b/examples/java/LightSample.java @@ -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(); diff --git a/examples/javascript/grovelight.js b/examples/javascript/light.js similarity index 97% rename from examples/javascript/grovelight.js rename to examples/javascript/light.js index d09b4627..36e4b74f 100644 --- a/examples/javascript/grovelight.js +++ b/examples/javascript/light.js @@ -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 diff --git a/examples/python/grovelight.py b/examples/python/light.py similarity index 98% rename from examples/python/grovelight.py rename to examples/python/light.py index a1939d75..337c976e 100644 --- a/examples/python/grovelight.py +++ b/examples/python/light.py @@ -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 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 54d25623..7603f75a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 () diff --git a/src/grove/CMakeLists.txt b/src/grove/CMakeLists.txt index ccf2f2d8..6c0ad0e3 100644 --- a/src/grove/CMakeLists.txt +++ b/src/grove/CMakeLists.txt @@ -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) diff --git a/src/grove/grove.hpp b/src/grove/grove.hpp index a7da8758..0f2d6e09 100644 --- a/src/grove/grove.hpp +++ b/src/grove/grove.hpp @@ -27,7 +27,6 @@ #include #include -#include #include #include #include diff --git a/src/grove/javaupm_grove.i b/src/grove/javaupm_grove.i index a70cdea1..7d07c738 100644 --- a/src/grove/javaupm_grove.i +++ b/src/grove/javaupm_grove.i @@ -24,11 +24,6 @@ #include "groveled.hpp" %} -%include "grovelight.hpp" -%{ - #include "grovelight.hpp" -%} - %include "groverelay.hpp" %{ #include "groverelay.hpp" diff --git a/src/grove/jsupm_grove.i b/src/grove/jsupm_grove.i index e3390235..6f81aef9 100644 --- a/src/grove/jsupm_grove.i +++ b/src/grove/jsupm_grove.i @@ -21,11 +21,6 @@ #include "groveled.hpp" %} -%include "grovelight.hpp" -%{ - #include "grovelight.hpp" -%} - %include "groverelay.hpp" %{ #include "groverelay.hpp" diff --git a/src/grove/pyupm_grove.i b/src/grove/pyupm_grove.i index 31412d02..8bc593df 100644 --- a/src/grove/pyupm_grove.i +++ b/src/grove/pyupm_grove.i @@ -25,11 +25,6 @@ #include "groveled.hpp" %} -%include "grovelight.hpp" -%{ - #include "grovelight.hpp" -%} - %include "groverelay.hpp" %{ #include "groverelay.hpp" diff --git a/src/light/CMakeLists.txt b/src/light/CMakeLists.txt new file mode 100644 index 00000000..a42386a7 --- /dev/null +++ b/src/light/CMakeLists.txt @@ -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) diff --git a/src/light/javaupm_light.i b/src/light/javaupm_light.i new file mode 100644 index 00000000..3a41414f --- /dev/null +++ b/src/light/javaupm_light.i @@ -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); + } + } +%} \ No newline at end of file diff --git a/src/light/jsupm_light.i b/src/light/jsupm_light.i new file mode 100644 index 00000000..32b7103a --- /dev/null +++ b/src/light/jsupm_light.i @@ -0,0 +1,8 @@ +%module jsupm_light +%include "../upm.i" + +%{ + #include "light.hpp" +%} + +%include "light.hpp" diff --git a/src/light/light.c b/src/light/light.c new file mode 100644 index 00000000..359f6c6e --- /dev/null +++ b/src/light/light.c @@ -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 +#include +#include + +#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; +} diff --git a/src/grove/grovelight.cxx b/src/light/light.cxx similarity index 87% rename from src/grove/grovelight.cxx rename to src/light/light.cxx index 39313782..657d1469 100644 --- a/src/grove/grovelight.cxx +++ b/src/light/light.cxx @@ -28,36 +28,35 @@ #include #include -#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); } diff --git a/src/light/light.h b/src/light/light.h new file mode 100644 index 00000000..e7ed9a55 --- /dev/null +++ b/src/light/light.h @@ -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 diff --git a/src/grove/grovelight.hpp b/src/light/light.hpp similarity index 79% rename from src/grove/grovelight.hpp rename to src/light/light.hpp index 10e45f72..42efae24 100644 --- a/src/grove/grovelight.hpp +++ b/src/light/light.hpp @@ -28,42 +28,41 @@ #include #include -#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"; }; } diff --git a/src/light/light_fti.c b/src/light/light_fti.c new file mode 100644 index 00000000..95b5ec78 --- /dev/null +++ b/src/light/light_fti.c @@ -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 +#include + +#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); +} diff --git a/src/light/pyupm_light.i b/src/light/pyupm_light.i new file mode 100644 index 00000000..cab85c7b --- /dev/null +++ b/src/light/pyupm_light.i @@ -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" +%}