mirror of
				https://github.com/eclipse/upm.git
				synced 2025-10-31 07:04:14 +03:00 
			
		
		
		
	Added static conversion methods for distance, pressure and temp ifaces
This commit is contained in:
		 UPSquared
					UPSquared
				
			
				
					committed by
					
						 Stefan Andritoiu
						Stefan Andritoiu
					
				
			
			
				
	
			
			
			 Stefan Andritoiu
						Stefan Andritoiu
					
				
			
						parent
						
							e73afa43b6
						
					
				
				
					commit
					8f94b8e165
				
			| @@ -24,8 +24,11 @@ | |||||||
|  |  | ||||||
| #pragma once | #pragma once | ||||||
|  |  | ||||||
|  | #include <stdexcept> | ||||||
|  |  | ||||||
| namespace upm | namespace upm | ||||||
| { | { | ||||||
|  |   enum class DistanceUnit { CM, INCH }; | ||||||
| /** | /** | ||||||
| * @brief Interface for Distance Measuring Sensors | * @brief Interface for Distance Measuring Sensors | ||||||
| */ | */ | ||||||
| @@ -34,5 +37,34 @@ namespace upm | |||||||
|   public: |   public: | ||||||
|     virtual ~iDistance() {} |     virtual ~iDistance() {} | ||||||
|     virtual int getDistance() = 0; |     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; | ||||||
|  |   } | ||||||
| } | } | ||||||
| @@ -24,8 +24,11 @@ | |||||||
|  |  | ||||||
| #pragma once | #pragma once | ||||||
|  |  | ||||||
|  | #include <stdexcept> | ||||||
|  |  | ||||||
| namespace upm | namespace upm | ||||||
| { | { | ||||||
|  |   enum class PressureUnit { PA, BAR, ATM, TORR, PSI }; | ||||||
| /** | /** | ||||||
| * @brief Interface for Pressure Measuring Sensors | * @brief Interface for Pressure Measuring Sensors | ||||||
| */ | */ | ||||||
| @@ -34,5 +37,47 @@ namespace upm | |||||||
|   public: |   public: | ||||||
|     virtual ~iPressure() {} |     virtual ~iPressure() {} | ||||||
|     virtual float getPressure() = 0; |     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; | ||||||
|  |   } | ||||||
| } | } | ||||||
| @@ -24,15 +24,54 @@ | |||||||
|  |  | ||||||
| #pragma once | #pragma once | ||||||
|  |  | ||||||
|  | #include <stdexcept> | ||||||
|  |  | ||||||
| namespace upm | namespace upm | ||||||
| { | { | ||||||
|  |   enum class TemperatureUnit { FAHRENHEIT, KELVIN, CELSIUS }; | ||||||
|  |  | ||||||
| /** | /** | ||||||
| * @brief Interface for Temperature Measuring Sensors | * @brief Interface for Temperature Measuring Sensors | ||||||
| */ | */ | ||||||
|   class iTemperature |   class iTemperature | ||||||
|   { |   { | ||||||
|   public: |   public: | ||||||
|  |  | ||||||
|     virtual ~iTemperature() {} |     virtual ~iTemperature() {} | ||||||
|     virtual float getTemperature() = 0; |     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; | ||||||
|  |   } | ||||||
| } | } | ||||||
		Reference in New Issue
	
	Block a user