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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #include <QObject>
  8. #ifndef TypicaUnitsIncluded
  9. #define TypicaUnitsIncluded
  10. class Units: public QObject
  11. {
  12. Q_OBJECT
  13. Q_ENUMS(Unit)
  14. public:
  15. enum Unit
  16. {
  17. Fahrenheit = 10144,
  18. Celsius = 10143,
  19. Kelvin = 10325,
  20. Rankine = 10145
  21. };
  22. static double convertTemperature(double value, Unit fromUnit, Unit toUnit);
  23. static bool isTemperatureUnit(Unit unit);
  24. };
  25. #endif
  26. @ Methods are implemented in a separate file.
  27. @(units.cpp@>=
  28. #include "units.h"
  29. #include "moc_units.cpp"
  30. @ The |isTemperatureUnit()| method may seem counter-intuitive while the enum
  31. only contains represenations of temperature measurements, but there are plans
  32. to extend this later to extend hardware support to devices that do not directly
  33. measure temperature values and also to support the measurement of properties
  34. that cannot sensibly be represented as temperatures such as fuel pressure and
  35. control settings.
  36. @(units.cpp@>=
  37. bool Units::isTemperatureUnit(Unit unit)
  38. {
  39. if(unit == Fahrenheit ||
  40. unit == Celsius ||
  41. unit == Kelvin ||
  42. unit == Rankine)
  43. {
  44. return true;
  45. }
  46. return false;
  47. }
  48. @ Temperature conversions can be performed by the |Units| class and should
  49. eliminate the need to have conversion code in multiple places. This method
  50. takes the measurement value, the unit that value is in, and the unit that we
  51. would like to have the measurement converted into. It returns the value as it
  52. would be expressed after unit conversion. A value of 0 is presently returned
  53. if either or both of the units to convert are not temperature units, however
  54. that behavior may change in the future. Any code activating that branch should
  55. be considered flawed.
  56. @(units.cpp@>=
  57. double Units::convertTemperature(double value, Unit fromUnit, Unit toUnit)
  58. {
  59. if(isTemperatureUnit(fromUnit) && isTemperatureUnit(toUnit) == false)
  60. {
  61. return 0;
  62. }
  63. switch(fromUnit)
  64. {
  65. case Fahrenheit:
  66. switch(toUnit)
  67. {
  68. case Fahrenheit:
  69. return value;
  70. break;
  71. case Celsius:
  72. return (value - 32) * 5 / 9;
  73. break;
  74. case Kelvin:
  75. return (value + 459.67) * 5 / 9;
  76. break;
  77. case Rankine:
  78. return value + 459.67;
  79. break;
  80. }
  81. break;
  82. case Celsius:
  83. switch(toUnit)
  84. {
  85. case Fahrenheit:
  86. return value * 9 / 5 + 32;
  87. break;
  88. case Celsius:
  89. return value;
  90. break;
  91. case Kelvin:
  92. return value + 273.15;
  93. break;
  94. case Rankine:
  95. return (value + 273.15) * 9 / 5;
  96. break;
  97. }
  98. break;
  99. case Kelvin:
  100. switch(toUnit)
  101. {
  102. case Fahrenheit:
  103. return value * 5 / 9 - 459.67;
  104. break;
  105. case Celsius:
  106. return value - 273.15;
  107. break;
  108. case Kelvin:
  109. return value;
  110. break;
  111. case Rankine:
  112. return value * 9 / 5;
  113. break;
  114. }
  115. break;
  116. case Rankine:
  117. switch(toUnit)
  118. {
  119. case Fahrenheit:
  120. return value - 457.67;
  121. break;
  122. case Celsius:
  123. return (value - 491.67) * 5 / 9;
  124. break;
  125. case Kelvin:
  126. return value * 5 / 9;
  127. break;
  128. case Rankine:
  129. return value;
  130. break;
  131. }
  132. break;
  133. default:
  134. return 0;
  135. break;
  136. }
  137. return 0;
  138. }
  139. @ This class is exposed to the host environment. Note the lack of constructor.
  140. We do not wish to create any instances, just have access to the |Unit|
  141. enumeration.
  142. @<Set up the scripting engine@>=
  143. value = engine->newQMetaObject(&Units::staticMetaObject);
  144. engine->globalObject().setProperty("Units", value);