diff --git a/include/interfaces/iDistance.hpp b/include/interfaces/iDistance.hpp index e5ba74bc..0d6f0a6d 100644 --- a/include/interfaces/iDistance.hpp +++ b/include/interfaces/iDistance.hpp @@ -24,15 +24,47 @@ #pragma once +#include + namespace upm { + enum class DistanceUnit { CM, INCH }; /** * @brief Interface for Distance Measuring Sensors */ - class iDistance + class iDistance + { + 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) { - public: - virtual ~iDistance() {} - virtual int getDistance() = 0; - }; + case DistanceUnit::CM: + break; + case DistanceUnit::INCH: + convertedValue *= 0.3937f; + break; + default: + throw std::invalid_argument("invalid distance unit"); + } + + return convertedValue; + } } \ No newline at end of file diff --git a/include/interfaces/iPressure.hpp b/include/interfaces/iPressure.hpp index 19ce3213..9e7f03e7 100644 --- a/include/interfaces/iPressure.hpp +++ b/include/interfaces/iPressure.hpp @@ -24,8 +24,11 @@ #pragma once +#include + 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); }; -} \ No newline at end of file + + 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; + } +} diff --git a/include/interfaces/iTemperature.hpp b/include/interfaces/iTemperature.hpp index bf4ce0eb..d4d246e4 100644 --- a/include/interfaces/iTemperature.hpp +++ b/include/interfaces/iTemperature.hpp @@ -24,15 +24,54 @@ #pragma once +#include + 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); }; -} \ No newline at end of file + + 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; + } +}