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,15 +24,47 @@ | ||||
|  | ||||
| #pragma once | ||||
|  | ||||
| #include <stdexcept> | ||||
|  | ||||
| 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; | ||||
|   } | ||||
| } | ||||
| @@ -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; | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -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; | ||||
|   } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user