2
0

BikeSpeedDisplay.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. This software is subject to the license described in the License.txt file
  3. included with this software distribution. You may not use this file except in compliance
  4. with this license.
  5. Copyright (c) Dynastream Innovations Inc. 2012
  6. All rights reserved.
  7. */
  8. #include "StdAfx.h"
  9. #include "BikeSpeedDisplay.h"
  10. /**************************************************************************
  11. * BikeSpeedeDisplay::ANT_eventNotification
  12. *
  13. * Process ANT channel event
  14. *
  15. * ucEventCode_: code of ANT channel event
  16. * pucEventBuffer_: pointer to buffer containing data received from ANT,
  17. * or a pointer to the transmit buffer in the case of an EVENT_TX
  18. *
  19. * returns: N/A
  20. *
  21. **************************************************************************/
  22. void BikeSpeedDisplay::ANT_eventNotification(UCHAR ucEventCode_, UCHAR* pucEventBuffer_)
  23. {
  24. switch(ucEventCode_)
  25. {
  26. case EVENT_RX_ACKNOWLEDGED:
  27. case EVENT_RX_BURST_PACKET: // intentional fall thru
  28. case EVENT_RX_BROADCAST:
  29. HandleReceive((UCHAR*) pucEventBuffer_);
  30. break;
  31. default:
  32. break;
  33. }
  34. }
  35. /**************************************************************************
  36. * BikeSpeedDisplay::InitializeSim
  37. *
  38. * Initializes simulator variables
  39. *
  40. * returns: N/A
  41. *
  42. **************************************************************************/
  43. void BikeSpeedDisplay::InitializeSim()
  44. {
  45. ucStatePage = BS_INIT_PAGE;
  46. ucWheelCircumference = System::Convert::ToByte(this->numericUpDown_Sim_WheelCircumference->Value); // Initial value set on UI
  47. usEventCount = 0;
  48. usPreviousEventCount = 0;
  49. usTime1024 = 0;
  50. usPreviousTime1024 = 0;
  51. ulElapsedTime2 = 0;
  52. bStop = FALSE;
  53. ulAcumEventCount = 0;
  54. ulAcumTime1024 = 0;
  55. ulSpeed = 0;
  56. ulDistance = 0;
  57. ucMfgID = 0;
  58. ucHwVersion = 0;
  59. ucSwVersion = 0;
  60. ucModelNum = 0;
  61. usSerialNum = 0;
  62. }
  63. /**************************************************************************
  64. * BikeSpeedDisplay::HandleReceive
  65. *
  66. * Decodes received data
  67. *
  68. * pucRxBuffer_: pointer to the buffer containing the received data
  69. *
  70. * returns: N/A
  71. *
  72. **************************************************************************/
  73. void BikeSpeedDisplay::HandleReceive(UCHAR* pucRxBuffer_)
  74. {
  75. static UCHAR ucNoEventCount = 0; // Counter for successive transmissions with no new speed events
  76. UCHAR ucPageNum = 0;
  77. USHORT usEventDiff = 0;
  78. USHORT usTimeDiff1024 = 0;
  79. // Monitor page toggle bit
  80. if(ucStatePage != BS_EXT_PAGE)
  81. {
  82. if(ucStatePage == BS_INIT_PAGE)
  83. {
  84. ucStatePage = (pucRxBuffer_[0] & BS_TOGGLE_MASK) >>7;
  85. // Initialize previous values to correctly get the cumulative values
  86. usPreviousTime1024 = pucRxBuffer_[4];
  87. usPreviousTime1024 += pucRxBuffer_[5]<<8;
  88. usPreviousEventCount = pucRxBuffer_[6];
  89. usPreviousEventCount += pucRxBuffer_[7]<<8;
  90. }
  91. else if(ucStatePage != ((pucRxBuffer_[0] & BS_TOGGLE_MASK) >>7))
  92. {
  93. // First page toggle was seen, enable advanced data
  94. ucStatePage = BS_EXT_PAGE;
  95. }
  96. }
  97. // Remove page toggle bit
  98. ucPageNum = pucRxBuffer_[0] & ~BS_TOGGLE_MASK;
  99. // Handle basic data
  100. usEventCount = pucRxBuffer_[6];
  101. usEventCount += pucRxBuffer_[7]<<8;
  102. usTime1024 = pucRxBuffer_[4];
  103. usTime1024 += pucRxBuffer_[5]<<8;
  104. // Handle advanced data, if supported
  105. if(ucStatePage == BS_EXT_PAGE)
  106. {
  107. switch(ucPageNum)
  108. {
  109. case BS_PAGE1:
  110. //Battery Time Page
  111. ulElapsedTime2 = pucRxBuffer_[1];
  112. ulElapsedTime2 += pucRxBuffer_[2] << 8;
  113. ulElapsedTime2+= pucRxBuffer_[3] << 16;
  114. break;
  115. case BS_PAGE2:
  116. //Manf Info Page
  117. ucMfgID = pucRxBuffer_[1];
  118. usSerialNum = pucRxBuffer_[2];
  119. usSerialNum += pucRxBuffer_[3] << 8;
  120. break;
  121. case BS_PAGE3:
  122. //Version Info Page
  123. ucHwVersion = pucRxBuffer_[1];
  124. ucSwVersion = pucRxBuffer_[2];
  125. ucModelNum = pucRxBuffer_[3];
  126. break;
  127. default:
  128. break;
  129. }
  130. }
  131. // Only need to do calculations if dealing with a new event
  132. if(usEventCount != usPreviousEventCount)
  133. {
  134. ucNoEventCount = 0;
  135. bStop = FALSE;
  136. // Update cumulative event count
  137. if(usEventCount > usPreviousEventCount)
  138. usEventDiff = usEventCount - usPreviousEventCount;
  139. else
  140. usEventDiff = (USHORT) (0xFFFF - usPreviousEventCount + usEventCount + 1);
  141. ulAcumEventCount += usEventDiff;
  142. // Update cumulative time (1/1024s)
  143. if(usTime1024 > usPreviousTime1024)
  144. usTimeDiff1024 = usTime1024 - usPreviousTime1024;
  145. else
  146. usTimeDiff1024 = (USHORT) (0xFFFF - usPreviousTime1024 + usTime1024 + 1);
  147. ulAcumTime1024 += usTimeDiff1024;
  148. // Calculate speed (meters/h)
  149. if(ucWheelCircumference)
  150. ulSpeed = (ucWheelCircumference * 36864 * (ULONG) usEventDiff) / (ULONG) usTimeDiff1024; // 1024 * 36 = 36864 (cm/(1/1024)s -> meters/h)
  151. // Calculate distance (cm)
  152. ulDistance = (ULONG) ucWheelCircumference * ulAcumEventCount;
  153. }
  154. else
  155. {
  156. ucNoEventCount++;
  157. if(ucNoEventCount >= MAX_NO_EVENTS)
  158. bStop = TRUE; // Stopping
  159. }
  160. // Display data
  161. UpdateDisplay(ucPageNum);
  162. // Update previous time and event count
  163. usPreviousEventCount = usEventCount;
  164. usPreviousTime1024 = usTime1024;
  165. }
  166. /**************************************************************************
  167. * BikeSpeedDisplay::UpdateDisplay
  168. *
  169. * Shows received decoded data on GUI
  170. *
  171. * ucPageNum_: received page
  172. *
  173. * returns: N/A
  174. *
  175. **************************************************************************/
  176. void BikeSpeedDisplay::UpdateDisplay(UCHAR ucPageNum_)
  177. {
  178. // Display basic data
  179. this->label_Trn_TimeDisplay->Text = System::Convert::ToString(usTime1024);
  180. this->label_Trn_EventCountDisplay->Text = System::Convert::ToString(usEventCount);
  181. this->label_Stopped->Visible = (bStop == TRUE);
  182. // Display advanced data, if available
  183. if(ucStatePage == BS_EXT_PAGE)
  184. {
  185. this->label_Calc_waitToggle->Visible = false; // Page toggle already seen
  186. switch(ucPageNum_)
  187. {
  188. case BS_PAGE1:
  189. this->label_Glb_BattTimeDisplay->Text = System::Convert::ToString((unsigned int)ulElapsedTime2*2);
  190. break;
  191. case BS_PAGE2:
  192. this->label_Glb_ManfIDDisplay->Text = System::Convert::ToString(ucMfgID);
  193. this->label_Glb_SerialNumDisplay->Text = System::Convert::ToString(usSerialNum);
  194. break;
  195. case BS_PAGE3:
  196. this->label_Glb_HardwareVerDisplay->Text = System::Convert::ToString(ucHwVersion);
  197. this->label_Glb_SoftwareVerDisplay->Text = System::Convert::ToString(ucSwVersion);
  198. this->label_Glb_ModelNumDisplay->Text = System::Convert::ToString(ucModelNum);
  199. break;
  200. default:
  201. break;
  202. }
  203. }
  204. // Display calculations
  205. // Cumulative time (in s)
  206. this->label_Calc_ElapsedSecsDisplay->Text = System::Convert::ToString(System::Math::Round((DOUBLE) ulAcumTime1024/1024,3));
  207. // Cumulative event count
  208. this->label_Calc_TotEventCountDisplay->Text = System::Convert::ToString((unsigned int) ulAcumEventCount);
  209. // Speed (km/h)
  210. this->label_Calc_SpeedDisplay->Text = System::Convert::ToString(System::Math::Round((DOUBLE) ulSpeed/1000,3)); // meters/h -> km/h
  211. // Distance (km)
  212. this->label_Calc_DistanceDisplay->Text = System::Convert::ToString(System::Math::Round((DOUBLE) ulDistance/100000, 3)); // cm -> km
  213. }
  214. /**************************************************************************
  215. * BikeSpeedDisplay::numericUpDown_Sim_WheelCircumference_ValueChanged
  216. *
  217. * Updates wheel circumference value, if updated (either by the user or internally)
  218. * Validation is already performed by the numericUpDown control
  219. *
  220. * returns: N/A
  221. *
  222. **************************************************************************/
  223. System::Void BikeSpeedDisplay::numericUpDown_Sim_WheelCircumference_ValueChanged(System::Object^ sender, System::EventArgs^ e)
  224. {
  225. ucWheelCircumference = System::Convert::ToByte(this->numericUpDown_Sim_WheelCircumference->Value);
  226. }