mirror of
https://github.com/eclipse/upm.git
synced 2025-07-25 21:21:18 +03:00
C++ Core: Add base class per sensor/actuator type
Adding base classes for UPM sensors and actuators. Signed-off-by: Noel Eck <noel.eck@intel.com>
This commit is contained in:
@ -7,7 +7,8 @@ set (SWIG_CXX_DISABLE_WARNINGS -Wno-error
|
||||
-Wno-delete-non-virtual-dtor
|
||||
-Wno-unused-function
|
||||
-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 (ANDROID)
|
||||
@ -514,13 +515,13 @@ function(upm_swig_java)
|
||||
swig_add_library (javaupm_${libname} LANGUAGE java SOURCES ${SWIG_CURRENT_DOT_I_FILE})
|
||||
endif ()
|
||||
|
||||
# If the C++ target depends on C++ interfaces, make the JAVA target
|
||||
# depend on the JAVA interfaces
|
||||
if ("${_c_cxx_dependency_list}" MATCHES interfaces)
|
||||
add_dependencies(javaupm_${libname} javaupm_interfaces)
|
||||
# If this target depends on interfaces, include the java interfaces
|
||||
# If the C++ target depends on C++ core, make the JAVA target
|
||||
# depend on the JAVA core
|
||||
if ("${_c_cxx_dependency_list}" MATCHES core)
|
||||
add_dependencies(javaupm_${libname} javaupm_core)
|
||||
# If this target depends on core, include the java core
|
||||
# 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 ()
|
||||
# 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.
|
||||
@ -564,10 +565,10 @@ function(upm_swig_java)
|
||||
set (JAR $ENV{JAVA_HOME_NATIVE}/bin/jar)
|
||||
endif ()
|
||||
|
||||
# Only include the upm_interfaces.jar in the classpath for targets which
|
||||
# depend on the interfaces target. This fixes an issue where javac
|
||||
# complains about an empty upm_interfaces.jar when trying to build a target
|
||||
# which does not depend on javaupm_interfaces. If not previously set,
|
||||
# Only include the upm_core.jar in the classpath for targets which
|
||||
# depend on the core target. This fixes an issue where javac
|
||||
# complains about an empty upm_core.jar when trying to build a target
|
||||
# which does not depend on javaupm_core. If not previously set,
|
||||
# INTERFACES_JAR_FILE will be empty, and javac *should* not complain.
|
||||
add_custom_command (TARGET javaupm_${libname}
|
||||
POST_BUILD
|
||||
@ -632,12 +633,12 @@ if (BUILDSWIGNODE)
|
||||
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}")
|
||||
|
||||
# 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/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_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
|
||||
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
|
||||
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.
|
||||
# This allows for transitive header dependencies via target_link_libraries
|
||||
target_include_directories(${libname} INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
@ -855,8 +872,14 @@ function(upm_module_init)
|
||||
DESTINATION include/upm
|
||||
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")
|
||||
# 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"
|
||||
DESTINATION ${CMAKE_INSTALL_DATADIR}/upm/${basename}
|
||||
COMPONENT ${CMAKE_PROJECT_NAME})
|
||||
@ -908,10 +931,10 @@ if (NOT "${MODULE_LIST}" MATCHES ";utilities;")
|
||||
set(MODULE_LIST "utilities;${MODULE_LIST}")
|
||||
endif()
|
||||
|
||||
# If the module list does NOT include the interfaces directory, prepend it since
|
||||
# some sensor library targets depend on interfaces
|
||||
if (NOT "${MODULE_LIST}" MATCHES ";interfaces;")
|
||||
set(MODULE_LIST "interfaces;${MODULE_LIST}")
|
||||
# If the module list does NOT include the core directory, prepend it since
|
||||
# some sensor library targets depend on core
|
||||
if (NOT "${MODULE_LIST}" MATCHES ";core;")
|
||||
set(MODULE_LIST "core;${MODULE_LIST}")
|
||||
endif()
|
||||
|
||||
# Iterate over each directory in MODULE_LIST
|
||||
|
@ -2,7 +2,7 @@ set (libname "ads1x15")
|
||||
set (libdescription "Texas Instruments I2C ADC Library")
|
||||
set (module_src ${libname}.cxx ads1115.cxx ads1015.cxx)
|
||||
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)
|
||||
if (is_supported)
|
||||
target_compile_options(${libname} PUBLIC -Wno-overloaded-virtual)
|
||||
|
@ -40,7 +40,7 @@ ADS1015::ADS1015(int bus, uint8_t address, float vref) : ADS1X15(bus, address) {
|
||||
m_bitShift = 4;
|
||||
ADS1X15::getCurrentConfig();
|
||||
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)
|
||||
setGain(GAIN_TWOTHIRDS);
|
||||
else if (vref > 2.048)
|
||||
@ -57,12 +57,6 @@ ADS1015::ADS1015(int bus, uint8_t address, float vref) : ADS1X15(bus, address) {
|
||||
|
||||
ADS1015::~ADS1015(){};
|
||||
|
||||
const char*
|
||||
ADS1015::getModuleName() {
|
||||
return m_name.c_str();
|
||||
}
|
||||
|
||||
|
||||
unsigned int
|
||||
ADS1015::getNumInputs() {
|
||||
return 4;
|
||||
@ -73,7 +67,7 @@ ADS1015::getResolutionInBits() {
|
||||
return 12;
|
||||
}
|
||||
|
||||
uint16_t
|
||||
unsigned int
|
||||
ADS1015::getRawValue(unsigned int input) {
|
||||
ADS1X15::ADSMUXMODE mode = getMuxMode(input);
|
||||
updateConfigRegister((m_config_reg & ~ADS1X15_MUX_MASK) | mode, true);
|
||||
@ -163,7 +157,7 @@ ADS1015::getMuxMode(unsigned int input) {
|
||||
case 3:
|
||||
return SINGLE_3;
|
||||
default:
|
||||
UPM_THROW("Invalid input");
|
||||
throw std::runtime_error(std::string(__FUNCTION__) + " : Invalid input");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -26,7 +26,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "ads1x15.hpp"
|
||||
#include "interfaces/iADC.hpp"
|
||||
#include "iADC.hpp"
|
||||
|
||||
#define ADS1015_VREF 2.048
|
||||
|
||||
@ -54,63 +54,63 @@
|
||||
/*=========================================================================*/
|
||||
|
||||
namespace upm {
|
||||
/**
|
||||
* @library ads1x15
|
||||
* @sensor ADS1015
|
||||
* @comname 12-bit ADC with Integrated MUX, PGA, Comparator, Oscillator, and Reference
|
||||
* @type electric
|
||||
* @man ti adafruit
|
||||
* @con i2c
|
||||
* @web http://www.ti.com/lit/ds/symlink/ads1015.pdf
|
||||
*
|
||||
* @brief API for ADS1015
|
||||
*
|
||||
* The ADS1013, ADS1014, and ADS1015 are precision analog-to-digital converters (ADCs) with 12 bits of resolution
|
||||
* offered in an ultra-small, leadless QFN-10 package or an MSOP-10 package. The ADS1013/4/5 are designed with
|
||||
* precision, power, and ease of implementation in mind. The ADS1013/4/5 feature an onboard reference and oscillator.
|
||||
* Data is transferred via an I2C-compatible serial interface; four I2C slave addresses can be selected. The ADS1013/4/5
|
||||
* operate from a single power supply ranging from 2.0V to 5.5V.
|
||||
* The ADS1013/4/5 can perform conversions at rates up to 3300 samples per second (SPS). An onboard PGA is available
|
||||
* on the ADS1014 and ADS1015 that offers input ranges from the supply to as low as +/- 256mV, allowing both large and small
|
||||
* signals to be measured with high resolution. The ADS1015 also features an input multiplexer (MUX) that provides two
|
||||
* differential or four single-ended inputs.
|
||||
* The ADS1013/4/5 operate either in continuous conversion mode or a single-shot mode that automatically powers down
|
||||
* after a conversion and greatly reduces current consumption during idle periods. The ADS1013/4/5 are specified from
|
||||
* -40 deg C to +125 deg C.
|
||||
*
|
||||
* Tested with Adafriut ADS1015 board: https://www.adafruit.com/products/1083
|
||||
*
|
||||
* @image html ads1015.jpg
|
||||
* @snippet ads1x15.cxx Interesting
|
||||
* @snippet ads1x15-ads1015.cxx Interesting
|
||||
* @snippet ads1x15-adc-sensor.cxx Interesting
|
||||
*/
|
||||
class ADS1015 : public ADS1X15, public IADC {
|
||||
/**
|
||||
* @library ads1x15
|
||||
* @sensor ADS1015
|
||||
* @comname 12-bit ADC with Integrated MUX, PGA, Comparator, Oscillator, and Reference
|
||||
* @type electric
|
||||
* @man ti adafruit
|
||||
* @con i2c
|
||||
* @web http://www.ti.com/lit/ds/symlink/ads1015.pdf
|
||||
*
|
||||
* @brief API for ADS1015
|
||||
*
|
||||
* The ADS1013, ADS1014, and ADS1015 are precision analog-to-digital converters (ADCs) with 12 bits of resolution
|
||||
* offered in an ultra-small, leadless QFN-10 package or an MSOP-10 package. The ADS1013/4/5 are designed with
|
||||
* precision, power, and ease of implementation in mind. The ADS1013/4/5 feature an onboard reference and oscillator.
|
||||
* Data is transferred via an I2C-compatible serial interface; four I2C slave addresses can be selected. The ADS1013/4/5
|
||||
* operate from a single power supply ranging from 2.0V to 5.5V.
|
||||
* The ADS1013/4/5 can perform conversions at rates up to 3300 samples per second (SPS). An onboard PGA is available
|
||||
* on the ADS1014 and ADS1015 that offers input ranges from the supply to as low as +/- 256mV, allowing both large and small
|
||||
* signals to be measured with high resolution. The ADS1015 also features an input multiplexer (MUX) that provides two
|
||||
* differential or four single-ended inputs.
|
||||
* The ADS1013/4/5 operate either in continuous conversion mode or a single-shot mode that automatically powers down
|
||||
* after a conversion and greatly reduces current consumption during idle periods. The ADS1013/4/5 are specified from
|
||||
* -40 deg C to +125 deg C.
|
||||
*
|
||||
* Tested with Adafriut ADS1015 board: https://www.adafruit.com/products/1083
|
||||
*
|
||||
* @image html ads1015.jpg
|
||||
* @snippet ads1x15.cxx Interesting
|
||||
* @snippet ads1x15-ads1015.cxx Interesting
|
||||
* @snippet ads1x15-adc-sensor.cxx Interesting
|
||||
*/
|
||||
class ADS1015 : public ADS1X15, public iADC {
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* @enum ADSSAMPLERATE
|
||||
* @brief uint16_t enum containing values
|
||||
* representing the sample rate of the device.
|
||||
*
|
||||
* @var ADSSAMPLERATE::SPS_128 = 0x0000
|
||||
* @var ADSSAMPLERATE::SPS_250 = 0x0020
|
||||
* @var ADSSAMPLERATE::SPS_490 = 0x0040
|
||||
* @var ADSSAMPLERATE::SPS_920 = 0x0060
|
||||
* @var ADSSAMPLERATE::SPS_1600 = 0x0080
|
||||
* @var ADSSAMPLERATE::SPS_2400 = 0x00A0
|
||||
* @var ADSSAMPLERATE::SPS_3300 = 0x00C0
|
||||
*/
|
||||
typedef enum ADSSAMPLERATE {
|
||||
SPS_128 = ADS1015_DR_128SPS,
|
||||
SPS_250 = ADS1015_DR_250SPS,
|
||||
SPS_490 = ADS1015_DR_490SPS,
|
||||
SPS_920 = ADS1015_DR_920SPS,
|
||||
SPS_1600 = ADS1015_DR_1600SPS,
|
||||
SPS_2400 = ADS1015_DR_2400SPS,
|
||||
SPS_3300 = ADS1015_DR_3300SPS
|
||||
} ADSSAMPLERATE;
|
||||
/**
|
||||
* @enum ADSSAMPLERATE
|
||||
* @brief uint16_t enum containing values
|
||||
* representing the sample rate of the device.
|
||||
*
|
||||
* @var ADSSAMPLERATE::SPS_128 = 0x0000
|
||||
* @var ADSSAMPLERATE::SPS_250 = 0x0020
|
||||
* @var ADSSAMPLERATE::SPS_490 = 0x0040
|
||||
* @var ADSSAMPLERATE::SPS_920 = 0x0060
|
||||
* @var ADSSAMPLERATE::SPS_1600 = 0x0080
|
||||
* @var ADSSAMPLERATE::SPS_2400 = 0x00A0
|
||||
* @var ADSSAMPLERATE::SPS_3300 = 0x00C0
|
||||
*/
|
||||
typedef enum ADSSAMPLERATE {
|
||||
SPS_128 = ADS1015_DR_128SPS,
|
||||
SPS_250 = ADS1015_DR_250SPS,
|
||||
SPS_490 = ADS1015_DR_490SPS,
|
||||
SPS_920 = ADS1015_DR_920SPS,
|
||||
SPS_1600 = ADS1015_DR_1600SPS,
|
||||
SPS_2400 = ADS1015_DR_2400SPS,
|
||||
SPS_3300 = ADS1015_DR_3300SPS
|
||||
} ADSSAMPLERATE;
|
||||
|
||||
|
||||
/**
|
||||
@ -130,6 +130,9 @@ namespace upm {
|
||||
*/
|
||||
~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
|
||||
* needs to be overridden in subclasses as the ADS1115 and
|
||||
@ -151,7 +154,7 @@ namespace upm {
|
||||
*
|
||||
* @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
|
||||
@ -167,14 +170,6 @@ namespace upm {
|
||||
*/
|
||||
unsigned int getResolutionInBits();
|
||||
|
||||
/**
|
||||
* Returns module name
|
||||
*
|
||||
* @return modulename as const char*
|
||||
*/
|
||||
const char* getModuleName();
|
||||
|
||||
|
||||
protected:
|
||||
float getMultiplier(void);
|
||||
void setDelay(void);
|
||||
|
@ -1,28 +1,12 @@
|
||||
%include "../common_top.i"
|
||||
%include "iADC.i"
|
||||
|
||||
/* BEGIN Java syntax ------------------------------------------------------- */
|
||||
#ifdef SWIGJAVA
|
||||
%import "../interfaces/javaupm_iADC.i"
|
||||
%typemap(javaimports) SWIGTYPE %{import upm_interfaces.*;%}
|
||||
|
||||
JAVA_JNI_LOADLIBRARY(javaupm_ads1x15)
|
||||
#endif
|
||||
/* 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 ------------------------------------------------- */
|
||||
%{
|
||||
#include "ads1x15.hpp"
|
||||
|
@ -5,5 +5,5 @@ upm_mixed_module_init (NAME bmp280
|
||||
CPP_HDR bmp280.hpp bme280.hpp
|
||||
CPP_SRC bmp280.cxx bme280.cxx
|
||||
CPP_WRAPS_C
|
||||
REQUIRES mraa interfaces utilities-c)
|
||||
REQUIRES mraa core utilities-c)
|
||||
target_link_libraries(${libnamec} m)
|
||||
|
@ -54,3 +54,17 @@ void BME280::setOversampleRateHumidity(BME280_OSRS_H_T 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;
|
||||
}
|
||||
|
||||
|
@ -26,7 +26,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "interfaces/iHumiditySensor.hpp"
|
||||
#include "iHumiditySensor.hpp"
|
||||
|
||||
#include "bmp280.hpp"
|
||||
|
||||
@ -57,7 +57,7 @@ namespace upm {
|
||||
* @snippet bmp280-bme280.cxx Interesting
|
||||
*/
|
||||
|
||||
class BME280 : public BMP280, public IHumiditySensor {
|
||||
class BME280 : public virtual BMP280, public virtual iHumiditySensor {
|
||||
public:
|
||||
|
||||
/**
|
||||
@ -88,6 +88,12 @@ namespace upm {
|
||||
*/
|
||||
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()
|
||||
* must have been called prior to calling this method. If the
|
||||
@ -108,12 +114,6 @@ namespace upm {
|
||||
*/
|
||||
void setOversampleRateHumidity(BME280_OSRS_H_T rate);
|
||||
|
||||
// Interface support
|
||||
const char *getModuleName()
|
||||
{
|
||||
return "BME280";
|
||||
};
|
||||
|
||||
int getHumidityRelative()
|
||||
{
|
||||
return int(getHumidity());
|
||||
|
@ -48,6 +48,8 @@ BMP280::BMP280(int bus, int addr, int cs) :
|
||||
if (!m_bmp280)
|
||||
throw std::runtime_error(string(__FUNCTION__)
|
||||
+ ": bmp280_init() failed");
|
||||
AddSource("temperature", "C");
|
||||
AddSource("pressure", "Pa");
|
||||
}
|
||||
|
||||
BMP280::~BMP280()
|
||||
@ -150,3 +152,29 @@ void BMP280::setUsageMode(BMP280_USAGE_MODE_T 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;
|
||||
}
|
||||
|
||||
|
@ -28,8 +28,8 @@
|
||||
#include <string>
|
||||
#include "bmp280.h"
|
||||
|
||||
#include "interfaces/iPressureSensor.hpp"
|
||||
#include "interfaces/iTemperatureSensor.hpp"
|
||||
#include "iPressureSensor.hpp"
|
||||
#include "iTemperatureSensor.hpp"
|
||||
|
||||
namespace upm {
|
||||
|
||||
@ -67,7 +67,7 @@ namespace upm {
|
||||
* @snippet bmp280.cxx Interesting
|
||||
*/
|
||||
|
||||
class BMP280 : public ITemperatureSensor, public IPressureSensor {
|
||||
class BMP280 : public virtual iTemperatureSensor, public virtual iPressureSensor {
|
||||
public:
|
||||
|
||||
/**
|
||||
@ -93,6 +93,16 @@ namespace upm {
|
||||
BMP280(int bus=BMP280_DEFAULT_I2C_BUS, int addr=BMP280_DEFAULT_ADDR,
|
||||
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.
|
||||
*/
|
||||
@ -217,12 +227,6 @@ namespace upm {
|
||||
void setMeasureMode(BMP280_MODES_T mode);
|
||||
|
||||
|
||||
// Interface support
|
||||
const char *getModuleName()
|
||||
{
|
||||
return "BMP280";
|
||||
};
|
||||
|
||||
int getTemperatureCelsius()
|
||||
{
|
||||
return int(getTemperature(false));
|
||||
|
@ -1,40 +1,17 @@
|
||||
%include "../common_top.i"
|
||||
%include "iHumiditySensor.i"
|
||||
%include "iTemperatureSensor.i"
|
||||
%include "iPressureSensor.i"
|
||||
|
||||
/* BEGIN Java syntax ------------------------------------------------------- */
|
||||
#ifdef SWIGJAVA
|
||||
%import "../interfaces/javaupm_iTemperatureSensor.i"
|
||||
%import "../interfaces/javaupm_iHumiditySensor.i"
|
||||
%import "../interfaces/javaupm_iPressureSensor.i"
|
||||
|
||||
%include "arrays_java.i";
|
||||
%include "../java_buffer.i"
|
||||
|
||||
%typemap(javaimports) SWIGTYPE %{
|
||||
import upm_interfaces.*;
|
||||
%}
|
||||
|
||||
JAVA_JNI_LOADLIBRARY(javaupm_bmp280)
|
||||
#endif
|
||||
/* 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 ------------------------------------------------- */
|
||||
%{
|
||||
#include "bmp280_regs.h"
|
||||
|
@ -6,5 +6,5 @@ upm_mixed_module_init (NAME bmpx8x
|
||||
CPP_SRC bmpx8x.cxx
|
||||
FTI_SRC bmpx8x_fti.c
|
||||
CPP_WRAPS_C
|
||||
REQUIRES mraa interfaces utilities-c)
|
||||
REQUIRES mraa core utilities-c)
|
||||
target_link_libraries(${libnamec} m)
|
||||
|
@ -44,6 +44,9 @@ BMPX8X::BMPX8X (int bus, int addr) :
|
||||
if (!m_bmpx8x)
|
||||
throw std::runtime_error(string(__FUNCTION__)
|
||||
+ ": bmpx8x_init() failed");
|
||||
|
||||
AddSource("temperature", "C");
|
||||
AddSource("pressure", "Pa");
|
||||
}
|
||||
|
||||
BMPX8X::~BMPX8X()
|
||||
@ -118,3 +121,24 @@ float BMPX8X::getAltitude(int 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;
|
||||
}
|
||||
|
||||
|
@ -32,8 +32,8 @@
|
||||
|
||||
#include "bmpx8x.h"
|
||||
|
||||
#include "interfaces/iPressureSensor.hpp"
|
||||
#include "interfaces/iTemperatureSensor.hpp"
|
||||
#include "iPressureSensor.hpp"
|
||||
#include "iTemperatureSensor.hpp"
|
||||
|
||||
namespace upm {
|
||||
|
||||
@ -68,7 +68,7 @@ namespace upm {
|
||||
* @snippet bmpx8x.cxx Interesting
|
||||
*/
|
||||
|
||||
class BMPX8X : public IPressureSensor, public ITemperatureSensor {
|
||||
class BMPX8X : public iPressureSensor, public iTemperatureSensor {
|
||||
public:
|
||||
/**
|
||||
* Instantiates a BMPX8X object
|
||||
@ -85,6 +85,18 @@ namespace upm {
|
||||
*/
|
||||
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
|
||||
* method must be called before calling getPressure(),
|
||||
@ -178,7 +190,7 @@ namespace upm {
|
||||
|
||||
/**
|
||||
* Return latest calculated temperature value in Celsius. See
|
||||
* ITemperatureSensor.
|
||||
* iTemperatureSensor.
|
||||
*
|
||||
* @return The current temperature in Celsius.
|
||||
*/
|
||||
@ -190,7 +202,7 @@ namespace upm {
|
||||
|
||||
/**
|
||||
* Return latest calculated pressure value in Pascals. See
|
||||
* IPressureSensor.
|
||||
* iPressureSensor.
|
||||
*
|
||||
* @return The current pressure in Pascals.
|
||||
*/
|
||||
@ -200,16 +212,6 @@ namespace upm {
|
||||
return getPressure();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of module.
|
||||
*
|
||||
* @return The name of the module.
|
||||
*/
|
||||
const char *getModuleName()
|
||||
{
|
||||
return "BMPX8X";
|
||||
}
|
||||
|
||||
protected:
|
||||
// our underlying C context.
|
||||
bmpx8x_context m_bmpx8x;
|
||||
|
@ -1,23 +1,13 @@
|
||||
%include "../common_top.i"
|
||||
%include "iPressureSensor.i"
|
||||
%include "iTemperatureSensor.i"
|
||||
|
||||
/* BEGIN Java syntax ------------------------------------------------------- */
|
||||
#ifdef SWIGJAVA
|
||||
%import "../interfaces/javaupm_iPressureSensor.i"
|
||||
%typemap(javaimports) SWIGTYPE %{import upm_interfaces.*;%}
|
||||
%import "../interfaces/javaupm_iTemperatureSensor.i"
|
||||
|
||||
JAVA_JNI_LOADLIBRARY(javaupm_bmpx8x)
|
||||
#endif
|
||||
/* 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 ------------------------------------------------- */
|
||||
%{
|
||||
#include "bmpx8x_defs.h"
|
||||
|
@ -2,15 +2,14 @@
|
||||
%include "cpointer.i"
|
||||
%include "stdint.i"
|
||||
%include "typemaps.i"
|
||||
|
||||
#if SWIGJAVA
|
||||
%include "java_exceptions.i"
|
||||
#else
|
||||
%include "upm_exception.i"
|
||||
#endif
|
||||
|
||||
/* Import additional SWIG helps (not exposed in wrapper) */
|
||||
%import _upm.i
|
||||
|
||||
%{
|
||||
#include "version.hpp"
|
||||
%}
|
||||
%include "version.hpp"
|
||||
%import "_upm.i"
|
||||
|
||||
%apply int { speed_t };
|
||||
%apply int { mraa_result_t };
|
||||
@ -35,7 +34,6 @@ void cleanUp()
|
||||
{;}
|
||||
}
|
||||
%}
|
||||
void cleanUp();
|
||||
#endif
|
||||
|
||||
#if (SWIGJAVA)
|
||||
@ -43,7 +41,7 @@ void cleanUp();
|
||||
%typemap(jstype) jobject runnable "java.lang.Runnable"
|
||||
#endif
|
||||
|
||||
// Disable nested struct warnings
|
||||
/* Disable nested struct warnings */
|
||||
#pragma SWIG nowarn=312,325
|
||||
|
||||
#if SWIGPYTHON
|
||||
|
11
src/core/.core.i
Normal file
11
src/core/.core.i
Normal 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
13
src/core/.iADC.i
Normal 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
14
src/core/.iActuatorType.i
Normal 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
13
src/core/.iCO2Sensor.i
Normal 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"
|
29
src/core/.iHumiditySensor.i
Normal file
29
src/core/.iHumiditySensor.i
Normal 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
|
13
src/core/.iLightController.i
Normal file
13
src/core/.iLightController.i
Normal 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
13
src/core/.iLightSensor.i
Normal 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"
|
13
src/core/.iMoistureSensor.i
Normal file
13
src/core/.iMoistureSensor.i
Normal 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
37
src/core/.iMraa.i
Normal 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*>;
|
13
src/core/.iPressureSensor.i
Normal file
13
src/core/.iPressureSensor.i
Normal 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
14
src/core/.iSensorType.i
Normal 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"
|
13
src/core/.iServoActuator.i
Normal file
13
src/core/.iServoActuator.i
Normal 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"
|
28
src/core/.iTemperatureSensor.i
Normal file
28
src/core/.iTemperatureSensor.i
Normal 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
68
src/core/.iUpmObject.i
Normal 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
38
src/core/CMakeLists.txt
Normal 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
25
src/core/core.i
Normal 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
3
src/core/iADC.cxx
Normal file
@ -0,0 +1,3 @@
|
||||
#include "iADC.hpp"
|
||||
|
||||
upm::iADC::~iADC () {}
|
@ -22,26 +22,24 @@
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include "iModuleStatus.hpp"
|
||||
|
||||
#include "iSensorType.hpp"
|
||||
|
||||
namespace upm
|
||||
{
|
||||
/**
|
||||
* @brief Interface for ADC Sensors
|
||||
*/
|
||||
|
||||
class IADC : virtual public IModuleStatus
|
||||
{
|
||||
public:
|
||||
virtual unsigned int getResolutionInBits() = 0;
|
||||
virtual unsigned int getNumInputs() = 0;
|
||||
virtual uint16_t getRawValue(unsigned int input) = 0;
|
||||
virtual float getVoltage(unsigned int input) = 0;
|
||||
virtual ~IADC() {}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Interface for ADC Sensors
|
||||
*/
|
||||
class iADC : public virtual iSensorType
|
||||
{
|
||||
public:
|
||||
virtual unsigned int getResolutionInBits() = 0;
|
||||
virtual unsigned int getNumInputs() = 0;
|
||||
virtual unsigned int getRawValue(unsigned int input) = 0;
|
||||
virtual float getVoltage(unsigned int input) = 0;
|
||||
virtual ~iADC();
|
||||
};
|
||||
}
|
||||
|
2
src/core/iADC.i
Normal file
2
src/core/iADC.i
Normal file
@ -0,0 +1,2 @@
|
||||
%import (module="upm.pyupm_core") ".iADC.i"
|
||||
%include ".core.i"
|
187
src/core/iActuatorType.cxx
Normal file
187
src/core/iActuatorType.cxx
Normal 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
156
src/core/iActuatorType.hpp
Normal 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
4
src/core/iActuatorType.i
Normal 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
54
src/core/iCO2Sensor.cxx
Normal 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
72
src/core/iCO2Sensor.hpp
Normal 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
2
src/core/iCO2Sensor.i
Normal file
@ -0,0 +1,2 @@
|
||||
%import (module="upm.pyupm_core") ".iCO2Sensor.i"
|
||||
%include ".core.i"
|
51
src/core/iHumiditySensor.cxx
Normal file
51
src/core/iHumiditySensor.cxx
Normal 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();
|
||||
}
|
72
src/core/iHumiditySensor.hpp
Normal file
72
src/core/iHumiditySensor.hpp
Normal 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);
|
||||
};
|
||||
}
|
2
src/core/iHumiditySensor.i
Normal file
2
src/core/iHumiditySensor.i
Normal file
@ -0,0 +1,2 @@
|
||||
%import (module="upm.pyupm_core") ".iHumiditySensor.i"
|
||||
%include ".core.i"
|
3
src/core/iLightController.cxx
Normal file
3
src/core/iLightController.cxx
Normal file
@ -0,0 +1,3 @@
|
||||
#include "iLightController.hpp"
|
||||
|
||||
upm::ILightController::~ILightController () {}
|
@ -24,7 +24,6 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "iModuleStatus.hpp"
|
||||
|
||||
namespace upm
|
||||
{
|
||||
@ -41,7 +40,7 @@ namespace upm
|
||||
* @snippet light-controllers.cxx Interesting
|
||||
*/
|
||||
|
||||
class ILightController : virtual public IModuleStatus
|
||||
class ILightController
|
||||
{
|
||||
public:
|
||||
/**
|
||||
@ -84,8 +83,8 @@ namespace upm
|
||||
* @throws std::runtime_error
|
||||
*/
|
||||
virtual int getBrightness() = 0;
|
||||
|
||||
virtual ~ILightController() {}
|
||||
|
||||
virtual ~ILightController();
|
||||
};
|
||||
|
||||
}
|
2
src/core/iLightController.i
Normal file
2
src/core/iLightController.i
Normal file
@ -0,0 +1,2 @@
|
||||
%import (module="upm.pyupm_core") ".iLightController.i"
|
||||
%include ".core.i"
|
54
src/core/iLightSensor.cxx
Normal file
54
src/core/iLightSensor.cxx
Normal 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
72
src/core/iLightSensor.hpp
Normal 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
2
src/core/iLightSensor.i
Normal file
@ -0,0 +1,2 @@
|
||||
%import (module="upm.pyupm_core") ".iLightSensor.i"
|
||||
%include ".core.i"
|
39
src/core/iMoistureSensor.cxx
Normal file
39
src/core/iMoistureSensor.cxx
Normal 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();
|
||||
}
|
29
src/core/iMoistureSensor.hpp
Normal file
29
src/core/iMoistureSensor.hpp
Normal 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);
|
||||
};
|
||||
}
|
2
src/core/iMoistureSensor.i
Normal file
2
src/core/iMoistureSensor.i
Normal file
@ -0,0 +1,2 @@
|
||||
%import (module="upm.pyupm_core") ".iMoistureSensor.i"
|
||||
%include ".core.i"
|
286
src/core/iMraa.cxx
Normal file
286
src/core/iMraa.cxx
Normal 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
55
src/core/iMraa.hpp
Normal 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
2
src/core/iMraa.i
Normal file
@ -0,0 +1,2 @@
|
||||
%import (module="upm.pyupm_core") ".iMraa.i"
|
||||
%include ".core.i"
|
54
src/core/iPressureSensor.cxx
Normal file
54
src/core/iPressureSensor.cxx
Normal 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();
|
||||
}
|
72
src/core/iPressureSensor.hpp
Normal file
72
src/core/iPressureSensor.hpp
Normal 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);
|
||||
};
|
||||
}
|
2
src/core/iPressureSensor.i
Normal file
2
src/core/iPressureSensor.i
Normal file
@ -0,0 +1,2 @@
|
||||
%import (module="upm.pyupm_core") ".iPressureSensor.i"
|
||||
%include ".core.i"
|
156
src/core/iSensorType.cxx
Normal file
156
src/core/iSensorType.cxx
Normal 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
143
src/core/iSensorType.hpp
Normal 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
4
src/core/iSensorType.i
Normal 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
147
src/core/iServoActuator.cxx
Normal 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();
|
||||
}
|
58
src/core/iServoActuator.hpp
Normal file
58
src/core/iServoActuator.hpp
Normal 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);
|
||||
};
|
||||
}
|
2
src/core/iServoActuator.i
Normal file
2
src/core/iServoActuator.i
Normal file
@ -0,0 +1,2 @@
|
||||
%import (module="upm.pyupm_core") ".iServoActuator.i"
|
||||
%include ".core.i"
|
90
src/core/iTemperatureSensor.cxx
Normal file
90
src/core/iTemperatureSensor.cxx
Normal 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();
|
||||
}
|
77
src/core/iTemperatureSensor.hpp
Normal file
77
src/core/iTemperatureSensor.hpp
Normal 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;
|
||||
|
||||
};
|
||||
}
|
2
src/core/iTemperatureSensor.i
Normal file
2
src/core/iTemperatureSensor.i
Normal file
@ -0,0 +1,2 @@
|
||||
%import (module="upm.pyupm_core") ".iTemperatureSensor.i"
|
||||
%include ".core.i"
|
287
src/core/iUpmObject.cxx
Normal file
287
src/core/iUpmObject.cxx
Normal 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
121
src/core/iUpmObject.hpp
Normal 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
2
src/core/iUpmObject.i
Normal file
@ -0,0 +1,2 @@
|
||||
%import (module="upm.pyupm_core") "iUpmObject.hpp"
|
||||
%include ".iUpmObject.i"
|
1
src/core/iUpmObject_generated.cxx.in
Normal file
1
src/core/iUpmObject_generated.cxx.in
Normal file
@ -0,0 +1 @@
|
||||
const char* DATA_DIRECTORY = "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_DATADIR@";
|
@ -2,4 +2,4 @@ set (libname "ds1808lc")
|
||||
set (libdescription "Lighting Controller")
|
||||
set (module_src ${libname}.cxx mraa-utils.cxx)
|
||||
set (module_hpp ${libname}.hpp)
|
||||
upm_module_init(interfaces mraa)
|
||||
upm_module_init(core mraa)
|
||||
|
@ -54,7 +54,7 @@ int DS1808LC::getBrightness()
|
||||
return getPercentBrightness(values[0], values[1]);
|
||||
}
|
||||
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);
|
||||
status = i2c->write(values, 2);
|
||||
if (status != mraa::SUCCESS)
|
||||
UPM_THROW("i2c write error");
|
||||
throw std::runtime_error(std::string(__FUNCTION__) + " : i2c write error");
|
||||
}
|
||||
|
||||
//
|
||||
|
@ -57,7 +57,6 @@ public:
|
||||
DS1808LC(int gpioPower, int i2cBus);
|
||||
~DS1808LC();
|
||||
|
||||
const char* getModuleName() { return "ds1808lc"; }
|
||||
bool isPowered();
|
||||
void setPowerOn();
|
||||
void setPowerOff();
|
||||
|
@ -1,30 +1,15 @@
|
||||
%include "../common_top.i"
|
||||
%include "iLightController.i"
|
||||
|
||||
/* BEGIN Java syntax ------------------------------------------------------- */
|
||||
#ifdef SWIGJAVA
|
||||
%include "arrays_java.i";
|
||||
%include "../java_buffer.i"
|
||||
%typemap(javaimports) SWIGTYPE %{import upm_interfaces.*;%}
|
||||
%import "../interfaces/javaupm_iLightController.i"
|
||||
|
||||
JAVA_JNI_LOADLIBRARY(javaupm_ds1808lc)
|
||||
#endif
|
||||
/* 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 ------------------------------------------------- */
|
||||
%{
|
||||
#include "ds1808lc.hpp"
|
||||
|
@ -46,7 +46,7 @@ void MraaUtils::setGpio(int pin, int level)
|
||||
mraa::Gpio gpio(pin);
|
||||
gpio.dir(mraa::DIR_OUT);
|
||||
if (gpio.write(level) != mraa::SUCCESS)
|
||||
UPM_THROW("gpio write failed");
|
||||
throw std::runtime_error(std::string(__FUNCTION__) + " : gpio write failed");
|
||||
}
|
||||
|
||||
|
||||
|
@ -202,9 +202,9 @@ and is not meant to be installed anywhere.
|
||||
### Groups for the various Sensor C++ Interfaces ###
|
||||
|
||||
|
||||
@defgroup ilightsensor ILightSensor
|
||||
@defgroup ilightsensor iLightSensor
|
||||
@ingroup byif
|
||||
@brief Implements ILightSensor
|
||||
@brief Implements iLightSensor
|
||||
|
||||
|
||||
@defgroup ilightcontroller ILightController
|
||||
@ -212,19 +212,19 @@ and is not meant to be installed anywhere.
|
||||
@brief Implements ILightController
|
||||
|
||||
|
||||
@defgroup ipressuresensor IPressureSensor
|
||||
@defgroup ipressuresensor iPressureSensor
|
||||
@ingroup byif
|
||||
@brief Implements IPressureSensor
|
||||
@brief Implements iPressureSensor
|
||||
|
||||
|
||||
@defgroup itemperaturesensor ITemperatureSensor
|
||||
@defgroup itemperaturesensor iTemperatureSensor
|
||||
@ingroup byif
|
||||
@brief Implements ITemperatureSensor
|
||||
@brief Implements iTemperatureSensor
|
||||
|
||||
|
||||
@defgroup iadc IADC
|
||||
@defgroup iadc iADC
|
||||
@ingroup byif
|
||||
@brief Implements IADC
|
||||
@brief Implements iADC
|
||||
|
||||
|
||||
@defgroup ico2sensor ICOSensor
|
||||
|
@ -2,4 +2,4 @@ set (libname "hlg150h")
|
||||
set (libdescription "150W Constant Voltage/current LED Driver")
|
||||
set (module_src ${libname}.cxx mraa-utils.cxx)
|
||||
set (module_hpp ${libname}.hpp)
|
||||
upm_module_init(mraa interfaces)
|
||||
upm_module_init(mraa core)
|
||||
|
@ -18,7 +18,7 @@ HLG150H::HLG150H(int pinRelay, int pinPWM)
|
||||
status = pwmBrightness->enable(true);
|
||||
status = pwmBrightness->period_us(PWM_PERIOD);
|
||||
if (status != mraa::SUCCESS)
|
||||
UPM_THROW("pwm config failed.");
|
||||
throw std::runtime_error(std::string(__FUNCTION__) + " : pwm config failed.");
|
||||
dutyPercent = getBrightness();
|
||||
isPoweredShadow = dutyPercent > 10;
|
||||
}
|
||||
@ -65,7 +65,7 @@ void HLG150H::setBrightness(int dutyPercent)
|
||||
status = pwmBrightness->pulsewidth_us(dutyUs);
|
||||
// std::cout << "Brightness = " << dutyPercent << "%, duty = " << dutyUs << "us" << std::endl;
|
||||
if (status != mraa::SUCCESS)
|
||||
UPM_THROW("setBrightness failed");
|
||||
throw std::runtime_error(std::string(__FUNCTION__) + " : setBrightness failed");
|
||||
|
||||
}
|
||||
|
||||
|
@ -22,7 +22,7 @@
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "interfaces/iLightController.hpp"
|
||||
#include "iLightController.hpp"
|
||||
// #include "mraa/gpio.hpp"
|
||||
#include "mraa/pwm.hpp"
|
||||
|
||||
@ -60,7 +60,6 @@ public:
|
||||
~HLG150H();
|
||||
|
||||
virtual int getBrightness();
|
||||
const char* getModuleName() { return "hlg150h"; }
|
||||
void setPowerOn();
|
||||
void setPowerOff();
|
||||
bool isPowered();
|
||||
|
@ -1,11 +1,10 @@
|
||||
%include "../common_top.i"
|
||||
%include "iLightController.i"
|
||||
|
||||
/* BEGIN Java syntax ------------------------------------------------------- */
|
||||
#ifdef SWIGJAVA
|
||||
%include "arrays_java.i";
|
||||
%include "../java_buffer.i"
|
||||
%typemap(javaimports) SWIGTYPE %{import upm_interfaces.*;%}
|
||||
%import "../interfaces/javaupm_iLightController.i"
|
||||
|
||||
JAVA_JNI_LOADLIBRARY(javaupm_hlg150h)
|
||||
#endif
|
||||
|
@ -35,7 +35,7 @@ void MraaUtils::setGpio(int pin, int level)
|
||||
mraa::Gpio gpio(pin);
|
||||
gpio.dir(mraa::DIR_OUT);
|
||||
if (gpio.write(level) != mraa::SUCCESS)
|
||||
UPM_THROW("gpio write failed");
|
||||
throw std::runtime_error(std::string(__FUNCTION__) + " : gpio write failed");
|
||||
}
|
||||
|
||||
|
||||
|
@ -6,4 +6,4 @@ upm_mixed_module_init (NAME ims
|
||||
CPP_SRC ims.cxx
|
||||
FTI_SRC ims_fti.c
|
||||
CPP_WRAPS_C
|
||||
REQUIRES mraa utilities-c)
|
||||
REQUIRES mraa core utilities-c)
|
||||
|
@ -22,6 +22,7 @@
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
@ -29,12 +30,74 @@
|
||||
|
||||
using namespace upm;
|
||||
|
||||
/* Optional serializer */
|
||||
static std::string OptionalData(IMS * inst)
|
||||
{
|
||||
if (inst == NULL) return "";
|
||||
|
||||
std::stringstream ss;
|
||||
ss << "\"ims version\" : \"" << inst->get_version() << "\"";
|
||||
return "";
|
||||
}
|
||||
|
||||
IMS::IMS(std::string init_str): iMraa(init_str)
|
||||
{
|
||||
}
|
||||
|
||||
IMS::IMS(int16_t i2c_bus, int16_t i2c_address) :
|
||||
_dev(ims_init(i2c_bus, i2c_address))
|
||||
{
|
||||
if (_dev == NULL)
|
||||
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||
": failed to initialize sensor, check syslog");
|
||||
|
||||
/* Added for interface implementation */
|
||||
AddSource("light", "normalized (0.0->1.0)");
|
||||
AddSource("temperature", "c");
|
||||
AddSource("moisture", "normalized (0.0->1.0)");
|
||||
|
||||
/* Option for interface implementation, can be used to add
|
||||
* additional JsonDefinition serializer methods for this class*/
|
||||
_child_value_serializers[(t_getJson)&OptionalData] = this;
|
||||
}
|
||||
|
||||
/* Added for interface implementation */
|
||||
std::map<std::string, float> IMS::LightForSources(std::vector<std::string> sources)
|
||||
{
|
||||
std::map<std::string, float> ret;
|
||||
|
||||
if (std::find(sources.begin(), sources.end(), "light") != sources.end())
|
||||
{
|
||||
uint16_t val = get_light();
|
||||
ret["light"] = (float)val/std::numeric_limits<typeof(val)>::max();
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Added for interface implementation */
|
||||
std::map<std::string, float> IMS::TemperatureForSources(std::vector<std::string> sources)
|
||||
{
|
||||
std::map<std::string, float> ret;
|
||||
|
||||
if (std::find(sources.begin(), sources.end(), "temperature") != sources.end())
|
||||
ret["temperature"] = get_temperature();
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Added for interface implementation */
|
||||
std::map<std::string, float> IMS::MoistureForSources(std::vector<std::string> sources)
|
||||
{
|
||||
std::map<std::string, float> ret;
|
||||
|
||||
if (std::find(sources.begin(), sources.end(), "moisture") != sources.end())
|
||||
{
|
||||
uint16_t val = get_moisture();
|
||||
ret["light"] = (float)val/std::numeric_limits<typeof(val)>::max();
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint16_t IMS::get_version()
|
||||
|
@ -24,7 +24,13 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <typeinfo>
|
||||
|
||||
#include "ims.h"
|
||||
#include "iMraa.hpp"
|
||||
#include "iLightSensor.hpp"
|
||||
#include "iMoistureSensor.hpp"
|
||||
#include "iTemperatureSensor.hpp"
|
||||
|
||||
namespace upm {
|
||||
/**
|
||||
@ -55,7 +61,12 @@ namespace upm {
|
||||
* @snippet ims.cxx Interesting
|
||||
*/
|
||||
|
||||
class IMS {
|
||||
class IMS :
|
||||
public virtual iMraa,
|
||||
public virtual iLightSensor,
|
||||
public virtual iMoistureSensor,
|
||||
public virtual iTemperatureSensor
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* I2C Moisture Sensor constructor
|
||||
@ -68,11 +79,20 @@ class IMS {
|
||||
*/
|
||||
IMS(int16_t i2c_bus, int16_t i2c_address = IMS_ADDRESS_DEFAULT);
|
||||
|
||||
IMS(std::string init_str);
|
||||
|
||||
/**
|
||||
* IMS destructor
|
||||
*/
|
||||
virtual ~IMS() {};
|
||||
|
||||
virtual std::string Name () {return "IMS";}
|
||||
virtual std::string Description () {return "Catnip Electronics I2C moisture sensor";}
|
||||
|
||||
virtual std::map<std::string, float> LightForSources(std::vector<std::string> sources);
|
||||
virtual std::map<std::string, float> TemperatureForSources(std::vector<std::string> sources);
|
||||
virtual std::map<std::string, float> MoistureForSources(std::vector<std::string> sources);
|
||||
|
||||
/**
|
||||
* Write I2C Moisture Sensor registers
|
||||
* @param cmd Write command
|
||||
|
@ -1,4 +1,8 @@
|
||||
%include "../common_top.i"
|
||||
%include "iMraa.i"
|
||||
%include "iLightSensor.i"
|
||||
%include "iMoistureSensor.i"
|
||||
%include "iTemperatureSensor.i"
|
||||
|
||||
/* BEGIN Java syntax ------------------------------------------------------- */
|
||||
#ifdef SWIGJAVA
|
||||
|
@ -1,22 +0,0 @@
|
||||
set (libname "interfaces")
|
||||
set (libdescription "CXX Interface Library")
|
||||
set (module_src ${libname}.cxx)
|
||||
|
||||
upm_module_init()
|
||||
|
||||
# Add a PUBLIC include directory to the CMAKE src dir
|
||||
target_include_directories (${libname} PUBLIC ${CMAKE_SOURCE_DIR}/src)
|
||||
|
||||
# Don't add the hpp files with upm_module_init, this allows
|
||||
# them to be installed separately
|
||||
set (module_hpp iADC.hpp
|
||||
iCO2Sensor.hpp
|
||||
iHumiditySensor.hpp
|
||||
iLightController.hpp
|
||||
iLightSensor.hpp
|
||||
iModuleStatus.hpp
|
||||
iPressureSensor.hpp
|
||||
iTemperatureSensor.hpp)
|
||||
# Install interfaces headers a bit differently
|
||||
install (FILES ${module_hpp} DESTINATION include/upm/${libname}
|
||||
COMPONENT ${CMAKE_PROJECT_NAME})
|
@ -1,43 +0,0 @@
|
||||
/*
|
||||
* Author: Henry Bruce <henry.bruce@intel.com>
|
||||
* Copyright (c) 2015 Intel Corporation.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
|
||||
#pragma once
|
||||
#include "iModuleStatus.hpp"
|
||||
|
||||
namespace upm
|
||||
{
|
||||
/**
|
||||
* @brief Interface for CO Sensor
|
||||
*/
|
||||
|
||||
class ICO2Sensor : virtual public IModuleStatus
|
||||
{
|
||||
public:
|
||||
virtual uint16_t getPpm() = 0;
|
||||
virtual ~ICO2Sensor() {}
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -1,42 +0,0 @@
|
||||
/*
|
||||
* Author: Henry Bruce <henry.bruce@intel.com>
|
||||
* Copyright (c) 2015 Intel Corporation.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "iModuleStatus.hpp"
|
||||
|
||||
namespace upm
|
||||
{
|
||||
/**
|
||||
* @brief Interface for Humidity Sensors
|
||||
*/
|
||||
|
||||
class IHumiditySensor : virtual public IModuleStatus
|
||||
{
|
||||
public:
|
||||
virtual int getHumidityRelative () = 0;
|
||||
virtual ~IHumiditySensor() {}
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -1,61 +0,0 @@
|
||||
/*
|
||||
* Author: Henry Bruce <henry.bruce@intel.com>
|
||||
* Copyright (c) 2015 Intel Corporation.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include "iModuleStatus.hpp"
|
||||
|
||||
namespace upm
|
||||
{
|
||||
/**
|
||||
* @brief ILightSensor Interface for Light Sensors
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @brief Interface for Light Sensors
|
||||
|
||||
* This interface is used to represent light sensors
|
||||
|
||||
* @snippet light-sensor.cxx Interesting
|
||||
*/
|
||||
|
||||
class ILightSensor : virtual public IModuleStatus
|
||||
{
|
||||
public:
|
||||
|
||||
/**
|
||||
* Get visible illuminance in Lux.
|
||||
*
|
||||
* @return double visible illuminance in Lux
|
||||
*/
|
||||
virtual double getVisibleLux() = 0;
|
||||
|
||||
|
||||
virtual ~ILightSensor() {}
|
||||
};
|
||||
}
|
||||
|
@ -1,53 +0,0 @@
|
||||
/*
|
||||
* Author: Henry Bruce <henry.bruce@intel.com>
|
||||
* Copyright (c) 2014 Intel Corporation.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
namespace upm
|
||||
{
|
||||
/**
|
||||
* @brief Interface for Module Status. Sensor and Actuactor Interfaces Derive from this Interface.
|
||||
*/
|
||||
|
||||
#define UPM_THROW(msg) throw std::runtime_error(std::string(__FUNCTION__) + ": " + (msg))
|
||||
|
||||
class IModuleStatus
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Returns name of module. This is the string in library name after libupm_
|
||||
|
||||
* @return name of module
|
||||
*/
|
||||
virtual const char* getModuleName() = 0;
|
||||
|
||||
virtual ~IModuleStatus() {}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,44 +0,0 @@
|
||||
/*
|
||||
* Author: Henry Bruce <henry.bruce@intel.com>
|
||||
* Copyright (c) 2014 Intel Corporation.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include "iModuleStatus.hpp"
|
||||
|
||||
namespace upm
|
||||
{
|
||||
/*
|
||||
* @brief Interface for Pressue Sensors
|
||||
*/
|
||||
|
||||
class IPressureSensor : virtual public IModuleStatus
|
||||
{
|
||||
public:
|
||||
virtual int getPressurePa() = 0;
|
||||
virtual ~IPressureSensor() {}
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -1,42 +0,0 @@
|
||||
/*
|
||||
* Author: Henry Bruce <henry.bruce@intel.com>
|
||||
* Copyright (c) 2015 Intel Corporation.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "iModuleStatus.hpp"
|
||||
|
||||
namespace upm
|
||||
{
|
||||
/**
|
||||
* @brief Interface for Temperature Sensors
|
||||
*/
|
||||
|
||||
class ITemperatureSensor : virtual public IModuleStatus
|
||||
{
|
||||
public:
|
||||
virtual int getTemperatureCelsius () = 0;
|
||||
virtual ~ITemperatureSensor() {}
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -1 +0,0 @@
|
||||
#include "iLightSensor.hpp"
|
@ -1,18 +0,0 @@
|
||||
%include "../common_top.i"
|
||||
|
||||
/* BEGIN Java syntax ------------------------------------------------------- */
|
||||
#ifdef SWIGJAVA
|
||||
%import "../_upm.i"
|
||||
|
||||
%include javaupm_iModuleStatus.i
|
||||
%include javaupm_iADC.i
|
||||
%include javaupm_iCO2Sensor.i
|
||||
%include javaupm_iHumiditySensor.i
|
||||
%include javaupm_iLightController.i
|
||||
%include javaupm_iLightSensor.i
|
||||
%include javaupm_iPressureSensor.i
|
||||
%include javaupm_iTemperatureSensor.i
|
||||
|
||||
JAVA_JNI_LOADLIBRARY(javaupm_interfaces)
|
||||
#endif
|
||||
/* END Java syntax */
|
@ -1,11 +0,0 @@
|
||||
#if SWIG_VERSION >= 0x030009
|
||||
%include <swiginterface.i>
|
||||
%interface_impl(upm::IADC);
|
||||
#endif
|
||||
%include "interfaces.i"
|
||||
%include "javaupm_iModuleStatus.i"
|
||||
|
||||
%include "iADC.hpp"
|
||||
%{
|
||||
#include "iADC.hpp"
|
||||
%}
|
@ -1,12 +0,0 @@
|
||||
#if SWIG_VERSION >= 0x030009
|
||||
%include <swiginterface.i>
|
||||
%interface_impl(upm::ICO2Sensor);
|
||||
#endif
|
||||
%include "stdint.i"
|
||||
%include "interfaces.i"
|
||||
%include "javaupm_iModuleStatus.i"
|
||||
|
||||
%include "iCO2Sensor.hpp"
|
||||
%{
|
||||
#include "iCO2Sensor.hpp"
|
||||
%}
|
@ -1,11 +0,0 @@
|
||||
#if SWIG_VERSION >= 0x030009
|
||||
%include <swiginterface.i>
|
||||
%interface_impl(upm::IHumiditySensor);
|
||||
#endif
|
||||
%include "interfaces.i"
|
||||
%include "javaupm_iModuleStatus.i"
|
||||
|
||||
%include "iHumiditySensor.hpp"
|
||||
%{
|
||||
#include "iHumiditySensor.hpp"
|
||||
%}
|
@ -1,11 +0,0 @@
|
||||
#if SWIG_VERSION >= 0x030009
|
||||
%include <swiginterface.i>
|
||||
%interface_impl(upm::ILightController);
|
||||
#endif
|
||||
%include "interfaces.i"
|
||||
%include "javaupm_iModuleStatus.i"
|
||||
|
||||
%{
|
||||
#include "iLightController.hpp"
|
||||
%}
|
||||
%include "iLightController.hpp"
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user