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 <jtrulson@ics.com>
This commit is contained in:
Jon Trulson 2017-03-17 16:18:40 -06:00
parent 27c6ac1d31
commit 54a84af1c3
7 changed files with 204 additions and 62 deletions

View File

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

View File

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

View File

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

View File

@ -1,4 +1,4 @@
from __future__ import print_function
#!/usr/bin/python
# Author: Sarah Knepper <sarah.knepper@intel.com>
# 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()

View File

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

View File

@ -2,7 +2,8 @@
* 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.
* Jon Trulson <jtrulson@ics.com>
* 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 <stdexcept>
#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;
}

View File

@ -2,7 +2,8 @@
* 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.
* Jon Trulson <jtrulson@ics.com>
* 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,7 +28,8 @@
#pragma once
#include <string>
#include <mraa/aio.hpp>
#include "light.h"
namespace upm {
@ -46,7 +48,7 @@ namespace upm {
* 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.
* A very approximate lux value can also be returned.
*
* @image html light.jpg
* @snippet light.cxx Interesting
@ -57,36 +59,116 @@ class Light {
* 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";
};
}