mirror of
https://github.com/eclipse/upm.git
synced 2025-03-15 04:57:30 +03:00
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:
parent
27c6ac1d31
commit
54a84af1c3
@ -34,10 +34,11 @@ main(int argc, char **argv)
|
|||||||
// Create the light sensor object using AIO pin 0
|
// Create the light sensor object using AIO pin 0
|
||||||
upm::Light* light = new upm::Light(0);
|
upm::Light* light = new upm::Light(0);
|
||||||
|
|
||||||
// Read the input and print both the raw value and a rough lux value,
|
// Read the input and print both the normalized ADC value and a
|
||||||
// waiting one second between readings
|
// rough lux value, waiting one second between readings
|
||||||
while( 1 ) {
|
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;
|
", which is roughly " << light->value() << " lux" << std::endl;
|
||||||
sleep(1);
|
sleep(1);
|
||||||
}
|
}
|
||||||
|
@ -25,13 +25,13 @@
|
|||||||
public class LightSample {
|
public class LightSample {
|
||||||
public static void main(String args[]) throws InterruptedException {
|
public static void main(String args[]) throws InterruptedException {
|
||||||
// ! [Interesting]
|
// ! [Interesting]
|
||||||
upm_light.Light gl = new upm_light.Light(2);
|
upm_light.Light gl = new upm_light.Light(0);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
float raw_value = gl.raw_value();
|
float raw_value = gl.getNormalized();
|
||||||
float value = gl.value();
|
float value = gl.value();
|
||||||
|
|
||||||
System.out.println("raw value: " + raw_value);
|
System.out.println("normalized: " + raw_value);
|
||||||
System.out.println("value: " + value);
|
System.out.println("value: " + value);
|
||||||
|
|
||||||
Thread.sleep(1000);
|
Thread.sleep(1000);
|
||||||
|
@ -28,10 +28,10 @@ var sensor = require('jsupm_light');
|
|||||||
// Create the light sensor object using AIO pin 0
|
// Create the light sensor object using AIO pin 0
|
||||||
var light = new sensor.Light(0);
|
var light = new sensor.Light(0);
|
||||||
|
|
||||||
// Read the input and print both the raw value and a rough lux value,
|
// Read the input and print both the normalized ADC value and a rough
|
||||||
// waiting one second between readings
|
// lux value, waiting one second between readings
|
||||||
function readLightSensorValue() {
|
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");
|
", which is roughly " + light.value() + " lux");
|
||||||
}
|
}
|
||||||
setInterval(readLightSensorValue, 1000);
|
setInterval(readLightSensorValue, 1000);
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
from __future__ import print_function
|
#!/usr/bin/python
|
||||||
# Author: Sarah Knepper <sarah.knepper@intel.com>
|
# Author: Sarah Knepper <sarah.knepper@intel.com>
|
||||||
# Copyright (c) 2014 Intel Corporation.
|
# 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
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
from __future__ import print_function
|
||||||
import time
|
import time
|
||||||
from upm import pyupm_light as light
|
from upm import pyupm_light as lightObj
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# Create the light sensor object using AIO pin 0
|
# 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,
|
# Read the input and print both the normalized ADC value and a
|
||||||
# waiting one second between readings
|
# rough lux value, waiting one second between readings
|
||||||
while 1:
|
while 1:
|
||||||
print(light.name() + " raw value is %d" % light.raw_value() + \
|
print(sensor.name() + " normalized value is %f" % sensor.getNormalized()
|
||||||
", which is roughly %d" % light.value() + " lux");
|
+ ", which is roughly %d" % sensor.value() + " lux");
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
# Delete the light sensor object
|
# Delete the light sensor object
|
||||||
del light
|
del lightObj
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
@ -5,5 +5,6 @@ upm_mixed_module_init (NAME light
|
|||||||
CPP_HDR light.hpp
|
CPP_HDR light.hpp
|
||||||
CPP_SRC light.cxx
|
CPP_SRC light.cxx
|
||||||
FTI_SRC light_fti.c
|
FTI_SRC light_fti.c
|
||||||
REQUIRES mraa grove m)
|
CPP_WRAPS_C
|
||||||
|
REQUIRES mraa m)
|
||||||
target_link_libraries(light-c m)
|
target_link_libraries(light-c m)
|
||||||
|
@ -2,7 +2,8 @@
|
|||||||
* Authors: Brendan Le Foll <brendan.le.foll@intel.com>
|
* Authors: Brendan Le Foll <brendan.le.foll@intel.com>
|
||||||
* Mihai Tudor Panu <mihai.tudor.panu@intel.com>
|
* Mihai Tudor Panu <mihai.tudor.panu@intel.com>
|
||||||
* Sarah Knepper <sarah.knepper@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
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
* a copy of this software and associated documentation files (the
|
* a copy of this software and associated documentation files (the
|
||||||
@ -29,34 +30,90 @@
|
|||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
#include "light.hpp"
|
#include "light.hpp"
|
||||||
#include "math.h"
|
|
||||||
|
|
||||||
using namespace upm;
|
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)) ) {
|
if ( !m_light )
|
||||||
throw std::invalid_argument(std::string(__FUNCTION__) +
|
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||||
": mraa_aio_init() failed, invalid pin?");
|
": light_init() failed.");
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Light::~Light()
|
Light::~Light()
|
||||||
{
|
{
|
||||||
mraa_aio_close(m_aio);
|
light_close(m_light);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Light::value()
|
int Light::value()
|
||||||
{
|
{
|
||||||
// rough conversion to lux, using formula from Starter Kit booklet
|
float value;
|
||||||
float a = (float) mraa_aio_read(m_aio);
|
|
||||||
if (a == -1.0) return -1;
|
if (light_get_lux(m_light, &value))
|
||||||
a = 10000.0/pow(((1023.0-a)*10.0/a)*15.0,4.0/3.0);
|
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||||
return (int) round(a);
|
": light_get_normalized() failed.");
|
||||||
|
|
||||||
|
return (int)roundf(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
float Light::raw_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;
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,8 @@
|
|||||||
* Authors: Brendan Le Foll <brendan.le.foll@intel.com>
|
* Authors: Brendan Le Foll <brendan.le.foll@intel.com>
|
||||||
* Mihai Tudor Panu <mihai.tudor.panu@intel.com>
|
* Mihai Tudor Panu <mihai.tudor.panu@intel.com>
|
||||||
* Sarah Knepper <sarah.knepper@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
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
* a copy of this software and associated documentation files (the
|
* a copy of this software and associated documentation files (the
|
||||||
@ -27,66 +28,147 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <mraa/aio.hpp>
|
|
||||||
|
#include "light.h"
|
||||||
|
|
||||||
namespace upm {
|
namespace upm {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @library light
|
* @library light
|
||||||
* @sensor light
|
* @sensor light
|
||||||
* @comname Analog Light Sensor
|
* @comname Analog Light Sensor
|
||||||
* @type light
|
* @type light
|
||||||
* @man seeed
|
* @man seeed
|
||||||
* @con analog
|
* @con analog
|
||||||
* @kit gsk
|
* @kit gsk
|
||||||
*
|
*
|
||||||
* @brief API for the Light Sensor
|
* @brief API for the Light Sensor
|
||||||
*
|
*
|
||||||
* The 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
|
* As the light intensity of the environment increases, the resistance
|
||||||
* of the sensor decreases. This means the raw value from the
|
* of the sensor decreases. This means the raw value from the
|
||||||
* analog pin is larger in bright light and smaller in the dark.
|
* 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
|
* @image html light.jpg
|
||||||
* @snippet light.cxx Interesting
|
* @snippet light.cxx Interesting
|
||||||
*/
|
*/
|
||||||
class Light {
|
class Light {
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* analog light sensor constructor
|
* analog light sensor constructor
|
||||||
*
|
*
|
||||||
* @param pin Analog pin to use
|
* @param pin Analog pin to use
|
||||||
|
* @throws std::runtime_error on initialization error
|
||||||
*/
|
*/
|
||||||
Light(unsigned int pin);
|
Light(unsigned int pin);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Light destructor
|
* Light destructor
|
||||||
*/
|
*/
|
||||||
~Light();
|
~Light();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the raw value from the AIO pin
|
* Gets the raw value from the AIO pin
|
||||||
*
|
*
|
||||||
|
* @deprecated This function is deprecated use
|
||||||
|
* getNormalized() instead.
|
||||||
* @return Raw value from the ADC
|
* @return Raw value from the ADC
|
||||||
|
* @throws std::runtime_error on error
|
||||||
*/
|
*/
|
||||||
float raw_value();
|
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();
|
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
|
/* Sensor name
|
||||||
*
|
*
|
||||||
* @return Sensor name
|
* @return Sensor name
|
||||||
*/
|
*/
|
||||||
std::string name()
|
std::string name()
|
||||||
{
|
{
|
||||||
return m_name;
|
return std::string("Light Sensor");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
light_context m_light;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mraa_aio_context m_aio;
|
};
|
||||||
const std::string m_name = "Light Sensor";
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user