mirror of
https://github.com/eclipse/upm.git
synced 2025-07-01 09:21:12 +03:00
upmc: Updates for building C modules w/base UPM
Test commit for building C UPM modules. * Added C include directory * Added C utilities directory * Rename C++ upm.h -> upm.hpp to make room for C upm.h * Added upm_mixed_module_init function to src/CMakeLists.txt. This function takes filesnames similar to upm_module_init and does a bit of processing before calling upm_module_init. * Added c example directory. Changed c++ example names. * Added dfrph implemention for testing (C++ wraps C). Added mraa to .pc requires for dfrph. Tested against stand-alone project. Added dfrph c example. * Update implemention of pkg-config file generation. * Added two cmake cache variables: BUILDCPP and BUILDFTI * Removed src from swig_add_module calls, added libname to swig_link_libraries calls. Shrinks swig'ed binaries by ~13%. * Added install target in upm/CMakeLists.txt to install C header, directory. Is this where we want this? * C FTI header directory is include/fti Signed-off-by: Noel Eck <noel.eck@intel.com>
This commit is contained in:
@ -3,7 +3,7 @@ macro (file_to_list readfile outlist)
|
||||
STRING(REGEX REPLACE ";" "\\\\;" contents "${contents}")
|
||||
STRING(REGEX REPLACE "\n" ";" contents "${contents}")
|
||||
set("${outlist}" "${contents}" )
|
||||
endmacro()
|
||||
endmacro(file_to_list)
|
||||
|
||||
file_to_list ("javaswig_blacklist" JAVASWIG_BLACKLIST)
|
||||
file_to_list ("pythonswig_blacklist" PYTHONSWIG_BLACKLIST)
|
||||
@ -18,15 +18,15 @@ macro(subdirlist result curdir)
|
||||
endif()
|
||||
endforeach()
|
||||
set(${result} ${dirlist})
|
||||
endmacro()
|
||||
endmacro(subdirlist)
|
||||
|
||||
macro (upm_CREATE_INSTALL_PKGCONFIG generated_file install_location)
|
||||
macro (upm_create_install_pkgconfig generated_file install_location)
|
||||
configure_file (${PROJECT_SOURCE_DIR}/src/pkgconfig.in
|
||||
${CMAKE_CURRENT_BINARY_DIR}/${generated_file} @ONLY)
|
||||
install (FILES ${CMAKE_CURRENT_BINARY_DIR}/${generated_file} DESTINATION ${install_location})
|
||||
endmacro (upm_CREATE_INSTALL_PKGCONFIG)
|
||||
endmacro(upm_create_install_pkgconfig)
|
||||
|
||||
macro(upm_SWIG_PYTHON)
|
||||
macro(upm_swig_python)
|
||||
if (BUILDSWIGPYTHON)
|
||||
|
||||
include_directories (
|
||||
@ -35,8 +35,9 @@ macro(upm_SWIG_PYTHON)
|
||||
|
||||
set_source_files_properties (pyupm_${libname}.i PROPERTIES CPLUSPLUS ON)
|
||||
set_source_files_properties (pyupm_${libname}.i PROPERTIES SWIG_FLAGS "-I${CMAKE_CURRENT_BINARY_DIR}/..")
|
||||
swig_add_module (pyupm_${libname} python pyupm_${libname}.i ${module_src})
|
||||
swig_link_libraries (pyupm_${libname} ${PYTHON_LIBRARIES} ${MRAA_LIBRARIES})
|
||||
|
||||
swig_add_module (pyupm_${libname} python pyupm_${libname}.i)
|
||||
swig_link_libraries (pyupm_${libname} ${PYTHON_LIBRARIES} ${MRAA_LIBRARIES} ${libname})
|
||||
target_include_directories ( ${SWIG_MODULE_pyupm_${libname}_REAL_NAME}
|
||||
PUBLIC
|
||||
"${PYTHON_INCLUDE_PATH}"
|
||||
@ -47,9 +48,9 @@ macro(upm_SWIG_PYTHON)
|
||||
DESTINATION ${LIB_INSTALL_DIR}/python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}/site-packages/
|
||||
COMPONENT ${libname})
|
||||
endif()
|
||||
endmacro()
|
||||
endmacro(upm_swig_python)
|
||||
|
||||
macro(upm_SWIG_NODE)
|
||||
macro(upm_swig_node)
|
||||
if (BUILDSWIGNODE)
|
||||
# SWIG treats SWIG_FLAGS as a list and not a string so semicolon seperation is
|
||||
# required. This hardcodes V8_VERSION to be <10 but I assume that's not going
|
||||
@ -68,8 +69,8 @@ macro(upm_SWIG_NODE)
|
||||
|
||||
set_property (SOURCE jsupm_${libname}.i PROPERTY SWIG_FLAGS "-node" "-DV8_VERSION=${V8_VERSION_HEX}")
|
||||
set_source_files_properties (jsupm_${libname}.i PROPERTIES CPLUSPLUS ON)
|
||||
swig_add_module (jsupm_${libname} javascript jsupm_${libname}.i ${module_src})
|
||||
swig_link_libraries (jsupm_${libname} ${MRAA_LIBRARIES} ${NODE_LIBRARIES})
|
||||
swig_add_module (jsupm_${libname} javascript jsupm_${libname}.i)
|
||||
swig_link_libraries (jsupm_${libname} ${MRAA_LIBRARIES} ${NODE_LIBRARIES} ${libname})
|
||||
target_include_directories ( ${SWIG_MODULE_jsupm_${libname}_REAL_NAME}
|
||||
PUBLIC
|
||||
"${NODE_INCLUDE_DIRS}"
|
||||
@ -109,9 +110,9 @@ macro(upm_SWIG_NODE)
|
||||
install (FILES ${CMAKE_CURRENT_BINARY_DIR}/jsupm_${libname}.node
|
||||
DESTINATION ${NODE_MODULE_INSTALL_PATH} COMPONENT ${libname})
|
||||
endif()
|
||||
endmacro()
|
||||
endmacro(upm_swig_node)
|
||||
|
||||
macro(upm_SWIG_JAVA)
|
||||
macro(upm_swig_java)
|
||||
if (BUILDSWIGJAVA)
|
||||
|
||||
FIND_PACKAGE (JNI REQUIRED)
|
||||
@ -125,8 +126,8 @@ macro(upm_SWIG_JAVA)
|
||||
|
||||
set_source_files_properties (javaupm_${libname}.i PROPERTIES CPLUSPLUS ON)
|
||||
set_source_files_properties (javaupm_${libname}.i PROPERTIES SWIG_FLAGS ";-package;upm_${libname};-I${CMAKE_BINARY_DIR}/src")
|
||||
swig_add_module (javaupm_${libname} java javaupm_${libname}.i ${module_src})
|
||||
swig_link_libraries (javaupm_${libname} ${MRAAJAVA_LIBRARIES} ${MRAA_LIBRARIES} ${JAVA_LIBRARIES})
|
||||
swig_add_module (javaupm_${libname} java javaupm_${libname}.i)
|
||||
swig_link_libraries (javaupm_${libname} ${MRAAJAVA_LIBRARIES} ${MRAA_LIBRARIES} ${JAVA_LIBRARIES} ${libname})
|
||||
target_include_directories ( ${SWIG_MODULE_javaupm_${libname}_REAL_NAME}
|
||||
PUBLIC
|
||||
"${JAVA_INCLUDE_DIRS}"
|
||||
@ -158,7 +159,7 @@ macro(upm_SWIG_JAVA)
|
||||
)
|
||||
|
||||
endif()
|
||||
endmacro()
|
||||
endmacro(upm_swig_java)
|
||||
|
||||
macro(upm_doxygen)
|
||||
if (DOXYGEN_FOUND)
|
||||
@ -173,7 +174,7 @@ macro(upm_doxygen)
|
||||
add_dependencies (${libname} doc)
|
||||
endif ()
|
||||
endif ()
|
||||
endmacro()
|
||||
endmacro(upm_doxygen)
|
||||
|
||||
if (SWIG_FOUND)
|
||||
if (BUILDSWIGPYTHON)
|
||||
@ -212,11 +213,96 @@ if (SWIG_FOUND)
|
||||
endif ()
|
||||
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/package.json
|
||||
DESTINATION ${NODE_MODULE_INSTALL_PATH} COMPONENT ${libname})
|
||||
endmacro()
|
||||
endmacro(createpackagejson)
|
||||
endif(BUILDSWIGNODE)
|
||||
endif()
|
||||
|
||||
# Process C/C++ sensor modules
|
||||
# This function pre-processes sensor library input and hands off the
|
||||
# necessary global variables to upm_module_init for library creation,
|
||||
# documenation, swigging, etc...
|
||||
function (UPM_MIXED_MODULE_INIT)
|
||||
# CPP_WRAPS_C -> Set to have CPP library link to C library
|
||||
# DESCRIPTION -> Library description string
|
||||
# CPP_HDR -> List of CPP header files
|
||||
# CPP_SRC -> List of CPP source files
|
||||
# C_HDR -> List of C header files
|
||||
# C_SRC -> List of C source files
|
||||
# FTI_SRC -> List of C FTI source files
|
||||
# REQUIRES -> List requires libraries for pkg-config
|
||||
set (options CPP_WRAPS_C)
|
||||
set (oneValueArgs NAME DESCRIPTION)
|
||||
set (multiValueArgs CPP_HDR CPP_SRC C_HDR C_SRC FTI_SRC FTI_HDR REQUIRES)
|
||||
# Parse function parameters
|
||||
cmake_parse_arguments(UPM_MIXED_MODULE_INIT "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
|
||||
|
||||
# Set the description
|
||||
set (libdescription ${UPM_MIXED_MODULE_INIT_DESCRIPTION})
|
||||
|
||||
# Always build C libs first
|
||||
if (UPM_MIXED_MODULE_INIT_C_SRC)
|
||||
set (libname ${UPM_MIXED_MODULE_INIT_NAME})
|
||||
# Set the src and hpp variables for upm_module_init
|
||||
set (module_src ${UPM_MIXED_MODULE_INIT_C_SRC})
|
||||
set (module_hpp ${UPM_MIXED_MODULE_INIT_C_HDR})
|
||||
|
||||
# Create the reqlibname list
|
||||
string(REPLACE ";" " " reqlibname "${UPM_MIXED_MODULE_INIT_REQUIRES}")
|
||||
# Append upm-utilities to the reqlibs
|
||||
set (reqlibname "${reqlibname} upm-utilities")
|
||||
|
||||
# If building FTI, and FTI src exists, add it in
|
||||
if (BUILDFTI AND UPM_MIXED_MODULE_INIT_FTI_SRC)
|
||||
#set (module_src ${UPM_MIXED_MODULE_INIT_C_SRC} ${UPM_MIXED_MODULE_INIT_FTI_SRC})
|
||||
list (APPEND module_src ${UPM_MIXED_MODULE_INIT_FTI_SRC})
|
||||
message ( INFO " XXX BUILDING FTI ${UPM_MIXED_MODULE_INIT_FTI_SRC}")
|
||||
endif (BUILDFTI AND UPM_MIXED_MODULE_INIT_FTI_SRC)
|
||||
message ( INFO " XXX BUILDING C src ${module_src}")
|
||||
|
||||
# Add include directories for C
|
||||
include_directories (${UPM_COMMON_HEADER_DIRS}
|
||||
${CMAKE_SOURCE_DIR}/src/utilities)
|
||||
|
||||
# Set a flag to tell upm_module_init that it's building a C library
|
||||
set (IS_C_LIBRARY TRUE)
|
||||
upm_module_init()
|
||||
endif (UPM_MIXED_MODULE_INIT_C_SRC)
|
||||
|
||||
# Build C++ if enabled AND C++ headers exist
|
||||
if (BUILDCPP AND UPM_MIXED_MODULE_INIT_CPP_HDR)
|
||||
# Set the src and hpp variables for upm_module_init
|
||||
set (module_src ${UPM_MIXED_MODULE_INIT_CPP_SRC})
|
||||
set (module_hpp ${UPM_MIXED_MODULE_INIT_CPP_HDR})
|
||||
|
||||
# Create the reqlibname list
|
||||
string(REPLACE ";" " " reqlibname "${UPM_MIXED_MODULE_INIT_REQUIRES}")
|
||||
|
||||
# Reset the libname (upm_module_init can change it)
|
||||
set (libname ${UPM_MIXED_MODULE_INIT_NAME})
|
||||
unset (IS_C_LIBRARY)
|
||||
message ( INFO " XXX BUILDING C++ src ${module_src}")
|
||||
upm_module_init()
|
||||
|
||||
# If the C++ wraps the C target, add the C target as a dependency
|
||||
if (UPM_MIXED_MODULE_INIT_CPP_WRAPS_C)
|
||||
target_link_libraries(${libname} ${libname}-c)
|
||||
endif (UPM_MIXED_MODULE_INIT_CPP_WRAPS_C)
|
||||
endif (BUILDCPP AND UPM_MIXED_MODULE_INIT_CPP_HDR)
|
||||
endfunction (UPM_MIXED_MODULE_INIT)
|
||||
|
||||
macro(upm_module_init)
|
||||
set (basename ${libname})
|
||||
|
||||
# If this is a C library, handle different collateral naming
|
||||
if (IS_C_LIBRARY)
|
||||
set (libname ${libname}-c)
|
||||
set (libprefix upmc-)
|
||||
set (pcname upmc-${basename}.pc)
|
||||
else ()
|
||||
set (libprefix upm-)
|
||||
set (pcname upm-${basename}.pc)
|
||||
endif (IS_C_LIBRARY)
|
||||
|
||||
link_directories (${MRAA_LIBDIR})
|
||||
add_library (${libname} SHARED ${module_src})
|
||||
foreach (linkflag ${ARGN})
|
||||
@ -226,12 +312,15 @@ macro(upm_module_init)
|
||||
target_link_libraries (${libname} ${MRAA_LIBRARIES})
|
||||
set_target_properties(
|
||||
${libname}
|
||||
PROPERTIES PREFIX "libupm-"
|
||||
PROPERTIES PREFIX lib${libprefix}
|
||||
OUTPUT_NAME ${basename}
|
||||
SOVERSION ${upm_VERSION_MAJOR}
|
||||
VERSION ${upm_VERSION_STRING}
|
||||
)
|
||||
upm_create_install_pkgconfig (upm-${libname}.pc ${LIB_INSTALL_DIR}/pkgconfig)
|
||||
if (SWIG_FOUND)
|
||||
upm_create_install_pkgconfig (${pcname} ${LIB_INSTALL_DIR}/pkgconfig)
|
||||
|
||||
# Don't SWIG C
|
||||
if (SWIG_FOUND AND NOT IS_C_LIBRARY)
|
||||
if (NOT ";${PYTHONSWIG_BLACKLIST};" MATCHES ";${libname};")
|
||||
upm_swig_python()
|
||||
endif()
|
||||
@ -241,11 +330,14 @@ macro(upm_module_init)
|
||||
if (NOT ";${JAVASWIG_BLACKLIST};" MATCHES ";${libname};")
|
||||
upm_swig_java()
|
||||
endif()
|
||||
endif()
|
||||
if (BUILDDOC)
|
||||
endif (SWIG_FOUND AND NOT IS_C_LIBRARY)
|
||||
|
||||
# Skip doxygen run on C (for now)
|
||||
if (BUILDDOC AND NOT IS_C_LIBRARY)
|
||||
upm_doxygen()
|
||||
endif()
|
||||
install(TARGETS ${libname} DESTINATION ${LIB_INSTALL_DIR})
|
||||
|
||||
install (TARGETS ${libname} DESTINATION ${LIB_INSTALL_DIR})
|
||||
install (FILES ${module_hpp} DESTINATION include/upm COMPONENT ${libname})
|
||||
|
||||
if (IPK)
|
||||
|
@ -1,5 +1,9 @@
|
||||
set (libname "dfrph")
|
||||
set (libdescription "upm dfrobot pH sensors")
|
||||
set (module_src ${libname}.cxx)
|
||||
set (module_hpp ${libname}.hpp)
|
||||
upm_module_init()
|
||||
upm_mixed_module_init (NAME dfrph
|
||||
DESCRIPTION "upm dfrobot pH sensor"
|
||||
C_HDR dfrph.h
|
||||
C_SRC dfrph.c
|
||||
CPP_HDR dfrph.hpp
|
||||
CPP_SRC dfrph.cxx
|
||||
FTI_SRC dfrph_fti.c
|
||||
CPP_WRAPS_C
|
||||
REQUIRES mraa)
|
||||
|
103
src/dfrph/dfrph.c
Normal file
103
src/dfrph/dfrph.c
Normal file
@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Author:
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "dfrph.h"
|
||||
|
||||
dfrph_context dfrph_init(int16_t pin)
|
||||
{
|
||||
dfrph_context dev =
|
||||
(dfrph_context) malloc(sizeof(struct _dfrph_context));
|
||||
|
||||
if(dev == NULL) return NULL;
|
||||
|
||||
/* Init aio pin */
|
||||
dev->aio = mraa_aio_init(pin);
|
||||
|
||||
/* Set the ref, offset, and scale */
|
||||
dev->m_aref = 5.0;
|
||||
dev->m_count_offset = 0.0;
|
||||
dev->m_count_scale = 1.0;
|
||||
|
||||
if(dev->aio == NULL) {
|
||||
free(dev);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return dev;
|
||||
}
|
||||
|
||||
void dfrph_close(dfrph_context dev)
|
||||
{
|
||||
mraa_aio_close(dev->aio);
|
||||
free(dev);
|
||||
}
|
||||
|
||||
upm_result_t dfrph_set_offset(const dfrph_context dev, float offset)
|
||||
{
|
||||
dev->m_count_offset = offset;
|
||||
return UPM_SUCCESS;
|
||||
}
|
||||
|
||||
upm_result_t dfrph_set_scale(const dfrph_context dev, float scale)
|
||||
{
|
||||
dev->m_count_scale = scale;
|
||||
return UPM_SUCCESS;
|
||||
}
|
||||
|
||||
upm_result_t dfrph_get_raw_volts(const dfrph_context dev, float *volts)
|
||||
{
|
||||
*volts = mraa_aio_read_float(dev->aio);
|
||||
if (*volts == -1.0) return UPM_ERROR_OPERATION_FAILED;
|
||||
|
||||
/* Scale by aref */
|
||||
*volts *= dev->m_aref;
|
||||
|
||||
return UPM_SUCCESS;
|
||||
}
|
||||
|
||||
upm_result_t dfrph_get_ph(const dfrph_context dev, float *value)
|
||||
{
|
||||
/* Read counts */
|
||||
int counts = mraa_aio_read(dev->aio);
|
||||
|
||||
/* Get max adc value range 1023, 2047, 4095, etc... */
|
||||
float max_adc = (1 << mraa_aio_get_bit(dev->aio)) - 1;
|
||||
|
||||
/* Apply raw scale */
|
||||
*value = counts * dev->m_count_scale;
|
||||
|
||||
/* Apply raw offset */
|
||||
*value += dev->m_count_offset * dev->m_count_scale;
|
||||
|
||||
/* Normalize the value */
|
||||
*value /= max_adc;
|
||||
|
||||
/* Vmax for sensor is 0.8 * Vref, so scale by 1/0.8 = 1.25 */
|
||||
*value *= 1.25 * 14; /* Convert to pH */
|
||||
|
||||
return UPM_SUCCESS;
|
||||
}
|
@ -23,52 +23,54 @@
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "dfrph.hpp"
|
||||
|
||||
using namespace std;
|
||||
using namespace upm;
|
||||
|
||||
DFRPH::DFRPH(int pin, float aref) :
|
||||
m_aio(pin)
|
||||
DFRPH::DFRPH(int pin, float vref) : _dev(dfrph_init(pin))
|
||||
{
|
||||
m_aRes = (1 << m_aio.getBit());
|
||||
m_aref = aref;
|
||||
|
||||
m_offset = 0.0;
|
||||
if (_dev == NULL)
|
||||
throw std::invalid_argument(std::string(__FUNCTION__) +
|
||||
": dfrph_init() failed, invalid pin?");
|
||||
}
|
||||
|
||||
DFRPH::~DFRPH()
|
||||
{
|
||||
}
|
||||
|
||||
float DFRPH::volts()
|
||||
{
|
||||
int val = m_aio.read();
|
||||
|
||||
return(val * (m_aref / m_aRes));
|
||||
dfrph_close(_dev);
|
||||
}
|
||||
|
||||
void DFRPH::setOffset(float offset)
|
||||
{
|
||||
m_offset = offset;
|
||||
dfrph_set_offset(_dev, offset);
|
||||
}
|
||||
|
||||
void DFRPH::setScale(float scale)
|
||||
{
|
||||
dfrph_set_scale(_dev, scale);
|
||||
}
|
||||
|
||||
float DFRPH::volts()
|
||||
{
|
||||
float volts = 0.0;
|
||||
dfrph_get_raw_volts(_dev, &volts);
|
||||
return volts;
|
||||
}
|
||||
|
||||
float DFRPH::pH(unsigned int samples)
|
||||
{
|
||||
if (!samples)
|
||||
samples = 1;
|
||||
float ph_avg = 0.0;
|
||||
|
||||
float sum = 0.0;
|
||||
// Read at least 1 sample
|
||||
if (samples == 0) samples = 1;
|
||||
|
||||
for (int i=0; i<samples; i++)
|
||||
float ph = 0.0;
|
||||
for (int i =0; i < samples; i++)
|
||||
{
|
||||
sum += volts();
|
||||
usleep(20000);
|
||||
dfrph_get_ph(_dev, &ph);
|
||||
ph_avg += ph;
|
||||
}
|
||||
|
||||
sum /= samples;
|
||||
|
||||
// 3.5 is a 'magic' DFRobot number. Seems to work though :)
|
||||
return (3.5 * sum + m_offset);
|
||||
return ph_avg/samples;
|
||||
}
|
||||
|
97
src/dfrph/dfrph.h
Normal file
97
src/dfrph/dfrph.h
Normal file
@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Author:
|
||||
* 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 "upm.h"
|
||||
#include "mraa/aio.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* device context
|
||||
*/
|
||||
typedef struct _dfrph_context {
|
||||
/* mraa aio pin context */
|
||||
mraa_aio_context aio;
|
||||
/* ADC reference */
|
||||
float m_aref;
|
||||
/* Raw count offset */
|
||||
float m_count_offset;
|
||||
/* Raw count scale */
|
||||
float m_count_scale;
|
||||
} *dfrph_context;
|
||||
|
||||
/**
|
||||
* Initialize analog sensor
|
||||
* @param pin is Analog pin
|
||||
* @return sensor context as void pointer
|
||||
*/
|
||||
dfrph_context dfrph_init(int16_t pin);
|
||||
|
||||
/**
|
||||
* Analog sensor destructor
|
||||
* @param sensor context pointer deallocate memory
|
||||
*/
|
||||
void dfrph_close(dfrph_context dev);
|
||||
|
||||
/**
|
||||
* Set sensor offset. This offset is applied to the return value:
|
||||
* counts = counts + offset
|
||||
* @param dev sensor context pointer
|
||||
* @param offset count offset value used
|
||||
* @return Function result code
|
||||
*/
|
||||
upm_result_t dfrph_set_offset(const dfrph_context dev, float offset);
|
||||
|
||||
/**
|
||||
* Set sensor scale. This scale is applied to the return value:
|
||||
* counts = counts * scale
|
||||
* @param dev sensor context pointer
|
||||
* @param scale count scale value used
|
||||
* @return Function result code
|
||||
*/
|
||||
upm_result_t dfrph_set_scale(const dfrph_context dev, float scale);
|
||||
|
||||
/**
|
||||
* Get raw volts
|
||||
* @param dev sensor context pointer
|
||||
* @param volts Raw sensor voltage
|
||||
* @return Function result code
|
||||
*/
|
||||
upm_result_t dfrph_get_raw_volts(const dfrph_context dev, float *volts);
|
||||
|
||||
/**
|
||||
* Read value from sensor
|
||||
* @param dev sensor context pointer
|
||||
* @param value pointer to returned pH value from sensor
|
||||
* @return Function result code
|
||||
*/
|
||||
upm_result_t dfrph_get_ph(const dfrph_context dev, float *value);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
@ -25,7 +25,8 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <mraa/aio.hpp>
|
||||
|
||||
#include "dfrph.h"
|
||||
|
||||
namespace upm {
|
||||
/**
|
||||
@ -94,22 +95,15 @@ namespace upm {
|
||||
* DFRPH constructor
|
||||
*
|
||||
* @param pin Analog pin to use
|
||||
* @param aref Analog reference voltage; default is 5.0 V
|
||||
* @param vref Analog reference voltage; default is 5.0 V
|
||||
*/
|
||||
DFRPH(int pin, float aref=5.0);
|
||||
DFRPH(int pin, float vref = 5.0);
|
||||
|
||||
/**
|
||||
* DFRPH destructor
|
||||
*/
|
||||
~DFRPH();
|
||||
|
||||
/**
|
||||
* Returns the voltage detected on the analog pin
|
||||
*
|
||||
* @return The detected voltage
|
||||
*/
|
||||
float volts();
|
||||
|
||||
/**
|
||||
* Specifies the offset determined from calibration. The default
|
||||
* is 0.0.
|
||||
@ -118,6 +112,16 @@ namespace upm {
|
||||
*/
|
||||
void setOffset(float offset);
|
||||
|
||||
/**
|
||||
* Specifies the scale determined from calibration. The default
|
||||
* is 1.0.
|
||||
*
|
||||
* @param scale The scale value to use
|
||||
*/
|
||||
void setScale(float scale);
|
||||
|
||||
float volts();
|
||||
|
||||
/**
|
||||
* Take a number of samples and return the detected pH value. The
|
||||
* default number of samples is 15.
|
||||
@ -125,18 +129,10 @@ namespace upm {
|
||||
* @param samples The number of samples to average over, default 15
|
||||
* @return The pH value detected
|
||||
*/
|
||||
float pH(unsigned int samples=15);
|
||||
|
||||
protected:
|
||||
mraa::Aio m_aio;
|
||||
float pH(unsigned int samples = 15);
|
||||
|
||||
private:
|
||||
float m_aref;
|
||||
// ADC resolution
|
||||
int m_aRes;
|
||||
|
||||
// voltage offset
|
||||
float m_offset;
|
||||
dfrph_context _dev;
|
||||
};
|
||||
}
|
||||
|
||||
|
118
src/dfrph/dfrph_fti.c
Normal file
118
src/dfrph/dfrph_fti.c
Normal file
@ -0,0 +1,118 @@
|
||||
/*
|
||||
* Author:
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "dfrph.h"
|
||||
#include "upm_fti.h"
|
||||
#include "fti/upm_sensor.h"
|
||||
|
||||
/**
|
||||
* This file implements the Function Table Interface (FTI) for this sensor
|
||||
*/
|
||||
|
||||
const char upm_dfrph_name[] = "DFRPH";
|
||||
const char upm_dfrph_description[] = "Analog pH Meter Pro";
|
||||
const upm_protocol_t upm_dfrph_protocol[] = {UPM_ANALOG};
|
||||
const upm_sensor_t upm_dfrph_category[] = {UPM_PH};
|
||||
|
||||
// forward declarations
|
||||
const void* upm_dfrph_get_ft(upm_sensor_t sensor_type);
|
||||
void* upm_dfrph_init_str(const char* protocol, const char* params);
|
||||
void upm_dfrph_close(void* dev);
|
||||
const upm_sensor_descriptor_t upm_dfrph_get_descriptor();
|
||||
upm_result_t upm_dfrph_set_offset(const void* dev, float offset);
|
||||
upm_result_t upm_dfrph_set_scale(const void* dev, float scale);
|
||||
upm_result_t upm_dfrph_get_value(const void* dev, float *value);
|
||||
|
||||
/* This sensor implementes 2 function tables */
|
||||
/* 1. Generic base function table */
|
||||
static const upm_sensor_ft ft_gen =
|
||||
{
|
||||
.upm_sensor_init_name = &upm_dfrph_init_str,
|
||||
.upm_sensor_close = &upm_dfrph_close,
|
||||
.upm_sensor_get_descriptor = &upm_dfrph_get_descriptor
|
||||
};
|
||||
|
||||
/* 2. PH function table */
|
||||
static const upm_ph_ft ft_ph =
|
||||
{
|
||||
.upm_ph_set_offset = &upm_dfrph_set_offset,
|
||||
.upm_ph_set_scale = &upm_dfrph_set_scale,
|
||||
.upm_ph_get_value = &upm_dfrph_get_value
|
||||
};
|
||||
|
||||
const void* upm_dfrph_get_ft(upm_sensor_t sensor_type)
|
||||
{
|
||||
switch(sensor_type)
|
||||
{
|
||||
case UPM_SENSOR:
|
||||
return &ft_gen;
|
||||
case UPM_PH:
|
||||
return &ft_ph;
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void* upm_dfrph_init_str(const char* protocol, const char* params)
|
||||
{
|
||||
fprintf(stderr, "String initialization - not implemented, using ain0: %s\n", __FILENAME__);
|
||||
return dfrph_init(0);
|
||||
}
|
||||
|
||||
void upm_dfrph_close(void* dev)
|
||||
{
|
||||
dfrph_close(dev);
|
||||
}
|
||||
|
||||
const upm_sensor_descriptor_t upm_dfrph_get_descriptor()
|
||||
{
|
||||
/* Fill in the descriptor */
|
||||
upm_sensor_descriptor_t usd;
|
||||
usd.name = upm_dfrph_name;
|
||||
usd.description = upm_dfrph_description;
|
||||
usd.protocol_size = 1;
|
||||
usd.protocol = upm_dfrph_protocol;
|
||||
usd.category_size = 1;
|
||||
usd.category = upm_dfrph_category;
|
||||
|
||||
return usd;
|
||||
}
|
||||
|
||||
upm_result_t upm_dfrph_set_offset(const void* dev, float offset)
|
||||
{
|
||||
return dfrph_set_offset((dfrph_context)dev, offset);
|
||||
}
|
||||
|
||||
upm_result_t upm_dfrph_set_scale(const void* dev, float scale)
|
||||
{
|
||||
return dfrph_set_scale((dfrph_context)dev, scale);
|
||||
}
|
||||
|
||||
upm_result_t upm_dfrph_get_value(const void* dev, float *value)
|
||||
{
|
||||
return dfrph_get_ph((dfrph_context)dev, value);
|
||||
}
|
0
src/lcd/CMakeLists.txt
Executable file → Normal file
0
src/lcd/CMakeLists.txt
Executable file → Normal file
0
src/lcd/jsupm_i2clcd.i
Executable file → Normal file
0
src/lcd/jsupm_i2clcd.i
Executable file → Normal file
0
src/lcd/pyupm_i2clcd.i
Executable file → Normal file
0
src/lcd/pyupm_i2clcd.i
Executable file → Normal file
@ -3,11 +3,11 @@ exec_prefix=${prefix}
|
||||
libdir=${exec_prefix}/lib@LIB_SUFFIX@
|
||||
includedir=${prefix}/include
|
||||
|
||||
Name: upm-@libname@
|
||||
Description: upm lib @libname@
|
||||
Name: @libprefix@@basename@
|
||||
Description: @libdescription@
|
||||
Version: @upm_VERSION_STRING@
|
||||
|
||||
Libs: -L${libdir} -lupm-@libname@
|
||||
Libs: -L${libdir} -l@libprefix@@basename@
|
||||
Cflags: -I${includedir}/upm
|
||||
|
||||
Requires: @reqlibname@
|
||||
|
4
src/utilities/CMakeLists.txt
Normal file
4
src/utilities/CMakeLists.txt
Normal file
@ -0,0 +1,4 @@
|
||||
upm_mixed_module_init (NAME utilities
|
||||
DESCRIPTION "UPM Utilities Library"
|
||||
C_HDR upm_utilities.h
|
||||
C_SRC upm_utilities.c)
|
69
src/utilities/upm_utilities.c
Normal file
69
src/utilities/upm_utilities.c
Normal file
@ -0,0 +1,69 @@
|
||||
#include <upm_utilities.h>
|
||||
|
||||
void* upm_malloc(int mem_map, int size){
|
||||
void *mem;
|
||||
#if defined(linux)
|
||||
mem = malloc(size);
|
||||
if(mem == NULL){
|
||||
printf("unable to allocate memory");
|
||||
}
|
||||
else{
|
||||
printf("memory allocated successfully\n");
|
||||
}
|
||||
#elif defined(CONFIG_BOARD_ARDUINO_101) || defined(CONFIG_BOARD_ARDUINO_101_SSS) || defined(CONFIG_BOARD_QUARK_D2000_CRB)
|
||||
kmemory_map_t map_name = (kmemory_map_t) mem_map;
|
||||
if(task_mem_map_alloc(map_name, &mem, TICKS_UNLIMITED) == RC_OK){
|
||||
printf("memory allocated successfully\n");
|
||||
}
|
||||
else{
|
||||
printf("unable to allocate memory");
|
||||
mem = NULL;
|
||||
}
|
||||
#endif
|
||||
return mem;
|
||||
}
|
||||
|
||||
void upm_free(int mem_map, void* ptr){
|
||||
#if defined(linux)
|
||||
free(ptr);
|
||||
#elif defined(CONFIG_BOARD_ARDUINO_101) || defined(CONFIG_BOARD_ARDUINO_101_SSS) || defined(CONFIG_BOARD_QUARK_D2000_CRB)
|
||||
kmemory_map_t map_name = (kmemory_map_t) mem_map;
|
||||
task_mem_map_free(map_name, &ptr);
|
||||
#endif
|
||||
}
|
||||
|
||||
void upm_delay(int time){
|
||||
#if defined(linux)
|
||||
sleep(time);
|
||||
#elif defined(CONFIG_BOARD_ARDUINO_101) || defined(CONFIG_BOARD_ARDUINO_101_SSS) || defined(CONFIG_BOARD_QUARK_D2000_CRB)
|
||||
struct nano_timer timer;
|
||||
void *timer_data[1];
|
||||
nano_timer_init(&timer, timer_data);
|
||||
nano_timer_start(&timer, SECONDS(time));
|
||||
nano_timer_test(&timer, TICKS_UNLIMITED);
|
||||
#endif
|
||||
}
|
||||
|
||||
void upm_delay_ms(int time){
|
||||
#if defined(linux)
|
||||
usleep(1000 * time);
|
||||
#elif defined(CONFIG_BOARD_ARDUINO_101) || defined(CONFIG_BOARD_ARDUINO_101_SSS) || defined(CONFIG_BOARD_QUARK_D2000_CRB)
|
||||
struct nano_timer timer;
|
||||
void *timer_data[1];
|
||||
nano_timer_init(&timer, timer_data);
|
||||
nano_timer_start(&timer, MSEC(time));
|
||||
nano_timer_test(&timer, TICKS_UNLIMITED);
|
||||
#endif
|
||||
}
|
||||
|
||||
void upm_delay_us(int time){
|
||||
#if defined(linux)
|
||||
usleep(time);
|
||||
#elif defined(CONFIG_BOARD_ARDUINO_101) || defined(CONFIG_BOARD_ARDUINO_101_SSS) || defined(CONFIG_BOARD_QUARK_D2000_CRB)
|
||||
struct nano_timer timer;
|
||||
void *timer_data[1];
|
||||
nano_timer_init(&timer, timer_data);
|
||||
nano_timer_start(&timer, USEC(time));
|
||||
nano_timer_test(&timer, TICKS_UNLIMITED);
|
||||
#endif
|
||||
}
|
68
src/utilities/upm_utilities.h
Normal file
68
src/utilities/upm_utilities.h
Normal file
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Authors:
|
||||
* Copyright (c) 2016 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.
|
||||
*/
|
||||
#ifndef UPM_UTILITIES_H_
|
||||
#define UPM_UTILITIES_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if defined(linux)
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#if defined(CONFIG_BOARD_ARDUINO_101) || defined(CONFIG_BOARD_ARDUINO_101_SSS) || defined(CONFIG_BOARD_QUARK_D2000_CRB)
|
||||
#include <zephyr.h>
|
||||
#include <device.h>
|
||||
#include <sys_clock.h>
|
||||
|
||||
#if defined(CONFIG_STDOUT_CONSOLE)
|
||||
#include <stdio.h>
|
||||
#define PRINT printf
|
||||
#else
|
||||
#include <misc/printk.h>
|
||||
#define PRINT printk
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* Get filename w/o path */
|
||||
#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
|
||||
|
||||
void upm_delay(int time);
|
||||
|
||||
void upm_delay_ms(int time);
|
||||
|
||||
void upm_delay_us(int time);
|
||||
|
||||
void* upm_malloc(int mem_map, int size);
|
||||
|
||||
void upm_free(int mem_map, void* ptr);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* UPM_UTILITIES_H_ */
|
Reference in New Issue
Block a user