Typica is a free program for professional coffee roasters. https://typica.us
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

units.w 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. @* Unit information.
  2. \noindent As unit information is required in several places, the enumeration
  3. listing these is moved to an external class. This derives from QObject only to
  4. take advantage of the meta-object system. There are no data members, however
  5. there are some convenience methods for working with unit data.
  6. @(units.h@>=
  7. class Units: public QObject
  8. {
  9. Q_OBJECT
  10. Q_ENUMS(Units)
  11. public:
  12. enum Units
  13. {
  14. Fahrenheit = 10144,
  15. Celsius = 10143,
  16. Kelvin = 10325,
  17. Rankine = 10145
  18. };
  19. double convertTemperature(double value, Unit fromUnit, Unit toUnit);
  20. bool isTemperatureUnit(Unit unit);
  21. }
  22. @ Methods are implemented in a separate file.
  23. @(units.cpp@>=
  24. #inculde "units.h"
  25. #include "moc_units.cpp"
  26. @ The |isTemperatureUnit()| method may seem counter-intuitive while the enum
  27. only contains represenations of temperature measurements, but there are plans
  28. to extend this later to extend hardware support to devices that do not directly
  29. measure temperature values and also to support the measurement of properties
  30. that cannot sensibly be represented as temperatures such as fuel pressure and
  31. control settings.
  32. @(units.cpp@>=
  33. bool Units::isTemperatureUnit(Unit unit)
  34. {
  35. if(unit == Fahrenheit ||
  36. unit == Celsius ||
  37. unit == Kelvin ||
  38. unit == Rankine)
  39. {
  40. return true;
  41. }
  42. return false;
  43. }
  44. @ Temperature conversions can be performed by the |Units| class and should
  45. eliminate the need to have conversion code in multiple places. This method
  46. takes the measurement value, the unit that value is in, and the unit that we
  47. would like to have the measurement converted into. It returns the value as it
  48. would be expressed after unit conversion. A value of 0 is presently returned
  49. if either or both of the units to convert are not temperature units, however
  50. that behavior may change in the future. Any code activating that branch should
  51. be considered flawed.
  52. @(units.cpp@>=
  53. double Units::convertTemperature(double value, Unit fromUnit, Unit toUnit)
  54. {
  55. if(isTemperatureUnit(fromUnit) && isTemperatureUnit(toUnit) == false)
  56. {
  57. return 0;
  58. }
  59. switch(fromUnit)
  60. {
  61. case Fahrenheit:
  62. switch(toUnit)
  63. {
  64. case Fahrenheit:
  65. return value;
  66. break;
  67. case Celsius:
  68. return (value - 32) * 5 / 9;
  69. break;
  70. case Kelvin:
  71. return (value + 459.67) * 5 / 9;
  72. break;
  73. case Rankine:
  74. return value + 459.67;
  75. break;
  76. }
  77. break;
  78. case Celsius:
  79. switch(toUnit)
  80. {
  81. case Fahrenheit:
  82. return value * 9 / 5 + 32;
  83. break;
  84. case Celsius:
  85. return value;
  86. break;
  87. case Kelvin:
  88. return value + 273.15;
  89. break;
  90. case Rankine:
  91. return (value + 273.15) * 9 / 5;
  92. break;
  93. }
  94. break;
  95. case Kelvin:
  96. switch(toUnit)
  97. {
  98. case Fahrenheit:
  99. return value * 5 / 9 - 459.67;
  100. break;
  101. case Celsius:
  102. return value - 273.15;
  103. break;
  104. case Kelvin:
  105. return value;
  106. break;
  107. case Rankine:
  108. return value * 9 / 5;
  109. break;
  110. }
  111. break;
  112. case Rankine:
  113. switch(toUnit)
  114. {
  115. case Fahrenheit:
  116. return value - 457.67;
  117. break;
  118. case Celsius:
  119. return (value - 491.67) * 5 / 9;
  120. break;
  121. case Kelvin:
  122. return value * 5 / 9;
  123. break;
  124. case Rankine:
  125. return value;
  126. break;
  127. }
  128. break;
  129. default:
  130. return 0;
  131. break;
  132. }
  133. return 0;
  134. }