123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
-
- #include "StdAfx.h"
- #include "BikeSpdCadDisplay.h"
- void BikeSpdCadDisplay::ANT_eventNotification(UCHAR ucEventCode_, UCHAR* pucEventBuffer_)
- {
- switch(ucEventCode_)
- {
- case EVENT_RX_ACKNOWLEDGED:
- case EVENT_RX_BURST_PACKET:
- case EVENT_RX_BROADCAST:
- HandleReceive((UCHAR*) pucEventBuffer_);
- UpdateDisplay();
- break;
- default:
- break;
- }
- }
- void BikeSpdCadDisplay::InitializeSim()
- {
- eState = BSC_INIT;
- ucWheelCircumference = System::Convert::ToByte(this->numericUpDown_Sim_WheelCircumference->Value);
- usCadEventCount = 0;
- usCadPreviousEventCount = 0;
- usCadTime1024 = 0;
- usCadPreviousTime1024 = 0;
- ulCadAcumEventCount = 0;
- ulCadAcumTime1024 = 0;
- usSpdEventCount = 0;
- usSpdPreviousEventCount = 0;
- usSpdTime1024 = 0;
- usSpdPreviousTime1024 = 0;
- ulSpdAcumEventCount = 0;
- ulSpdAcumTime1024 = 0;
- ucCadence = 0;
- ulSpeed = 0;
- ulDistance = 0;
- bStop = FALSE;
- bCoast = FALSE;
- }
- void BikeSpdCadDisplay::HandleReceive(UCHAR* pucRxBuffer_)
- {
- static UCHAR ucNoSpdEventCount = 0;
- static UCHAR ucNoCadEventCount = 0;
- USHORT usEventDiff = 0;
- USHORT usTimeDiff1024 = 0;
-
- usCadTime1024 = pucRxBuffer_[0];
- usCadTime1024 += pucRxBuffer_[1] <<8;
- usCadEventCount = pucRxBuffer_[2];
- usCadEventCount += pucRxBuffer_[3] <<8;
- usSpdTime1024 = pucRxBuffer_[4];
- usSpdTime1024 += pucRxBuffer_[5] <<8;
- usSpdEventCount = pucRxBuffer_[6];
- usSpdEventCount += pucRxBuffer_[7] <<8;
-
- if(eState == BSC_INIT)
- {
- usCadPreviousTime1024 = usCadTime1024;
- usCadPreviousEventCount = usCadEventCount;
- usSpdPreviousTime1024 = usSpdTime1024;
- usSpdPreviousEventCount = usSpdEventCount;
- eState = BSC_ACTIVE;
- }
-
- if(usCadEventCount != usCadPreviousEventCount)
- {
- ucNoCadEventCount = 0;
- bCoast = FALSE;
-
- if(usCadEventCount > usCadPreviousEventCount)
- usEventDiff = usCadEventCount - usCadPreviousEventCount;
- else
- usEventDiff = (USHORT) (0xFFFF - usCadPreviousEventCount + usCadEventCount + 1);
- ulCadAcumEventCount += usEventDiff;
-
- if(usCadTime1024 > usCadPreviousTime1024)
- usTimeDiff1024 = usCadTime1024 - usCadPreviousTime1024;
- else
- usTimeDiff1024 = (USHORT) (0xFFFF - usCadPreviousTime1024 + usCadTime1024 + 1);
- ulCadAcumTime1024 += usTimeDiff1024;
-
- if(usTimeDiff1024 > 0)
- ucCadence = (UCHAR) ( ((ULONG) usEventDiff * 0xF000) / (ULONG) usTimeDiff1024 );
- }
- else
- {
- ucNoCadEventCount++;
- if(ucNoCadEventCount >= MAX_NO_EVENTS)
- bCoast = TRUE;
- }
-
- if(usSpdEventCount != usSpdPreviousEventCount)
- {
- ucNoSpdEventCount = 0;
- bStop = FALSE;
-
- if(usSpdEventCount > usSpdPreviousEventCount)
- usEventDiff = usSpdEventCount - usSpdPreviousEventCount;
- else
- usEventDiff = (USHORT) (0xFFFF - usSpdPreviousEventCount + usSpdEventCount + 1);
- ulSpdAcumEventCount += usEventDiff;
-
- if(usSpdTime1024 > usSpdPreviousTime1024)
- usTimeDiff1024 = usSpdTime1024 - usSpdPreviousTime1024;
- else
- usTimeDiff1024 = (USHORT) (0xFFFF - usSpdPreviousTime1024 + usSpdTime1024 + 1);
- ulSpdAcumTime1024 += usTimeDiff1024;
-
- if(ucWheelCircumference)
- ulSpeed = (ucWheelCircumference * 0x9000 * (ULONG) usEventDiff) / (ULONG) usTimeDiff1024;
-
- ulDistance = (ULONG) ucWheelCircumference * ulSpdAcumEventCount;
- }
- else
- {
- ucNoSpdEventCount++;
- if(ucNoSpdEventCount >= MAX_NO_EVENTS)
- bStop = TRUE;
- }
-
- usCadPreviousTime1024 = usCadTime1024;
- usCadPreviousEventCount = usCadEventCount;
- usSpdPreviousTime1024 = usSpdTime1024;
- usSpdPreviousEventCount = usSpdEventCount;
- }
- void BikeSpdCadDisplay::UpdateDisplay()
- {
-
- this->label_Trn_CadenceTimeDisplay->Text = System::Convert::ToString(usCadTime1024);
- this->label_Trn_CadCountDisplay->Text = System::Convert::ToString(usCadEventCount);
- this->label_Trn_SpeedTimeDisplay->Text = System::Convert::ToString(usSpdTime1024);
- this->label_Trn_SpdCountDisplay->Text = System::Convert::ToString(usSpdEventCount);
-
- this->label_Calc_CadenceDisplay->Text = System::Convert::ToString(ucCadence);
- this->label_Calc_CadEventCountDisplay->Text = System::Convert::ToString((unsigned int) ulCadAcumEventCount);
- this->label_Calc_SpdEventCountDisplay->Text = System::Convert::ToString((unsigned int) ulSpdAcumEventCount);
- this->label_Calc_SpeedDisplay->Text = System::Convert::ToString((double) ulSpeed/1000);
- this->label_Calc_DistanceDisplay->Text = System::Convert::ToString(System::Math::Round((double) ulDistance/100000, 3));
-
- if(ulSpdAcumTime1024 < ulCadAcumTime1024)
- this->label_Calc_ElapsedSecsDisplay->Text = System::Convert::ToString(System::Math::Round((double) ulCadAcumTime1024/1024,3));
- else
- this->label_Calc_ElapsedSecsDisplay->Text = System::Convert::ToString(System::Math::Round((double) ulSpdAcumTime1024/1024,3));
-
- this->label_Coasting->Visible = (bCoast == TRUE && bStop == FALSE);
- this->label_Stopped->Visible = (bStop == TRUE);
- }
- System::Void BikeSpdCadDisplay::numericUpDown_Sim_WheelCircumference_ValueChanged(System::Object^ sender, System::EventArgs^ e)
- {
- ucWheelCircumference = System::Convert::ToByte(this->numericUpDown_Sim_WheelCircumference->Value);
- }
|