Added iVDiv interface

Signed-off-by: Stefan Andritoiu <stefan.andritoiu@gmail.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
Stefan Andritoiu 2018-06-20 18:29:18 +03:00 committed by Mihai Tudor Panu
parent fd34b9669d
commit f5f2920c70
5 changed files with 75 additions and 6 deletions

View File

@ -0,0 +1,45 @@
/*
* Author: Mihai Stefanescu <mihai.stefanescu@rinftech.com>
* 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;
};
}

View File

@ -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);
}

View File

@ -27,6 +27,7 @@
#include <iostream>
#include <stdint.h>
#include <mraa/aio.h>
#include <interfaces/iVDiv.hpp>
// 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
*

View File

@ -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);
}

View File

@ -27,6 +27,7 @@
#include <iostream>
#include <stdint.h>
#include <mraa/aio.h>
#include <interfaces/iVDiv.hpp>
// 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
*