mirror of
https://github.com/eclipse/upm.git
synced 2025-07-26 13:41:16 +03:00
C++ Core: Add base class per sensor/actuator type
Adding base classes for UPM sensors and actuators. Signed-off-by: Noel Eck <noel.eck@intel.com>
This commit is contained in:
@ -4,15 +4,15 @@ file (GLOB example_src_list RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.cxx")
|
||||
# - Handle special cases here --------------------------------------------------
|
||||
|
||||
# Test humidity interface for 2 sensor libraries
|
||||
add_example(interfaces-humiditysensor.cxx TARGETS si7005 bmp280)
|
||||
add_example(core-humiditysensor.cxx TARGETS si7005 bmp280)
|
||||
# Test pressure interface for 2 sensor libraries
|
||||
add_example(interfaces-pressuresensor.cxx TARGETS bmp280 bmpx8x)
|
||||
add_example(core-pressuresensor.cxx TARGETS bmp280 bmpx8x)
|
||||
# Test temperature interface for 3 sensor libraries
|
||||
add_example(interfaces-temperaturesensor.cxx TARGETS bmp280 bmpx8x si7005)
|
||||
add_example(core-temperaturesensor.cxx TARGETS bmp280 bmpx8x si7005)
|
||||
# Test light interface for 2 sensor libraries
|
||||
add_example(interfaces-lightsensor.cxx TARGETS si1132 max44009)
|
||||
add_example(core-lightsensor.cxx TARGETS si1132 max44009)
|
||||
# Test light controller interface for 3 sensor libraries
|
||||
add_example(interfaces-lightcontroller.cxx TARGETS lp8860 ds1808lc hlg150h)
|
||||
add_example(core-lightcontroller.cxx TARGETS lp8860 ds1808lc hlg150h)
|
||||
|
||||
# - Create an executable for all other src files in this directory -------------
|
||||
foreach (_example_src ${example_src_list})
|
||||
|
@ -43,14 +43,14 @@ main()
|
||||
mraa::Gpio gpio(EDISON_GPIO_SI7005_CS);
|
||||
gpio.dir(mraa::DIR_OUT_HIGH);
|
||||
|
||||
/* Show usage from the IADC interface */
|
||||
upm::IADC* adc = static_cast<upm::IADC*>(&sensor);
|
||||
/* Show usage from the iADC interface */
|
||||
upm::iADC* adc = static_cast<upm::iADC*>(&sensor);
|
||||
|
||||
if (adc == NULL) {
|
||||
std::cout << "ADC not detected" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
std::cout << "ADC " << adc->getModuleName() << " detected. ";
|
||||
std::cout << "ADC " << adc->Name() << " detected. ";
|
||||
std::cout << adc->getNumInputs() << " inputs available" << std::endl;
|
||||
while (true) {
|
||||
for (unsigned int i = 0; i < adc->getNumInputs(); ++i) {
|
||||
|
@ -38,14 +38,14 @@
|
||||
#define EDISON_GPIO_SI7005_CS 20
|
||||
|
||||
//! [Interesting]
|
||||
// Simple example of using ILightSensor to determine
|
||||
// Simple example of using iHumiditySensor to determine
|
||||
// which sensor is present and return its name.
|
||||
// ILightSensor is then used to get readings from sensor
|
||||
// iHumiditySensor is then used to get readings from sensor
|
||||
|
||||
upm::IHumiditySensor*
|
||||
upm::iHumiditySensor*
|
||||
getHumiditySensor()
|
||||
{
|
||||
upm::IHumiditySensor* humiditySensor = NULL;
|
||||
upm::iHumiditySensor* humiditySensor = NULL;
|
||||
|
||||
try {
|
||||
humiditySensor = new upm::BME280(mraa_get_sub_platform_id(FT4222_I2C_BUS));
|
||||
@ -66,22 +66,26 @@ getHumiditySensor()
|
||||
int
|
||||
main()
|
||||
{
|
||||
upm::IHumiditySensor* humiditySensor = getHumiditySensor();
|
||||
if (humiditySensor == NULL) {
|
||||
upm::iHumiditySensor* sensor = getHumiditySensor();
|
||||
|
||||
if (sensor == NULL) {
|
||||
std::cout << "Humidity sensor not detected" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
std::cout << "Humidity sensor " << humiditySensor->getModuleName() << " detected" << std::endl;
|
||||
while (true) {
|
||||
try {
|
||||
int value = humiditySensor->getHumidityRelative();
|
||||
std::cout << "Humidity = " << value << "%" << std::endl;
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
}
|
||||
upm_delay(1);
|
||||
|
||||
std::cout << "Humidity sensor " << sensor->Name() << " detected" << std::endl;
|
||||
|
||||
try {
|
||||
std::map<std::string, float> values = sensor->HumidityAll();
|
||||
for (std::map<std::string, float>::const_iterator it = values.begin();
|
||||
it != values.end(); ++it)
|
||||
std::cout << it->first << " = " << it->second
|
||||
<< sensor->Unit(it->first) << std::endl;
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
}
|
||||
delete humiditySensor;
|
||||
|
||||
delete sensor;
|
||||
return 0;
|
||||
}
|
||||
|
@ -66,7 +66,7 @@ main(int argc, char** argv)
|
||||
|
||||
upm::ILightController* lightController = getLightController();
|
||||
if (lightController != NULL) {
|
||||
std::cout << "Detected light controller " << lightController->getModuleName() << std::endl;
|
||||
//std::cout << "Detected light controller " << lightController->getModuleName() << std::endl;
|
||||
} else {
|
||||
std::cerr << "Error. Unsupported platform." << std::endl;
|
||||
return 1;
|
@ -36,14 +36,14 @@
|
||||
#define FT4222_I2C_BUS 0
|
||||
|
||||
//! [Interesting]
|
||||
// Simple example of using ILightSensor to determine
|
||||
// Simple example of using iLightSensor to determine
|
||||
// which sensor is present and return its name.
|
||||
// ILightSensor is then used to get readings from sensor
|
||||
// iLightSensor is then used to get readings from sensor
|
||||
|
||||
upm::ILightSensor*
|
||||
upm::iLightSensor*
|
||||
getLightSensor()
|
||||
{
|
||||
upm::ILightSensor* lightSensor = NULL;
|
||||
upm::iLightSensor* lightSensor = NULL;
|
||||
try {
|
||||
lightSensor = new upm::SI1132(mraa_get_sub_platform_id(FT4222_I2C_BUS));
|
||||
return lightSensor;
|
||||
@ -62,22 +62,26 @@ getLightSensor()
|
||||
int
|
||||
main()
|
||||
{
|
||||
upm::ILightSensor* lightSensor = getLightSensor();
|
||||
if (lightSensor == NULL) {
|
||||
upm::iLightSensor* sensor = getLightSensor();
|
||||
|
||||
if (sensor == NULL) {
|
||||
std::cout << "Light sensor not detected" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
std::cout << "Light sensor " << lightSensor->getModuleName() << " detected" << std::endl;
|
||||
while (true) {
|
||||
try {
|
||||
float value = lightSensor->getVisibleLux();
|
||||
std::cout << "Light level = " << value << " lux" << std::endl;
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
}
|
||||
upm_delay(1);
|
||||
|
||||
std::cout << "Light sensor " << sensor->Name() << " detected" << std::endl;
|
||||
|
||||
try {
|
||||
std::map<std::string, float> values = sensor->LightAll();
|
||||
for (std::map<std::string, float>::const_iterator it = values.begin();
|
||||
it != values.end(); ++it)
|
||||
std::cout << it->first << " = " << it->second
|
||||
<< sensor->Unit(it->first) << std::endl;
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
}
|
||||
delete lightSensor;
|
||||
|
||||
delete sensor;
|
||||
return 0;
|
||||
}
|
||||
|
@ -36,14 +36,14 @@
|
||||
#define FT4222_I2C_BUS 0
|
||||
|
||||
//! [Interesting]
|
||||
// Simple example of using ILightSensor to determine
|
||||
// Simple example of using iPressureSensor to determine
|
||||
// which sensor is present and return its name.
|
||||
// ILightSensor is then used to get readings from sensor
|
||||
// iPressureSensor is then used to get readings from sensor
|
||||
|
||||
upm::IPressureSensor*
|
||||
upm::iPressureSensor*
|
||||
getPressureSensor()
|
||||
{
|
||||
upm::IPressureSensor* pressureSensor = NULL;
|
||||
upm::iPressureSensor* pressureSensor = NULL;
|
||||
try {
|
||||
pressureSensor = new upm::BME280(mraa_get_sub_platform_id(FT4222_I2C_BUS));
|
||||
return pressureSensor;
|
||||
@ -63,22 +63,26 @@ getPressureSensor()
|
||||
int
|
||||
main()
|
||||
{
|
||||
upm::IPressureSensor* pressureSensor = getPressureSensor();
|
||||
if (pressureSensor == NULL) {
|
||||
upm::iPressureSensor* sensor = getPressureSensor();
|
||||
|
||||
if (sensor == NULL) {
|
||||
std::cout << "Pressure sensor not detected" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
std::cout << "Pressure sensor " << pressureSensor->getModuleName() << " detected" << std::endl;
|
||||
while (true) {
|
||||
try {
|
||||
int value = pressureSensor->getPressurePa();
|
||||
std::cout << "Pressure = " << value << " Pa" << std::endl;
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
}
|
||||
upm_delay(1);
|
||||
|
||||
std::cout << "Pressure sensor " << sensor->Name() << " detected" << std::endl;
|
||||
|
||||
try {
|
||||
std::map<std::string, float> values = sensor->PressureAll();
|
||||
for (std::map<std::string, float>::const_iterator it = values.begin();
|
||||
it != values.end(); ++it)
|
||||
std::cout << it->first << " = " << it->second
|
||||
<< sensor->Unit(it->first) << std::endl;
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
}
|
||||
delete pressureSensor;
|
||||
|
||||
delete sensor;
|
||||
return 0;
|
||||
}
|
||||
|
@ -39,14 +39,14 @@
|
||||
#define EDISON_GPIO_SI7005_CS 20
|
||||
|
||||
//! [Interesting]
|
||||
// Simple example of using ITemperatureSensor to determine
|
||||
// Simple example of using iTemperatureSensor to determine
|
||||
// which sensor is present and return its name.
|
||||
// ITemperatureSensor is then used to get readings from sensor
|
||||
// iTemperatureSensor is then used to get readings from sensor
|
||||
|
||||
upm::ITemperatureSensor*
|
||||
upm::iTemperatureSensor*
|
||||
getTemperatureSensor()
|
||||
{
|
||||
upm::ITemperatureSensor* temperatureSensor = NULL;
|
||||
upm::iTemperatureSensor* temperatureSensor = NULL;
|
||||
|
||||
try {
|
||||
temperatureSensor = new upm::BME280(mraa_get_sub_platform_id(FT4222_I2C_BUS));
|
||||
@ -73,23 +73,26 @@ getTemperatureSensor()
|
||||
int
|
||||
main()
|
||||
{
|
||||
upm::ITemperatureSensor* temperatureSensor = getTemperatureSensor();
|
||||
if (temperatureSensor == NULL) {
|
||||
upm::iTemperatureSensor* sensor = getTemperatureSensor();
|
||||
|
||||
if (sensor == NULL) {
|
||||
std::cout << "Temperature sensor not detected" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
std::cout << "Temperature sensor " << temperatureSensor->getModuleName() << " detected"
|
||||
<< std::endl;
|
||||
while (true) {
|
||||
try {
|
||||
int value = temperatureSensor->getTemperatureCelsius();
|
||||
std::cout << "Temperature = " << value << "C" << std::endl;
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
}
|
||||
upm_delay(1);
|
||||
|
||||
std::cout << "Temperature sensor " << sensor->Name() << " detected" << std::endl;
|
||||
|
||||
try {
|
||||
std::map<std::string, float> values = sensor->TemperatureAll();
|
||||
for (std::map<std::string, float>::const_iterator it = values.begin();
|
||||
it != values.end(); ++it)
|
||||
std::cout << it->first << " = " << it->second
|
||||
<< sensor->Unit(it->first) << std::endl;
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
}
|
||||
delete temperatureSensor;
|
||||
|
||||
delete sensor;
|
||||
return 0;
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ main(int argc, char** argv)
|
||||
//! [Interesting]
|
||||
|
||||
// Instantiate a IMS instance using i2c bus 0 and default address
|
||||
upm::IMS sensor(0);
|
||||
upm::IMS sensor("i:0:0x20");
|
||||
|
||||
int i2c_addr_cur = IMS_ADDRESS_DEFAULT + 1;
|
||||
while (shouldRun) {
|
||||
|
49
examples/c++/noelstemplightreader.cxx
Normal file
49
examples/c++/noelstemplightreader.cxx
Normal file
@ -0,0 +1,49 @@
|
||||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "core/iUpmObject.hpp"
|
||||
#include "noelstemplightreader.hpp"
|
||||
|
||||
void printMap(upm::NoelsTempLightReader &sensor, std::map<std::string, float> &data)
|
||||
{
|
||||
if (data.empty())
|
||||
std::cout << "Empty map" << std::endl;
|
||||
for (std::map<std::string, float>::const_iterator it = data.begin();
|
||||
it != data.end(); ++it)
|
||||
{
|
||||
std::cout << "label: " << it->first << ", value: " << it->second
|
||||
<< ", unit: " << sensor.Unit(it->first) << std::endl;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
int main ()
|
||||
{
|
||||
upm::NoelsTempLightReader sensor;
|
||||
std::cout << "iUpmObject JsonDefinition..." << std::endl << ((upm::iUpmObject&)sensor).JsonDefinition() << std::endl << std::endl;
|
||||
std::cout << "iSensorType JsonDefinition..." << std::endl << ((upm::iSensorType&)sensor).JsonDefinition() << std::endl << std::endl;
|
||||
std::cout << "iMraa JsonDefinition..." << std::endl << ((upm::iMraa&)sensor).JsonDefinition() << std::endl << std::endl;
|
||||
std::cout << "NoelsTempLightReader JsonDefinition..." << std::endl << sensor.JsonDefinition() << std::endl << std::endl;
|
||||
|
||||
std::cout << "Read all light values..." << std::endl;
|
||||
std::map<std::string, float> values = sensor.LightAll();
|
||||
printMap(sensor, values);
|
||||
|
||||
std::cout << "Read a single light value for light0..." << std::endl;
|
||||
std::cout << "Single value = " << sensor.LightForSource("light0") << std::endl << std::endl;
|
||||
|
||||
try
|
||||
{
|
||||
std::cout << "Read a single light value for lightX (doesn't exist)..." << std::endl;
|
||||
sensor.LightForSource("lightX");
|
||||
}
|
||||
catch (const std::exception& e) { std::cout << e.what() << std::endl << std::endl;}
|
||||
|
||||
std::cout << "Read a light value for lightX (doesn't exist)..." << std::endl;
|
||||
values = sensor.LightForSources(std::vector<std::string>({"lightX"}));
|
||||
printMap(sensor, values);
|
||||
|
||||
std::cout << "Read all values as JsonDefinition..." << std::endl << sensor.JsonValues() << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
@ -31,7 +31,7 @@ int
|
||||
main(int argc, char** argv)
|
||||
{
|
||||
//! [Interesting]
|
||||
upm::ES08A servo(5);
|
||||
upm::ES08A servo("p:5");
|
||||
|
||||
// Sets the shaft to 180, then to 90, then to 0,
|
||||
// then back to 90, and finally back to 180,
|
||||
|
@ -42,18 +42,18 @@ main()
|
||||
/* Create an instance of the T6713 sensor */
|
||||
upm::T6713 sensor(EDISON_I2C_BUS);
|
||||
|
||||
/* Show usage from the ICO2Sensor interface */
|
||||
upm::ICO2Sensor* cO2Sensor = static_cast<upm::ICO2Sensor*>(&sensor);
|
||||
/* Show usage from the iCO2Sensor interface */
|
||||
upm::iCO2Sensor* cO2Sensor = static_cast<upm::iCO2Sensor*>(&sensor);
|
||||
|
||||
if (cO2Sensor == NULL) {
|
||||
std::cout << "CO2 sensor not detected" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
std::cout << "CO2 sensor " << cO2Sensor->getModuleName() << " detected" << std::endl;
|
||||
std::cout << "CO2 sensor " << cO2Sensor->Name() << " detected" << std::endl;
|
||||
while (true) {
|
||||
try {
|
||||
uint16_t value = cO2Sensor->getPpm();
|
||||
std::cout << "CO2 level = " << value << " ppm" << std::endl;
|
||||
uint16_t value = cO2Sensor->CO2ForSource(cO2Sensor->Sources()[0]);
|
||||
std::cout << "CO2 level = " << value << cO2Sensor->Units()[0] << std::endl;
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << e.what() << std::endl;
|
||||
}
|
||||
|
Reference in New Issue
Block a user