From b608232cb977f0903f254ae9fb0d6228438aa17e Mon Sep 17 00:00:00 2001 From: Noel Eck Date: Thu, 1 Sep 2016 16:32:49 -0700 Subject: [PATCH] vdiv: Added vdiv C source. Multiple changes related to building the vdiv c source w/c example. * Renamed GroveVdiv to vdiv (all cases) throughout source, examples, directory names, and documentation. * Added C source. * Tested C sensor code on edison * Updated CMakeLists.txt for examples-c to build from -c. This was a small change to get c examples to build for sensors in which the C++ does NOT wrap the C. * Added C example for vdiv. Signed-off-by: Noel Eck --- doxy/samples.mapping.txt | 2 +- examples/c++/CMakeLists.txt | 2 +- examples/c++/{grovevdiv.cxx => vdiv.cxx} | 4 +- examples/c/CMakeLists.txt | 3 +- examples/c/vdiv.c | 74 ++++++++++ examples/java/CMakeLists.txt | 2 +- .../{GroveVDivSample.java => VDivSample.java} | 4 +- examples/javascript/{grovevdiv.js => vdiv.js} | 4 +- examples/python/{grovevdiv.py => vdiv.py} | 24 +-- src/grovevdiv/CMakeLists.txt | 5 - src/grovevdiv/jsupm_grovevdiv.i | 8 - src/vdiv/CMakeLists.txt | 8 + .../javaupm_vdiv.i} | 8 +- src/vdiv/jsupm_vdiv.i | 8 + .../pyupm_grovevdiv.i => vdiv/pyupm_vdiv.i} | 6 +- src/vdiv/vdiv.c | 122 +++++++++++++++ .../grovevdiv.cxx => vdiv/vdiv.cxx} | 10 +- src/vdiv/vdiv.h | 139 ++++++++++++++++++ .../grovevdiv.hpp => vdiv/vdiv.hpp} | 38 ++--- src/vdiv/vdiv_fti.c | 118 +++++++++++++++ 20 files changed, 523 insertions(+), 66 deletions(-) rename examples/c++/{grovevdiv.cxx => vdiv.cxx} (96%) create mode 100644 examples/c/vdiv.c rename examples/java/{GroveVDivSample.java => VDivSample.java} (95%) rename examples/javascript/{grovevdiv.js => vdiv.js} (94%) rename examples/python/{grovevdiv.py => vdiv.py} (77%) delete mode 100644 src/grovevdiv/CMakeLists.txt delete mode 100644 src/grovevdiv/jsupm_grovevdiv.i create mode 100644 src/vdiv/CMakeLists.txt rename src/{grovevdiv/javaupm_grovevdiv.i => vdiv/javaupm_vdiv.i} (65%) create mode 100644 src/vdiv/jsupm_vdiv.i rename src/{grovevdiv/pyupm_grovevdiv.i => vdiv/pyupm_vdiv.i} (61%) create mode 100644 src/vdiv/vdiv.c rename src/{grovevdiv/grovevdiv.cxx => vdiv/vdiv.cxx} (89%) create mode 100644 src/vdiv/vdiv.h rename src/{grovevdiv/grovevdiv.hpp => vdiv/vdiv.hpp} (75%) create mode 100644 src/vdiv/vdiv_fti.c diff --git a/doxy/samples.mapping.txt b/doxy/samples.mapping.txt index 78e57737..72e66724 100644 --- a/doxy/samples.mapping.txt +++ b/doxy/samples.mapping.txt @@ -21,7 +21,7 @@ groverotary.cxx GroveRotarySample.java grovescam.cxx GROVESCAMSample.java groveslide.cxx GroveSlideSample.java grovespeaker.cxx GroveSpeakerSample.java -grovevdiv.cxx GroveVDivSample.java +vdiv.cxx VDivSample.java grovewater.cxx GroveWaterSample.java grovewfs.cxx GroveWFSSample.java guvas12d.cxx GUVAS12DSample.java diff --git a/examples/c++/CMakeLists.txt b/examples/c++/CMakeLists.txt index eee1b27c..74c52bb5 100644 --- a/examples/c++/CMakeLists.txt +++ b/examples/c++/CMakeLists.txt @@ -146,7 +146,7 @@ add_example (grovemoisture) add_example (groveehr) add_example (ta12200) add_example (grovelinefinder) -add_example (grovevdiv) +add_example (vdiv) add_example (grovewater) add_example (guvas12d) add_example (mpr121) diff --git a/examples/c++/grovevdiv.cxx b/examples/c++/vdiv.cxx similarity index 96% rename from examples/c++/grovevdiv.cxx rename to examples/c++/vdiv.cxx index 0f903338..6a704bff 100644 --- a/examples/c++/grovevdiv.cxx +++ b/examples/c++/vdiv.cxx @@ -25,7 +25,7 @@ #include #include #include -#include "grovevdiv.hpp" +#include "vdiv.hpp" using namespace std; @@ -44,7 +44,7 @@ int main () //! [Interesting] // Instantiate a Grove Voltage Divider sensor on analog pin A0 - upm::GroveVDiv* vDiv = new upm::GroveVDiv(0); + upm::VDiv* vDiv = new upm::VDiv(0); // collect data and output measured voltage according to the setting // of the scaling switch (3 or 10) diff --git a/examples/c/CMakeLists.txt b/examples/c/CMakeLists.txt index 035be1b3..04940057 100644 --- a/examples/c/CMakeLists.txt +++ b/examples/c/CMakeLists.txt @@ -38,7 +38,7 @@ macro(add_custom_example example_bin example_src example_module_list) if (${module} STREQUAL "lcd") set(module "i2clcd") endif() - target_link_libraries (${example_bin} ${module}) + target_link_libraries (${example_bin} ${module}-c) endforeach() else() MESSAGE(INFO " Ignored ${example_bin}") @@ -94,6 +94,7 @@ add_example (urm37) add_example (urm37-uart) add_example (hka5) add_example (dfrorp) +add_example (vdiv) # Custom examples add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps) diff --git a/examples/c/vdiv.c b/examples/c/vdiv.c new file mode 100644 index 00000000..323faae7 --- /dev/null +++ b/examples/c/vdiv.c @@ -0,0 +1,74 @@ +/* + * Author: Jon Trulson + * 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. + */ + +#include +#include + +#include "vdiv.h" + +bool shouldRun = true; + +void sig_handler(int signo) +{ + if (signo == SIGINT) + shouldRun = false; +} + +int main() +{ + signal(SIGINT, sig_handler); + + //! [Interesting] + + // Instantiate a sensor on analog pin A0 + vdiv_context sensor = vdiv_init(0, 5); + + if (!sensor) + { + printf("vdiv_init() failed.\n"); + return(1); + } + + // Every half a second, sample the sensor output + while (shouldRun) + { + float raw_volts = 0.0, computed_volts = 0.0; + + vdiv_get_raw_volts(sensor, &raw_volts); + vdiv_get_computed_volts(sensor, &computed_volts); + + printf("Divide SW: %d ADC voltage: %0.03f Sensor voltage: %0.03f\n", + vdiv_get_divsw(sensor), raw_volts, computed_volts); + + usleep(500000); + } + + //! [Interesting] + + printf("Exiting\n"); + + vdiv_close(sensor); + + return 0; +} diff --git a/examples/java/CMakeLists.txt b/examples/java/CMakeLists.txt index a5c6afed..4f0f0941 100644 --- a/examples/java/CMakeLists.txt +++ b/examples/java/CMakeLists.txt @@ -52,7 +52,7 @@ add_example(GROVESCAMSample grovescam) add_example(GroveSlideSample grove) add_example(GroveSpeakerSample grovespeaker) add_example(GroveTempSample grove) -add_example(GroveVDivSample grovevdiv) +add_example(VDivSample vdiv) add_example(GroveWaterSample grovewater) add_example(GroveWFSSample grovewfs) add_example(GUVAS12DSample guvas12d) diff --git a/examples/java/GroveVDivSample.java b/examples/java/VDivSample.java similarity index 95% rename from examples/java/GroveVDivSample.java rename to examples/java/VDivSample.java index 579f2c3b..cb5b2cd5 100644 --- a/examples/java/GroveVDivSample.java +++ b/examples/java/VDivSample.java @@ -23,14 +23,14 @@ */ //NOT TESTED!!! -public class GroveVDivSample { +public class VDivSample { private static final short gain3 = 3; private static final short gain10 = 10; public static void main(String[] args) throws InterruptedException { // ! [Interesting] // Instantiate a Grove Voltage Divider sensor on analog pin A0 - upm_grovevdiv.GroveVDiv vDiv = new upm_grovevdiv.GroveVDiv(0); + upm_vdiv.VDiv vDiv = new upm_vdiv.VDiv(0); // collect data and output measured voltage according to the setting // of the scaling switch (3 or 10) diff --git a/examples/javascript/grovevdiv.js b/examples/javascript/vdiv.js similarity index 94% rename from examples/javascript/grovevdiv.js rename to examples/javascript/vdiv.js index 5fb393e4..497bc55e 100644 --- a/examples/javascript/grovevdiv.js +++ b/examples/javascript/vdiv.js @@ -22,9 +22,9 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -var voltageDivider = require('jsupm_grovevdiv'); +var voltageDivider = require('jsupm_vdiv'); // Instantiate a Grove Voltage Divider sensor on analog pin A0 -var myVoltageDivider = new voltageDivider.GroveVDiv(0); +var myVoltageDivider = new voltageDivider.VDiv(0); // collect data and output measured voltage according to the setting // of the scaling switch (3 or 10) diff --git a/examples/python/grovevdiv.py b/examples/python/vdiv.py similarity index 77% rename from examples/python/grovevdiv.py rename to examples/python/vdiv.py index 7719277c..03a6a014 100644 --- a/examples/python/grovevdiv.py +++ b/examples/python/vdiv.py @@ -22,22 +22,22 @@ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import time, sys, signal, atexit -import pyupm_grovevdiv as upmGrovevdiv +import pyupm_vdiv as upmvdiv -# Instantiate a Grove Voltage Divider sensor on analog pin A0 -myVoltageDivider = upmGrovevdiv.GroveVDiv(0) +# Instantiate a Voltage Divider sensor on analog pin A0 +myVoltageDivider = upmvdiv.VDiv(0) ## Exit handlers ## # This stops python from printing a stacktrace when you hit control-C def SIGINTHandler(signum, frame): - raise SystemExit + raise SystemExit # This function lets you run code on exit, # including functions from myVoltageDivider def exitHandler(): - print "Exiting" - sys.exit(0) + print "Exiting" + sys.exit(0) # Register exit handlers atexit.register(exitHandler) @@ -45,10 +45,10 @@ signal.signal(signal.SIGINT, SIGINTHandler) while(1): - val = myVoltageDivider.value(100) - gain3val = myVoltageDivider.computedValue(3, val) - gain10val = myVoltageDivider.computedValue(10, val) - print "ADC value: {0} Gain 3: {1}v Gain 10: {2}v".format( - val, gain3val, gain10val) + val = myVoltageDivider.value(100) + gain3val = myVoltageDivider.computedValue(3, val) + gain10val = myVoltageDivider.computedValue(10, val) + print "ADC value: {0} Gain 3: {1}v Gain 10: {2}v".format( + val, gain3val, gain10val) - time.sleep(1) + time.sleep(1) diff --git a/src/grovevdiv/CMakeLists.txt b/src/grovevdiv/CMakeLists.txt deleted file mode 100644 index 66d925be..00000000 --- a/src/grovevdiv/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -set (libname "grovevdiv") -set (libdescription "upm grove voltage divider module") -set (module_src ${libname}.cxx) -set (module_hpp ${libname}.hpp) -upm_module_init() diff --git a/src/grovevdiv/jsupm_grovevdiv.i b/src/grovevdiv/jsupm_grovevdiv.i deleted file mode 100644 index ab9bedfe..00000000 --- a/src/grovevdiv/jsupm_grovevdiv.i +++ /dev/null @@ -1,8 +0,0 @@ -%module jsupm_grovevdiv -%include "../upm.i" - -%{ - #include "grovevdiv.hpp" -%} - -%include "grovevdiv.hpp" diff --git a/src/vdiv/CMakeLists.txt b/src/vdiv/CMakeLists.txt new file mode 100644 index 00000000..501e8e5e --- /dev/null +++ b/src/vdiv/CMakeLists.txt @@ -0,0 +1,8 @@ +upm_mixed_module_init (NAME vdiv + DESCRIPTION "upm grove voltage divider module" + C_HDR vdiv.h + C_SRC vdiv.c + CPP_HDR vdiv.hpp + CPP_SRC vdiv.cxx + FTI_SRC vdiv_fti.c + REQUIRES mraa) diff --git a/src/grovevdiv/javaupm_grovevdiv.i b/src/vdiv/javaupm_vdiv.i similarity index 65% rename from src/grovevdiv/javaupm_grovevdiv.i rename to src/vdiv/javaupm_vdiv.i index b3c5e02f..bc615e62 100644 --- a/src/grovevdiv/javaupm_grovevdiv.i +++ b/src/vdiv/javaupm_vdiv.i @@ -1,16 +1,16 @@ -%module javaupm_grovevdiv +%module javaupm_vdiv %include "../upm.i" %{ - #include "grovevdiv.hpp" + #include "vdiv.hpp" %} -%include "grovevdiv.hpp" +%include "vdiv.hpp" %pragma(java) jniclasscode=%{ static { try { - System.loadLibrary("javaupm_grovevdiv"); + System.loadLibrary("javaupm_vdiv"); } catch (UnsatisfiedLinkError e) { System.err.println("Native code library failed to load. \n" + e); System.exit(1); diff --git a/src/vdiv/jsupm_vdiv.i b/src/vdiv/jsupm_vdiv.i new file mode 100644 index 00000000..1fdf2deb --- /dev/null +++ b/src/vdiv/jsupm_vdiv.i @@ -0,0 +1,8 @@ +%module jsupm_vdiv +%include "../upm.i" + +%{ + #include "vdiv.hpp" +%} + +%include "vdiv.hpp" diff --git a/src/grovevdiv/pyupm_grovevdiv.i b/src/vdiv/pyupm_vdiv.i similarity index 61% rename from src/grovevdiv/pyupm_grovevdiv.i rename to src/vdiv/pyupm_vdiv.i index 5ad7aafb..eedecfdd 100644 --- a/src/grovevdiv/pyupm_grovevdiv.i +++ b/src/vdiv/pyupm_vdiv.i @@ -1,11 +1,11 @@ // Include doxygen-generated documentation %include "pyupm_doxy2swig.i" -%module pyupm_grovevdiv +%module pyupm_vdiv %include "../upm.i" %feature("autodoc", "3"); -%include "grovevdiv.hpp" +%include "vdiv.hpp" %{ - #include "grovevdiv.hpp" + #include "vdiv.hpp" %} diff --git a/src/vdiv/vdiv.c b/src/vdiv/vdiv.c new file mode 100644 index 00000000..6f08e5ca --- /dev/null +++ b/src/vdiv/vdiv.c @@ -0,0 +1,122 @@ +/* + * 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 +#include + +#include "vdiv.h" + +vdiv_context vdiv_init(int16_t pin, float voltage_ref) +{ + vdiv_context dev = (vdiv_context)malloc(sizeof(struct _vdiv_context)); + + if (dev == NULL) return NULL; + + /* Init aio pin */ + dev->aio = mraa_aio_init(pin); + + if (dev->aio == NULL) { + free(dev); + return NULL; + } + + /* Set defaults */ + dev->m_aRef = 5.0; + dev->m_scale = 1.0; + dev->m_offset = 0.0; + dev->m_vdiv_sw = 3; + + return dev; +} + +void vdiv_close(vdiv_context dev) +{ + mraa_aio_close(dev->aio); + free(dev); +} + +upm_result_t vdiv_set_scale(const vdiv_context dev, float scale) +{ + dev->m_scale = scale; + return UPM_SUCCESS; +} + +upm_result_t vdiv_set_offset(const vdiv_context dev, float offset) +{ + dev->m_offset = offset; + return UPM_SUCCESS; +} + +float vdiv_get_scale(const vdiv_context dev) +{ + return dev->m_scale; +} + +float vdiv_get_offset(const vdiv_context dev) +{ + return dev->m_offset; +} + +upm_result_t vdiv_set_divsw(const vdiv_context dev, int vdiv_sw) +{ + dev->m_vdiv_sw = vdiv_sw; + return UPM_SUCCESS; +} + +int vdiv_get_divsw(const vdiv_context dev) +{ + return dev->m_vdiv_sw; +} + +upm_result_t vdiv_get_raw_volts(const vdiv_context dev, float *value) +{ + *value = mraa_aio_read_float(dev->aio); + if (*value < 0) + return UPM_ERROR_OPERATION_FAILED; + + /* Scale by the ADC reference voltage */ + *value *= dev->m_aRef; + + return UPM_SUCCESS; +} + +upm_result_t vdiv_get_computed_volts(const vdiv_context dev, float *value) +{ + // JET - this is wrong. + + *value = mraa_aio_read_float(dev->aio); + if (*value < 0) + return UPM_ERROR_OPERATION_FAILED; + + /* Apply raw scale */ + *value *= dev->m_scale; + + /* Scale to the ADC referecen then to vdiv gain */ + *value *= dev->m_aRef * dev->m_vdiv_sw; + + /* Apply the offset in volts */ + *value += dev->m_offset; + + return UPM_SUCCESS; +} diff --git a/src/grovevdiv/grovevdiv.cxx b/src/vdiv/vdiv.cxx similarity index 89% rename from src/grovevdiv/grovevdiv.cxx rename to src/vdiv/vdiv.cxx index b1ebe154..829a1ea7 100644 --- a/src/grovevdiv/grovevdiv.cxx +++ b/src/vdiv/vdiv.cxx @@ -26,12 +26,12 @@ #include #include -#include "grovevdiv.hpp" +#include "vdiv.hpp" using namespace upm; using namespace std; -GroveVDiv::GroveVDiv(int pin) +VDiv::VDiv(int pin) { if ( !(m_aio = mraa_aio_init(pin)) ) { @@ -41,12 +41,12 @@ GroveVDiv::GroveVDiv(int pin) } } -GroveVDiv::~GroveVDiv() +VDiv::~VDiv() { mraa_aio_close(m_aio); } -unsigned int GroveVDiv::value(unsigned int samples) +unsigned int VDiv::value(unsigned int samples) { unsigned int sum = 0; @@ -60,7 +60,7 @@ unsigned int GroveVDiv::value(unsigned int samples) return (sum / samples); } -float GroveVDiv::computedValue(uint8_t gain, unsigned int val, int vref, int res) +float VDiv::computedValue(uint8_t gain, unsigned int val, int vref, int res) { return ((float(gain) * float(val) * float(vref) / float(res)) / 1000.0); diff --git a/src/vdiv/vdiv.h b/src/vdiv/vdiv.h new file mode 100644 index 00000000..943554db --- /dev/null +++ b/src/vdiv/vdiv.h @@ -0,0 +1,139 @@ +/* + * 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 _vdiv_context { + /* mraa aio pin context */ + mraa_aio_context aio; + /* ADC voltage reference */ + float m_aRef; + /* Scale */ + float m_scale; + /* Offset in sensor units */ + float m_offset; + /* Offset in sensor units */ + int m_vdiv_sw; +} *vdiv_context; + + +/** + * Initialize analog sensor + * @param pin is Analog pin + * @return sensor context as void pointer + */ +vdiv_context vdiv_init(int16_t pin, float voltage_ref); + +/** + * Analog sensor destructor + * @param sensor context pointer deallocate memory + */ +void vdiv_close(vdiv_context dev); + +/** + * 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 vdiv_set_scale(const vdiv_context dev, float scale); + +/** + * 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 vdiv_set_offset(const vdiv_context dev, float offset); + +/** + * Get sensor scale + * @param dev sensor context pointer + * @return Sensor scale + */ +float vdiv_get_scale(const vdiv_context dev); + +/** + * Get sensor offset + * @param dev sensor context pointer + * @return Sensor offset + */ +float vdiv_get_offset(const vdiv_context dev); + +/** + * Set sensor divide switch value + * @param dev sensor context pointer + * @param vdiv_sw Divide switch value + * @return Function result code + */ +upm_result_t vdiv_set_divsw(const vdiv_context dev, int vdiv_sw); + +/** + * Get divide switch value + * @param dev sensor context pointer + * @return Sensor divide switch value + */ +int vdiv_get_divsw(const vdiv_context dev); + +/** + * Read raw voltage from the sensor + * @param dev sensor context pointer + * @param *value Raw sensor voltage + * @return Function result code + */ +upm_result_t vdiv_get_raw_volts(const vdiv_context dev, float *value); + +/** + * Gets the true voltage value from the sensor. Voltage divides by 3 or 10, + * depending on the toggle switch on the board. The dynamic range of the + * vdiv sensor is listed below. + * + * ADC Ref SW max VOL in + * ------- --- ---------- + * 3.3v 3 8.5v + * 3.3v 10 28.4v + * 5.0v 3 12.9v + * 5.0v 10 43.0v + * + * @param dev sensor context pointer + * @param *value Voltage (v) + * @return Function result code + */ +upm_result_t vdiv_get_computed_volts(const vdiv_context dev, float *value); + +#ifdef __cplusplus +} +#endif diff --git a/src/grovevdiv/grovevdiv.hpp b/src/vdiv/vdiv.hpp similarity index 75% rename from src/grovevdiv/grovevdiv.hpp rename to src/vdiv/vdiv.hpp index 9e93879d..416744a3 100644 --- a/src/grovevdiv/grovevdiv.hpp +++ b/src/vdiv/vdiv.hpp @@ -29,47 +29,47 @@ #include // reference voltage in millivolts -#define GROVEVDIV_VREF 4980 +#define VDIV_VREF 4980 // default ADC resolution -#define GROVEVDIV_ADC_RES 1024 +#define VDIV_ADC_RES 1024 namespace upm { /** - * @brief Grove Voltage Divider Sensor library - * @defgroup grovevdiv libupm-grovevdiv + * @brief Voltage Divider Sensor library + * @defgroup vdiv libupm-vdiv * @ingroup seeed analog electric robok */ /** - * @library grovevdiv - * @sensor grovevdiv - * @comname Grove Voltage Divider + * @library vdiv + * @sensor vdiv + * @comname Voltage Divider * @type electric * @man seeed * @con analog * @kit robok * - * @brief API for the Grove Voltage Divider Sensor + * @brief API for the Voltage Divider Sensor * - * UPM module for the Grove Voltage Divider sensor + * UPM module for the Voltage Divider sensor * - * @image html grovevdiv.jpg - * @snippet grovevdiv.cxx Interesting + * @image html vdiv.jpg + * @snippet vdiv.cxx Interesting */ - class GroveVDiv { + class VDiv { public: /** - * Grove Voltage Divider sensor constructor + * Voltage Divider sensor constructor * * @param pin Analog pin to use */ - GroveVDiv(int pin); + VDiv(int pin); /** - * Grove Voltage Divider destructor + * Voltage Divider destructor */ - ~GroveVDiv(); + ~VDiv(); /** * Gets the conversion value from the sensor @@ -82,15 +82,15 @@ namespace upm { /** * Computes the measured voltage * - * @param gain Gain switch, either 3 or 10 for Grove + * @param gain Gain switch, example: either 3 or 10 grove vdiv * @param val Measured voltage (from value()) * @param vref Reference voltage in millivolts * @param res ADC resolution * * @return Measured voltage */ - float computedValue(uint8_t gain, unsigned int val, int vref=GROVEVDIV_VREF, - int res=GROVEVDIV_ADC_RES); + float computedValue(uint8_t gain, unsigned int val, int vref=VDIV_VREF, + int res=VDIV_ADC_RES); private: mraa_aio_context m_aio; diff --git a/src/vdiv/vdiv_fti.c b/src/vdiv/vdiv_fti.c new file mode 100644 index 00000000..f42660f7 --- /dev/null +++ b/src/vdiv/vdiv_fti.c @@ -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 +#include + +#include "vdiv.h" +#include "fti/upm_sensor.h" +#include "fti/upm_voltage.h" + +/** + * This file implements the Function Table Interface (FTI) for this sensor + */ + +const char upm_vdiv_name[] = "VDIV"; +const char upm_vdiv_description[] = "Analog voltage divider sensor"; +const upm_protocol_t upm_vdiv_protocol[] = {UPM_ANALOG}; +const upm_sensor_t upm_vdiv_category[] = {UPM_VOLTAGE}; + +// forward declarations +const void* upm_vdiv_get_ft(upm_sensor_t sensor_type); +void* upm_vdiv_init_str(const char* protocol, const char* params); +void upm_vdiv_close(void* dev); +const upm_sensor_descriptor_t upm_vdiv_get_descriptor(); +upm_result_t upm_vdiv_set_offset(const void* dev, float offset); +upm_result_t upm_vdiv_set_scale(const void* dev, float scale); +upm_result_t upm_vdiv_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_vdiv_init_str, + .upm_sensor_close = &upm_vdiv_close, + .upm_sensor_get_descriptor = &upm_vdiv_get_descriptor +}; + +/* 2. VOLTAGE function table */ +static const upm_voltage_ft ft_voltage = +{ + .upm_voltage_set_offset = &upm_vdiv_set_offset, + .upm_voltage_set_scale = &upm_vdiv_set_scale, + .upm_voltage_get_value = &upm_vdiv_get_value +}; + +const void* upm_vdiv_get_ft(upm_sensor_t sensor_type) +{ + switch(sensor_type) + { + case UPM_SENSOR: + return &ft_gen; + case UPM_VOLTAGE: + return &ft_voltage; + default: + return NULL; + } +} + +void* upm_vdiv_init_str(const char* protocol, const char* params) +{ + fprintf(stderr, "String initialization - not implemented, using ain0: %s\n", __FILENAME__); + return vdiv_init(0, 5.0); +} + +void upm_vdiv_close(void* dev) +{ + vdiv_close((vdiv_context)dev); +} + +const upm_sensor_descriptor_t upm_vdiv_get_descriptor() +{ + /* Fill in the descriptor */ + upm_sensor_descriptor_t usd; + usd.name = upm_vdiv_name; + usd.description = upm_vdiv_description; + usd.protocol_size = 1; + usd.protocol = upm_vdiv_protocol; + usd.category_size = 1; + usd.category = upm_vdiv_category; + + return usd; +} + +upm_result_t upm_vdiv_set_offset(const void* dev, float offset) +{ + return vdiv_set_offset((vdiv_context)dev, offset); +} + +upm_result_t upm_vdiv_set_scale(const void* dev, float scale) +{ + return vdiv_set_scale((vdiv_context)dev, scale); +} + +upm_result_t upm_vdiv_get_value(const void* dev, float *value) +{ + return vdiv_get_computed_volts((vdiv_context)dev, value); +}