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

@ -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,66 +28,147 @@
#pragma once
#include <string>
#include <mraa/aio.hpp>
#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";
};
};
}