ReportSegment.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #region License
  2. /* Copyright 2011 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. using System.Collections.Generic;
  16. namespace HidSharp.ReportDescriptors.Parser
  17. {
  18. public class ReportSegment : ReportMainItem
  19. {
  20. Report _report;
  21. public int ConvertArbitraryRangeToValue(double quantity, double minimum, double maximum)
  22. {
  23. return Math.Max(LogicalMinimum, Math.Min(LogicalMaximum,
  24. (int)((quantity - minimum) * LogicalRange / (maximum - minimum))));
  25. }
  26. public int ConvertPhysicalQuantityToValue(double quantity)
  27. {
  28. double exp = Math.Pow(10, UnitExponent);
  29. return ConvertArbitraryRangeToValue(quantity,
  30. PhysicalMinimum * exp, PhysicalMaximum * exp);
  31. }
  32. public double ConvertValueToArbitraryRange(int value,
  33. double minimum, double maximum)
  34. {
  35. return minimum + (value - LogicalMinimum) * (maximum - minimum) / LogicalRange;
  36. }
  37. public double ConvertValueToPhysicalQuantity(int value)
  38. {
  39. double exp = Math.Pow(10, UnitExponent);
  40. return ConvertValueToArbitraryRange(value,
  41. PhysicalMinimum * exp, PhysicalRange * exp);
  42. }
  43. public int DecodeSigned(uint value)
  44. {
  45. uint signBit = 1u << (ElementSize - 1), mask = signBit - 1;
  46. return (value & signBit) != 0 ? (int)(value | ~mask) : (int)value;
  47. }
  48. public uint EncodeSigned(int value)
  49. {
  50. uint usValue = (uint)value;
  51. uint signBit = 1u << (ElementSize - 1), mask = signBit - 1;
  52. return (usValue & mask) | (value < 0 ? signBit : 0);
  53. }
  54. public bool IsValueOutOfRange(int value)
  55. {
  56. return LogicalIsSigned
  57. ? value < LogicalMinimum || value > LogicalMaximum
  58. : ((uint)value < (uint)LogicalMinimum || (uint)value > (uint)LogicalMaximum)
  59. ;
  60. }
  61. public int Read(byte[] buffer, int bitOffset, int element)
  62. {
  63. uint rawValue = ReadRaw(buffer, bitOffset, element);
  64. return LogicalIsSigned ? DecodeSigned(rawValue) : (int)rawValue;
  65. }
  66. public uint ReadRaw(byte[] buffer, int bitOffset, int element)
  67. {
  68. uint value = 0; int totalBits = Math.Min(ElementSize, 32);
  69. bitOffset += element * ElementSize;
  70. for (int i = 0; i < totalBits; i++, bitOffset ++)
  71. {
  72. int byteStart = bitOffset >> 3; byte bitStart = (byte)(1u << (bitOffset & 7));
  73. value |= (buffer[byteStart] & bitStart) != 0 ? (1u << i) : 0;
  74. }
  75. return value;
  76. }
  77. public void Write(byte[] buffer, int bitOffset, int element, int value)
  78. {
  79. WriteRaw(buffer, bitOffset, element,
  80. LogicalIsSigned ? EncodeSigned(value) : (uint)value);
  81. }
  82. public void WriteRaw(byte[] buffer, int bitOffset, int element, uint value)
  83. {
  84. int totalBits = Math.Min(ElementSize, 32);
  85. bitOffset += element * ElementSize;
  86. for (int i = 0; i < totalBits; i++, bitOffset++)
  87. {
  88. int byteStart = bitOffset >> 3; uint bitStart = 1u << (bitOffset & 7);
  89. if ((value & (1 << i)) != 0) { buffer[byteStart] |= (byte)bitStart; } else { buffer[byteStart] &= (byte)(~bitStart); }
  90. }
  91. }
  92. public int BitCount
  93. {
  94. get { return ElementCount * ElementSize; }
  95. }
  96. public int ElementCount
  97. {
  98. get;
  99. set;
  100. }
  101. public int ElementSize
  102. {
  103. get;
  104. set;
  105. }
  106. public DataMainItemFlags Flags
  107. {
  108. get;
  109. set;
  110. }
  111. public bool LogicalIsSigned
  112. {
  113. get;
  114. set;
  115. }
  116. public int LogicalMinimum
  117. {
  118. get;
  119. set;
  120. }
  121. public int LogicalMaximum
  122. {
  123. get;
  124. set;
  125. }
  126. public int LogicalRange
  127. {
  128. get { return LogicalMaximum - LogicalMinimum; }
  129. }
  130. public int PhysicalMinimum
  131. {
  132. get;
  133. set;
  134. }
  135. public int PhysicalMaximum
  136. {
  137. get;
  138. set;
  139. }
  140. public int PhysicalRange
  141. {
  142. get { return PhysicalMaximum - PhysicalMinimum; }
  143. }
  144. public Report Report
  145. {
  146. get { return _report; }
  147. set
  148. {
  149. if (_report == value) { return; }
  150. if (_report != null) { _report._segments.Remove(this); }
  151. _report = value;
  152. if (_report != null) { _report._segments.Add(this); }
  153. }
  154. }
  155. public double Resolution
  156. {
  157. get
  158. {
  159. return (double)LogicalRange /
  160. ((double)PhysicalRange *
  161. Math.Pow(10, UnitExponent));
  162. }
  163. }
  164. public Units.Unit Unit
  165. {
  166. get;
  167. set;
  168. }
  169. public int UnitExponent
  170. {
  171. get;
  172. set;
  173. }
  174. }
  175. }