From f5f2920c709407ddea39862ea116be58042c3779 Mon Sep 17 00:00:00 2001 From: Stefan Andritoiu Date: Wed, 20 Jun 2018 18:29:18 +0300 Subject: [PATCH] Added iVDiv interface Signed-off-by: Stefan Andritoiu Signed-off-by: Mihai Tudor Panu --- include/interfaces/iVDiv.hpp | 45 ++++++++++++++++++++++++++++++++++++ src/grovevdiv/grovevdiv.cxx | 8 +++++-- src/grovevdiv/grovevdiv.hpp | 10 +++++++- src/vdiv/vdiv.cxx | 8 +++++-- src/vdiv/vdiv.hpp | 10 +++++++- 5 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 include/interfaces/iVDiv.hpp diff --git a/include/interfaces/iVDiv.hpp b/include/interfaces/iVDiv.hpp new file mode 100644 index 00000000..e46acb82 --- /dev/null +++ b/include/interfaces/iVDiv.hpp @@ -0,0 +1,45 @@ +/* + * Author: Mihai Stefanescu + * Copyright (c) 2018 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 + +namespace upm +{ +/** + * @brief Interface for Voltage Dividing sensors +*/ + class iVDiv + { + public: + virtual ~iVDiv() {} + + /** + * Gets the conversion value from the sensor + * + * @return ADC conversion value + */ + virtual unsigned int getValue() = 0; + + }; +} diff --git a/src/grovevdiv/grovevdiv.cxx b/src/grovevdiv/grovevdiv.cxx index fdf62754..73038cee 100644 --- a/src/grovevdiv/grovevdiv.cxx +++ b/src/grovevdiv/grovevdiv.cxx @@ -56,13 +56,17 @@ unsigned int GroveVDiv::value(unsigned int samples) if (sum == -1) return 0; usleep(2000); } - + return (sum / samples); } +unsigned int GroveVDiv::getValue() +{ + return GroveVDiv::value(1); +} + float GroveVDiv::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/grovevdiv/grovevdiv.hpp b/src/grovevdiv/grovevdiv.hpp index de74a855..bddbc6f4 100644 --- a/src/grovevdiv/grovevdiv.hpp +++ b/src/grovevdiv/grovevdiv.hpp @@ -27,6 +27,7 @@ #include #include #include +#include // reference voltage in millivolts #define GROVEVDIV_VREF 4980 @@ -59,7 +60,7 @@ namespace upm { * @image html grovevdiv.jpg * @snippet grovevdiv.cxx Interesting */ - class GroveVDiv { + class GroveVDiv : virtual public iVDiv { public: /** * Grove Voltage Divider sensor constructor @@ -81,6 +82,13 @@ namespace upm { */ unsigned int value(unsigned int samples); + /** + * Gets the conversion value from the sensor + * + * @return ADC conversion value + */ + virtual unsigned int getValue(); + /** * Computes the measured voltage * diff --git a/src/vdiv/vdiv.cxx b/src/vdiv/vdiv.cxx index 867cf66e..133f382a 100644 --- a/src/vdiv/vdiv.cxx +++ b/src/vdiv/vdiv.cxx @@ -56,13 +56,17 @@ unsigned int VDiv::value(unsigned int samples) if (sum == -1) return 0; usleep(2000); } - + return (sum / samples); } +unsigned int VDiv::getValue() +{ + return VDiv::value(1); +} + 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.hpp b/src/vdiv/vdiv.hpp index f04b7f3f..e5168c29 100644 --- a/src/vdiv/vdiv.hpp +++ b/src/vdiv/vdiv.hpp @@ -27,6 +27,7 @@ #include #include #include +#include // reference voltage in millivolts #define VDIV_VREF 4980 @@ -58,7 +59,7 @@ namespace upm { * @image html vdiv.jpg * @snippet vdiv.cxx Interesting */ - class VDiv { + class VDiv : virtual public iVDiv { public: /** * Voltage Divider sensor constructor @@ -80,6 +81,13 @@ namespace upm { */ unsigned int value(unsigned int samples); + /** + * Gets the conversion value from the sensor + * + * @return ADC conversion value + */ + virtual unsigned int getValue(); + /** * Computes the measured voltage *