Unit.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #region License
  2. /* Copyright 2011, 2013 James F. Bellinger <http://www.zer7.com/software/hidsharp>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  13. #endregion
  14. using System;
  15. namespace HidSharp.ReportDescriptors.Units
  16. {
  17. /// <summary>
  18. /// Describes the units of a report value.
  19. /// </summary>
  20. public class Unit
  21. {
  22. uint _value;
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="Unit"/> class.
  25. /// </summary>
  26. /// <param name="value">The raw HID value describing the units.</param>
  27. public Unit(uint value)
  28. {
  29. _value = value;
  30. }
  31. uint Element(int index)
  32. {
  33. return (Value >> (index << 2)) & 0xf;
  34. }
  35. int Exponent(int index)
  36. {
  37. return DecodeExponent(Element(index));
  38. }
  39. /// <summary>
  40. /// Decodes an encoded HID unit exponent.
  41. /// </summary>
  42. /// <param name="value">The encoded exponent.</param>
  43. /// <returns>The exponent.</returns>
  44. public static int DecodeExponent(uint value)
  45. {
  46. if (value > 15) { throw new ArgumentOutOfRangeException("value", "Value range is [0, 15]."); }
  47. return value >= 8 ? (int)value - 16 : (int)value;
  48. }
  49. void Element(int index, uint value)
  50. {
  51. Value &= 0xfu << (index << 2); Value |= (value & 0xfu) << (index << 2);
  52. }
  53. /// <summary>
  54. /// Encodes an exponent in HID unit form.
  55. /// </summary>
  56. /// <param name="value">The exponent.</param>
  57. /// <returns>The encoded exponent.</returns>
  58. public static uint EncodeExponent(int value)
  59. {
  60. if (value < -8 || value > 7)
  61. { throw new ArgumentOutOfRangeException("value", "Exponent range is [-8, 7]."); }
  62. return (uint)(value < 0 ? value + 16 : value);
  63. }
  64. void Exponent(int index, int value)
  65. {
  66. Element(index, EncodeExponent(value));
  67. }
  68. /// <summary>
  69. /// Gets or sets the unit system.
  70. /// </summary>
  71. public UnitSystem System
  72. {
  73. get { return (UnitSystem)Element(0); }
  74. set { Element(0, (uint)value); }
  75. }
  76. /// <summary>
  77. /// Gets or sets the exponent of the report value's units of length.
  78. /// </summary>
  79. public int LengthExponent
  80. {
  81. get { return Exponent(1); }
  82. set { Exponent(1, value); }
  83. }
  84. /// <summary>
  85. /// Gets the units of length corresponding to <see cref="System"/>.
  86. /// </summary>
  87. public LengthUnit LengthUnit
  88. {
  89. get
  90. {
  91. switch (System)
  92. {
  93. case UnitSystem.SILinear: return LengthUnit.Centimeter;
  94. case UnitSystem.SIRotation: return LengthUnit.Radians;
  95. case UnitSystem.EnglishLinear: return LengthUnit.Inch;
  96. case UnitSystem.EnglishRotation: return LengthUnit.Degrees;
  97. default: return LengthUnit.None;
  98. }
  99. }
  100. }
  101. /// <summary>
  102. /// Gets or sets the exponent of the report value's units of mass.
  103. /// </summary>
  104. public int MassExponent
  105. {
  106. get { return Exponent(2); }
  107. set { Exponent(2, value); }
  108. }
  109. /// <summary>
  110. /// Gets the units of mass corresponding to <see cref="System"/>.
  111. /// </summary>
  112. public MassUnit MassUnit
  113. {
  114. get
  115. {
  116. switch (System)
  117. {
  118. case UnitSystem.SILinear:
  119. case UnitSystem.SIRotation: return MassUnit.Gram;
  120. case UnitSystem.EnglishLinear:
  121. case UnitSystem.EnglishRotation: return MassUnit.Slug;
  122. default: return MassUnit.None;
  123. }
  124. }
  125. }
  126. /// <summary>
  127. /// Gets or sets the exponent of the report value's units of time.
  128. /// </summary>
  129. public int TimeExponent
  130. {
  131. get { return Exponent(3); }
  132. set { Exponent(3, value); }
  133. }
  134. /// <summary>
  135. /// Gets the units of time corresponding to <see cref="System"/>.
  136. /// </summary>
  137. public TimeUnit TimeUnit
  138. {
  139. get
  140. {
  141. return System != UnitSystem.None
  142. ? TimeUnit.Seconds : TimeUnit.None;
  143. }
  144. }
  145. /// <summary>
  146. /// Gets or sets the exponent of the report value's units of temperature.
  147. /// </summary>
  148. public int TemperatureExponent
  149. {
  150. get { return Exponent(4); }
  151. set { Exponent(4, value); }
  152. }
  153. /// <summary>
  154. /// Gets the units of temperature corresponding to <see cref="System"/>.
  155. /// </summary>
  156. public TemperatureUnit TemperatureUnit
  157. {
  158. get
  159. {
  160. switch (System)
  161. {
  162. case UnitSystem.SILinear:
  163. case UnitSystem.SIRotation: return TemperatureUnit.Kelvin;
  164. case UnitSystem.EnglishLinear:
  165. case UnitSystem.EnglishRotation: return TemperatureUnit.Fahrenheit;
  166. default: return TemperatureUnit.None;
  167. }
  168. }
  169. }
  170. /// <summary>
  171. /// Gets or sets the exponent of the report value's units of current.
  172. /// </summary>
  173. public int CurrentExponent
  174. {
  175. get { return Exponent(5); }
  176. set { Exponent(5, value); }
  177. }
  178. /// <summary>
  179. /// Gets the units of current corresponding to <see cref="System"/>.
  180. /// </summary>
  181. public CurrentUnit CurrentUnit
  182. {
  183. get
  184. {
  185. return System != UnitSystem.None
  186. ? CurrentUnit.Ampere : CurrentUnit.None;
  187. }
  188. }
  189. /// <summary>
  190. /// Gets or sets the exponent of the report value's units of luminous intensity.
  191. /// </summary>
  192. public int LuminousIntensityExponent
  193. {
  194. get { return Exponent(6); }
  195. set { Exponent(6, value); }
  196. }
  197. /// <summary>
  198. /// Gets the units of luminous intensity corresponding to <see cref="System"/>.
  199. /// </summary>
  200. public LuminousIntensityUnit LuminousIntensityUnit
  201. {
  202. get
  203. {
  204. return System != UnitSystem.None
  205. ? LuminousIntensityUnit.Candela : LuminousIntensityUnit.None;
  206. }
  207. }
  208. /// <summary>
  209. /// Gets or sets the raw HID value describing the units.
  210. /// </summary>
  211. public uint Value
  212. {
  213. get { return _value; }
  214. set { _value = value; }
  215. }
  216. }
  217. }