123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- #include "StdAfx.h"
- #include "BikeSpdCadSensor.h"
- void BikeSpdCadSensor::ANT_eventNotification(UCHAR ucEventCode_, UCHAR* pucEventBuffer_)
- {
- switch(ucEventCode_)
- {
- case EVENT_TX:
- HandleTransmit((UCHAR*) pucEventBuffer_);
- break;
- default:
- break;
- }
- }
- void BikeSpdCadSensor::InitializeSim()
- {
-
- ulRunTime16000 = 0;
- ulTimerInterval = 247;
- ulNextCadInterval = 667;
- ulNextSpdInterval = 248;
-
- usCadEventCount = 0;
- usCadTime1024 = 0;
- ucCurCadence = (UCHAR)this->numericUpDown_Sim_CadCurOutput->Value;
- ucMinCadence = (UCHAR)this->numericUpDown_Sim_CadMinOutput->Value;
- ucMaxCadence = (UCHAR)this->numericUpDown_Sim_CadMaxOutput->Value;
- ucCadSimDataType = SIM_FIXED;
- bCadSweepAscending = TRUE;
-
-
- usSpdEventCount = 0;
- usSpdTime1024 = 0;
- ulCurSpeed = (ULONG) (System::Convert::ToDouble(this->numericUpDown_Sim_SpdCurOutput->Value) * 1000);
- ulMinSpeed = (ULONG) (System::Convert::ToDouble(this->numericUpDown_Sim_SpdMinOutput->Value) * 1000);
- ulMaxSpeed = (ULONG) (System::Convert::ToDouble(this->numericUpDown_Sim_SpdMaxOutput->Value) * 1000);
- ucWheelCircumference = System::Convert::ToByte(this->numericUpDown_Sim_WheelCircumference->Value);
- ucSpdSimDataType = SIM_FIXED;
- bSpdSweepAscending = TRUE;
- }
- void BikeSpdCadSensor::HandleTransmit(UCHAR* pucTxBuffer_)
- {
-
- pucTxBuffer_[0] = (UCHAR) (usCadTime1024 & 0xFF);
- pucTxBuffer_[1] = (UCHAR) (usCadTime1024 >> 8) & 0xFF;
- pucTxBuffer_[2] = (UCHAR) (usCadEventCount & 0xFF);
- pucTxBuffer_[3] = (UCHAR) (usCadEventCount >> 8) & 0xFF;
- pucTxBuffer_[4] = (UCHAR) (usSpdTime1024 & 0xFF);
- pucTxBuffer_[5] = (UCHAR) (usSpdTime1024 >> 8) & 0xFF;
- pucTxBuffer_[6] = (UCHAR) (usSpdEventCount & 0xFF);
- pucTxBuffer_[7] = (UCHAR) (usSpdEventCount >> 8) & 0xFF;
- }
- void BikeSpdCadSensor::onTimerTock(USHORT eventTime)
- {
-
- ulRunTime16000 += (ulTimerInterval << 4);
-
- if(ulNextCadInterval < ulNextSpdInterval)
- {
-
- ++usCadEventCount;
-
- usCadTime1024 = (USHORT) ((ulRunTime16000 << 3) / 125);
-
- switch(ucCadSimDataType)
- {
- case SIM_FIXED:
-
- break;
- case SIM_SWEEP:
- {
-
-
- ULONG tempOffset = ucMaxCadence-ucCurCadence;
- tempOffset = ((tempOffset & 0xC0) >> 6) + ((tempOffset & 0x20) >>5) + ((tempOffset & 0x10) >>4)+1;
- if(bCadSweepAscending)
- ucCurCadence += (UCHAR) tempOffset;
- else
- ucCurCadence -= (UCHAR) tempOffset;
-
- if(ucCurCadence >= ucMaxCadence)
- {
- ucCurCadence = ucMaxCadence;
- bCadSweepAscending = FALSE;
- }
- if(ucCurCadence <= ucMinCadence)
- {
- ucCurCadence = ucMinCadence;
- bCadSweepAscending = TRUE;
- }
- break;
- }
- default:
- break;
- }
-
- ulNextSpdInterval -= ulNextCadInterval;
-
- if(ucCurCadence)
- ulNextCadInterval = (ULONG) 60000/ucCurCadence;
- else
- ulNextCadInterval = 600000;
-
- if(ulNextCadInterval < ulNextSpdInterval)
- ulTimerInterval = ulNextCadInterval;
- else
- ulTimerInterval = ulNextSpdInterval;
- }
- else
- {
-
- ++usSpdEventCount;
-
- usSpdTime1024 = (USHORT) ((ulRunTime16000 << 3) / 125);
- switch(ucSpdSimDataType)
- {
- case SIM_FIXED:
-
- break;
- case SIM_SWEEP:
- {
-
-
- ULONG tempOffset = ulMaxSpeed-ulCurSpeed;
- tempOffset = ((tempOffset & 0x7000) >> 6) + ((tempOffset & 0xE00) >> 5) + ((tempOffset & 0x1C) >> 4)+0x7F;
- if(bSpdSweepAscending)
- ulCurSpeed += tempOffset;
- else
- ulCurSpeed -= tempOffset;
-
- if(ulCurSpeed >= ulMaxSpeed)
- {
- ulCurSpeed = ulMaxSpeed;
- bSpdSweepAscending = FALSE;
- }
- if(ulCurSpeed <= ulMinSpeed)
- {
- ulCurSpeed = ulMinSpeed;
- bSpdSweepAscending = TRUE;
- }
- break;
- }
- default:
- break;
- }
-
- ulNextCadInterval -= ulNextSpdInterval;
-
- if(ulCurSpeed)
- ulNextSpdInterval = (ULONG) 36000 * ucWheelCircumference/ulCurSpeed;
- else
- ulNextSpdInterval = 600000;
-
- if(ulNextSpdInterval < ulNextCadInterval)
- ulTimerInterval = ulNextSpdInterval;
- else
- ulTimerInterval = ulNextCadInterval;
- }
- if(ulTimerInterval < 1)
- ulTimerInterval = 1;
- UpdateDisplay();
- }
- void BikeSpdCadSensor::UpdateDisplay()
- {
- this->label_Trn_CadCountDisplay->Text = System::Convert::ToString(usCadEventCount);
- this->label_Trn_CadenceTimeDisplay->Text = System::Convert::ToString(usCadTime1024);
- this->label_Trn_SpdCountDisplay->Text = System::Convert::ToString(usSpdEventCount);
- this->label_Trn_SpeedTimeDisplay->Text = System::Convert::ToString(usSpdTime1024);
- if(ucCadSimDataType == SIM_SWEEP)
- this->numericUpDown_Sim_CadCurOutput->Value = ucCurCadence;
- if(ucSpdSimDataType == SIM_SWEEP)
- this->numericUpDown_Sim_SpdCurOutput->Value = System::Convert::ToDecimal((double) ulCurSpeed/1000);
- }
- System::Void BikeSpdCadSensor::numericUpDown_Sim_WheelCircumference_ValueChanged(System::Object^ sender, System::EventArgs^ e)
- {
- ucWheelCircumference = System::Convert::ToByte(this->numericUpDown_Sim_WheelCircumference->Value);
- }
- System::Void BikeSpdCadSensor::checkBox_Sim_SpeedSweeping_CheckedChanged(System::Object^ sender, System::EventArgs^ e)
- {
- this->numericUpDown_Sim_SpdCurOutput->Enabled = !this->numericUpDown_Sim_SpdCurOutput->Enabled;
- this->numericUpDown_Sim_SpdMinOutput->Enabled = !this->numericUpDown_Sim_SpdMinOutput->Enabled;
- this->numericUpDown_Sim_SpdMaxOutput->Enabled = !this->numericUpDown_Sim_SpdMaxOutput->Enabled;
- if(this->checkBox_Sim_SpdSweeping->Checked)
- {
- bSpdSweepAscending = TRUE;
- ucSpdSimDataType = SIM_SWEEP;
- }
- else
- {
- ucSpdSimDataType = SIM_FIXED;
- }
- }
- System::Void BikeSpdCadSensor::checkBox_Sim_CadenceSweeping_CheckedChanged(System::Object^ sender, System::EventArgs^ e)
- {
- this->numericUpDown_Sim_CadCurOutput->Enabled = !this->numericUpDown_Sim_CadCurOutput->Enabled;
- this->numericUpDown_Sim_CadMinOutput->Enabled = !this->numericUpDown_Sim_CadMinOutput->Enabled;
- this->numericUpDown_Sim_CadMaxOutput->Enabled = !this->numericUpDown_Sim_CadMaxOutput->Enabled;
-
- if(this->checkBox_Sim_CadSweeping->Checked)
- {
- bCadSweepAscending = TRUE;
- ucCadSimDataType = SIM_SWEEP;
- }
- else
- {
- ucCadSimDataType = SIM_FIXED;
- }
- }
- System::Void BikeSpdCadSensor::numericUpDown_Sim_SpdCurOutput_ValueChanged(System::Object^ sender, System::EventArgs^ e)
- {
-
-
- if(this->numericUpDown_Sim_SpdCurOutput->Enabled)
- {
- ulCurSpeed = (ULONG) (System:: Convert::ToDouble(this->numericUpDown_Sim_SpdCurOutput->Value) * 1000);
- if(ulCurSpeed)
- ulNextSpdInterval = (ULONG) 36000 * ucWheelCircumference/ulCurSpeed;
- else
- ulNextSpdInterval = 600000;
- ForceUpdate();
- }
- }
- System::Void BikeSpdCadSensor::numericUpDown_Sim_SpdMinMaxOutput_ValueChanged(System::Object^ sender, System::EventArgs^ e)
- {
- ULONG ulPrevSpeed = ulCurSpeed;
-
-
- if(this->numericUpDown_Sim_SpdMinOutput->Value < this->numericUpDown_Sim_SpdMaxOutput->Value)
- {
- ulMinSpeed = (ULONG) (System::Convert::ToDouble(this->numericUpDown_Sim_SpdMinOutput->Value) * 1000);
- ulMaxSpeed = (ULONG) (System::Convert::ToDouble(this->numericUpDown_Sim_SpdMaxOutput->Value) * 1000);
- if(ulCurSpeed > ulMaxSpeed)
- ulCurSpeed = ulMaxSpeed;
- else if(ulCurSpeed < ulMinSpeed)
- ulCurSpeed = ulMinSpeed;
- if(ulCurSpeed != ulPrevSpeed)
- {
- this->numericUpDown_Sim_SpdCurOutput->Value = System::Convert::ToDecimal((double) ulCurSpeed/1000);
- if(ulCurSpeed)
- ulNextSpdInterval = (ULONG) 36000 * ucWheelCircumference/ulCurSpeed;
- else
- ulNextSpdInterval = 600000;
- ForceUpdate();
- }
- }
- else
- {
-
- this->numericUpDown_Sim_SpdMinOutput->Value = System::Convert::ToDecimal((double) ulMinSpeed/1000);
- this->numericUpDown_Sim_SpdMaxOutput->Value = System::Convert::ToDecimal((double) ulMaxSpeed/1000);
- }
- }
- System::Void BikeSpdCadSensor::numericUpDown_Sim_CadCurOutput_ValueChanged(System::Object^ sender, System::EventArgs^ e)
- {
-
-
- if(this->numericUpDown_Sim_CadCurOutput->Enabled)
- {
- ucCurCadence = System::Convert::ToByte(this->numericUpDown_Sim_CadCurOutput->Value);
- if(ucCurCadence)
- ulNextCadInterval = (ULONG) 60000/ucCurCadence;
- else
- ulNextCadInterval = 600000;
- ForceUpdate();
- }
- }
- System::Void BikeSpdCadSensor::numericUpDown_Sim_CadMinMaxOutput_ValueChanged(System::Object^ sender, System::EventArgs^ e)
- {
- UCHAR ucPrevCadence = ucCurCadence;
-
-
- if(this->numericUpDown_Sim_CadMinOutput->Value < this->numericUpDown_Sim_CadMaxOutput->Value)
- {
- ucMinCadence = (UCHAR) this->numericUpDown_Sim_CadMinOutput->Value;
- ucMaxCadence = (UCHAR) this->numericUpDown_Sim_CadMaxOutput->Value;
- if(ucCurCadence > ucMaxCadence)
- ucCurCadence = ucMaxCadence;
- else if(ucCurCadence < ucMinCadence)
- ucCurCadence = ucMinCadence;
- if(ucCurCadence != ucPrevCadence)
- {
- this->numericUpDown_Sim_CadCurOutput->Value = ucCurCadence;
- if(ucCurCadence)
- ulNextCadInterval = (ULONG) 60000/ucCurCadence;
- else
- ulNextCadInterval = 600000;
- ForceUpdate();
- }
- }
- else
- {
-
- this->numericUpDown_Sim_CadMinOutput->Value = ucMinCadence;
- this->numericUpDown_Sim_CadMaxOutput->Value = ucMaxCadence;
- }
- }
- void BikeSpdCadSensor::ForceUpdate()
- {
- timerHandle->Interval = 250;
- }
|