Added static conversion methods for distance, pressure and temp ifaces

This commit is contained in:
UPSquared 2018-06-06 17:07:42 +03:00 committed by Stefan Andritoiu
parent e73afa43b6
commit 8f94b8e165
3 changed files with 123 additions and 7 deletions

View File

@ -24,8 +24,11 @@
#pragma once
#include <stdexcept>
namespace upm
{
enum class DistanceUnit { CM, INCH };
/**
* @brief Interface for Distance Measuring Sensors
*/
@ -34,5 +37,34 @@ namespace upm
public:
virtual ~iDistance() {}
virtual int getDistance() = 0;
/**
* Convert distance value from Cm(default) to one
* of the following:
*
* 1. Inch
*
* @param celsiusValue Celsius degrees value
* @param unit The temperature unit for the conversion.
* @return The converted temperature value
*/
static float convertCmTo(float cmValue, DistanceUnit unit);
};
float iDistance::convertCmTo(float cmValue, DistanceUnit unit)
{
float convertedValue = cmValue;
switch (unit)
{
case DistanceUnit::CM:
break;
case DistanceUnit::INCH:
convertedValue *= 0.3937f;
break;
default:
throw std::invalid_argument("invalid distance unit");
}
return convertedValue;
}
}

View File

@ -24,8 +24,11 @@
#pragma once
#include <stdexcept>
namespace upm
{
enum class PressureUnit { PA, BAR, ATM, TORR, PSI };
/**
* @brief Interface for Pressure Measuring Sensors
*/
@ -34,5 +37,47 @@ namespace upm
public:
virtual ~iPressure() {}
virtual float getPressure() = 0;
/**
* Convert pressure value from Pascal(default) to one
* of the following:
*
* 1. Bar
* 2. Standard atmosphere
* 3. Torr
* 4. Pounds per square inch
*
* @param celsiusValue Celsius degrees value
* @param unit The temperature unit for the conversion.
* @return The converted temperature value
*/
static float convertPaTo(float paValue, PressureUnit unit);
};
float iPressure::convertPaTo(float paValue, PressureUnit unit)
{
float convertedValue = paValue;
switch (unit)
{
case PressureUnit::PA:
break;
case PressureUnit::BAR:
convertedValue *= 0.00001f;
break;
case PressureUnit::ATM:
convertedValue *= 0.0000098692f;
break;
case PressureUnit::TORR:
convertedValue *= 0.0075006f;
break;
case PressureUnit::PSI:
convertedValue *= 0.0001450377f;
break;
default:
throw std::invalid_argument("invalid pressure unit");
}
return convertedValue;
}
}

View File

@ -24,15 +24,54 @@
#pragma once
#include <stdexcept>
namespace upm
{
enum class TemperatureUnit { FAHRENHEIT, KELVIN, CELSIUS };
/**
* @brief Interface for Temperature Measuring Sensors
*/
class iTemperature
{
public:
virtual ~iTemperature() {}
virtual float getTemperature() = 0;
/**
* Convert temperature value from Celsius(default) to one
* of the following:
*
* 1. Fahrenheit
* 2. Kelvin
*
* @param celsiusValue Celsius degrees value
* @param unit The temperature unit for the conversion.
* @return The converted temperature value
*/
static float convertCelsiusTo(float celsiusValue, TemperatureUnit unit);
};
float iTemperature::convertCelsiusTo(float celsiusValue, TemperatureUnit unit)
{
float convertedValue = celsiusValue;
switch (unit)
{
case TemperatureUnit::CELSIUS:
break;
case TemperatureUnit::FAHRENHEIT:
convertedValue = celsiusValue * 1.8f + 32;
break;
case TemperatureUnit::KELVIN:
convertedValue += 273.15f;
break;
default:
throw std::invalid_argument("invalid temperature unit");
}
return convertedValue;
}
}