BikeSpdCadDisplay.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*******************************************************************************
  2. * THE FOLLOWING EXAMPLE CODE IS INTENDED FOR LIMITED CIRCULATION ONLY.
  3. *
  4. * Please forward all questions regarding this code to ANT Technical Support.
  5. *
  6. * Dynastream Innovations Inc.
  7. *
  8. * (P) +1 403 932 9292
  9. * (F) +1 403 932 4196
  10. * (E) support@thisisant.com
  11. *
  12. * www.thisisant.com
  13. *
  14. * Reference Design Disclaimer
  15. *
  16. * The references designs and codes provided may be used with ANT devices only and remain the copyrighted property of
  17. * Dynastream Innovations Inc. The reference designs and codes are being provided on an "as-is" basis and as an accommodation,
  18. * and therefore all warranties, representations, or guarantees of any kind (whether express, implied or statutory) including,
  19. * without limitation, warranties of merchantability, non-infringement,
  20. * or fitness for a particular purpose, are specifically disclaimed.
  21. *
  22. * ©2012 Dynastream Innovations Inc. All Rights Reserved
  23. * This software may not be reproduced by
  24. * any means without express written approval of Dynastream
  25. * Innovations Inc.
  26. *
  27. *******************************************************************************/
  28. #include "StdAfx.h"
  29. #include "BikeSpdCadDisplay.h"
  30. /**************************************************************************
  31. * BikeSpdCadDisplay::ANT_eventNotification
  32. *
  33. * Process ANT channel event
  34. *
  35. * ucEventCode_: code of ANT channel event
  36. * pucEventBuffer_: pointer to buffer containing data received from ANT,
  37. * or a pointer to the transmit buffer in the case of an EVENT_TX
  38. *
  39. * returns: N/A
  40. *
  41. **************************************************************************/
  42. void BikeSpdCadDisplay::ANT_eventNotification(UCHAR ucEventCode_, UCHAR* pucEventBuffer_)
  43. {
  44. switch(ucEventCode_)
  45. {
  46. case EVENT_RX_ACKNOWLEDGED:
  47. case EVENT_RX_BURST_PACKET: // intentional fall thru
  48. case EVENT_RX_BROADCAST:
  49. HandleReceive((UCHAR*) pucEventBuffer_);
  50. UpdateDisplay();
  51. break;
  52. default:
  53. break;
  54. }
  55. }
  56. /**************************************************************************
  57. * BikeSpdCadDisplay::InitializeSim
  58. *
  59. * Initializes simulator variables
  60. *
  61. * returns: N/A
  62. *
  63. **************************************************************************/
  64. void BikeSpdCadDisplay::InitializeSim()
  65. {
  66. eState = BSC_INIT;
  67. ucWheelCircumference = System::Convert::ToByte(this->numericUpDown_Sim_WheelCircumference->Value); // Initial value set on UI
  68. usCadEventCount = 0;
  69. usCadPreviousEventCount = 0;
  70. usCadTime1024 = 0;
  71. usCadPreviousTime1024 = 0;
  72. ulCadAcumEventCount = 0;
  73. ulCadAcumTime1024 = 0;
  74. usSpdEventCount = 0;
  75. usSpdPreviousEventCount = 0;
  76. usSpdTime1024 = 0;
  77. usSpdPreviousTime1024 = 0;
  78. ulSpdAcumEventCount = 0;
  79. ulSpdAcumTime1024 = 0;
  80. ucCadence = 0;
  81. ulSpeed = 0;
  82. ulDistance = 0;
  83. bStop = FALSE;
  84. bCoast = FALSE;
  85. }
  86. /**************************************************************************
  87. * BikeSpdCadDisplay::HandleReceive
  88. *
  89. * Decodes received data
  90. *
  91. * pucRxBuffer_: pointer to the buffer containing the received data
  92. *
  93. * returns: N/A
  94. *
  95. **************************************************************************/
  96. void BikeSpdCadDisplay::HandleReceive(UCHAR* pucRxBuffer_)
  97. {
  98. static UCHAR ucNoSpdEventCount = 0; // Counter for successive transmissions with no new speed events
  99. static UCHAR ucNoCadEventCount = 0; // Counter for successive transmissions with no new cadence events
  100. USHORT usEventDiff = 0;
  101. USHORT usTimeDiff1024 = 0;
  102. // Decode data page
  103. usCadTime1024 = pucRxBuffer_[0];
  104. usCadTime1024 += pucRxBuffer_[1] <<8;
  105. usCadEventCount = pucRxBuffer_[2];
  106. usCadEventCount += pucRxBuffer_[3] <<8;
  107. usSpdTime1024 = pucRxBuffer_[4];
  108. usSpdTime1024 += pucRxBuffer_[5] <<8;
  109. usSpdEventCount = pucRxBuffer_[6];
  110. usSpdEventCount += pucRxBuffer_[7] <<8;
  111. // Initialize previous values on first message received
  112. if(eState == BSC_INIT)
  113. {
  114. usCadPreviousTime1024 = usCadTime1024;
  115. usCadPreviousEventCount = usCadEventCount;
  116. usSpdPreviousTime1024 = usSpdTime1024;
  117. usSpdPreviousEventCount = usSpdEventCount;
  118. eState = BSC_ACTIVE;
  119. }
  120. // Update cadence calculations on new cadence event
  121. if(usCadEventCount != usCadPreviousEventCount)
  122. {
  123. ucNoCadEventCount = 0;
  124. bCoast = FALSE;
  125. // Update cumulative event count
  126. if(usCadEventCount > usCadPreviousEventCount)
  127. usEventDiff = usCadEventCount - usCadPreviousEventCount;
  128. else
  129. usEventDiff = (USHORT) (0xFFFF - usCadPreviousEventCount + usCadEventCount + 1);
  130. ulCadAcumEventCount += usEventDiff;
  131. // Update cumulative time (1/1024s)
  132. if(usCadTime1024 > usCadPreviousTime1024)
  133. usTimeDiff1024 = usCadTime1024 - usCadPreviousTime1024;
  134. else
  135. usTimeDiff1024 = (USHORT) (0xFFFF - usCadPreviousTime1024 + usCadTime1024 + 1);
  136. ulCadAcumTime1024 += usTimeDiff1024;
  137. // Calculate cadence (rpm)
  138. if(usTimeDiff1024 > 0)
  139. ucCadence = (UCHAR) ( ((ULONG) usEventDiff * 0xF000) / (ULONG) usTimeDiff1024 ); // 1 min = 0xF000 = 60 * 1024
  140. }
  141. else
  142. {
  143. ucNoCadEventCount++;
  144. if(ucNoCadEventCount >= MAX_NO_EVENTS)
  145. bCoast = TRUE; // Coasting
  146. }
  147. // Update speed calculations on new speed event
  148. if(usSpdEventCount != usSpdPreviousEventCount)
  149. {
  150. ucNoSpdEventCount = 0;
  151. bStop = FALSE;
  152. // Update cumulative event count
  153. if(usSpdEventCount > usSpdPreviousEventCount)
  154. usEventDiff = usSpdEventCount - usSpdPreviousEventCount;
  155. else
  156. usEventDiff = (USHORT) (0xFFFF - usSpdPreviousEventCount + usSpdEventCount + 1);
  157. ulSpdAcumEventCount += usEventDiff;
  158. // Update cumulative time (1/1024s)
  159. if(usSpdTime1024 > usSpdPreviousTime1024)
  160. usTimeDiff1024 = usSpdTime1024 - usSpdPreviousTime1024;
  161. else
  162. usTimeDiff1024 = (USHORT) (0xFFFF - usSpdPreviousTime1024 + usSpdTime1024 + 1);
  163. ulSpdAcumTime1024 += usTimeDiff1024;
  164. // Calculate speed (meters/h)
  165. if(ucWheelCircumference)
  166. ulSpeed = (ucWheelCircumference * 0x9000 * (ULONG) usEventDiff) / (ULONG) usTimeDiff1024; // 1024 * 36 = 0x9000
  167. // Calculate distance (cm)
  168. ulDistance = (ULONG) ucWheelCircumference * ulSpdAcumEventCount;
  169. }
  170. else
  171. {
  172. ucNoSpdEventCount++;
  173. if(ucNoSpdEventCount >= MAX_NO_EVENTS)
  174. bStop = TRUE; // Stopping
  175. }
  176. // Update previous values
  177. usCadPreviousTime1024 = usCadTime1024;
  178. usCadPreviousEventCount = usCadEventCount;
  179. usSpdPreviousTime1024 = usSpdTime1024;
  180. usSpdPreviousEventCount = usSpdEventCount;
  181. }
  182. /**************************************************************************
  183. * BikeSpdCadDisplay::UpdateDisplay
  184. *
  185. * Shows received decoded data on GUI
  186. *
  187. * ucPageNum_: received page
  188. *
  189. * returns: N/A
  190. *
  191. **************************************************************************/
  192. void BikeSpdCadDisplay::UpdateDisplay()
  193. {
  194. // Display received data
  195. this->label_Trn_CadenceTimeDisplay->Text = System::Convert::ToString(usCadTime1024);
  196. this->label_Trn_CadCountDisplay->Text = System::Convert::ToString(usCadEventCount);
  197. this->label_Trn_SpeedTimeDisplay->Text = System::Convert::ToString(usSpdTime1024);
  198. this->label_Trn_SpdCountDisplay->Text = System::Convert::ToString(usSpdEventCount);
  199. // Display calculations
  200. this->label_Calc_CadenceDisplay->Text = System::Convert::ToString(ucCadence);
  201. this->label_Calc_CadEventCountDisplay->Text = System::Convert::ToString((unsigned int) ulCadAcumEventCount);
  202. this->label_Calc_SpdEventCountDisplay->Text = System::Convert::ToString((unsigned int) ulSpdAcumEventCount);
  203. this->label_Calc_SpeedDisplay->Text = System::Convert::ToString((double) ulSpeed/1000); // meters/h -> km/h
  204. this->label_Calc_DistanceDisplay->Text = System::Convert::ToString(System::Math::Round((double) ulDistance/100000, 3)); // cm -> km
  205. // Most recent available time
  206. if(ulSpdAcumTime1024 < ulCadAcumTime1024)
  207. this->label_Calc_ElapsedSecsDisplay->Text = System::Convert::ToString(System::Math::Round((double) ulCadAcumTime1024/1024,3)); // 1/1024 s -> s
  208. else
  209. this->label_Calc_ElapsedSecsDisplay->Text = System::Convert::ToString(System::Math::Round((double) ulSpdAcumTime1024/1024,3)); // 1/1024 s -> s
  210. // Stopping and coasting detected
  211. this->label_Coasting->Visible = (bCoast == TRUE && bStop == FALSE); // Coasting not displayed if bike has stopped
  212. this->label_Stopped->Visible = (bStop == TRUE);
  213. }
  214. /**************************************************************************
  215. * BikeSpdCadDisplay::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 BikeSpdCadDisplay::numericUpDown_Sim_WheelCircumference_ValueChanged(System::Object^ sender, System::EventArgs^ e)
  224. {
  225. ucWheelCircumference = System::Convert::ToByte(this->numericUpDown_Sim_WheelCircumference->Value);
  226. }