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:
Noel Eck 2017-06-08 13:50:09 -07:00
parent b55501e327
commit 0223cd2b85
184 changed files with 4462 additions and 1099 deletions

View File

@ -482,9 +482,6 @@ endif()
# UPM common headers # UPM common headers
set (UPM_COMMON_HEADER_DIRS ${CMAKE_HOME_DIRECTORY}/include) set (UPM_COMMON_HEADER_DIRS ${CMAKE_HOME_DIRECTORY}/include)
# Generate a build-only C++ header to add functionality to SWIG'ed modules
configure_file (${PROJECT_SOURCE_DIR}/cmake/modules/version.hpp.in ${PROJECT_BINARY_DIR}/src/version.hpp @ONLY)
# UPM source # UPM source
add_subdirectory (src) add_subdirectory (src)

View File

@ -1,6 +0,0 @@
#include <string>
inline std::string getVersion()
{
return "@upm_VERSION_STRING@";
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

View File

@ -120,7 +120,7 @@ if(BUILDEXAMPLES)
# Add all examples as an install component (if building examples) # Add all examples as an install component (if building examples)
install (DIRECTORY ${PROJECT_SOURCE_DIR}/examples install (DIRECTORY ${PROJECT_SOURCE_DIR}/examples
DESTINATION ${CMAKE_INSTALL_DATADIR}/upm/ DESTINATION ${CMAKE_INSTALL_DATADIR}/upm
COMPONENT ${CMAKE_PROJECT_NAME}-examples COMPONENT ${CMAKE_PROJECT_NAME}-examples
FILES_MATCHING FILES_MATCHING
PATTERN "*.c" PATTERN "*.c"

View File

@ -4,15 +4,15 @@ file (GLOB example_src_list RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.cxx")
# - Handle special cases here -------------------------------------------------- # - Handle special cases here --------------------------------------------------
# Test humidity interface for 2 sensor libraries # 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 # 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 # 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 # 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 # 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 ------------- # - Create an executable for all other src files in this directory -------------
foreach (_example_src ${example_src_list}) foreach (_example_src ${example_src_list})

View File

@ -43,14 +43,14 @@ main()
mraa::Gpio gpio(EDISON_GPIO_SI7005_CS); mraa::Gpio gpio(EDISON_GPIO_SI7005_CS);
gpio.dir(mraa::DIR_OUT_HIGH); gpio.dir(mraa::DIR_OUT_HIGH);
/* Show usage from the IADC interface */ /* Show usage from the iADC interface */
upm::IADC* adc = static_cast<upm::IADC*>(&sensor); upm::iADC* adc = static_cast<upm::iADC*>(&sensor);
if (adc == NULL) { if (adc == NULL) {
std::cout << "ADC not detected" << std::endl; std::cout << "ADC not detected" << std::endl;
return 1; return 1;
} }
std::cout << "ADC " << adc->getModuleName() << " detected. "; std::cout << "ADC " << adc->Name() << " detected. ";
std::cout << adc->getNumInputs() << " inputs available" << std::endl; std::cout << adc->getNumInputs() << " inputs available" << std::endl;
while (true) { while (true) {
for (unsigned int i = 0; i < adc->getNumInputs(); ++i) { for (unsigned int i = 0; i < adc->getNumInputs(); ++i) {

View File

@ -38,14 +38,14 @@
#define EDISON_GPIO_SI7005_CS 20 #define EDISON_GPIO_SI7005_CS 20
//! [Interesting] //! [Interesting]
// Simple example of using ILightSensor to determine // Simple example of using iHumiditySensor to determine
// which sensor is present and return its name. // 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() getHumiditySensor()
{ {
upm::IHumiditySensor* humiditySensor = NULL; upm::iHumiditySensor* humiditySensor = NULL;
try { try {
humiditySensor = new upm::BME280(mraa_get_sub_platform_id(FT4222_I2C_BUS)); humiditySensor = new upm::BME280(mraa_get_sub_platform_id(FT4222_I2C_BUS));
@ -66,22 +66,26 @@ getHumiditySensor()
int int
main() main()
{ {
upm::IHumiditySensor* humiditySensor = getHumiditySensor(); upm::iHumiditySensor* sensor = getHumiditySensor();
if (humiditySensor == NULL) {
if (sensor == NULL) {
std::cout << "Humidity sensor not detected" << std::endl; std::cout << "Humidity sensor not detected" << std::endl;
return 1; return 1;
} }
std::cout << "Humidity sensor " << humiditySensor->getModuleName() << " detected" << std::endl;
while (true) { std::cout << "Humidity sensor " << sensor->Name() << " detected" << std::endl;
try { try {
int value = humiditySensor->getHumidityRelative(); std::map<std::string, float> values = sensor->HumidityAll();
std::cout << "Humidity = " << value << "%" << std::endl; 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) { } catch (std::exception& e) {
std::cerr << e.what() << std::endl; std::cerr << e.what() << std::endl;
} }
upm_delay(1);
} delete sensor;
delete humiditySensor;
return 0; return 0;
} }

View File

@ -66,7 +66,7 @@ main(int argc, char** argv)
upm::ILightController* lightController = getLightController(); upm::ILightController* lightController = getLightController();
if (lightController != NULL) { if (lightController != NULL) {
std::cout << "Detected light controller " << lightController->getModuleName() << std::endl; //std::cout << "Detected light controller " << lightController->getModuleName() << std::endl;
} else { } else {
std::cerr << "Error. Unsupported platform." << std::endl; std::cerr << "Error. Unsupported platform." << std::endl;
return 1; return 1;

View File

@ -36,14 +36,14 @@
#define FT4222_I2C_BUS 0 #define FT4222_I2C_BUS 0
//! [Interesting] //! [Interesting]
// Simple example of using ILightSensor to determine // Simple example of using iLightSensor to determine
// which sensor is present and return its name. // 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() getLightSensor()
{ {
upm::ILightSensor* lightSensor = NULL; upm::iLightSensor* lightSensor = NULL;
try { try {
lightSensor = new upm::SI1132(mraa_get_sub_platform_id(FT4222_I2C_BUS)); lightSensor = new upm::SI1132(mraa_get_sub_platform_id(FT4222_I2C_BUS));
return lightSensor; return lightSensor;
@ -62,22 +62,26 @@ getLightSensor()
int int
main() main()
{ {
upm::ILightSensor* lightSensor = getLightSensor(); upm::iLightSensor* sensor = getLightSensor();
if (lightSensor == NULL) {
if (sensor == NULL) {
std::cout << "Light sensor not detected" << std::endl; std::cout << "Light sensor not detected" << std::endl;
return 1; return 1;
} }
std::cout << "Light sensor " << lightSensor->getModuleName() << " detected" << std::endl;
while (true) { std::cout << "Light sensor " << sensor->Name() << " detected" << std::endl;
try { try {
float value = lightSensor->getVisibleLux(); std::map<std::string, float> values = sensor->LightAll();
std::cout << "Light level = " << value << " lux" << std::endl; 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) { } catch (std::exception& e) {
std::cerr << e.what() << std::endl; std::cerr << e.what() << std::endl;
} }
upm_delay(1);
} delete sensor;
delete lightSensor;
return 0; return 0;
} }

View File

@ -36,14 +36,14 @@
#define FT4222_I2C_BUS 0 #define FT4222_I2C_BUS 0
//! [Interesting] //! [Interesting]
// Simple example of using ILightSensor to determine // Simple example of using iPressureSensor to determine
// which sensor is present and return its name. // 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() getPressureSensor()
{ {
upm::IPressureSensor* pressureSensor = NULL; upm::iPressureSensor* pressureSensor = NULL;
try { try {
pressureSensor = new upm::BME280(mraa_get_sub_platform_id(FT4222_I2C_BUS)); pressureSensor = new upm::BME280(mraa_get_sub_platform_id(FT4222_I2C_BUS));
return pressureSensor; return pressureSensor;
@ -63,22 +63,26 @@ getPressureSensor()
int int
main() main()
{ {
upm::IPressureSensor* pressureSensor = getPressureSensor(); upm::iPressureSensor* sensor = getPressureSensor();
if (pressureSensor == NULL) {
if (sensor == NULL) {
std::cout << "Pressure sensor not detected" << std::endl; std::cout << "Pressure sensor not detected" << std::endl;
return 1; return 1;
} }
std::cout << "Pressure sensor " << pressureSensor->getModuleName() << " detected" << std::endl;
while (true) { std::cout << "Pressure sensor " << sensor->Name() << " detected" << std::endl;
try { try {
int value = pressureSensor->getPressurePa(); std::map<std::string, float> values = sensor->PressureAll();
std::cout << "Pressure = " << value << " Pa" << std::endl; 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) { } catch (std::exception& e) {
std::cerr << e.what() << std::endl; std::cerr << e.what() << std::endl;
} }
upm_delay(1);
} delete sensor;
delete pressureSensor;
return 0; return 0;
} }

View File

@ -39,14 +39,14 @@
#define EDISON_GPIO_SI7005_CS 20 #define EDISON_GPIO_SI7005_CS 20
//! [Interesting] //! [Interesting]
// Simple example of using ITemperatureSensor to determine // Simple example of using iTemperatureSensor to determine
// which sensor is present and return its name. // 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() getTemperatureSensor()
{ {
upm::ITemperatureSensor* temperatureSensor = NULL; upm::iTemperatureSensor* temperatureSensor = NULL;
try { try {
temperatureSensor = new upm::BME280(mraa_get_sub_platform_id(FT4222_I2C_BUS)); temperatureSensor = new upm::BME280(mraa_get_sub_platform_id(FT4222_I2C_BUS));
@ -73,23 +73,26 @@ getTemperatureSensor()
int int
main() main()
{ {
upm::ITemperatureSensor* temperatureSensor = getTemperatureSensor(); upm::iTemperatureSensor* sensor = getTemperatureSensor();
if (temperatureSensor == NULL) {
if (sensor == NULL) {
std::cout << "Temperature sensor not detected" << std::endl; std::cout << "Temperature sensor not detected" << std::endl;
return 1; return 1;
} }
std::cout << "Temperature sensor " << temperatureSensor->getModuleName() << " detected"
<< std::endl; std::cout << "Temperature sensor " << sensor->Name() << " detected" << std::endl;
while (true) {
try { try {
int value = temperatureSensor->getTemperatureCelsius(); std::map<std::string, float> values = sensor->TemperatureAll();
std::cout << "Temperature = " << value << "C" << std::endl; 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) { } catch (std::exception& e) {
std::cerr << e.what() << std::endl; std::cerr << e.what() << std::endl;
} }
upm_delay(1);
} delete sensor;
delete temperatureSensor;
return 0; return 0;
} }

View File

@ -47,7 +47,7 @@ main(int argc, char** argv)
//! [Interesting] //! [Interesting]
// Instantiate a IMS instance using i2c bus 0 and default address // 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; int i2c_addr_cur = IMS_ADDRESS_DEFAULT + 1;
while (shouldRun) { while (shouldRun) {

View 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;
}

View File

@ -31,7 +31,7 @@ int
main(int argc, char** argv) main(int argc, char** argv)
{ {
//! [Interesting] //! [Interesting]
upm::ES08A servo(5); upm::ES08A servo("p:5");
// Sets the shaft to 180, then to 90, then to 0, // Sets the shaft to 180, then to 90, then to 0,
// then back to 90, and finally back to 180, // then back to 90, and finally back to 180,

View File

@ -42,18 +42,18 @@ main()
/* Create an instance of the T6713 sensor */ /* Create an instance of the T6713 sensor */
upm::T6713 sensor(EDISON_I2C_BUS); upm::T6713 sensor(EDISON_I2C_BUS);
/* Show usage from the ICO2Sensor interface */ /* Show usage from the iCO2Sensor interface */
upm::ICO2Sensor* cO2Sensor = static_cast<upm::ICO2Sensor*>(&sensor); upm::iCO2Sensor* cO2Sensor = static_cast<upm::iCO2Sensor*>(&sensor);
if (cO2Sensor == NULL) { if (cO2Sensor == NULL) {
std::cout << "CO2 sensor not detected" << std::endl; std::cout << "CO2 sensor not detected" << std::endl;
return 1; return 1;
} }
std::cout << "CO2 sensor " << cO2Sensor->getModuleName() << " detected" << std::endl; std::cout << "CO2 sensor " << cO2Sensor->Name() << " detected" << std::endl;
while (true) { while (true) {
try { try {
uint16_t value = cO2Sensor->getPpm(); uint16_t value = cO2Sensor->CO2ForSource(cO2Sensor->Sources()[0]);
std::cout << "CO2 level = " << value << " ppm" << std::endl; std::cout << "CO2 level = " << value << cO2Sensor->Units()[0] << std::endl;
} catch (std::exception& e) { } catch (std::exception& e) {
std::cerr << e.what() << std::endl; std::cerr << e.what() << std::endl;
} }

View File

@ -24,7 +24,7 @@
*/ */
import upm_bmp280.*; import upm_bmp280.*;
import upm_interfaces.*; import upm_core.*;
public class BME280_Interface_Example public class BME280_Interface_Example
{ {
@ -35,9 +35,9 @@ public class BME280_Interface_Example
// Instantiate a BME280 instance using default i2c bus and address // Instantiate a BME280 instance using default i2c bus and address
BME280 sensor = new BME280(); BME280 sensor = new BME280();
ITemperatureSensor t_sensor = sensor; iTemperatureSensor t_sensor = sensor;
IHumiditySensor h_sensor = sensor; iHumiditySensor h_sensor = sensor;
IPressureSensor p_sensor = sensor; iPressureSensor p_sensor = sensor;
// For SPI, bus 0, you would pass -1 as the address, and a // For SPI, bus 0, you would pass -1 as the address, and a
// valid pin for CS: // valid pin for CS:
@ -51,15 +51,15 @@ public class BME280_Interface_Example
System.out.println("Calling Interface Functions: "); System.out.println("Calling Interface Functions: ");
System.out.println("Compensation Temperature: " System.out.println("Compensation Temperature: "
+ t_sensor.getTemperatureCelsius() + t_sensor.TemperatureForSource("temperature")
+ " C / "); + " C / ");
System.out.println("Pressure: " System.out.println("Pressure: "
+ p_sensor.getPressurePa() + p_sensor.PressureForSource("pressure")
+ " Pa"); + " Pa");
System.out.println("Humidity: " System.out.println("Humidity: "
+ h_sensor.getHumidityRelative() + h_sensor.HumidityForSource("humidity")
+ " %RH"); + " %RH");
System.out.println(); System.out.println();

View File

@ -24,9 +24,9 @@ include(UseJava)
# add_example(SensorFooExample sensorfoo) # add_example(SensorFooExample sensorfoo)
# #
# # Creates SensorFooExample JAVA target, depends on targets: sensorfoo, # # Creates SensorFooExample JAVA target, depends on targets: sensorfoo,
# # and interfaces and will add both upm_sensorfoo.jar and upm_interfaces.jar # # and core and will add both upm_sensorfoo.jar and upm_core.jar
# # to the javac classpath. # # to the javac classpath.
# add_example(SensorFooExample "sensorfoo;interfaces"") # add_example(SensorFooExample "sensorfoo;core"")
# #
function(add_example example_class_name dependency_list) function(add_example example_class_name dependency_list)
set(example_file "${example_class_name}.java") set(example_file "${example_class_name}.java")
@ -68,7 +68,7 @@ add_example(A110X_Example a110x)
add_example(A110X_intr_Example a110x) add_example(A110X_intr_Example a110x)
add_example(Ad8232_Example ad8232) add_example(Ad8232_Example ad8232)
add_example(ADC121C021_Example adc121c021) add_example(ADC121C021_Example adc121c021)
add_example(Ads1015_Example "ads1x15;interfaces") add_example(Ads1015_Example "ads1x15;core")
add_example(Ads1115_Example ads1x15) add_example(Ads1115_Example ads1x15)
add_example(Adxl345_Example adxl345) add_example(Adxl345_Example adxl345)
add_example(AM2315_Example am2315) add_example(AM2315_Example am2315)
@ -78,13 +78,13 @@ add_example(BH1750_Example bh1750)
add_example(BISS0001_Example biss0001) add_example(BISS0001_Example biss0001)
add_example(BMA250E_Example bma250e) add_example(BMA250E_Example bma250e)
add_example(BMC150_Example bmx055) add_example(BMC150_Example bmx055)
add_example(BME280_Example "bmp280;interfaces") add_example(BME280_Example "bmp280;core")
add_example(BMG160_Example bmg160) add_example(BMG160_Example bmg160)
add_example(BMI055_Example bmx055) add_example(BMI055_Example bmx055)
add_example(BMI160_Example bmi160) add_example(BMI160_Example bmi160)
add_example(BMM150_Example bmm150) add_example(BMM150_Example bmm150)
add_example(BMP280_Example "bmp280;interfaces") add_example(BMP280_Example "bmp280;core")
add_example(BMPX8X_Example "bmpx8x;interfaces") add_example(BMPX8X_Example "bmpx8x;core")
add_example(BMX055_Example bmx055) add_example(BMX055_Example bmx055)
add_example(BNO055_Example bno055) add_example(BNO055_Example bno055)
add_example(Button_Example button) add_example(Button_Example button)
@ -101,7 +101,7 @@ add_example(ECS1030_Example ecs1030)
add_example(EHR_Example ehr) add_example(EHR_Example ehr)
add_example(Emg_Example emg) add_example(Emg_Example emg)
add_example(ENC03R_Example enc03r) add_example(ENC03R_Example enc03r)
add_example(ES08A_Example "servo;interfaces") add_example(ES08A_Example "servo;core")
add_example(FlexSensor_Example flex) add_example(FlexSensor_Example flex)
add_example(Gp2y0a_Example gp2y0a) add_example(Gp2y0a_Example gp2y0a)
add_example(GroveButton_Example grove) add_example(GroveButton_Example grove)
@ -229,9 +229,9 @@ add_example(YG1006_Example yg1006)
add_example(ZFM20_Example zfm20) add_example(ZFM20_Example zfm20)
if(SWIG_VERSION VERSION_GREATER 3.0.8) if(SWIG_VERSION VERSION_GREATER 3.0.8)
add_example(BME280_Interface_Example "bmp280;interfaces") add_example(BME280_Interface_Example "bmp280;core")
add_example(IMS_Example "ims;interfaces") add_example(IMS_Example "ims;core")
add_example(RHUSB_Example "rhusb;interfaces") add_example(RHUSB_Example "rhusb;core")
endif() endif()
if (OPENZWAVE_FOUND) if (OPENZWAVE_FOUND)

View File

@ -22,8 +22,6 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
import upm_ims.IMS;
public class IMS_Example public class IMS_Example
{ {
public static void main(String[] args) throws InterruptedException public static void main(String[] args) throws InterruptedException
@ -31,18 +29,18 @@ public class IMS_Example
// ! [Interesting] // ! [Interesting]
// Instantiate a IMS instance using bus 0 and default i2c address // Instantiate a IMS instance using bus 0 and default i2c address
IMS sensor = new IMS((short)0); upm_ims.IMS sensor = new upm_ims.IMS((short)0);
while (true) while (true)
{ {
System.out.println("Version: " System.out.println("Version: "
+ sensor.get_version() + upm_ims.javaupm_ims.LibraryVersion()
+ " light: " + " light: "
+ sensor.get_light() + sensor.LightForSource("light")
+ " moisture: " + " moisture: "
+ sensor.get_moisture() + sensor.MoistureForSource("moisture")
+ " temp: " + " temp: "
+ sensor.get_temperature() + sensor.TemperatureForSource("temperature")
+ " C"); + " C");
Thread.sleep(1000); Thread.sleep(1000);

View File

@ -0,0 +1,42 @@
/*
* The MIT License (MIT)
*
* Author: Your Full Name <your@email.address>
* Copyright (c) <year> <copyright holder>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import upm_noelstemplightreader.*;
public class NoelsTempLightReaderSample {
public static void main (String args[]) throws InterruptedException {
//! [Interesting]
// Instantiate new sensor instance
upm_noelstemplightreader.NoelsTempLightReader sensor = new upm_noelstemplightreader.NoelsTempLightReader();
while (true) {
//System.out.println("NoelsTempLightReader says: " + sensor.Name());
// Repeate every 2 seconds
Thread.sleep(2000);
}
//! [Interesting]
}
}

View File

@ -39,6 +39,12 @@ public class RHUSB_Example {
// Instantiate an RHUSB instance on defaultDev // Instantiate an RHUSB instance on defaultDev
upm_rhusb.RHUSB sensor = new upm_rhusb.RHUSB(defaultDev); upm_rhusb.RHUSB sensor = new upm_rhusb.RHUSB(defaultDev);
// Print out info from the sensor core
System.out.println("UPM Version:" + System.lineSeparator()
+ upm_rhusb.javaupm_rhusb.LibraryVersion() + System.lineSeparator());
System.out.println("JSON Definition:" + System.lineSeparator()
+ sensor.JsonDefinition() + System.lineSeparator());
// output the firmware ID // output the firmware ID
System.out.println("Firmware ID: " + sensor.getFirmwareID()); System.out.println("Firmware ID: " + sensor.getFirmwareID());

View File

@ -0,0 +1,40 @@
/*
* The MIT License (MIT)
*
* Author: Your Full Name <your@email.address>
* Copyright (c) <year> <copyright holder>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var jsupm_noelstemplightreader = require('jsupm_noelstemplightreader');
// Create an instance of the sensor
var sensor = new upm_noelstemplightreader.NoelsTempLightReader(0);
loop();
function loop()
{
// Call a method from the noelstemplightreader
console.log("NoelsTempLightReader says: " + sensor.helloWorld());
// Call loop every 2 seconds
setTimeout(loop, 2000);
}

View File

@ -0,0 +1,39 @@
# The MIT License (MIT)
#
# Author: Your Full Name <your@email.address>
# Copyright (c) <year> <copyright holder>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION 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_noelstemplightreader
def main():
# Create an instance of NoelsTempLightReader
sensor = pyupm_noelstemplightreader.NoelsTempLightReader(0)
while True:
print("NoelsTempLightReader says: %s" % sensor.helloWorld)
# Repeat every 2 seconds
sleep(2)
if __name__ == '__main__':
main()

View File

@ -68,7 +68,7 @@ make -j8 -Cbuild
# Anotate the .java src from doxygen # Anotate the .java src from doxygen
find src/ -name "*.i" > build/upm.i.list find src/ -name "*.i" > build/upm.i.list
#../doxy/doxyport build/upm.i.list \ #../doxy/doxyport build/upm.i.list \
# -s src/interfaces/,src/bacnetmstp,src/bmg160,src/bma250e,src/bmm150 \ # -s src/core/,src/bacnetmstp,src/bmg160,src/bma250e,src/bmm150 \
# -m doxy/samples.mapping.txt \ # -m doxy/samples.mapping.txt \
# -d build/src/ \ # -d build/src/ \
# --convert-protected-to-private \ # --convert-protected-to-private \

View File

@ -25,7 +25,7 @@ cd ${ROOT_DIR} && make -j8 -Cbuild 2> ${BUILD_LOGS_DIR}/build-doc.log
cd ${BUILD_DIR} && find ../src/ -name "*.i" > upm.i.list && \ cd ${BUILD_DIR} && find ../src/ -name "*.i" > upm.i.list && \
../doxy/doxyport/doxyport upm.i.list \ ../doxy/doxyport/doxyport upm.i.list \
--cmake ./compile_commands.json \ --cmake ./compile_commands.json \
--source ../src/interfaces/,../src/bacnetmstp,src \ --source ../src/core/,../src/bacnetmstp,src \
--destination src/ \ --destination src/ \
--convert-protected-to-private \ --convert-protected-to-private \
--output upm-java-files.txt \ --output upm-java-files.txt \

View File

@ -7,7 +7,8 @@ set (SWIG_CXX_DISABLE_WARNINGS -Wno-error
-Wno-delete-non-virtual-dtor -Wno-delete-non-virtual-dtor
-Wno-unused-function -Wno-unused-function
-Wno-maybe-uninitialized -Wno-maybe-uninitialized
-Wno-strict-aliasing) -Wno-strict-aliasing
-Wno-unused-but-set-variable)
# If building under android, make sure swig gets an ANDROID flag # If building under android, make sure swig gets an ANDROID flag
if (ANDROID) if (ANDROID)
@ -514,13 +515,13 @@ function(upm_swig_java)
swig_add_library (javaupm_${libname} LANGUAGE java SOURCES ${SWIG_CURRENT_DOT_I_FILE}) swig_add_library (javaupm_${libname} LANGUAGE java SOURCES ${SWIG_CURRENT_DOT_I_FILE})
endif () endif ()
# If the C++ target depends on C++ interfaces, make the JAVA target # If the C++ target depends on C++ core, make the JAVA target
# depend on the JAVA interfaces # depend on the JAVA core
if ("${_c_cxx_dependency_list}" MATCHES interfaces) if ("${_c_cxx_dependency_list}" MATCHES core)
add_dependencies(javaupm_${libname} javaupm_interfaces) add_dependencies(javaupm_${libname} javaupm_core)
# If this target depends on interfaces, include the java interfaces # If this target depends on core, include the java core
# target .jar file in the classpath, otherwise this variable will be empty # target .jar file in the classpath, otherwise this variable will be empty
set (INTERFACES_JAR_FILE ${CMAKE_BINARY_DIR}/src/interfaces/upm_interfaces.jar) set (INTERFACES_JAR_FILE ${CMAKE_BINARY_DIR}/src/core/upm_core.jar)
endif () endif ()
# For linker to report unresolved symbols. Note, there is currently no test # For linker to report unresolved symbols. Note, there is currently no test
# for linker flags (similar to compile files), so this is it for now. # for linker flags (similar to compile files), so this is it for now.
@ -564,10 +565,10 @@ function(upm_swig_java)
set (JAR $ENV{JAVA_HOME_NATIVE}/bin/jar) set (JAR $ENV{JAVA_HOME_NATIVE}/bin/jar)
endif () endif ()
# Only include the upm_interfaces.jar in the classpath for targets which # Only include the upm_core.jar in the classpath for targets which
# depend on the interfaces target. This fixes an issue where javac # depend on the core target. This fixes an issue where javac
# complains about an empty upm_interfaces.jar when trying to build a target # complains about an empty upm_core.jar when trying to build a target
# which does not depend on javaupm_interfaces. If not previously set, # which does not depend on javaupm_core. If not previously set,
# INTERFACES_JAR_FILE will be empty, and javac *should* not complain. # INTERFACES_JAR_FILE will be empty, and javac *should* not complain.
add_custom_command (TARGET javaupm_${libname} add_custom_command (TARGET javaupm_${libname}
POST_BUILD POST_BUILD
@ -632,12 +633,12 @@ if (BUILDSWIGNODE)
file (COPY ${CMAKE_SOURCE_DIR}/include DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/jsupm_${libname}) file (COPY ${CMAKE_SOURCE_DIR}/include DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/jsupm_${libname})
set (upm_LIB_INCLUDE_DIRS_GYP "'.',\n${upm_LIB_INCLUDE_DIRS_GYP}") set (upm_LIB_INCLUDE_DIRS_GYP "'.',\n${upm_LIB_INCLUDE_DIRS_GYP}")
# Utilities and interfaces # Utilities and core
file (COPY ${CMAKE_SOURCE_DIR}/src/utilities DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/jsupm_${libname}) file (COPY ${CMAKE_SOURCE_DIR}/src/utilities DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/jsupm_${libname})
file (COPY ${CMAKE_SOURCE_DIR}/src/interfaces DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/jsupm_${libname}) file (COPY ${CMAKE_SOURCE_DIR}/src/core DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/jsupm_${libname})
set (upm_LIB_SRCS_GYP "'utilities/upm_utilities.c',\n${upm_LIB_SRCS_GYP}") set (upm_LIB_SRCS_GYP "'utilities/upm_utilities.c',\n${upm_LIB_SRCS_GYP}")
set (upm_LIB_INCLUDE_DIRS_GYP "'utilities',\n${upm_LIB_INCLUDE_DIRS_GYP}") set (upm_LIB_INCLUDE_DIRS_GYP "'utilities',\n${upm_LIB_INCLUDE_DIRS_GYP}")
set (upm_LIB_INCLUDE_DIRS_GYP "'interfaces',\n${upm_LIB_INCLUDE_DIRS_GYP}") set (upm_LIB_INCLUDE_DIRS_GYP "'core',\n${upm_LIB_INCLUDE_DIRS_GYP}")
# Add readme, package.json for NPM and node-gyp config file # Add readme, package.json for NPM and node-gyp config file
configure_file (${PROJECT_SOURCE_DIR}/src/binding.gyp.in ${CMAKE_CURRENT_BINARY_DIR}/jsupm_${libname}/binding.gyp @ONLY) configure_file (${PROJECT_SOURCE_DIR}/src/binding.gyp.in ${CMAKE_CURRENT_BINARY_DIR}/jsupm_${libname}/binding.gyp @ONLY)
@ -789,6 +790,22 @@ function(upm_module_init)
# Create the target library from src/hdrs # Create the target library from src/hdrs
add_library (${libname} SHARED ${module_src} ${module_hpp}) add_library (${libname} SHARED ${module_src} ${module_hpp})
# Add a upm_library_globals to all C++ libraries
if (NOT IS_C_LIBRARY)
# Share some GLOBAL (per library) info with the library (UPM version and library base name)
target_compile_definitions(${libname}
PRIVATE -DUPM_LIBRARY_BASE_NAME=${libname}
-DUPM_VERSION_STRING=${upm_VERSION_STRING})
# Add a source file which contains the shared GLOBAL info
target_sources(${libname}
PRIVATE ${CMAKE_SOURCE_DIR}/src/upm_library_globals.cxx
${CMAKE_SOURCE_DIR}/src/upm_library_globals.hpp)
# This source depends on libdl (uses dladdr), make sure to link this
target_link_libraries (${libname} ${CMAKE_DL_LIBS})
endif()
# Specify the current source directory as an INTERFACE include dir. # Specify the current source directory as an INTERFACE include dir.
# This allows for transitive header dependencies via target_link_libraries # This allows for transitive header dependencies via target_link_libraries
target_include_directories(${libname} INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}) target_include_directories(${libname} INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
@ -855,8 +872,14 @@ function(upm_module_init)
DESTINATION include/upm DESTINATION include/upm
COMPONENT ${CMAKE_PROJECT_NAME}-dev) COMPONENT ${CMAKE_PROJECT_NAME}-dev)
# Install JSON library descriptor files into datadir (if they exist) # Handle build/install for JSON library descriptor file
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${basename}.json") if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${basename}.json")
# Copy JSON library descriptor file to binary directory (for testing/running from build dir)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/${basename}.json"
"${CMAKE_CURRENT_BINARY_DIR}/${basename}.json"
COPYONLY)
# Install JSON library descriptor files into datadir (if they exist)
install (FILES "${CMAKE_CURRENT_SOURCE_DIR}/${basename}.json" install (FILES "${CMAKE_CURRENT_SOURCE_DIR}/${basename}.json"
DESTINATION ${CMAKE_INSTALL_DATADIR}/upm/${basename} DESTINATION ${CMAKE_INSTALL_DATADIR}/upm/${basename}
COMPONENT ${CMAKE_PROJECT_NAME}) COMPONENT ${CMAKE_PROJECT_NAME})
@ -908,10 +931,10 @@ if (NOT "${MODULE_LIST}" MATCHES ";utilities;")
set(MODULE_LIST "utilities;${MODULE_LIST}") set(MODULE_LIST "utilities;${MODULE_LIST}")
endif() endif()
# If the module list does NOT include the interfaces directory, prepend it since # If the module list does NOT include the core directory, prepend it since
# some sensor library targets depend on interfaces # some sensor library targets depend on core
if (NOT "${MODULE_LIST}" MATCHES ";interfaces;") if (NOT "${MODULE_LIST}" MATCHES ";core;")
set(MODULE_LIST "interfaces;${MODULE_LIST}") set(MODULE_LIST "core;${MODULE_LIST}")
endif() endif()
# Iterate over each directory in MODULE_LIST # Iterate over each directory in MODULE_LIST

View File

@ -2,7 +2,7 @@ set (libname "ads1x15")
set (libdescription "Texas Instruments I2C ADC Library") set (libdescription "Texas Instruments I2C ADC Library")
set (module_src ${libname}.cxx ads1115.cxx ads1015.cxx) set (module_src ${libname}.cxx ads1115.cxx ads1015.cxx)
set (module_hpp ${libname}.hpp ads1115.hpp ads1015.hpp) set (module_hpp ${libname}.hpp ads1115.hpp ads1015.hpp)
upm_module_init(interfaces mraa) upm_module_init(core mraa)
compiler_flag_supported(CXX is_supported -Wno-overloaded-virtual) compiler_flag_supported(CXX is_supported -Wno-overloaded-virtual)
if (is_supported) if (is_supported)
target_compile_options(${libname} PUBLIC -Wno-overloaded-virtual) target_compile_options(${libname} PUBLIC -Wno-overloaded-virtual)

View File

@ -40,7 +40,7 @@ ADS1015::ADS1015(int bus, uint8_t address, float vref) : ADS1X15(bus, address) {
m_bitShift = 4; m_bitShift = 4;
ADS1X15::getCurrentConfig(); ADS1X15::getCurrentConfig();
if (vref < 0.0 || vref > 6.144) if (vref < 0.0 || vref > 6.144)
UPM_THROW("vref out of range"); throw std::runtime_error(std::string(__FUNCTION__) + " : vref out of range");
else if (vref > 4.096) else if (vref > 4.096)
setGain(GAIN_TWOTHIRDS); setGain(GAIN_TWOTHIRDS);
else if (vref > 2.048) else if (vref > 2.048)
@ -57,12 +57,6 @@ ADS1015::ADS1015(int bus, uint8_t address, float vref) : ADS1X15(bus, address) {
ADS1015::~ADS1015(){}; ADS1015::~ADS1015(){};
const char*
ADS1015::getModuleName() {
return m_name.c_str();
}
unsigned int unsigned int
ADS1015::getNumInputs() { ADS1015::getNumInputs() {
return 4; return 4;
@ -73,7 +67,7 @@ ADS1015::getResolutionInBits() {
return 12; return 12;
} }
uint16_t unsigned int
ADS1015::getRawValue(unsigned int input) { ADS1015::getRawValue(unsigned int input) {
ADS1X15::ADSMUXMODE mode = getMuxMode(input); ADS1X15::ADSMUXMODE mode = getMuxMode(input);
updateConfigRegister((m_config_reg & ~ADS1X15_MUX_MASK) | mode, true); updateConfigRegister((m_config_reg & ~ADS1X15_MUX_MASK) | mode, true);
@ -163,7 +157,7 @@ ADS1015::getMuxMode(unsigned int input) {
case 3: case 3:
return SINGLE_3; return SINGLE_3;
default: default:
UPM_THROW("Invalid input"); throw std::runtime_error(std::string(__FUNCTION__) + " : Invalid input");
} }
} }

View File

@ -26,7 +26,7 @@
#pragma once #pragma once
#include "ads1x15.hpp" #include "ads1x15.hpp"
#include "interfaces/iADC.hpp" #include "iADC.hpp"
#define ADS1015_VREF 2.048 #define ADS1015_VREF 2.048
@ -85,7 +85,7 @@ namespace upm {
* @snippet ads1x15-ads1015.cxx Interesting * @snippet ads1x15-ads1015.cxx Interesting
* @snippet ads1x15-adc-sensor.cxx Interesting * @snippet ads1x15-adc-sensor.cxx Interesting
*/ */
class ADS1015 : public ADS1X15, public IADC { class ADS1015 : public ADS1X15, public iADC {
public: public:
@ -130,6 +130,9 @@ namespace upm {
*/ */
~ADS1015 (); ~ADS1015 ();
virtual std::string Name() {return "ADS1015";}
virtual std::string Description() {return "12-bit ADC with integrated MUX, PGA, comparator, oscillator, and reference";}
/** /**
* Sets the sample rate of the device. This function * Sets the sample rate of the device. This function
* needs to be overridden in subclasses as the ADS1115 and * needs to be overridden in subclasses as the ADS1115 and
@ -151,7 +154,7 @@ namespace upm {
* *
* @return current conversion value * @return current conversion value
*/ */
uint16_t getRawValue(unsigned int input); unsigned int getRawValue(unsigned int input);
/** /**
* Read current voltage for current single ended analogue input * Read current voltage for current single ended analogue input
@ -167,14 +170,6 @@ namespace upm {
*/ */
unsigned int getResolutionInBits(); unsigned int getResolutionInBits();
/**
* Returns module name
*
* @return modulename as const char*
*/
const char* getModuleName();
protected: protected:
float getMultiplier(void); float getMultiplier(void);
void setDelay(void); void setDelay(void);

View File

@ -1,28 +1,12 @@
%include "../common_top.i" %include "../common_top.i"
%include "iADC.i"
/* BEGIN Java syntax ------------------------------------------------------- */ /* BEGIN Java syntax ------------------------------------------------------- */
#ifdef SWIGJAVA #ifdef SWIGJAVA
%import "../interfaces/javaupm_iADC.i"
%typemap(javaimports) SWIGTYPE %{import upm_interfaces.*;%}
JAVA_JNI_LOADLIBRARY(javaupm_ads1x15) JAVA_JNI_LOADLIBRARY(javaupm_ads1x15)
#endif #endif
/* END Java syntax */ /* END Java syntax */
/* BEGIN Javascript syntax ------------------------------------------------- */
#ifdef SWIGJAVASCRIPT
%include "iModuleStatus.hpp"
%include "iADC.hpp"
#endif
/* END Javascript syntax */
/* BEGIN Python syntax ----------------------------------------------------- */
#ifdef SWIGPYTHON
%include "iModuleStatus.hpp"
%include "iADC.hpp"
#endif
/* END Python syntax */
/* BEGIN Common SWIG syntax ------------------------------------------------- */ /* BEGIN Common SWIG syntax ------------------------------------------------- */
%{ %{
#include "ads1x15.hpp" #include "ads1x15.hpp"

View File

@ -5,5 +5,5 @@ upm_mixed_module_init (NAME bmp280
CPP_HDR bmp280.hpp bme280.hpp CPP_HDR bmp280.hpp bme280.hpp
CPP_SRC bmp280.cxx bme280.cxx CPP_SRC bmp280.cxx bme280.cxx
CPP_WRAPS_C CPP_WRAPS_C
REQUIRES mraa interfaces utilities-c) REQUIRES mraa core utilities-c)
target_link_libraries(${libnamec} m) target_link_libraries(${libnamec} m)

View File

@ -54,3 +54,17 @@ void BME280::setOversampleRateHumidity(BME280_OSRS_H_T rate)
{ {
bmp280_set_oversample_rate_humidity(m_bmp280, rate); bmp280_set_oversample_rate_humidity(m_bmp280, rate);
} }
std::map<std::string, float> BME280::HumidityForSources(std::vector<std::string> sources)
{
std::map<std::string, float> ret;
if (std::find(sources.begin(), sources.end(), "humidity") != sources.end())
{
update();
ret["humidity"] = getHumidity();
}
return ret;
}

View File

@ -26,7 +26,7 @@
#pragma once #pragma once
#include <string> #include <string>
#include "interfaces/iHumiditySensor.hpp" #include "iHumiditySensor.hpp"
#include "bmp280.hpp" #include "bmp280.hpp"
@ -57,7 +57,7 @@ namespace upm {
* @snippet bmp280-bme280.cxx Interesting * @snippet bmp280-bme280.cxx Interesting
*/ */
class BME280 : public BMP280, public IHumiditySensor { class BME280 : public virtual BMP280, public virtual iHumiditySensor {
public: public:
/** /**
@ -88,6 +88,12 @@ namespace upm {
*/ */
virtual ~BME280(); virtual ~BME280();
virtual std::string Name () {return "BME280";}
virtual std::string Description () {return "Digital absolute pressure sensor with humidity";}
/* Provide an implementation of a method to get sensor values by source */
virtual std::map<std::string, float> HumidityForSources(std::vector<std::string> sources);
/** /**
* Return the current measured relative humidity. update() * Return the current measured relative humidity. update()
* must have been called prior to calling this method. If the * must have been called prior to calling this method. If the
@ -108,12 +114,6 @@ namespace upm {
*/ */
void setOversampleRateHumidity(BME280_OSRS_H_T rate); void setOversampleRateHumidity(BME280_OSRS_H_T rate);
// Interface support
const char *getModuleName()
{
return "BME280";
};
int getHumidityRelative() int getHumidityRelative()
{ {
return int(getHumidity()); return int(getHumidity());

View File

@ -48,6 +48,8 @@ BMP280::BMP280(int bus, int addr, int cs) :
if (!m_bmp280) if (!m_bmp280)
throw std::runtime_error(string(__FUNCTION__) throw std::runtime_error(string(__FUNCTION__)
+ ": bmp280_init() failed"); + ": bmp280_init() failed");
AddSource("temperature", "C");
AddSource("pressure", "Pa");
} }
BMP280::~BMP280() BMP280::~BMP280()
@ -150,3 +152,29 @@ void BMP280::setUsageMode(BMP280_USAGE_MODE_T mode)
bmp280_set_usage_mode(m_bmp280, mode); bmp280_set_usage_mode(m_bmp280, mode);
} }
std::map<std::string, float> BMP280::TemperatureForSources(std::vector<std::string> sources)
{
std::map<std::string, float> ret;
if (std::find(sources.begin(), sources.end(), "temperature") != sources.end())
{
update();
ret["temperature"] = getTemperature(false);
}
return ret;
}
std::map<std::string, float> BMP280::PressureForSources(std::vector<std::string> sources)
{
std::map<std::string, float> ret;
if (std::find(sources.begin(), sources.end(), "pressure") != sources.end())
{
update();
ret["pressure"] = getPressure();
}
return ret;
}

View File

@ -28,8 +28,8 @@
#include <string> #include <string>
#include "bmp280.h" #include "bmp280.h"
#include "interfaces/iPressureSensor.hpp" #include "iPressureSensor.hpp"
#include "interfaces/iTemperatureSensor.hpp" #include "iTemperatureSensor.hpp"
namespace upm { namespace upm {
@ -67,7 +67,7 @@ namespace upm {
* @snippet bmp280.cxx Interesting * @snippet bmp280.cxx Interesting
*/ */
class BMP280 : public ITemperatureSensor, public IPressureSensor { class BMP280 : public virtual iTemperatureSensor, public virtual iPressureSensor {
public: public:
/** /**
@ -93,6 +93,16 @@ namespace upm {
BMP280(int bus=BMP280_DEFAULT_I2C_BUS, int addr=BMP280_DEFAULT_ADDR, BMP280(int bus=BMP280_DEFAULT_I2C_BUS, int addr=BMP280_DEFAULT_ADDR,
int cs=-1); int cs=-1);
virtual std::string Name () {return "BMP280";}
virtual std::string Description () {return "Digital absolute pressure sensor";}
/* Provide an implementation of a method to get sensor values by source */
virtual std::map<std::string, float> TemperatureForSources(std::vector<std::string> sources);
/* Provide an implementation of a method to get sensor values by source */
virtual std::map<std::string, float> PressureForSources(std::vector<std::string> sources);
/** /**
* BMP280 Destructor. * BMP280 Destructor.
*/ */
@ -217,12 +227,6 @@ namespace upm {
void setMeasureMode(BMP280_MODES_T mode); void setMeasureMode(BMP280_MODES_T mode);
// Interface support
const char *getModuleName()
{
return "BMP280";
};
int getTemperatureCelsius() int getTemperatureCelsius()
{ {
return int(getTemperature(false)); return int(getTemperature(false));

View File

@ -1,40 +1,17 @@
%include "../common_top.i" %include "../common_top.i"
%include "iHumiditySensor.i"
%include "iTemperatureSensor.i"
%include "iPressureSensor.i"
/* BEGIN Java syntax ------------------------------------------------------- */ /* BEGIN Java syntax ------------------------------------------------------- */
#ifdef SWIGJAVA #ifdef SWIGJAVA
%import "../interfaces/javaupm_iTemperatureSensor.i"
%import "../interfaces/javaupm_iHumiditySensor.i"
%import "../interfaces/javaupm_iPressureSensor.i"
%include "arrays_java.i"; %include "arrays_java.i";
%include "../java_buffer.i" %include "../java_buffer.i"
%typemap(javaimports) SWIGTYPE %{
import upm_interfaces.*;
%}
JAVA_JNI_LOADLIBRARY(javaupm_bmp280) JAVA_JNI_LOADLIBRARY(javaupm_bmp280)
#endif #endif
/* END Java syntax */ /* END Java syntax */
/* BEGIN Javascript syntax ------------------------------------------------- */
#ifdef SWIGJAVASCRIPT
%include "iModuleStatus.hpp"
%include "iTemperatureSensor.hpp"
%include "iPressureSensor.hpp"
%include "iHumiditySensor.hpp"
#endif
/* END Javascript syntax */
/* BEGIN Python syntax ----------------------------------------------------- */
#ifdef SWIGPYTHON
%include "iModuleStatus.hpp"
%include "iTemperatureSensor.hpp"
%include "iPressureSensor.hpp"
%include "iHumiditySensor.hpp"
#endif
/* END Python syntax */
/* BEGIN Common SWIG syntax ------------------------------------------------- */ /* BEGIN Common SWIG syntax ------------------------------------------------- */
%{ %{
#include "bmp280_regs.h" #include "bmp280_regs.h"

View File

@ -6,5 +6,5 @@ upm_mixed_module_init (NAME bmpx8x
CPP_SRC bmpx8x.cxx CPP_SRC bmpx8x.cxx
FTI_SRC bmpx8x_fti.c FTI_SRC bmpx8x_fti.c
CPP_WRAPS_C CPP_WRAPS_C
REQUIRES mraa interfaces utilities-c) REQUIRES mraa core utilities-c)
target_link_libraries(${libnamec} m) target_link_libraries(${libnamec} m)

View File

@ -44,6 +44,9 @@ BMPX8X::BMPX8X (int bus, int addr) :
if (!m_bmpx8x) if (!m_bmpx8x)
throw std::runtime_error(string(__FUNCTION__) throw std::runtime_error(string(__FUNCTION__)
+ ": bmpx8x_init() failed"); + ": bmpx8x_init() failed");
AddSource("temperature", "C");
AddSource("pressure", "Pa");
} }
BMPX8X::~BMPX8X() BMPX8X::~BMPX8X()
@ -118,3 +121,24 @@ float BMPX8X::getAltitude(int sealevelPressure)
{ {
return bmpx8x_get_altitude(m_bmpx8x, sealevelPressure); return bmpx8x_get_altitude(m_bmpx8x, sealevelPressure);
} }
std::map<std::string, float> BMPX8X::TemperatureForSources(std::vector<std::string> sources)
{
std::map<std::string, float> ret;
if (std::find(sources.begin(), sources.end(), "temperature") != sources.end())
ret["temperature"] = getTemperatureCelsius();
return ret;
}
std::map<std::string, float> BMPX8X::PressureForSources(std::vector<std::string> sources)
{
std::map<std::string, float> ret;
if (std::find(sources.begin(), sources.end(), "pressure") != sources.end())
ret["pressure"] = getPressurePa();
return ret;
}

View File

@ -32,8 +32,8 @@
#include "bmpx8x.h" #include "bmpx8x.h"
#include "interfaces/iPressureSensor.hpp" #include "iPressureSensor.hpp"
#include "interfaces/iTemperatureSensor.hpp" #include "iTemperatureSensor.hpp"
namespace upm { namespace upm {
@ -68,7 +68,7 @@ namespace upm {
* @snippet bmpx8x.cxx Interesting * @snippet bmpx8x.cxx Interesting
*/ */
class BMPX8X : public IPressureSensor, public ITemperatureSensor { class BMPX8X : public iPressureSensor, public iTemperatureSensor {
public: public:
/** /**
* Instantiates a BMPX8X object * Instantiates a BMPX8X object
@ -85,6 +85,18 @@ namespace upm {
*/ */
virtual ~BMPX8X(); virtual ~BMPX8X();
/** Return the name of this device */
virtual std::string Name () {return "BMPX8X";}
/** Return the description of this device */
virtual std::string Description () {return "Atmospheric pressure sensor";}
/* Provide an implementation of a method to get sensor values by source */
virtual std::map<std::string, float> TemperatureForSources(std::vector<std::string> sources);
/* Provide an implementation of a method to get sensor values by source */
virtual std::map<std::string, float> PressureForSources(std::vector<std::string> sources);
/** /**
* Query the device and update the internal state. This * Query the device and update the internal state. This
* method must be called before calling getPressure(), * method must be called before calling getPressure(),
@ -178,7 +190,7 @@ namespace upm {
/** /**
* Return latest calculated temperature value in Celsius. See * Return latest calculated temperature value in Celsius. See
* ITemperatureSensor. * iTemperatureSensor.
* *
* @return The current temperature in Celsius. * @return The current temperature in Celsius.
*/ */
@ -190,7 +202,7 @@ namespace upm {
/** /**
* Return latest calculated pressure value in Pascals. See * Return latest calculated pressure value in Pascals. See
* IPressureSensor. * iPressureSensor.
* *
* @return The current pressure in Pascals. * @return The current pressure in Pascals.
*/ */
@ -200,16 +212,6 @@ namespace upm {
return getPressure(); return getPressure();
} }
/**
* Returns the name of module.
*
* @return The name of the module.
*/
const char *getModuleName()
{
return "BMPX8X";
}
protected: protected:
// our underlying C context. // our underlying C context.
bmpx8x_context m_bmpx8x; bmpx8x_context m_bmpx8x;

View File

@ -1,23 +1,13 @@
%include "../common_top.i" %include "../common_top.i"
%include "iPressureSensor.i"
%include "iTemperatureSensor.i"
/* BEGIN Java syntax ------------------------------------------------------- */ /* BEGIN Java syntax ------------------------------------------------------- */
#ifdef SWIGJAVA #ifdef SWIGJAVA
%import "../interfaces/javaupm_iPressureSensor.i"
%typemap(javaimports) SWIGTYPE %{import upm_interfaces.*;%}
%import "../interfaces/javaupm_iTemperatureSensor.i"
JAVA_JNI_LOADLIBRARY(javaupm_bmpx8x) JAVA_JNI_LOADLIBRARY(javaupm_bmpx8x)
#endif #endif
/* END Java syntax */ /* END Java syntax */
/* BEGIN Python syntax ----------------------------------------------------- */
#ifdef SWIGPYTHON
%include "iModuleStatus.hpp"
%include "iTemperatureSensor.hpp"
%include "iPressureSensor.hpp"
#endif
/* END Python syntax */
/* BEGIN Common SWIG syntax ------------------------------------------------- */ /* BEGIN Common SWIG syntax ------------------------------------------------- */
%{ %{
#include "bmpx8x_defs.h" #include "bmpx8x_defs.h"

View File

@ -2,15 +2,14 @@
%include "cpointer.i" %include "cpointer.i"
%include "stdint.i" %include "stdint.i"
%include "typemaps.i" %include "typemaps.i"
#if SWIGJAVA
%include "java_exceptions.i"
#else
%include "upm_exception.i" %include "upm_exception.i"
#endif
/* Import additional SWIG helps (not exposed in wrapper) */ %import "_upm.i"
%import _upm.i
%{
#include "version.hpp"
%}
%include "version.hpp"
%apply int { speed_t }; %apply int { speed_t };
%apply int { mraa_result_t }; %apply int { mraa_result_t };
@ -35,7 +34,6 @@ void cleanUp()
{;} {;}
} }
%} %}
void cleanUp();
#endif #endif
#if (SWIGJAVA) #if (SWIGJAVA)
@ -43,7 +41,7 @@ void cleanUp();
%typemap(jstype) jobject runnable "java.lang.Runnable" %typemap(jstype) jobject runnable "java.lang.Runnable"
#endif #endif
// Disable nested struct warnings /* Disable nested struct warnings */
#pragma SWIG nowarn=312,325 #pragma SWIG nowarn=312,325
#if SWIGPYTHON #if SWIGPYTHON

11
src/core/.core.i Normal file
View File

@ -0,0 +1,11 @@
/* Any java package including an interface .i requires an import for
upm_core java package in the JNI source */
%pragma(java) jniclassimports=%{
import upm_core.*;
%}
/* Add the global UPM methods to all wrappers */
%{
#include "../upm_library_globals.hpp"
%}
%include "../upm_library_globals.hpp"

13
src/core/.iADC.i Normal file
View File

@ -0,0 +1,13 @@
%include ".iSensorType.i"
#if (SWIGJAVA)
#if SWIG_VERSION >= 0x030009
%include "swiginterface.i"
%interface_impl(upm::iADC);
#endif
#endif
%{
#include "iADC.hpp"
%}
%include "iADC.hpp"

14
src/core/.iActuatorType.i Normal file
View File

@ -0,0 +1,14 @@
%include ".iUpmObject.i"
#if (SWIGJAVA)
#if SWIG_VERSION >= 0x030009
%include "swiginterface.i"
%interface(upm::iActuatorType);
#endif
%ignore upm::iUpmObject::JsonDefinition;
#endif
%{
#include "iActuatorType.hpp"
%}
%include "iActuatorType.hpp"

13
src/core/.iCO2Sensor.i Normal file
View File

@ -0,0 +1,13 @@
%include ".iSensorType.i"
#if (SWIGJAVA)
#if SWIG_VERSION >= 0x030009
%include "swiginterface.i"
%interface_impl(upm::iCO2Sensor);
#endif
#endif
%{
#include "iCO2Sensor.hpp"
%}
%include "iCO2Sensor.hpp"

View File

@ -0,0 +1,29 @@
%include ".iSensorType.i"
#if (SWIGJAVA)
#if SWIG_VERSION >= 0x030009
%include "swiginterface.i"
%interface_impl(upm::iHumiditySensor);
#endif
#endif
%{
#include "iHumiditySensor.hpp"
%}
%include "iHumiditySensor.hpp"
/* FOR JAVASCRIPT ONLY!
If there's a better way to allow multiple inheritance w/javascript, the
following macro definitions can go away.
usage: INHERIT_IHUMIDITYSENSOR(upm::MySensorClass)
*/
%define INHERIT_IHUMIDITYSENSOR(YourClassName)
%extend YourClassName {
/*using upm::iSensorType::JsonDefinition;*/
/*using upm::iSensorType::JsonValues;*/
using upm::iHumiditySensor::JsonHumidity;
using upm::iHumiditySensor::HumidityAll;
using upm::iHumiditySensor::HumidityForSources;
}
%enddef

View File

@ -0,0 +1,13 @@
%include ".iSensorType.i"
#if (SWIGJAVA)
#if SWIG_VERSION >= 0x030009
%include "swiginterface.i"
%interface_impl(upm::iLightController);
#endif
#endif
%{
#include "iLightController.hpp"
%}
%include "iLightController.hpp"

13
src/core/.iLightSensor.i Normal file
View File

@ -0,0 +1,13 @@
%include ".iSensorType.i"
#if (SWIGJAVA)
#if SWIG_VERSION >= 0x030009
%include "swiginterface.i"
%interface_impl(upm::iLightSensor);
#endif
#endif
%{
#include "iLightSensor.hpp"
%}
%include "iLightSensor.hpp"

View File

@ -0,0 +1,13 @@
%include ".iSensorType.i"
#if (SWIGJAVA)
#if SWIG_VERSION >= 0x030009
%include "swiginterface.i"
%interface_impl(upm::iMoistureSensor);
#endif
#endif
%{
#include "iMoistureSensor.hpp"
%}
%include "iMoistureSensor.hpp"

37
src/core/.iMraa.i Normal file
View File

@ -0,0 +1,37 @@
%include ".iUpmObject.i"
#if (SWIGJAVA)
#if SWIG_VERSION >= 0x030009
%include "swiginterface.i"
%interface_impl(upm::iMraa);
#endif
#endif
%{
#include "iMraa.hpp"
%}
%include "iMraa.hpp"
typedef std::vector<mraa::Aio*> vec_aio;
%template(vec_aio) std::vector<mraa::Aio*>;
typedef std::vector<mraa::Gpio*> vec_gpio;
%template(vec_gpio) std::vector<mraa::Gpio*>;
typedef std::vector<mraa::I2c*> vec_i2c;
%template(vec_i2c) std::vector<mraa::I2c*>;
typedef std::vector<mraa::Iio*> vec_iio;
%template(vec_iio) std::vector<mraa::Iio*>;
typedef std::vector<mraa::Pwm*> vec_pwm;
%template(vec_pwm) std::vector<mraa::Pwm*>;
typedef std::vector<mraa::Spi*> vec_spi;
%template(vec_spi) std::vector<mraa::Spi*>;
typedef std::vector<mraa::Uart*> vec_uart;
%template(vec_uart) std::vector<mraa::Uart*>;
typedef std::vector<mraa::UartOW*> vec_uartow;
%template(vec_uartow) std::vector<mraa::UartOW*>;

View File

@ -0,0 +1,13 @@
%include ".iSensorType.i"
#if (SWIGJAVA)
#if SWIG_VERSION >= 0x030009
%include "swiginterface.i"
%interface_impl(upm::iPressureSensor);
#endif
#endif
%{
#include "iPressureSensor.hpp"
%}
%include "iPressureSensor.hpp"

14
src/core/.iSensorType.i Normal file
View File

@ -0,0 +1,14 @@
%include ".iUpmObject.i"
#if (SWIGJAVA)
#if SWIG_VERSION >= 0x030009
%include "swiginterface.i"
%interface(upm::iSensorType);
#endif
%ignore upm::iUpmObject::JsonDefinition;
#endif
%{
#include "iSensorType.hpp"
%}
%include "iSensorType.hpp"

View File

@ -0,0 +1,13 @@
%include ".iActuatorType.i"
#if (SWIGJAVA)
#if SWIG_VERSION >= 0x030009
%include "swiginterface.i"
%interface_impl(upm::iServoActuator);
#endif
#endif
%{
#include "iServoActuator.hpp"
%}
%include "iServoActuator.hpp"

View File

@ -0,0 +1,28 @@
%include ".iSensorType.i"
#if (SWIGJAVA)
#if SWIG_VERSION >= 0x030009
%include "swiginterface.i"
%interface_impl(upm::iTemperatureSensor);
#endif
#endif
%{
#include "iTemperatureSensor.hpp"
%}
%include "iTemperatureSensor.hpp"
/* FOR JAVASCRIPT ONLY!
If there's a better way to allow multiple inheritance w/javascript, the
following macro definitions can go away.
usage: INHERIT_ITEMPERATURESENSOR(upm::MySensorClass)
*/
%define INHERIT_ITEMPERATURESENSOR(YourClassName)
%extend YourClassName {
/*using upm::iSensorType::JsonDefinition;*/
/*using upm::iSensorType::JsonValues;*/
using upm::iTemperatureSensor::JsonTemperature;
using upm::iTemperatureSensor::TemperatureAll;
using upm::iTemperatureSensor::TemperatureForSources;
}
%enddef

68
src/core/.iUpmObject.i Normal file
View File

@ -0,0 +1,68 @@
%include "../common_top.i"
%include "std_vector.i"
%include "std_map.i"
/* Using the JAVA class types outside the core package requires
getCPtr to be public, modify that here */
%typemap(javabody) SWIGTYPE %{
private long swigCPtr;
protected boolean swigCMemOwn;
public $javaclassname(long cPtr, boolean cMemoryOwn) {
swigCMemOwn = cMemoryOwn;
swigCPtr = cPtr;
}
public static long getCPtr($javaclassname obj) {
return (obj == null) ? 0 : obj.swigCPtr;
}
%}
typedef std::vector<std::string> vec_str;
%template(vec_str) std::vector<std::string>;
typedef std::map<std::string, float> map_str_float;
%template(map_str_float) std::map<std::string, float>;
typedef std::map<std::string, std::string> map_str_str;
%template(map_str_str) std::map<std::string, std::string>;
/* Any java package including an interface .i will import the
upm_core java package in the JAVA source */
%typemap(javaimports) SWIGTYPE %{
import upm_core.*;
%}
#if (SWIGJAVA)
#if SWIG_VERSION >= 0x030009
%include "swiginterface.i"
%interface_impl(upm::iUpmObject);
#endif
#endif
%{
#include "iUpmObject.hpp"
%}
%include "iUpmObject.hpp"
/* FOR JAVASCRIPT ONLY!
If there's a better way to allow multiple inheritance w/javascript, the
following macro definitions can go away.
usage: INHERIT_IUPMOBJECT(upm::MySensorClass)
*/
%define INHERIT_IUPMOBJECT(YourClassName)
%extend YourClassName {
using upm::iUpmObject::Description;
using upm::iUpmObject::JsonDefinition;
using upm::iUpmObject::Name;
using upm::iUpmObject::initFromJsonLibDesc;
using upm::iUpmObject::DataDirectory;
using upm::iUpmObject::DerivedClassName;
using upm::iUpmObject::LibraryAbsolutePath;
using upm::iUpmObject::LibraryBaseName;
using upm::iUpmObject::LibraryJsonFilename;
using upm::iUpmObject::LibraryJsonRaw;
using upm::iUpmObject::LibraryVersion;
}
%enddef

38
src/core/CMakeLists.txt Normal file
View File

@ -0,0 +1,38 @@
# Create a source file for constants
configure_file (iUpmObject_generated.cxx.in iUpmObject_generated.cxx @ONLY)
upm_mixed_module_init (NAME core
DESCRIPTION "CXX Interface Library"
CPP_HDR iADC.hpp
iCO2Sensor.hpp
iHumiditySensor.hpp
iLightController.hpp
iLightSensor.hpp
iMoistureSensor.hpp
iMraa.hpp
iPressureSensor.hpp
iSensorType.hpp
iActuatorType.hpp
iServoActuator.hpp
iTemperatureSensor.hpp
iUpmObject.hpp
CPP_SRC "${CMAKE_CURRENT_BINARY_DIR}/iUpmObject_generated.cxx"
iADC.cxx
iCO2Sensor.cxx
iHumiditySensor.cxx
iLightController.cxx
iLightSensor.cxx
iMoistureSensor.cxx
iMraa.cxx
iPressureSensor.cxx
iSensorType.cxx
iActuatorType.cxx
iServoActuator.cxx
iTemperatureSensor.cxx
iUpmObject.cxx
REQUIRES mraa utilities-c)
# Add a PUBLIC include directory to the CMAKE src dir
target_include_directories (core
PUBLIC ${CMAKE_SOURCE_DIR}/src
${CMAKE_BINARY_DIR}/src)

25
src/core/core.i Normal file
View File

@ -0,0 +1,25 @@
%include "../common_top.i"
#ifdef SWIGJAVA
JAVA_JNI_LOADLIBRARY(javaupm_core)
/* JAVA has multiple define collisions once these get flattened out,
so ignore the base implementation */
%ignore "upm::iUpmObject::JsonDefinition";
#endif
%include ".iUpmObject.i"
%include ".iSensorType.i"
%include ".iTemperatureSensor.i"
%include ".iHumiditySensor.i"
%include ".iMraa.i"
%include ".iADC.i"
%include ".iPressureSensor.i"
%include ".iMoistureSensor.i"
%include ".iLightSensor.i"
%include ".iLightController.i"
%include ".iCO2Sensor.i"
/* Actuator types */
%include ".iActuatorType.i"
%include ".iServoActuator.i"

3
src/core/iADC.cxx Normal file
View File

@ -0,0 +1,3 @@
#include "iADC.hpp"
upm::iADC::~iADC () {}

View File

@ -22,26 +22,24 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#pragma once #pragma once
#include <stdint.h> #include <stdint.h>
#include "iModuleStatus.hpp"
#include "iSensorType.hpp"
namespace upm namespace upm
{ {
/** /**
* @brief Interface for ADC Sensors * @brief Interface for ADC Sensors
*/ */
class iADC : public virtual iSensorType
class IADC : virtual public IModuleStatus
{ {
public: public:
virtual unsigned int getResolutionInBits() = 0; virtual unsigned int getResolutionInBits() = 0;
virtual unsigned int getNumInputs() = 0; virtual unsigned int getNumInputs() = 0;
virtual uint16_t getRawValue(unsigned int input) = 0; virtual unsigned int getRawValue(unsigned int input) = 0;
virtual float getVoltage(unsigned int input) = 0; virtual float getVoltage(unsigned int input) = 0;
virtual ~IADC() {} virtual ~iADC();
}; };
} }

2
src/core/iADC.i Normal file
View File

@ -0,0 +1,2 @@
%import (module="upm.pyupm_core") ".iADC.i"
%include ".core.i"

187
src/core/iActuatorType.cxx Normal file
View File

@ -0,0 +1,187 @@
#include "iActuatorType.hpp"
#include "external/json/json.hpp"
using namespace upm;
namespace upm {
template <typename T>
void to_json(nlohmann::json& j, const ActuatorSink<T> &p) {
j = nlohmann::json{{"unit", p.unit}, {"min", p.min}, {"max", p.max}, {"accuracy", p.accuracy}};
}
template <typename T>
void from_json(const nlohmann::json& j, ActuatorSink<T>& p) {
p.unit = j.at("unit").get<std::string>();
p.min = j.at("min").get<T>();
p.max = j.at("max").get<T>();
p.accuracy = j.at("accuracy").get<T>();
}
template <typename T>
void to_json(nlohmann::json& j, const ActuatorCommand<T> &p) {
j = nlohmann::json{{"command", p.command}, {"unit", p.unit}, {"value", p.value}};
}
template <typename T>
void from_json(const nlohmann::json& j, ActuatorCommand<T>& p) {
p.command= j.at("command").get<std::string>();
p.unit = j.at("unit").get<std::string>();
p.value = j.at("value").get<T>();
}
template <class T>
std::ostream &operator<<(std::ostream& os, const ActuatorCommand<T>& ac)
{
nlohmann::json jcmd = ac;
return os << jcmd;
}
}
iActuatorType::iActuatorType()
{
DEBUG_MSG("XXX");
}
iActuatorType::~iActuatorType()
{
DEBUG_MSG("XXX");
}
std::string iActuatorType::LibraryJsonCommands()
{
std::stringstream ss;
ss << this->LibraryJson()["Sensor Class"][Name()]["Commands"] << std::endl;
return ss.str();
}
void iActuatorType::AddCommand (std::string source, std::string unit)
{
/* Add the source:unit to the map */
_sinks_2_units[source] = unit;
/* Each call regens the sinks and units vectors */
_sinks.clear();
_units.clear();
for(std::map<std::string, std::string>::const_iterator it = CommandMap().begin();
it != CommandMap().end(); ++it)
{
_sinks.push_back(it->first);
_units.push_back(it->second);
}
/* Uniquify the sinks vector */
std::sort(_sinks.begin(), _sinks.end());
_sinks.erase(std::unique(_sinks.begin(), _sinks.end()), _sinks.end());
/* Uniquify the units vector */
std::sort(_units.begin(), _units.end());
_units.erase(std::unique(_units.begin(), _units.end()), _units.end());
}
std::map <std::string, std::string> const & iActuatorType::CommandMap () const
{ return _sinks_2_units;}
std::vector <std::string> const & iActuatorType::Commands ()
{
/* If there are zero sinks, look to the library json descriptor file */
if (_sinks.empty())
{
/* Copy all commands out of the JSON */
nlohmann::json j_sinks = nlohmann::json::parse(LibraryJsonCommands());
for (nlohmann::json::iterator it = j_sinks.begin(); it != j_sinks.end(); ++it)
_sinks.push_back(it.key());
}
return _sinks;
}
std::vector <std::string> const & iActuatorType::Units () const
{ return _units; }
std::string const iActuatorType::Unit (std::string source) const
{
std::map<std::string, std::string>::const_iterator it =
CommandMap().find(source);
if (it != CommandMap().end())
return it->second;
else
return "";
}
bool iActuatorType::HasCommand (std::string source)
{
return std::find(Commands().begin(), Commands().end(), source) != Commands().end();
}
bool iActuatorType::HasUnit (std::string unit) const
{
return std::find(Units().begin(), Units().end(), unit) != Units().end();
}
std::string iActuatorType::JsonDefinition ()
{
std::stringstream ss;
ss << "{" << std::endl
<< " \"name\" : \"" << Name() << "\"," << std::endl
<< " \"description\" : \"" << Description() << "\"";
if (!CommandMap().empty())
{
ss << "," << std::endl << " \"sinks\" :" << std::endl << " {" << std::endl;
for(std::map<std::string, std::string>::const_iterator it = CommandMap().begin();
it != CommandMap().end();)
{
ss << " \"" << it->first << "\" : {\"units\" : \"" << it->second << "\"}";
if (++it != CommandMap().end())
ss << "," << std::endl;
}
ss << std::endl << " }";
}
ss << std::endl << "}";
return ss.str();
}
void iActuatorType::JsonCommands(std::string commands)
{
std::stringstream ss;
ss << "{" << std::endl;
if (!_child_value_serializers.empty())
{
/* Iterate over each serializer method */
for (std::map<t_getJson, iUpmObject*>::const_iterator it = _child_value_serializers.begin();
it != _child_value_serializers.end();)
{
// Call static method to serialize data from the derived classes
ss << it->first(it->second);
if (++it != _child_value_serializers.end()) ss << ",";
ss << std::endl;
}
}
ss << "}";
/* TODO Change this method to a command method */
}
/* Since nlohmann::json has only been fwd declared (not included in a header),
* json is a partial type. Because of this explicitly instantiate basic
* types.
*/
template void upm::to_json(nlohmann::json& j, const ActuatorSink<int>& p);
template void upm::from_json(const nlohmann::json& j, ActuatorSink<int>& p);
template void upm::to_json(nlohmann::json& j, const ActuatorSink<float>& p);
template void upm::from_json(const nlohmann::json& j, ActuatorSink<float>& p);
template void upm::to_json(nlohmann::json& j, const ActuatorSink<double>& p);
template void upm::from_json(const nlohmann::json& j, ActuatorSink<double>& p);
template void upm::to_json(nlohmann::json& j, const ActuatorCommand<int>& p);
template void upm::from_json(const nlohmann::json& j, ActuatorCommand<int>& p);
template void upm::to_json(nlohmann::json& j, const ActuatorCommand<float>& p);
template void upm::from_json(const nlohmann::json& j, ActuatorCommand<float>& p);
template void upm::to_json(nlohmann::json& j, const ActuatorCommand<double>& p);
template void upm::from_json(const nlohmann::json& j, ActuatorCommand<double>& p);
template std::ostream &upm::operator<<(std::ostream& os, const ActuatorCommand<float>& ac);

156
src/core/iActuatorType.hpp Normal file
View File

@ -0,0 +1,156 @@
#pragma once
#include "iUpmObject.hpp"
/* I/O fwd declaration header */
#include <iosfwd>
namespace upm
{
template <typename T>
struct ActuatorSink
{
std::string unit;
T min;
T max;
T accuracy;
};
template <class T>
void to_json(nlohmann::json& j, const ActuatorSink<T>& p);
template <class T>
void from_json(const nlohmann::json& j, ActuatorSink<T>& p);
template <typename T>
struct ActuatorCommand
{
std::string command;
std::string unit;
T value;
};
template <class T>
void to_json(nlohmann::json& j, const ActuatorCommand<T>& p);
template <class T>
void from_json(const nlohmann::json& j, ActuatorCommand<T>& p);
template <class T>
std::ostream &operator<<(std::ostream&, const ActuatorCommand<T>&);
/**
* Forward declaration of iActuatorType class for ostream usage
*/
class iActuatorType;
/**
* iActuatorType abstract class.
*
* Provides a common interface for actuator classes. This interface is
* meant to be used by derived actuator categories.
*
* For example, iTemperatureSensor or iLightSensor
*
*/
class iActuatorType : public virtual iUpmObject
{
public:
iActuatorType();
/**
* Allow derived classes to provide their own destructor
*/
virtual ~iActuatorType();
std::string LibraryJsonCommands();
/**
* Return a map reference of sinks to units. This map can
* be used to get the units for a actuator value given a source.
*
* @return Map of sinks to units.
*/
const std::map<std::string, std::string>& CommandMap() const;
/**
* Return a vector of all known sinks for this actuator.
*
* @return Vector of sinks
*/
const std::vector<std::string>& Commands();
/**
* Return a vector of all known units for this actuator.
*
* @return Vector of units
*/
const std::vector<std::string>& Units() const;
/**
* Return a unit string given a single source.
*
* @return Corresponding unit for source or empty string if not found.
*/
virtual const std::string Unit(std::string source) const;
/**
* Determine if this actuator has a given source.
*
* @return True if source string is available, fails otherwise.
*/
virtual bool HasCommand(std::string source);
/**
* Determine if this actuator has a given unit.
*
* @return True if unit string is available, fails otherwise.
*/
virtual bool HasUnit(std::string unit) const;
/**
* Return a JsonDefinition string which defines this actuator
*
* @return JsonDefinition-encoded string.
*/
virtual std::string JsonDefinition();
/**
* @brief Json string of actuator data.
*
* The JsonValues call serializes ALL values returned by this actuator
* as a Json string.
*
* Example:
*
* "Temperature" : {"value" : 25.0, "units" : "C"}
* "Acceleration" : {"value" : 1.1, "units" : "m/s^2"}
*
* @return Json-encoded string.
*/
virtual void JsonCommands(std::string commands);
private:
/**
* Sensor source vector
*/
std::vector<std::string> _sinks;
/**
* Sensor unit vector
*/
std::vector<std::string> _units;
/**
* Mapping of sinks to units
*/
std::map<std::string, std::string> _sinks_2_units;
protected:
/**
* Mapping of sinks to units
*/
void AddCommand(std::string source, std::string unit);
};
}

4
src/core/iActuatorType.i Normal file
View File

@ -0,0 +1,4 @@
%import (module="upm.pyupm_core") "iActuatorType.hpp"
%include ".iActuatorType.i"
%include ".core.i"

54
src/core/iCO2Sensor.cxx Normal file
View File

@ -0,0 +1,54 @@
#include "iCO2Sensor.hpp"
using namespace upm;
std::map <std::string, float> iCO2Sensor::CO2All ()
{return CO2ForSources(Sources());}
float iCO2Sensor::CO2ForSource(std::string source)
{
std::map<std::string, float> vals = CO2ForSources(std::vector<std::string>(1, source));
if (vals.empty())
{
std::stringstream ss;
ss << __FUNCTION__ << " sensor does not provide source: '"
<< source << ". Valid sources are: {";
std::copy(Sources().begin(), Sources().end() - 1,
std::ostream_iterator<std::string>(ss, ", "));
ss << Sources().back() << "}";
throw std::invalid_argument(ss.str());
}
return vals[source];
}
iCO2Sensor::iCO2Sensor ()
{
AddValueSerializer(this, &_JsonCO2);
}
std::string iCO2Sensor::JsonCO2 () const
{
return "{" + _JsonCO2((iCO2Sensor*)this) + "}";
}
std::string iCO2Sensor::_JsonCO2 (iUpmObject * inst)
{
std::stringstream ss;
/* Downcast to reference (throws if cast fails) */
iCO2Sensor& ref = dynamic_cast<iCO2Sensor&>(*inst);
std::map<std::string, float> data = ref.CO2All();
for (std::map<std::string, float>::const_iterator it = data.begin();
it != data.end();)
{
ss << "\"" << it->first << "\" : {\"value\" : " << it->second
<< ", \"units\" : \"" << ref.Unit(it->first) << "\"}";
if (++it != data.end()) ss << "," << std::endl;
}
return ss.str();
}

72
src/core/iCO2Sensor.hpp Normal file
View File

@ -0,0 +1,72 @@
#pragma once
#include <map>
#include <vector>
#include "iSensorType.hpp"
namespace upm
{
/**
* iCO2Sensor abstract class.
*
* Provides a common interface for all sensors which detect co2.
*/
class iCO2Sensor : public virtual upm::iSensorType
{
public:
/**
* Read and return all values for this sensor as a map of sources
* to values.
*
* @return Map of sources to values.
*/
virtual std::map<std::string, float> CO2All();
/**
* Read and return a single value from the source provided
*
* @param source Target source to read
*
* @throws std::invalid_argument If source is NOT valid for this sensor
*
* @return Map of sources to values.
*/
virtual float CO2ForSource(std::string source);
/**
* Read and return all values for this sensor for the provided
* vector of sources.
*
* @param sources Vector of sources to read
*
* @return Map of sources to values.
*/
virtual std::map<std::string, float> CO2ForSources(std::vector<std::string> sources) = 0;
/**
* Add a pointer to this type and a proxy function pointer for
* serializing all values from this sensor type.
*/
iCO2Sensor();
/**
* Read and return all values for this sensor as JSON
*
* @return JSON string of co2 values
*/
virtual std::string JsonCO2() const;
private:
/**
* Provide a means to read and serialize values from this sensor
* as a static method. This method, along with a pointer to the
* class can be called from a base class
*
* @param inst Instance of iCO2Sensor to call _JsonCO2 on
*
* @return JSON string of co2 values (minus wrapping '{' and '}'
*/
static std::string _JsonCO2(iUpmObject * inst);
};
}

2
src/core/iCO2Sensor.i Normal file
View File

@ -0,0 +1,2 @@
%import (module="upm.pyupm_core") ".iCO2Sensor.i"
%include ".core.i"

View File

@ -0,0 +1,51 @@
#include "iHumiditySensor.hpp"
using namespace upm;
std::map <std::string, float> iHumiditySensor::HumidityAll ()
{return HumidityForSources(Sources());}
float iHumiditySensor::HumidityForSource (std::string source)
{
std::map<std::string, float> vals = HumidityForSources(std::vector<std::string>(1, source));
if (vals.empty())
{
std::stringstream ss;
ss << __FUNCTION__ << " sensor does not provide source: '"
<< source << ". Valid sources are: {";
std::copy(Sources().begin(), Sources().end() - 1,
std::ostream_iterator<std::string>(ss, ", "));
ss << Sources().back() << "}";
throw std::invalid_argument(ss.str());
}
return vals[source];
}
iHumiditySensor::iHumiditySensor ()
{
AddValueSerializer(this, &_JsonHumidity);
}
std::string iHumiditySensor::JsonHumidity () const
{
return "{" + _JsonHumidity((iHumiditySensor*)this) + "}";
}
std::string iHumiditySensor::_JsonHumidity (iUpmObject * inst)
{
std::stringstream ss;
/* Downcast to reference (throws if cast fails) */
iHumiditySensor& ref = dynamic_cast<iHumiditySensor&>(*inst);
std::map<std::string, float> data = ref.HumidityAll();
for (std::map<std::string, float>::const_iterator it = data.begin();
it != data.end();)
{
ss << "\"" << it->first << "\" : {\"value\" : " << it->second
<< ", \"units\" : \"" << ref.Unit(it->first) << "\"}";
if (++it != data.end()) ss << "," << std::endl;
}
return ss.str();
}

View File

@ -0,0 +1,72 @@
#pragma once
#include <map>
#include <vector>
#include "iSensorType.hpp"
namespace upm
{
/**
* iHumiditySensor abstract class.
*
* Provides a common interface for all sensors which detect humidity.
*/
class iHumiditySensor : public virtual iSensorType
{
public:
/**
* Read and return all values for this sensor as a map of sources
* to values.
*
* @return Map of sources to values.
*/
virtual std::map<std::string, float> HumidityAll();
/**
* Read and return a single value from the source provided
*
* @param source Target source to read
*
* @throws std::invalid_argument If source is NOT valid for this sensor
*
* @return Map of sources to values.
*/
virtual float HumidityForSource(std::string source);
/**
* Read and return all values for this sensor for the provided
* vector of sources.
*
* @param sources Vector of sources to read
*
* @return Map of sources to values.
*/
virtual std::map<std::string, float> HumidityForSources(std::vector<std::string> sources) = 0;
/**
* Add a pointer to this type and a proxy function pointer for
* serializing all values from this sensor type.
*/
iHumiditySensor();
/**
* Read and return all values for this sensor as JSON
*
* @return JSON string of humidity values
*/
virtual std::string JsonHumidity() const;
private:
/**
* Provide a means to read and serialize values from this sensor
* as a static method. This method, along with a pointer to the
* class can be called from a base class
*
* @param inst Instance of iHumiditySensor to call _JsonHumidity on
*
* @return JSON string of humidity values (minus wrapping '{' and '}'
*/
static std::string _JsonHumidity(iUpmObject * inst);
};
}

View File

@ -0,0 +1,2 @@
%import (module="upm.pyupm_core") ".iHumiditySensor.i"
%include ".core.i"

View File

@ -0,0 +1,3 @@
#include "iLightController.hpp"
upm::ILightController::~ILightController () {}

View File

@ -24,7 +24,6 @@
#pragma once #pragma once
#include "iModuleStatus.hpp"
namespace upm namespace upm
{ {
@ -41,7 +40,7 @@ namespace upm
* @snippet light-controllers.cxx Interesting * @snippet light-controllers.cxx Interesting
*/ */
class ILightController : virtual public IModuleStatus class ILightController
{ {
public: public:
/** /**
@ -85,7 +84,7 @@ namespace upm
*/ */
virtual int getBrightness() = 0; virtual int getBrightness() = 0;
virtual ~ILightController() {} virtual ~ILightController();
}; };
} }

View File

@ -0,0 +1,2 @@
%import (module="upm.pyupm_core") ".iLightController.i"
%include ".core.i"

54
src/core/iLightSensor.cxx Normal file
View File

@ -0,0 +1,54 @@
#include "iLightSensor.hpp"
using namespace upm;
std::map <std::string, float> iLightSensor::LightAll ()
{return LightForSources(Sources());}
float iLightSensor::LightForSource (std::string source)
{
std::map<std::string, float> vals = LightForSources(std::vector<std::string>(1, source));
if (vals.empty())
{
std::stringstream ss;
ss << __FUNCTION__ << " sensor does not provide source: '"
<< source << ". Valid sources are: {";
std::copy(Sources().begin(), Sources().end() - 1,
std::ostream_iterator<std::string>(ss, ", "));
ss << Sources().back() << "}";
throw std::invalid_argument(ss.str());
}
return vals[source];
}
iLightSensor::iLightSensor ()
{
AddValueSerializer(this, &_JsonLight);
}
std::string iLightSensor::JsonLight ()
{
return "{" + _JsonLight(dynamic_cast<iLightSensor*>(this)) + "}";
}
std::string iLightSensor::_JsonLight (iUpmObject * inst)
{
std::stringstream ss;
/* Downcast to reference (throws if cast fails) */
iLightSensor& ref = dynamic_cast<iLightSensor&>(*inst);
std::map<std::string, float> data = ref.LightAll();
for (std::map<std::string, float>::const_iterator it = data.begin();
it != data.end();)
{
ss << "\"" << it->first << "\" : {\"value\" : " << it->second
<< ", \"units\" : \"" << ref.Unit(it->first) << "\"}";
if (++it != data.end()) ss << "," << std::endl;
}
return ss.str();
}

72
src/core/iLightSensor.hpp Normal file
View File

@ -0,0 +1,72 @@
#pragma once
#include <map>
#include <vector>
#include "iSensorType.hpp"
namespace upm
{
/**
* iLightSensor abstract class.
*
* Provides a common interface for all sensors which detect light.
*/
class iLightSensor : public virtual iSensorType
{
public:
/**
* Read and return all values for this sensor as a map of sources
* to values.
*
* @return Map of sources to values.
*/
virtual std::map<std::string, float> LightAll();
/**
* Read and return a single value from the source provided
*
* @param source Target source to read
*
* @throws std::invalid_argument If source is NOT valid for this sensor
*
* @return Map of sources to values.
*/
virtual float LightForSource(std::string source);
/**
* Read and return all values for this sensor for the provided
* vector of sources.
*
* @param sources Vector of sources to read
*
* @return Map of sources to values.
*/
virtual std::map<std::string, float> LightForSources(std::vector<std::string> sources) = 0;
/**
* Add a pointer to this type and a proxy function pointer for
* serializing all values from this sensor type.
*/
iLightSensor();
/**
* Read and return all values for this sensor as JSON
*
* @return JSON string of light values
*/
virtual std::string JsonLight();
private:
/**
* Provide a means to read and serialize values from this sensor
* as a static method. This method, along with a pointer to the
* class can be called from a base class
*
* @param inst Instance of iLightSensor to call _JsonLight on
*
* @return JSON string of light values (minus wrapping '{' and '}'
*/
static std::string _JsonLight(iUpmObject * inst);
};
}

2
src/core/iLightSensor.i Normal file
View File

@ -0,0 +1,2 @@
%import (module="upm.pyupm_core") ".iLightSensor.i"
%include ".core.i"

View File

@ -0,0 +1,39 @@
#include "iMoistureSensor.hpp"
using namespace upm;
std::map <std::string, float> iMoistureSensor::MoistureAll ()
{return MoistureForSources(Sources());}
std::map <std::string, float> iMoistureSensor::MoistureForSource (std::string source)
{ return MoistureForSources(std::vector<std::string>(1, source)); }
iMoistureSensor::iMoistureSensor ()
{
AddValueSerializer(this, &_JsonMoisture);
}
std::string iMoistureSensor::JsonMoisture () const
{
return "{" + _JsonMoisture((iMoistureSensor*)this) + "}";
}
std::string iMoistureSensor::_JsonMoisture (iUpmObject * inst)
{
std::stringstream ss;
/* Downcast to reference (throws if cast fails) */
iMoistureSensor& ref = dynamic_cast<iMoistureSensor&>(*inst);
std::map<std::string, float> data = ref.MoistureAll();
for (std::map<std::string, float>::const_iterator it = data.begin();
it != data.end();)
{
ss << "\"" << it->first << "\" : {\"value\" : " << it->second
<< ", \"units\" : \"" << ref.Unit(it->first) << "\"}";
if (++it != data.end()) ss << "," << std::endl;
}
return ss.str();
}

View File

@ -0,0 +1,29 @@
#pragma once
#include <map>
#include <vector>
#include "iSensorType.hpp"
namespace upm
{
class iMoistureSensor : public virtual iSensorType
{
public:
virtual std::map<std::string, float> MoistureAll();
virtual std::map<std::string, float> MoistureForSource(std::string source);
virtual std::map<std::string, float> MoistureForSources(std::vector<std::string> sources) = 0;
iMoistureSensor();
/**
* Read and return all values for this sensor as JSON
*
* @return JSON string of moisture values
*/
virtual std::string JsonMoisture() const;
private:
static std::string _JsonMoisture(iUpmObject * inst);
};
}

View File

@ -0,0 +1,2 @@
%import (module="upm.pyupm_core") ".iMoistureSensor.i"
%include ".core.i"

286
src/core/iMraa.cxx Normal file
View File

@ -0,0 +1,286 @@
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <regex>
#include "iUpmObject.hpp"
#include "iMraa.hpp"
#include "mraa/iio.hpp"
#include "mraa/uart_ow.hpp"
#define iMraaThrow(failure, protocol, fullstring) throw std::runtime_error("Failed to initialize " + \
std::string(protocol) + " from " + std::string(fullstring) + ", cause: " + \
std::string(failure))
using namespace upm;
template<typename Out>
void split(const std::string &s, char delim, Out result)
{
std::stringstream ss;
ss.str(s);
std::string item;
while (std::getline(ss, item, delim))
*(result++) = item;
}
std::vector<std::string> split(const std::string &s, char delim)
{
std::vector<std::string> elems;
split(s, delim, std::back_inserter(elems));
return elems;
}
uint32_t str2uint32(const std::string &number)
{
std::string lwr = number;
std::transform(lwr.begin(), lwr.end(), lwr.begin(), ::tolower);
uint32_t ret = 0;
std::stringstream ss;
if ((lwr.compare(0, 2, "0x") == 0) &&
(lwr.substr(2).find_first_not_of("0123456789abcdef") == std::string::npos))
{
ss << std::hex << lwr.substr(2);
ss >> ret;
}
else if (!lwr.empty() && lwr.find_first_not_of("0123456789") == std::string::npos)
{
ss << lwr;
ss >> ret;
}
else
throw std::runtime_error("Cannot convert '" + number + "' to an integer");
return ret;
}
std::map<std::string, mraa::Mode> _mraa_Mode =
{
{"strong", mraa::MODE_STRONG},
{"pullup", mraa::MODE_PULLUP},
{"pulldown", mraa::MODE_PULLDOWN},
{"hiz", mraa::MODE_HIZ},
};
std::map<std::string, mraa::Dir> _mraa_Dir =
{
{"out", mraa::DIR_OUT},
{"in", mraa::DIR_IN},
{"high", mraa::DIR_OUT_HIGH},
{"low", mraa::DIR_OUT_LOW},
};
std::map<std::string, mraa::Edge> _mraa_Edge =
{
{"none", mraa::EDGE_NONE},
{"both", mraa::EDGE_BOTH},
{"rising", mraa::EDGE_RISING},
{"falling", mraa::EDGE_FALLING},
};
std::map<std::string, mraa::InputMode> _mraa_InputMode =
{
{"active_high", mraa::MODE_IN_ACTIVE_HIGH},
{"avtive_low", mraa::MODE_IN_ACTIVE_LOW},
};
std::map<std::string, mraa::OutputMode> _mraa_OutputMode =
{
{"open_drain", mraa::MODE_OUT_OPEN_DRAIN},
{"push_pull", mraa::MODE_OUT_PUSH_PULL},
};
std::map<std::string, mraa::I2cMode> _mraa_I2cModes =
{
{"std", mraa::I2C_STD},
{"fast", mraa::I2C_FAST},
{"high", mraa::I2C_HIGH},
};
iMraa::iMraa()
{
DEBUG_MSG("XXX");
if (mraa::init() != mraa::SUCCESS)
throw std::runtime_error("Failed to initialize MRAA");
}
iMraa::iMraa(const std::string &init_string) : iMraa()
{
DEBUG_MSG("XXX");
/* Convert to lower case for processing */
std::string lwr = init_string;
std::transform(lwr.begin(), lwr.end(), lwr.begin(), ::tolower);
/* Split string into protocols separated by ',' */
std::vector<std::string> protocols = split(lwr, ',');
for (std::vector<std::string>::const_iterator it = protocols.begin();
it != protocols.end(); ++it)
{
/* Assign a reference for easier use */
const std::string& proto_str = *it;
/* Split proto_str into chunks separated by ':' */
std::vector<std::string> params = split(proto_str, ':');
/* Some basic error checking */
if (params.size() < 2)
throw std::runtime_error("Invalid initialization string: '" +
proto_str + "'. Format is proto0:val0:val1,proto1:val0:val1");
/* First character of first parameter defines the protocol */
const char proto = params[0][0];
switch (proto)
{
case 'a':
{
/* Next chunk should be a pin # */
uint32_t pin = str2uint32(params[1]);
try {_aio.push_back(new mraa::Aio(pin));}
catch (std::exception &e)
{
throw std::invalid_argument("Failed to initialize Aio from string: '" +
proto_str + "'. Reason: " + e.what());
}
break;
}
case 'g':
{
/* Next chunk should be a pin # */
uint32_t pin = str2uint32(params[1]);
try { _gpio.push_back(new mraa::Gpio(pin)); }
catch (std::exception &e)
{
throw std::invalid_argument("Failed to initialize Gpio from string: '" +
proto_str + "'. Reason: " + e.what());
}
/* Parse any additional parameters */
for(size_t i = 2; i < params.size(); ++i)
{
/* Gpio output modes */
if (_mraa_Mode.find(params[i]) != _mraa_Mode.end())
{
if (_gpio.back()->mode(_mraa_Mode[params[i]]) != mraa::SUCCESS)
iMraaThrow("mode(" + params[i] + ") failed.", "GPIO", proto_str);
}
/* Gpio direction options */
else if (_mraa_Dir.find(params[i]) != _mraa_Dir.end())
{
if (_gpio.back()->dir(_mraa_Dir[params[i]]) != mraa::SUCCESS)
iMraaThrow("dir(" + params[i] + ") failed.", "GPIO", proto_str);
}
/* Gpio edge types for interrupts */
else if (_mraa_Edge.find(params[i]) != _mraa_Edge.end())
{
if (_gpio.back()->edge(_mraa_Edge[params[i]]) != mraa::SUCCESS)
iMraaThrow("edge(" + params[i] + ") failed.", "GPIO", proto_str);
}
/* Gpio input modes */
else if (_mraa_InputMode.find(params[i]) != _mraa_InputMode.end())
{
if (_gpio.back()->inputMode(_mraa_InputMode[params[i]]) != mraa::SUCCESS)
iMraaThrow("inputMode(" + params[i] + ") failed.", "GPIO", proto_str);
}
/* Gpio output driver modes */
else if (_mraa_OutputMode.find(params[i]) != _mraa_OutputMode.end())
{
if (_gpio.back()->outputMode(_mraa_OutputMode[params[i]]) != mraa::SUCCESS)
iMraaThrow("outputMode(" + params[i] + ") failed.", "GPIO", proto_str);
}
/* Empty chunk? Skip it */
else if (params[i].empty()) {}
/* Unknown string? Write to stderr and continue */
else std::cerr << "Unknown parameter: '" << params[i] << "' from "
<< proto_str<< std::endl;
}
break;
}
case 'i':
{
/* Next chunk should be a bus # */
uint32_t bus = str2uint32(params[1]);
try { _i2c.push_back(new mraa::I2c(bus)); }
catch (std::exception &e)
{
throw std::invalid_argument("Failed to initialize I2c from string: '" +
proto_str + "'. Reason: " + e.what());
}
/* Parse any additional parameters */
for(size_t i = 2; i < params.size(); ++i)
{
/* I2c modes */
if (_mraa_I2cModes.find(params[i]) != _mraa_I2cModes.end())
{
if (_i2c.back()->frequency(_mraa_I2cModes[params[i]]) != mraa::SUCCESS)
iMraaThrow("frequency(" + params[i] + ") failed.", "I2C", proto_str);
}
}
break;
}
case 'o':
break;
case 'p': /* p:<pin>[:min_pw_us][:max_pw_us] */
{
/* Next chunk should be a pin # */
uint32_t pin = str2uint32(params[1]);
try { _pwm.push_back(new mraa::Pwm(pin)); }
catch (std::exception &e)
{
throw std::invalid_argument("Failed to initialize Pwm from string: '" +
proto_str + "'. Reason: " + e.what());
}
///* Parse any additional parameters */
///* Min pulse width in us */
//if (params.size() > 2)
//{
// if (_pwm.back()->min(_mraa_I2cModes[params[i]]) != mraa::SUCCESS)
// iMraaThrow("frequency(" + params[i] + ") failed.", "I2C", proto_str);
//}
break;
}
case 's':
break;
case 'u':
break;
case 'w':
break;
default:
break;
}
}
return;
}
iMraa::~iMraa()
{
for (std::vector<mraa::Aio*>::iterator it = _aio.begin();
it != _aio.end(); ++it) delete *it;
for (std::vector<mraa::Gpio*>::iterator it = _gpio.begin();
it != _gpio.end(); ++it) delete *it;
for (std::vector<mraa::I2c*>::iterator it = _i2c.begin();
it != _i2c.end(); ++it) delete *it;
for (std::vector<mraa::Pwm*>::iterator it = _pwm.begin();
it != _pwm.end(); ++it) delete *it;
}
std::string iMraa::Connections()
{
std::stringstream ss;
ss << "MRAA connections ("
<< _aio.size() + _gpio.size() + _i2c.size() + _iio.size() +
_pwm.size() + _spi.size() + _uart.size() + _uartow.size()
<< "):" << std::endl;
if (!_aio.empty()) ss << "\tAIO: " << _aio.size() << std::endl;
if (!_gpio.empty()) ss << "\tGPIO: " << _gpio.size() << std::endl;
if (!_i2c.empty()) ss << "\tI2C: " << _i2c.size() << std::endl;
if (!_iio.empty()) ss << "\tIIO: " << _iio.size() << std::endl;
if (!_pwm.empty()) ss << "\tPWM: " << _pwm.size() << std::endl;
if (!_spi.empty()) ss << "\tSPI: " << _spi.size() << std::endl;
if (!_uart.empty()) ss << "\tUART: " << _uart.size() << std::endl;
if (!_uartow.empty()) ss << "\tOneWire: " << _uartow.size() << std::endl;
return ss.str();
}

55
src/core/iMraa.hpp Normal file
View File

@ -0,0 +1,55 @@
#pragma once
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <regex>
#include "iUpmObject.hpp"
#include "mraa.hpp"
#include "mraa/iio.hpp"
#include "mraa/uart_ow.hpp"
namespace upm
{
class iMraa : public virtual iUpmObject
{
public:
iMraa();
iMraa(const std::string &init_string);
virtual ~iMraa();
std::string Connections();
//
// template <typename T>
// T& Connection(std::string label);
//
// template <typename T>
// bool DefineConnections(std::vector<std::string> labels)
// {
// if (std::is_same<T, mraa::Aio>::value)
// {
// }
// else if (std::is_same<T, mraa::Gpio>::value) {}
// else if (std::is_same<T, mraa::I2c>::value)
// {
// }
//
// return false;
// }
//private:
std::vector<mraa::Aio*> _aio;
std::vector<mraa::Gpio*> _gpio;
std::vector<mraa::I2c*> _i2c;
std::vector<mraa::Iio*> _iio;
std::vector<mraa::Pwm*> _pwm;
std::vector<mraa::Spi*> _spi;
std::vector<mraa::Uart*> _uart;
std::vector<mraa::UartOW*> _uartow;
};
}

2
src/core/iMraa.i Normal file
View File

@ -0,0 +1,2 @@
%import (module="upm.pyupm_core") ".iMraa.i"
%include ".core.i"

View File

@ -0,0 +1,54 @@
#include "iPressureSensor.hpp"
using namespace upm;
std::map <std::string, float> iPressureSensor::PressureAll ()
{return PressureForSources(Sources());}
float iPressureSensor::PressureForSource (std::string source)
{
std::map<std::string, float> vals = PressureForSources(std::vector<std::string>(1, source));
if (vals.empty())
{
std::stringstream ss;
ss << __FUNCTION__ << " sensor does not provide source: '"
<< source << ". Valid sources are: {";
std::copy(Sources().begin(), Sources().end() - 1,
std::ostream_iterator<std::string>(ss, ", "));
ss << Sources().back() << "}";
throw std::invalid_argument(ss.str());
}
return vals[source];
}
iPressureSensor::iPressureSensor ()
{
AddValueSerializer(this, &_JsonPressure);
}
std::string iPressureSensor::JsonPressure () const
{
return "{" + _JsonPressure((iPressureSensor*)this) + "}";
}
std::string iPressureSensor::_JsonPressure (iUpmObject * inst)
{
std::stringstream ss;
/* Downcast to reference (throws if cast fails) */
iPressureSensor& ref = dynamic_cast<iPressureSensor&>(*inst);
std::map<std::string, float> data = ref.PressureAll();
for (std::map<std::string, float>::const_iterator it = data.begin();
it != data.end();)
{
ss << "\"" << it->first << "\" : {\"value\" : " << it->second
<< ", \"units\" : \"" << ref.Unit(it->first) << "\"}";
if (++it != data.end()) ss << "," << std::endl;
}
return ss.str();
}

View File

@ -0,0 +1,72 @@
#pragma once
#include <map>
#include <vector>
#include "iSensorType.hpp"
namespace upm
{
/**
* iPressureSensor abstract class.
*
* Provides a common interface for all sensors which detect pressure.
*/
class iPressureSensor : public virtual iSensorType
{
public:
/**
* Read and return all values for this sensor as a map of sources
* to values.
*
* @return Map of sources to values.
*/
virtual std::map<std::string, float> PressureAll();
/**
* Read and return a single value from the source provided
*
* @param source Target source to read
*
* @throws std::invalid_argument If source is NOT valid for this sensor
*
* @return Map of sources to values.
*/
virtual float PressureForSource(std::string source);
/**
* Read and return all values for this sensor for the provided
* vector of sources.
*
* @param sources Vector of sources to read
*
* @return Map of sources to values.
*/
virtual std::map<std::string, float> PressureForSources(std::vector<std::string> sources) = 0;
/**
* Add a pointer to this type and a proxy function pointer for
* serializing all values from this sensor type.
*/
iPressureSensor();
/**
* Read and return all values for this sensor as JSON
*
* @return JSON string of pressure values
*/
virtual std::string JsonPressure() const;
private:
/**
* Provide a means to read and serialize values from this sensor
* as a static method. This method, along with a pointer to the
* class can be called from a base class
*
* @param inst Instance of iPressureSensor to call _JsonPressure on
*
* @return JSON string of pressure values (minus wrapping '{' and '}'
*/
static std::string _JsonPressure(iUpmObject * inst);
};
}

View File

@ -0,0 +1,2 @@
%import (module="upm.pyupm_core") ".iPressureSensor.i"
%include ".core.i"

156
src/core/iSensorType.cxx Normal file
View File

@ -0,0 +1,156 @@
#include "iSensorType.hpp"
#include "external/json/json.hpp"
using namespace upm;
namespace upm {
template <typename T>
void to_json(nlohmann::json& j, const SensorSource<T> &p) {
j = nlohmann::json{{"unit", p.unit}, {"min", p.min}, {"max", p.max}, {"accuracy", p.accuracy}};
}
template <typename T>
void from_json(const nlohmann::json& j, SensorSource<T>& p) {
p.unit = j.at("unit").get<std::string>();
p.min = j.at("min").get<T>();
p.max = j.at("max").get<T>();
p.accuracy = j.at("accuracy").get<T>();
}
}
iSensorType::iSensorType()
{
DEBUG_MSG("XXX");
}
iSensorType::~iSensorType()
{
DEBUG_MSG("XXX");
}
std::string iSensorType::LibraryJsonSources()
{
std::stringstream ss;
ss << this->LibraryJson()["Sensor Class"][Name()]["Sources"] << std::endl;
return ss.str();
}
void iSensorType::AddSource (std::string source, std::string unit)
{
/* Add the source:unit to the map */
_sources_2_units[source] = unit;
/* Each call regens the sources and units vectors */
_sources.clear();
_units.clear();
for(std::map<std::string, std::string>::const_iterator it = SourceMap().begin();
it != SourceMap().end(); ++it)
{
_sources.push_back(it->first);
_units.push_back(it->second);
}
/* Uniquify the sources vector */
std::sort(_sources.begin(), _sources.end());
_sources.erase(std::unique(_sources.begin(), _sources.end()), _sources.end());
/* Uniquify the units vector */
std::sort(_units.begin(), _units.end());
_units.erase(std::unique(_units.begin(), _units.end()), _units.end());
}
std::map <std::string, std::string> const & iSensorType::SourceMap () const
{ return _sources_2_units;}
std::vector <std::string> const & iSensorType::Sources ()
{
/* If there are zero sources, look to the library json descriptor file */
if (_sources.empty())
{
/* Copy all commands out of the JSON */
nlohmann::json j_sources = nlohmann::json::parse(LibraryJsonSources());
for (nlohmann::json::iterator it = j_sources.begin(); it != j_sources.end(); ++it)
_sources.push_back(it.key());
}
return _sources; }
std::vector <std::string> const & iSensorType::Units () const
{ return _units; }
std::string const iSensorType::Unit (std::string source) const
{
std::map<std::string, std::string>::const_iterator it =
SourceMap().find(source);
if (it != SourceMap().end())
return it->second;
else
return "";
}
bool iSensorType::HasSource (std::string source)
{
return std::find(Sources().begin(), Sources().end(), source) != Sources().end();
}
bool iSensorType::HasUnit (std::string unit) const
{
return std::find(Units().begin(), Units().end(), unit) != Units().end();
}
std::string iSensorType::JsonDefinition ()
{
std::stringstream ss;
ss << "{" << std::endl
<< " \"name\" : \"" << Name() << "\"," << std::endl
<< " \"description\" : \"" << Description() << "\"";
if (!SourceMap().empty())
{
ss << "," << std::endl << " \"sources\" :" << std::endl << " {" << std::endl;
for(std::map<std::string, std::string>::const_iterator it = SourceMap().begin();
it != SourceMap().end();)
{
ss << " \"" << it->first << "\" : {\"units\" : \"" << it->second << "\"}";
if (++it != SourceMap().end())
ss << "," << std::endl;
}
ss << std::endl << " }";
}
ss << std::endl << "}";
return ss.str();
}
std::string iSensorType::JsonValues ()
{
std::stringstream ss;
ss << "{" << std::endl;
if (!_child_value_serializers.empty())
{
/* Iterate over each serializer method */
for (std::map<t_getJson, iUpmObject*>::const_iterator it = _child_value_serializers.begin();
it != _child_value_serializers.end();)
{
// Call static method to serialize data from the derived classes
ss << it->first(it->second);
if (++it != _child_value_serializers.end()) ss << ",";
ss << std::endl;
}
}
ss << "}";
return ss.str();
}
/* Since nlohmann::json has only been fwd declared (not included in a header),
* json is a partial type. Because of this explicitly instantiate basic
* types.
*/
template void upm::to_json(nlohmann::json& j, const SensorSource<int>& p);
template void upm::from_json(const nlohmann::json& j, SensorSource<int>& p);
template void upm::to_json(nlohmann::json& j, const SensorSource<float>& p);
template void upm::from_json(const nlohmann::json& j, SensorSource<float>& p);
template void upm::to_json(nlohmann::json& j, const SensorSource<double>& p);
template void upm::from_json(const nlohmann::json& j, SensorSource<double>& p);

143
src/core/iSensorType.hpp Normal file
View File

@ -0,0 +1,143 @@
#pragma once
#include <map>
#include <sstream>
#include <vector>
#include <iterator>
#include <algorithm>
#include "iUpmObject.hpp"
namespace upm
{
template <typename T>
struct SensorSource
{
std::string unit;
T min;
T max;
T accuracy;
};
template <class T>
void to_json(nlohmann::json& j, const SensorSource<T>& p);
template <class T>
void from_json(const nlohmann::json& j, SensorSource<T>& p);
/**
* Forward declaration of iSensorType class for ostream usage
*/
class iSensorType;
/**
* iSensorType abstract class.
*
* Provides a common interface for sensor classes. This interface is
* meant to be used by derived sensor categories.
*
* For example, iTemperatureSensor or iLightSensor
*
*/
class iSensorType : public virtual iUpmObject
{
public:
iSensorType();
/**
* Allow derived classes to provide their own destructor
*/
virtual ~iSensorType();
std::string LibraryJsonSources();
/**
* Return a map reference of sources to units. This map can
* be used to get the units for a sensor value given a source.
*
* @return Map of sources to units.
*/
const std::map<std::string, std::string>& SourceMap() const;
/**
* Return a vector of all known sources for this sensor.
*
* @return Vector of sources
*/
const std::vector<std::string>& Sources();
/**
* Return a vector of all known units for this sensor.
*
* @return Vector of units
*/
const std::vector<std::string>& Units() const;
/**
* Return a unit string given a single source.
*
* @return Corresponding unit for source or empty string if not found.
*/
virtual const std::string Unit(std::string source) const;
/**
* Determine if this sensor has a given source.
*
* @return True if source string is available, fails otherwise.
*/
virtual bool HasSource(std::string source);
/**
* Determine if this sensor has a given unit.
*
* @return True if unit string is available, fails otherwise.
*/
virtual bool HasUnit(std::string unit) const;
/**
* Return a JsonDefinition string which defines this sensor
*
* @return JsonDefinition-encoded string.
*/
virtual std::string JsonDefinition();
/**
* @brief Json string of sensor data.
*
* The JsonValues call serializes ALL values returned by this sensor
* as a Json string.
*
* Example:
*
* "Temperature" : {"value" : 25.0, "units" : "C"}
* "Acceleration" : {"value" : 1.1, "units" : "m/s^2"}
*
* @return Json-encoded string.
*/
virtual std::string JsonValues();
private:
/**
* Sensor source vector
*/
std::vector<std::string> _sources;
/**
* Sensor unit vector
*/
std::vector<std::string> _units;
/**
* Mapping of sources to units
*/
std::map<std::string, std::string> _sources_2_units;
protected:
/**
* Mapping of sources to units
*/
void AddSource(std::string source, std::string unit);
};
}

4
src/core/iSensorType.i Normal file
View File

@ -0,0 +1,4 @@
%import (module="upm.pyupm_core") "iSensorType.hpp"
%include ".iSensorType.i"
%include ".core.i"

147
src/core/iServoActuator.cxx Normal file
View File

@ -0,0 +1,147 @@
#include <iterator>
#include "iServoActuator.hpp"
#include "external/json/json.hpp"
#include <iostream>
using namespace upm;
iServoActuator::iServoActuator ()
{
DEBUG_MSG("XXX");
AddJsonDeserializer(this, &_DeserializeAngleCommands);
}
iServoActuator::~iServoActuator ()
{
DEBUG_MSG("XXX");
}
void iServoActuator::AngleAll (float angle)
{
std::map<std::string, float> tmpcmds;
for(std::vector<std::string>::const_iterator it = Commands().begin();
it != Commands().end(); ++it)
tmpcmds[*it] = angle;
AngleForCommands(tmpcmds);
}
void iServoActuator::AngleForCommand (float angle, std::string command)
{
DEBUG_MSG("Number of commands: " << Commands().size() << std::endl);
if (std::find(Commands().begin(), Commands().end(), command) == Commands().end())
{
std::stringstream ss;
ss << DerivedClassName() << " does not provide command: '"
<< command << "'. Valid commands are: {";
std::copy(Commands().begin(), Commands().end() - 1,
std::ostream_iterator<std::string>(ss, ", "));
if (!Commands().empty())
ss << Commands().back();
ss << "}";
throw std::invalid_argument(ss.str());
}
AngleForCommands(std::map<std::string, float> {{command, angle}});
}
void iServoActuator::AngleFromJson(std::string json_commands)
{
std::vector<ActuatorCommand<float>> cmds;
/* First, try to deserialize 1 command */
try
{
nlohmann::json json_cmd = nlohmann::json::parse(json_commands);
cmds.push_back(json_cmd);
}
catch (const std::exception& e)
{
DEBUG_MSG("Deserialize single command failed, attempting to deserialize command vector...");
}
/* If no commands were created, try to deserialize a vector of commands */
if (cmds.empty())
{
try
{
nlohmann::json json_cmd = nlohmann::json::parse(json_commands);
/* TODO: get rid of this second copy */
std::vector<ActuatorCommand<float>> tmp_cmds = json_cmd;
cmds = tmp_cmds;
}
catch (const std::exception& e)
{
DEBUG_MSG("Deserialize command vector failed...");
}
}
/* If no commands exist, throw */
if (cmds.empty())
{
std::stringstream ss;
ss << "Failed to deserialize commands from json string: '";
ss << json_commands << "'" << std::endl;
ss << "Valid JSON command string examples:" << std::endl;
/* Make the examples (at least 2) */
std::vector<ActuatorCommand<float>> tmp_cmds(Commands().size() < 2 ? 2 : Commands().size());
for(size_t i = 0; i < Commands().size(); i++)
tmp_cmds[i].command = Commands()[i];
/* Example strings show a single command and vector or commands */
nlohmann::json jcmd = tmp_cmds[0];
nlohmann::json jcmds = tmp_cmds;
ss << "'" << jcmd << "' or '" << jcmds << "'";
throw std::invalid_argument(ss.str());
}
/* Run the commands */
for (std::vector<ActuatorCommand<float>>::const_iterator it = cmds.begin(); it != cmds.end(); ++it)
{
AngleForCommand(it->value, it->command);
}
}
std::string iServoActuator::_DeserializeAngleCommands (iUpmObject * inst)
{
/* Downcast to reference (throws if cast fails) */
iServoActuator& ref = dynamic_cast<iServoActuator&>(*inst);
/* Make sure the derived class name (from std::type_info) matches a JSON sensor class name */
std::string classname_derived = ref.DerivedClassName();
/* First, get a json reference to the "Sensor Class" */
nlohmann::json json_class = ref.LibraryJson()["Sensor Class"];
if (json_class.find(classname_derived) == json_class.end())
{
std::stringstream ss;
for (nlohmann::json::iterator it = json_class.begin(); it != json_class.end(); ++it)
{
ss << it.key();
if (*it != json_class.back()) ss << ", ";
}
throw std::invalid_argument("JSON library descriptor file " + inst->LibraryJsonFilename() + " does not contain a class object named '" +
classname_derived + "'. Valid classes are: {" + ss.str() + "}");
}
/* Get a reference to the 'Commands' object, if none exists, catch and continue */
nlohmann::json jsrs;
try { jsrs = ref.LibraryJson()["Sensor Class"][classname_derived].at("Commands"); }
catch (...) {}
/* Add all commands to iSensorType's command vector */
for (nlohmann::json::iterator it = jsrs.begin(); it != jsrs.end(); ++it)
{
upm::ActuatorSink<float> _as = it.value();
ref.AddCommand(it.key(), _as.unit);
}
/* Return a string */
return jsrs.dump();
}

View File

@ -0,0 +1,58 @@
#pragma once
#include "iActuatorType.hpp"
namespace upm
{
/**
* iServoActuator abstract class.
*
* Provides a common interface for all sensors which detect light.
*/
class iServoActuator : public virtual iActuatorType
{
public:
/**
* Add a pointer to this type and a proxy function pointer for
* serializing all values from this sensor type.
*/
iServoActuator();
/* Provide a virtual destructor */
virtual ~iServoActuator();
/**
* Read and return all values for this sensor as a map of commands
* to values.
*
* @return Map of commands to values.
*/
virtual void AngleAll(float angle);
/**
* Read and return a single value from the command provided
*
* @param command Target command to read
*
* @throws std::invalid_argument If command is NOT valid for this sensor
*
* @return Map of commands to values.
*/
virtual void AngleForCommand(float angle, std::string command);
/**
* Read and return all values for this sensor for the provided
* vector of commands.
*
* @param commands Vector of commands to read
*
* @return Map of commands to values.
*/
virtual void AngleForCommands(std::map<std::string, float> commands) = 0;
virtual void AngleFromJson(std::string json_commands);
private:
static std::string _DeserializeAngleCommands(iUpmObject * inst);
};
}

View File

@ -0,0 +1,2 @@
%import (module="upm.pyupm_core") ".iServoActuator.i"
%include ".core.i"

View File

@ -0,0 +1,90 @@
#include "iTemperatureSensor.hpp"
#include "external/json/json.hpp"
using namespace upm;
typedef struct
{
std::string unit;
float min;
float max;
float accuracy;
} SensorSource ;
std::map <std::string, float> iTemperatureSensor::TemperatureAll ()
{return TemperatureForSources(Sources());}
float iTemperatureSensor::TemperatureForSource (std::string source)
{
std::map<std::string, float> vals = TemperatureForSources(std::vector<std::string>(1, source));
if (vals.empty())
{
std::stringstream ss;
ss << __FUNCTION__ << " sensor does not provide source: '"
<< source << "'. Valid sources are: {";
std::copy(Sources().begin(), Sources().end() - 1,
std::ostream_iterator<std::string>(ss, ", "));
ss << Sources().back() << "}";
throw std::invalid_argument(ss.str());
}
return vals[source];
}
iTemperatureSensor::iTemperatureSensor ()
{
AddValueSerializer(this, &_JsonTemperature);
AddJsonDeserializer(this, &_DeserializeTemperatureSources);
}
std::string iTemperatureSensor::JsonTemperature () const
{
return "{" + _JsonTemperature((iTemperatureSensor*)this) + "}";
}
std::string iTemperatureSensor::_JsonTemperature (iUpmObject * inst)
{
std::stringstream ss;
/* Downcast to reference (throws if cast fails) */
iTemperatureSensor& ref = dynamic_cast<iTemperatureSensor&>(*inst);
std::map<std::string, float> data = ref.TemperatureAll();
for (std::map<std::string, float>::const_iterator it = data.begin();
it != data.end();)
{
ss << "\"" << it->first << "\" : {\"value\" : " << it->second
<< ", \"units\" : \"" << ref.Unit(it->first) << "\"}";
if (++it != data.end()) ss << "," << std::endl;
}
return ss.str();
}
std::string iTemperatureSensor::_DeserializeTemperatureSources (iUpmObject * inst)
{
/* Downcast to reference (throws if cast fails) */
iTemperatureSensor& ref = dynamic_cast<iTemperatureSensor&>(*inst);
/* Make sure the derived class name (from std::type_info) matches a JSON sensor class name */
std::string classname_derived = ref.DerivedClassName();
/* Get a reference to the object */
nlohmann::json jsrs = ref.LibraryJson()["Sensor Class"][classname_derived]["Sources"];
/* Deserialize to a temperature source map */
std::map<std::string, upm::SensorSource<float>> sources = jsrs;
/* Assign to this instance */
ref._temp_sources = sources;
/* Add all sources to iSensorType's source vector */
for (std::map<std::string, upm::SensorSource<float>>::const_iterator it = ref._temp_sources.begin();
it != ref._temp_sources.end(); ++it)
ref.AddSource(it->first, it->second.unit);
/* Return a string */
return jsrs.dump();
}

View File

@ -0,0 +1,77 @@
#pragma once
#include <map>
#include <vector>
#include "iSensorType.hpp"
namespace upm
{
/**
* iTemperatureSensor abstract class.
*
* Provides a common interface for all sensors which detect temperature.
*/
class iTemperatureSensor : public virtual iSensorType
{
public:
/**
* Read and return all values for this sensor as a map of sources
* to values.
*
* @return Map of sources to values.
*/
virtual std::map<std::string, float> TemperatureAll();
/**
* Read and return a single value from the source provided
*
* @param source Target source to read
*
* @throws std::invalid_argument If source is NOT valid for this sensor
*
* @return Map of sources to values.
*/
virtual float TemperatureForSource(std::string source);
/**
* Read and return all values for this sensor for the provided
* vector of sources.
*
* @param sources Vector of sources to read
*
* @return Map of sources to values.
*/
virtual std::map<std::string, float> TemperatureForSources(std::vector<std::string> sources) = 0;
/**
* Add a pointer to this type and a proxy function pointer for
* serializing all values from this sensor type.
*/
iTemperatureSensor();
/**
* Read and return all values for this sensor as JSON
*
* @return JSON string of temperature values
*/
virtual std::string JsonTemperature() const;
private:
/**
* Provide a means to read and serialize values from this sensor
* as a static method. This method, along with a pointer to the
* class can be called from a base class
*
* @param inst Instance of iTemperatureSensor to call _JsonTemperature on
*
* @return JSON string of temperature values (minus wrapping '{' and '}'
*/
static std::string _JsonTemperature(iUpmObject * inst);
static std::string _DeserializeTemperatureSources(iUpmObject * inst);
std::map<std::string, upm::SensorSource<float>> _temp_sources;
};
}

View File

@ -0,0 +1,2 @@
%import (module="upm.pyupm_core") ".iTemperatureSensor.i"
%include ".core.i"

287
src/core/iUpmObject.cxx Normal file
View File

@ -0,0 +1,287 @@
#include <dlfcn.h>
#include <typeinfo>
#include <cxxabi.h>
#include <string.h>
#include <iostream>
#include <regex>
#include <fstream>
#include "external/json/json.hpp"
#include "iUpmObject.hpp"
#include "upm_library_globals.hpp"
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#define THROW_RUNTIME(msg) throw std::runtime_error(std::string(__FILENAME__) + ":" + std::to_string(__LINE__) + " [" + std::string(__FUNCTION__) + "] " + msg)
extern const char* DATA_DIRECTORY;
static std::string DATA_DIRECTORY_STR = DATA_DIRECTORY;
using namespace upm;
using json = nlohmann::json;
/* Locate method to test if a file exists */
static bool exists(const std::string& filename, bool throw_exception = false)
{
std::ifstream f;
if (throw_exception)
f.exceptions(std::ifstream::failbit);
try {
f.open(filename.c_str());
}
catch (const std::exception& e)
{
throw std::runtime_error("'" + filename + "' does not exist or is not accessible.");
}
return f.good();
}
/* Always create an instance of the _json pointer (empty) */
iUpmObject::iUpmObject()
{
DEBUG_MSG("XXX");
}
iUpmObject::~iUpmObject()
{
DEBUG_MSG("XXX");
if (_json)
{
delete _json;
_json = NULL;
}
}
void iUpmObject::initFromJsonLibDesc(std::string path_to_json_file)
{
/* Throw if the file exists (as is, no realpath) */
exists(path_to_json_file, true);
/* Next, convert to a realpath */
const char * rp = realpath(path_to_json_file.c_str(), NULL);
if (!rp)
throw std::runtime_error("'" + path_to_json_file + "' does not exist or is not accessible.");
std::string realpath_json_file(rp);
/* If this method is getting called ALWAYS delete and re-init */
if (_json)
{
delete _json;
_json = NULL;
}
_json = new json;
/* Does the provided file exist? Is it readable? */
std::string json_str;
std::ifstream t(realpath_json_file);
json_str.assign((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
/* Parse the file as JSON */
*_json = _json->parse(json_str.c_str(), nullptr, false);
/* Set the library JSON filename */
_LibraryJsonFilename = realpath_json_file;
/* Run any additional deserializers available on the JSON */
if (!_child_json_deserializers.empty())
{
std::stringstream ss;
/* Iterate over each serializer method */
for (std::map<t_getJson, iUpmObject*>::const_iterator it = _child_json_deserializers.begin();
it != _child_json_deserializers.end();)
{
// Call static method to serialize data from the derived classes
ss << it->first(it->second);
if (++it != _child_json_deserializers.end()) ss << ",";
ss << std::endl;
}
DEBUG_MSG("XXX Deserializers: " << ss.str());
}
}
std::string iUpmObject::Description()
{
json x = LibraryJson()["Sensor Class"][Name()]["Description"];
if (x.empty())
THROW_RUNTIME("No Description found for sensor class '" + Name() + "' in the JSON library descriptor file: " + LibraryJsonFilename());
return x;
}
std::string iUpmObject::JsonDefinition ()
{
std::stringstream ss;
ss << "{" << std::endl
<< " \"name\" : \"" << Name() << "\"," << std::endl
<< " \"description\" : \"" << Description() << "\""
<< std::endl << "}";
return ss.str();
}
std::string iUpmObject::Name()
{
/* Make sure the derived class name (from std::type_info) matches a JSON sensor class name */
std::string classname_derived = DerivedClassName();
/* Look at all objects under 'Sensor Class' */
json x = LibraryJson()["Sensor Class"][classname_derived];
if (x.empty())
THROW_RUNTIME("Sensor class Name '" + classname_derived + "' was not found in the JSON library descriptor file: " + LibraryJsonFilename());
return classname_derived;
}
json &iUpmObject::LibraryJson()
{
/* If no object, create one and load the library descriptor file*/
if (!_json)
{
_json = new json;
initFromJsonLibDesc(LibraryJsonFilename());
}
/* If the library descriptor JSON file has NOT been loaded, load it */
// if (_json->empty())
// {
// initFromJsonLibDesc(LibraryJsonFilename().c_str());
// //*_json = _json->parse(LibraryJsonRaw());
// }
/* Perform a quick error check: Library element must be equal to LibraryBaseName() */
// std::string lib_base_name = LibraryBaseName();
// std::string lib_json_name = (*_json)["Library"];
// if (lib_base_name != (*_json)["Library"])
// THROW_RUNTIME("Library base name '" + lib_base_name + "' does NOT match the Library name specified in the JSON file '" + lib_json_name +"'");
return *_json;
}
std::string iUpmObject::DerivedClassName() const
{
int status = 0;
char *name_demangled;
const std::type_info &this_type = typeid(*this);
/* Demangle class name */
name_demangled = abi::__cxa_demangle(this_type.name(), 0, 0, &status);
if (status)
THROW_RUNTIME("Failed to demangle class name.");
std::string name = name_demangled;
free(name_demangled);
Dl_info info;
/* Returns 0 on failure */
if (!dladdr((const void*)&this_type, &info))
return NULL;
/* Return class name only */
return name.substr(name.find_last_of(":") + 1);
}
std::string iUpmObject::LibraryAbsolutePath() const
{
Dl_info info;
const std::type_info &this_type = typeid(*this);
/* Throw on failure */
if (!dladdr((const void*)&this_type, &info))
THROW_RUNTIME("Failed to locate the library for this type.");
/* Attempt a realpath */
return realpath(info.dli_fname, NULL);
}
std::string iUpmObject::LibraryBaseName() const
{
/* First, get the full filename */
std::string filename = LibraryAbsolutePath();
std::string basename = filename.substr(filename.find_last_of("/\\") + 1);
/* Regex the library *base* name from the library file name */
std::smatch sm;
std::regex reg1("libupm-(\\S+)\\.so");
if (std::regex_search(basename, sm, reg1))
basename = sm.str(1);
// else
// std::cout << "XXX warning, binary: " << filename << " does not match format: libupm-<libname>.so, using: " << basename;
//THROW_RUNTIME("Could not get base library name. UPM library name does not follow the libupm-<library>.so format.");
return basename;
}
std::string iUpmObject::LibraryJsonFilename() const
{
if (!_LibraryJsonFilename.empty())
return _LibraryJsonFilename;
std::string json_file = DataDirectory() + "/" + LibraryBaseName() + ".json";
if (!exists(json_file))
THROW_RUNTIME("Failed to locate library JSON definition: " + json_file);
return json_file;
}
std::string iUpmObject::DataDirectory() const
{
std::string lib_loc = LibraryAbsolutePath();
/* If a json file has already been provided, use it */
std::string json_lib_def_file = _LibraryJsonFilename;
/* Otherwise, attempt to build the location of the json file */
if (json_lib_def_file.empty())
json_lib_def_file = lib_loc.substr(0, lib_loc.find_last_of("/\\")) + "/" + LibraryBaseName() + ".json";
/* If no local <libname>.json exists, look in DATADIR */
if (!exists(json_lib_def_file))
{
// If the library is:
// /usr/local/lib/libupm-rhusb.so.1.6.0
// then look one directory up for share/upm like so:
// /usr/local/share/upm/rhusb/rhusb.json
json_lib_def_file = DATA_DIRECTORY_STR + "/upm/" + LibraryBaseName() + "/" + LibraryBaseName() + ".json";
}
if (!exists(json_lib_def_file))
THROW_RUNTIME("Data directory for UPM was not found. Library found: " + lib_loc + ", expected JSON definition file: " + json_lib_def_file);
return json_lib_def_file.substr(0, json_lib_def_file.find_last_of("/\\"));
}
std::string iUpmObject::LibraryJsonRaw() const
{
std::string json_file = LibraryJsonFilename();
/* Return the contents as a string */
std::string json_str;
std::ifstream t(json_file);
json_str.assign((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
return json_str.c_str();
}
std::string iUpmObject::LibraryVersion() const
{
return ::LibraryVersion();
}
void iUpmObject::AddValueSerializer (iUpmObject * instance, t_getJson method)
{ _child_value_serializers[method] = instance; }
void iUpmObject::AddJsonDeserializer(iUpmObject * instance, t_getJson method)
{ _child_json_deserializers[method] = instance; }

121
src/core/iUpmObject.hpp Normal file
View File

@ -0,0 +1,121 @@
#pragma once
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include <stdint.h>
#include <memory>
#if !defined(NDEBUG)
#include <iostream>
#endif
#ifndef SWIG
/* Forward declare the basic_json class */
namespace nlohmann
{
template<typename, typename>
struct adl_serializer;
template<template<typename, typename, typename...> class ObjectType,
template<typename, typename...> class ArrayType,
class StringType,
class BooleanType,
class NumberIntegerType,
class NumberUnsignedType,
class NumberFloatType,
template<typename> class AllocatorType,
template<typename, typename> class JSONSerializer>
class basic_json;
using json = basic_json<std::map, std::vector, std::string, bool, std::int64_t, std::uint64_t, double, std::allocator, nlohmann::adl_serializer>;
}
#endif
namespace upm
{
inline std::string __func_or_memberfunc(const std::string& cxx_function)
{
size_t colons = cxx_function.find("::");
size_t begin = cxx_function.substr(0, colons).rfind(" ") + 1;
size_t end = cxx_function.rfind(")") - begin + 1;
return cxx_function.substr(begin, end);
}
#define __PRETTY_FUNCTIONS__ __func_or_memberfunc(__PRETTY_FUNCTION__)
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#if !defined(NDEBUG)
#define DEBUG_MSG(msg) do { std::cout << __FILENAME__ << ":" << __LINE__ \
<< " [" << __PRETTY_FUNCTIONS__ << "] " << msg << std::endl; } while (0)
#else
#define DEBUG_MSG(msg) do {} while (0)
#endif
class iUpmObject;
/**
* Function pointer typedef for child-to-parent proxy call
*/
typedef std::string (*t_getJson)(iUpmObject*);
class iUpmObject
{
public:
iUpmObject();
virtual ~iUpmObject();
virtual std::string Description();
virtual std::string JsonDefinition();
virtual std::string Name();
virtual void initFromJsonLibDesc(std::string path_to_json_file);
#ifndef SWIG
nlohmann::json& LibraryJson();
#endif
std::string DataDirectory() const;
std::string DerivedClassName() const;
std::string LibraryAbsolutePath() const;
std::string LibraryBaseName() const;
std::string LibraryJsonFilename() const;
std::string LibraryJsonRaw() const;
std::string LibraryVersion() const;
protected:
void AddValueSerializer(iUpmObject* instance, t_getJson method);
void AddJsonDeserializer(iUpmObject* instance, t_getJson method);
/**
* Used by child classes for child-to-parent proxy call
*/
std::map<t_getJson, iUpmObject*> _child_value_serializers;
/**
* Used by child classes for child-to-parent proxy call
*/
std::map<t_getJson, iUpmObject*> _child_json_deserializers;
///*
//friend std::ostream& operator<<(std::ostream& os, const iUpmObject& o)
//{
// return os << " \"name\" : \"" << o.Name() << "\"," << std::endl
// << " \"description\" : \"" << o.Description() << "\"";
//}
//friend std::ostream& operator<<(std::ostream& os, const iUpmObject* o)
//{ return os << *o; }
//*/
private:
nlohmann::json *_json = NULL;
std::string _Description;
std::string _LibraryJsonFilename;
std::string _LibraryJsonRaw;
};
}

2
src/core/iUpmObject.i Normal file
View File

@ -0,0 +1,2 @@
%import (module="upm.pyupm_core") "iUpmObject.hpp"
%include ".iUpmObject.i"

View File

@ -0,0 +1 @@
const char* DATA_DIRECTORY = "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_DATADIR@";

View File

@ -2,4 +2,4 @@ set (libname "ds1808lc")
set (libdescription "Lighting Controller") set (libdescription "Lighting Controller")
set (module_src ${libname}.cxx mraa-utils.cxx) set (module_src ${libname}.cxx mraa-utils.cxx)
set (module_hpp ${libname}.hpp) set (module_hpp ${libname}.hpp)
upm_module_init(interfaces mraa) upm_module_init(core mraa)

View File

@ -54,7 +54,7 @@ int DS1808LC::getBrightness()
return getPercentBrightness(values[0], values[1]); return getPercentBrightness(values[0], values[1]);
} }
else else
UPM_THROW("i2c read error"); throw std::runtime_error(std::string(__FUNCTION__) + " : i2c read error");
} }
@ -65,7 +65,7 @@ void DS1808LC::setBrightness(int dutyPercent)
values[1] = getPot2Value(dutyPercent); values[1] = getPot2Value(dutyPercent);
status = i2c->write(values, 2); status = i2c->write(values, 2);
if (status != mraa::SUCCESS) if (status != mraa::SUCCESS)
UPM_THROW("i2c write error"); throw std::runtime_error(std::string(__FUNCTION__) + " : i2c write error");
} }
// //

View File

@ -57,7 +57,6 @@ public:
DS1808LC(int gpioPower, int i2cBus); DS1808LC(int gpioPower, int i2cBus);
~DS1808LC(); ~DS1808LC();
const char* getModuleName() { return "ds1808lc"; }
bool isPowered(); bool isPowered();
void setPowerOn(); void setPowerOn();
void setPowerOff(); void setPowerOff();

View File

@ -1,30 +1,15 @@
%include "../common_top.i" %include "../common_top.i"
%include "iLightController.i"
/* BEGIN Java syntax ------------------------------------------------------- */ /* BEGIN Java syntax ------------------------------------------------------- */
#ifdef SWIGJAVA #ifdef SWIGJAVA
%include "arrays_java.i"; %include "arrays_java.i";
%include "../java_buffer.i" %include "../java_buffer.i"
%typemap(javaimports) SWIGTYPE %{import upm_interfaces.*;%}
%import "../interfaces/javaupm_iLightController.i"
JAVA_JNI_LOADLIBRARY(javaupm_ds1808lc) JAVA_JNI_LOADLIBRARY(javaupm_ds1808lc)
#endif #endif
/* END Java syntax */ /* END Java syntax */
/* BEGIN Javascript syntax ------------------------------------------------- */
#ifdef SWIGJAVASCRIPT
%include "iModuleStatus.hpp"
%include "iLightController.hpp"
#endif
/* END Javascript syntax */
/* BEGIN Python syntax ----------------------------------------------------- */
#ifdef SWIGPYTHON
%include "iModuleStatus.hpp"
%include "iLightController.hpp"
#endif
/* END Python syntax */
/* BEGIN Common SWIG syntax ------------------------------------------------- */ /* BEGIN Common SWIG syntax ------------------------------------------------- */
%{ %{
#include "ds1808lc.hpp" #include "ds1808lc.hpp"

Some files were not shown because too many files have changed in this diff Show More