BikeCadenceDisplay.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 "BikeCadenceDisplay.h"
  10. /**************************************************************************
  11. * BikeCadenceDisplay::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 BikeCadenceDisplay::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. * BikeCadenceDisplay::InitializeSim
  37. *
  38. * Initializes simulator variables
  39. *
  40. * returns: N/A
  41. *
  42. **************************************************************************/
  43. void BikeCadenceDisplay::InitializeSim()
  44. {
  45. ucStatePage = BC_INIT_PAGE;
  46. usEventCount = 0;
  47. usPreviousEventCount = 0;
  48. usTime1024 = 0;
  49. usPreviousTime1024 = 0;
  50. ulElapsedTime2 = 0;
  51. bCoast = FALSE;
  52. ulAcumEventCount = 0;
  53. ulAcumTime1024 = 0;
  54. ucCadence = 0;
  55. ucMfgID = 0;
  56. ucHwVersion = 0;
  57. ucSwVersion = 0;
  58. ucModelNum = 0;
  59. usSerialNum = 0;
  60. }
  61. /**************************************************************************
  62. * BikeCadenceDisplay::HandleReceive
  63. *
  64. * Decodes received data
  65. *
  66. * pucRxBuffer_: pointer to the buffer containing the received data
  67. *
  68. * returns: N/A
  69. *
  70. **************************************************************************/
  71. void BikeCadenceDisplay::HandleReceive(UCHAR* pucRxBuffer_)
  72. {
  73. static UCHAR ucNoEventCount = 0; // Counter for successive transmissions with no new cadence events
  74. UCHAR ucPageNum = 0;
  75. USHORT usEventDiff = 0;
  76. USHORT usTimeDiff1024 = 0;
  77. // Monitor page toggle bit
  78. if(ucStatePage != BC_EXT_PAGE)
  79. {
  80. if(ucStatePage == BC_INIT_PAGE)
  81. {
  82. ucStatePage = (pucRxBuffer_[0] & BC_TOGGLE_MASK) >>7;
  83. // Initialize previous values to correctly get the cumulative values
  84. usPreviousTime1024 = pucRxBuffer_[4];
  85. usPreviousTime1024 += pucRxBuffer_[5]<<8;
  86. usPreviousEventCount = pucRxBuffer_[6];
  87. usPreviousEventCount += pucRxBuffer_[7]<<8;
  88. }
  89. else if(ucStatePage != ((pucRxBuffer_[0] & BC_TOGGLE_MASK) >>7))
  90. {
  91. // First page toggle was seen, enable advanced data
  92. ucStatePage = BC_EXT_PAGE;
  93. }
  94. }
  95. // Remove page toggle bit
  96. ucPageNum = pucRxBuffer_[0] & ~BC_TOGGLE_MASK;
  97. // Handle basic data
  98. usEventCount = pucRxBuffer_[6];
  99. usEventCount += pucRxBuffer_[7]<<8;
  100. usTime1024 = pucRxBuffer_[4];
  101. usTime1024 += pucRxBuffer_[5]<<8;
  102. // Handle advanced data, if supported
  103. if(ucStatePage == BC_EXT_PAGE)
  104. {
  105. switch(ucPageNum)
  106. {
  107. case BC_PAGE1:
  108. // Battery Time Page
  109. ulElapsedTime2 = pucRxBuffer_[1];
  110. ulElapsedTime2 += pucRxBuffer_[2] << 8;
  111. ulElapsedTime2 += pucRxBuffer_[3] << 16;
  112. break;
  113. case BC_PAGE2:
  114. // Manf Info Page
  115. ucMfgID = pucRxBuffer_[1];
  116. usSerialNum = pucRxBuffer_[2];
  117. usSerialNum += pucRxBuffer_[3] << 8;
  118. break;
  119. case BC_PAGE3:
  120. // Version Info Page
  121. ucHwVersion = pucRxBuffer_[1];
  122. ucSwVersion = pucRxBuffer_[2];
  123. ucModelNum = pucRxBuffer_[3];
  124. break;
  125. default:
  126. break;
  127. }
  128. }
  129. // Only need to do calculations if dealing with a new event
  130. if(usEventCount != usPreviousEventCount)
  131. {
  132. ucNoEventCount = 0;
  133. bCoast = FALSE;
  134. // Update cumulative event count
  135. if(usEventCount > usPreviousEventCount)
  136. usEventDiff = usEventCount - usPreviousEventCount;
  137. else
  138. usEventDiff = (USHORT) (0xFFFF - usPreviousEventCount + usEventCount + 1);
  139. ulAcumEventCount += usEventDiff;
  140. // Update cumulative time (1/1024s)
  141. if(usTime1024 > usPreviousTime1024)
  142. usTimeDiff1024 = usTime1024 - usPreviousTime1024;
  143. else
  144. usTimeDiff1024 = (USHORT) (0xFFFF - usPreviousTime1024 + usTime1024 + 1);
  145. ulAcumTime1024 += usTimeDiff1024;
  146. // Calculate cadence (rpm)
  147. if(usTimeDiff1024 > 0)
  148. ucCadence = (UCHAR) ( ((ULONG) usEventDiff * 0xF000) / (ULONG) usTimeDiff1024 ); // 1 min = 0xF000 = 60 * 1024
  149. }
  150. else
  151. {
  152. ucNoEventCount++;
  153. if(ucNoEventCount >= MAX_NO_EVENTS)
  154. bCoast = TRUE; // Coasting
  155. }
  156. // Display data
  157. UpdateDisplay(ucPageNum);
  158. // Update previous time and event count
  159. usPreviousEventCount = usEventCount;
  160. usPreviousTime1024 = usTime1024;
  161. }
  162. /**************************************************************************
  163. * BikeCadenceDisplay::UpdateDisplay
  164. *
  165. * Shows received decoded data on GUI
  166. *
  167. * ucPageNum_: received page
  168. *
  169. * returns: N/A
  170. *
  171. **************************************************************************/
  172. void BikeCadenceDisplay::UpdateDisplay(UCHAR ucPageNum_)
  173. {
  174. // Display basic data
  175. this->label_Trn_TimeDisplay->Text = usTime1024.ToString();
  176. this->label_Trn_EventCountDisplay->Text = usEventCount.ToString();
  177. this->label_Coasting->Visible = (bCoast == TRUE);
  178. // Display advanced data, if available
  179. if(ucStatePage == BC_EXT_PAGE)
  180. {
  181. this->label_Calc_waitToggle->Visible = false; // Page toggle already seen
  182. switch(ucPageNum_)
  183. {
  184. case BC_PAGE1:
  185. this->label_Glb_BattTimeDisplay->Text = (ulElapsedTime2*2).ToString();
  186. break;
  187. case BC_PAGE2:
  188. this->label_Glb_ManfIDDisplay->Text = ucMfgID.ToString();
  189. this->label_Glb_SerialNumDisplay->Text = usSerialNum.ToString();
  190. break;
  191. case BC_PAGE3:
  192. this->label_Glb_HardwareVerDisplay->Text = ucHwVersion.ToString();
  193. this->label_Glb_SoftwareVerDisplay->Text = ucSwVersion.ToString();
  194. this->label_Glb_ModelNumDisplay->Text = ucModelNum.ToString();
  195. break;
  196. default:
  197. break;
  198. }
  199. }
  200. // Display calculations
  201. // Cumulative time (in s)
  202. this->label_Calc_ElapsedSecsDisplay->Text = System::Math::Round((DOUBLE) ulAcumTime1024/1024,3).ToString();
  203. // Cumulative event count
  204. this->label_Calc_TotEventCountDisplay->Text = ulAcumEventCount.ToString();
  205. // Calculated cadence
  206. this->label_Calc_CadenceDisplay->Text = ucCadence.ToString();
  207. }