From 54a84af1c33a23766a02ca0da9af75be36db4c95 Mon Sep 17 00:00:00 2001 From: Jon Trulson Date: Fri, 17 Mar 2017 16:18:40 -0600 Subject: [PATCH] light: modify so C++ wraps C code This also provides some more functionality. get_raw() has been deprecated in favor of using getNormalized(). Signed-off-by: Jon Trulson --- examples/c++/light.cxx | 7 +- examples/java/LightSample.java | 6 +- examples/javascript/light.js | 6 +- examples/python/light.py | 17 ++-- src/light/CMakeLists.txt | 3 +- src/light/light.cxx | 87 ++++++++++++++++---- src/light/light.hpp | 140 ++++++++++++++++++++++++++------- 7 files changed, 204 insertions(+), 62 deletions(-) diff --git a/examples/c++/light.cxx b/examples/c++/light.cxx index e5c079b4..cf26ee3c 100644 --- a/examples/c++/light.cxx +++ b/examples/c++/light.cxx @@ -34,10 +34,11 @@ main(int argc, char **argv) // Create the light sensor object using AIO pin 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 + // Read the input and print both the normalized ADC value and a + // rough lux value, waiting one second between readings while( 1 ) { - std::cout << light->name() << " raw value is " << light->raw_value() << + std::cout << light->name() << " normalized value is " + << light->getNormalized() << ", which is roughly " << light->value() << " lux" << std::endl; sleep(1); } diff --git a/examples/java/LightSample.java b/examples/java/LightSample.java index 1a6a122f..3ed87174 100644 --- a/examples/java/LightSample.java +++ b/examples/java/LightSample.java @@ -25,13 +25,13 @@ public class LightSample { public static void main(String args[]) throws InterruptedException { // ! [Interesting] - upm_light.Light gl = new upm_light.Light(2); + upm_light.Light gl = new upm_light.Light(0); while (true) { - float raw_value = gl.raw_value(); + float raw_value = gl.getNormalized(); float value = gl.value(); - System.out.println("raw value: " + raw_value); + System.out.println("normalized: " + raw_value); System.out.println("value: " + value); Thread.sleep(1000); diff --git a/examples/javascript/light.js b/examples/javascript/light.js index 9ce63a1f..5b93e9d6 100644 --- a/examples/javascript/light.js +++ b/examples/javascript/light.js @@ -28,10 +28,10 @@ var sensor = require('jsupm_light'); // Create the light sensor object using AIO pin 0 var light = new sensor.Light(0); -// Read the input and print both the raw value and a rough lux value, -// waiting one second between readings +// Read the input and print both the normalized ADC value and a rough +// lux value, waiting one second between readings function readLightSensorValue() { - console.log(light.name() + " raw value is " + light.raw_value() + + console.log(light.name() + " normalized value is " + light.getNormalized() + ", which is roughly " + light.value() + " lux"); } setInterval(readLightSensorValue, 1000); diff --git a/examples/python/light.py b/examples/python/light.py index d8d494b1..312b7398 100755 --- a/examples/python/light.py +++ b/examples/python/light.py @@ -1,4 +1,4 @@ -from __future__ import print_function +#!/usr/bin/python # Author: Sarah Knepper # Copyright (c) 2014 Intel Corporation. # @@ -21,22 +21,23 @@ from __future__ import print_function # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +from __future__ import print_function import time -from upm import pyupm_light as light +from upm import pyupm_light as lightObj def main(): # Create the light sensor object using AIO pin 0 - light = light.Light(0) + sensor = lightObj.Light(0) - # Read the input and print both the raw value and a rough lux value, - # waiting one second between readings + # Read the input and print both the normalized ADC value and a + # rough lux value, waiting one second between readings while 1: - print(light.name() + " raw value is %d" % light.raw_value() + \ - ", which is roughly %d" % light.value() + " lux"); + print(sensor.name() + " normalized value is %f" % sensor.getNormalized() + + ", which is roughly %d" % sensor.value() + " lux"); time.sleep(1) # Delete the light sensor object - del light + del lightObj if __name__ == '__main__': main() diff --git a/src/light/CMakeLists.txt b/src/light/CMakeLists.txt index b6275ace..b7c5c7f2 100644 --- a/src/light/CMakeLists.txt +++ b/src/light/CMakeLists.txt @@ -5,5 +5,6 @@ upm_mixed_module_init (NAME light CPP_HDR light.hpp CPP_SRC light.cxx FTI_SRC light_fti.c - REQUIRES mraa grove m) + CPP_WRAPS_C + REQUIRES mraa m) target_link_libraries(light-c m) diff --git a/src/light/light.cxx b/src/light/light.cxx index 657d1469..5fe23180 100644 --- a/src/light/light.cxx +++ b/src/light/light.cxx @@ -2,7 +2,8 @@ * Authors: Brendan Le Foll * Mihai Tudor Panu * Sarah Knepper - * Copyright (c) 2014 - 2016 Intel Corporation. + * Jon Trulson + * Copyright (c) 2014 - 2017 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -29,34 +30,90 @@ #include #include "light.hpp" -#include "math.h" using namespace upm; -Light::Light(unsigned int pin) +Light::Light(unsigned int pin) : + m_light(light_init(pin)) { - if ( !(m_aio = mraa_aio_init(pin)) ) { - throw std::invalid_argument(std::string(__FUNCTION__) + - ": mraa_aio_init() failed, invalid pin?"); - return; - } + if ( !m_light ) + throw std::runtime_error(std::string(__FUNCTION__) + + ": light_init() failed."); } Light::~Light() { - mraa_aio_close(m_aio); + light_close(m_light); } int Light::value() { - // 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 value; + + if (light_get_lux(m_light, &value)) + throw std::runtime_error(std::string(__FUNCTION__) + + ": light_get_normalized() failed."); + + return (int)roundf(value); } float Light::raw_value() { - return (float) mraa_aio_read(m_aio); + // This is a hack. Deprecated. Should be removed ASAP. + int value = mraa_aio_read(m_light->aio); + if (value < 0) + throw std::runtime_error(std::string(__FUNCTION__) + + ": mraa_aio_read() failed."); + + return (float)value; // ?? why a float? +} + +void Light::setAref(float aref) +{ + light_set_aref(m_light, aref); +} + +void Light::setScale(float scale) +{ + light_set_scale(m_light, scale); +} + +void Light::setOffset(float offset) +{ + light_set_aref(m_light, offset); +} + +float Light::getAref() +{ + return light_get_aref(m_light); +} + +float Light::getScale() +{ + return light_get_scale(m_light); +} + +float Light::getOffset() +{ + return light_get_offset(m_light); +} + +float Light::getNormalized() +{ + float value; + + if (light_get_normalized(m_light, &value)) + throw std::runtime_error(std::string(__FUNCTION__) + + ": light_get_normalized() failed."); + return value; +} + +float Light::getRawVolts() +{ + float value; + + if (light_get_raw_volts(m_light, &value)) + throw std::runtime_error(std::string(__FUNCTION__) + + ": light_get_raw_volts() failed."); + return value; } diff --git a/src/light/light.hpp b/src/light/light.hpp index 4ba72354..09a2b1f2 100644 --- a/src/light/light.hpp +++ b/src/light/light.hpp @@ -2,7 +2,8 @@ * Authors: Brendan Le Foll * Mihai Tudor Panu * Sarah Knepper - * Copyright (c) 2014 - 2016 Intel Corporation. + * Jon Trulson + * Copyright (c) 2014 - 2017 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -27,66 +28,147 @@ #pragma once #include -#include + +#include "light.h" namespace upm { -/** - * @library light - * @sensor light - * @comname Analog Light Sensor - * @type light - * @man seeed - * @con analog - * @kit gsk - * - * @brief API for the Light Sensor - * - * 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 light.jpg - * @snippet light.cxx Interesting - */ -class Light { + /** + * @library light + * @sensor light + * @comname Analog Light Sensor + * @type light + * @man seeed + * @con analog + * @kit gsk + * + * @brief API for the Light Sensor + * + * 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. + * A very approximate lux value can also be returned. + * + * @image html light.jpg + * @snippet light.cxx Interesting + */ + class Light { public: /** * analog light sensor constructor * * @param pin Analog pin to use + * @throws std::runtime_error on initialization error */ Light(unsigned int pin); + /** * Light destructor */ ~Light(); + /** * Gets the raw value from the AIO pin * + * @deprecated This function is deprecated use + * getNormalized() instead. * @return Raw value from the ADC + * @throws std::runtime_error on error */ float raw_value(); + /** - * Gets an approximate light value, in lux, from the sensor + * Gets an approximate light value in lux from the sensor * - * @return Normalized light reading in lux + * @return Approximate light reading in lux + * @throws std::runtime_error on error */ int value(); + /** + * Set ADC reference voltage + * + * @param dev sensor context pointer + * @param aref ADC reference voltage + */ + void setAref(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 + */ + void setScale(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 + */ + void setOffset(float offset); + + /** + * Get sensor aref + * + * @param dev sensor context pointer + * @return Sensor ADC reference voltage + */ + float getAref(); + + /** + * Get sensor scale + * + * @param dev sensor context pointer + * @return Sensor scale + */ + float getScale(); + + /** + * Get sensor offset + * + * @param dev sensor context pointer + * @return Sensor offset + */ + float getOffset(); + + /** + * Read normalized value for sensor + * + * @param dev sensor context pointer + * @param *value Normalized value (0.0 -> 1.0) + * @return Function result code + * @throws std::runtime_error on error + */ + float getNormalized(); + + /** + * Read raw voltage from the sensor + * + * @param dev sensor context pointer + * @param *value Raw sensor voltage + * @return Function result code + * @throws std::runtime_error on error + */ + float getRawVolts(); + /* Sensor name * * @return Sensor name */ std::string name() { - return m_name; + return std::string("Light Sensor"); } + protected: + light_context m_light; + private: - mraa_aio_context m_aio; - const std::string m_name = "Light Sensor"; -}; + }; }