123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539 |
-
- #include "StdAfx.h"
- #include "BikeCadenceSensor.h"
- void BikeCadenceSensor::ANT_eventNotification(UCHAR ucEventCode_, UCHAR* pucEventBuffer_)
- {
- switch(ucEventCode_)
- {
- case EVENT_TX:
- HandleTransmit((UCHAR*) pucEventBuffer_);
- break;
- default:
- break;
- }
- }
- void BikeCadenceSensor::InitializeSim()
- {
-
- ulTimerInterval = 667;
-
- ulRunTime = 0;
- ulRunTime16000 = 0;
- ucReserved = BC_RESERVED;
- usEventCount = 0;
- ucMinCadence = (UCHAR)this->numericUpDown_Sim_MinCadence->Value;
- ucCurCadence = (UCHAR)this->numericUpDown_Sim_CurrentCadence->Value;
- ucMaxCadence = (UCHAR)this->numericUpDown_Sim_MaxCadence->Value;
- ulElapsedTime2 = 0;
- usTime1024 = 0;
-
-
- ucMfgID = System::Convert::ToByte(this->textBox_ManfIDChange->Text);
- ucHwVersion = System::Convert::ToByte(this->textBox_HardwareVerChange->Text);
- ucSwVersion = System::Convert::ToByte(this->textBox_SoftwareVerChange->Text);
- ucModelNum = System::Convert::ToByte(this->textBox_ModelNumChange->Text);
- usSerialNum = System::Convert::ToUInt16(this->textBox_SerialNumChange->Text);
-
- ucSimDataType = SIM_FIXED;
- bLegacy = FALSE;
- bTxMinimum = FALSE;
- bSweepAscending = TRUE;
- ucBackgroundCount = 0;
- ucNextBackgroundPage = BC_PAGE1;
- }
- void BikeCadenceSensor::HandleTransmit(UCHAR* pucTxBuffer_)
- {
- static UCHAR ucMessageNum = 0;
- static UCHAR ucPageToggle = 0;
- UCHAR ucPageNum = BC_PAGE0;
-
- if(!bLegacy)
- {
- if(ucMessageNum++ == BC_BACKGROUND_INTERVAL-1)
- {
- ucMessageNum = 0;
- switch(ucNextBackgroundPage)
- {
- case 1:
- ucPageNum = BC_PAGE1;
- ucNextBackgroundPage++;
- break;
- case 2:
- ucPageNum = BC_PAGE2;
- if(bTxMinimum)
- {
-
- if(++ucBackgroundCount > 2)
- {
- ucBackgroundCount = 0;
- ucNextBackgroundPage ++;
- }
- }
- else
- {
-
- ucNextBackgroundPage++;
- }
- break;
- case 3:
- ucPageNum = BC_PAGE3;
- if(bTxMinimum)
- {
-
- if(++ucBackgroundCount > 2)
- {
- ucBackgroundCount = 0;
- ucNextBackgroundPage = BC_PAGE2;
- }
- }
- else
- {
-
- ucNextBackgroundPage = BC_PAGE1;
- }
- break;
- default:
- break;
- }
- }
- }
-
- if(!bLegacy)
- pucTxBuffer_[0] = ucPageNum;
- else
- pucTxBuffer_[0] = ucReserved;
- pucTxBuffer_[4] = (UCHAR) (usTime1024 & 0xFF);
- pucTxBuffer_[5] = (UCHAR) (usTime1024 >> 8) & 0xFF;
- pucTxBuffer_[6] = (UCHAR) (usEventCount & 0xFF);
- pucTxBuffer_[7] = (UCHAR) (usEventCount >> 8) & 0xFF;
-
- switch(ucPageNum)
- {
- case BC_PAGE0:
- pucTxBuffer_[1] = ucReserved;
- pucTxBuffer_[2] = ucReserved;
- pucTxBuffer_[3] = ucReserved;
- break;
- case BC_PAGE1:
- pucTxBuffer_[1] = (ulElapsedTime2 & 0xFF);
- pucTxBuffer_[2] = (ulElapsedTime2 >> 8) & 0xFF;
- pucTxBuffer_[3] = (ulElapsedTime2 >> 16) & 0xFF;
- break;
- case BC_PAGE2:
- pucTxBuffer_[1] = ucMfgID;
- pucTxBuffer_[2] = usSerialNum & 0x00FF;
- pucTxBuffer_[3] = (usSerialNum & 0xFF00) >>8;
- break;
- case BC_PAGE3:
- pucTxBuffer_[1] = ucHwVersion;
- pucTxBuffer_[2] = ucSwVersion;
- pucTxBuffer_[3] = ucModelNum;
- break;
- default:
- break;
- }
-
- if(!bLegacy)
- {
- ucPageToggle += 0x20;
- pucTxBuffer_[0] += (ucPageToggle & BC_TOGGLE_MASK);
- }
- }
- void BikeCadenceSensor::onTimerTock(USHORT eventTime)
- {
- UCHAR tempOffset = 0;
-
- ++usEventCount;
-
- ulRunTime16000 += (ulTimerInterval << 4);
- usTime1024 = (USHORT) ((ulRunTime16000 << 3) / 125);
- ulRunTime += ulTimerInterval;
- while(ulRunTime/2000)
- {
- ++ulElapsedTime2;
- ulRunTime -=2000;
- }
- switch(ucSimDataType)
- {
- case SIM_FIXED:
-
- break;
- case SIM_SWEEP:
-
-
- tempOffset = ucMaxCadence-ucCurCadence;
- tempOffset = ((tempOffset & 0xC0) >> 6) + ((tempOffset & 0x20) >>5) + ((tempOffset & 0x10) >>4)+1;
- if(bSweepAscending)
- ucCurCadence += tempOffset;
- else
- ucCurCadence -= tempOffset;
-
- if(ucCurCadence >= ucMaxCadence)
- {
- ucCurCadence = ucMaxCadence;
- bSweepAscending = FALSE;
- }
- if(ucCurCadence <= ucMinCadence)
- {
- ucCurCadence = ucMinCadence;
- bSweepAscending = TRUE;
- }
- break;
- default:
- break;
- }
-
- if(ucCurCadence)
- ulTimerInterval = (ULONG) 60000/ucCurCadence;
- else
- ulTimerInterval = 600000;
-
-
- UpdateDisplay();
- }
- void BikeCadenceSensor::UpdateDisplay()
- {
- this->label_Trn_EventCountDisplay->Text = usEventCount.ToString();
- this->label_ElapsedSecsDisplay->Text = ((ulElapsedTime2 & 0x00FFFFFF) << 1).ToString();
- if(ucSimDataType == SIM_SWEEP)
- this->numericUpDown_Sim_CurrentCadence->Value = ucCurCadence;
- this->label_Trn_TimeDisplay->Text = usTime1024.ToString();
- }
- void BikeCadenceSensor::radioButton_SimTypeChanged (System::Object^ sender, System::EventArgs^ e)
- {
- this->numericUpDown_Sim_CurrentCadence->Enabled = !this->numericUpDown_Sim_CurrentCadence->Enabled;
- this->numericUpDown_Sim_MaxCadence->Enabled = !this->numericUpDown_Sim_MaxCadence->Enabled;
- this->numericUpDown_Sim_MinCadence->Enabled = !this->numericUpDown_Sim_MinCadence->Enabled;
- if(this->radioButton_Sim_Fixed->Checked)
- {
- ucSimDataType = SIM_FIXED;
- }
- else if(this->radioButton_Sim_Sweep->Checked)
- {
- bSweepAscending = TRUE;
- ucSimDataType = SIM_SWEEP;
- }
- }
- System::Void BikeCadenceSensor::button_AdvancedUpdate_Click(System::Object^ sender, System::EventArgs^ e)
- {
- label_AdvancedError->Visible = false;
- label_AdvancedError->Text = "Error: ";
-
- try{
- ucMfgID = System::Convert::ToByte(this->textBox_ManfIDChange->Text);
- }
- catch(...){
- label_AdvancedError->Text = System::String::Concat(label_AdvancedError->Text, " MFID");
- label_AdvancedError->Visible = true;
- }
- try{
- usSerialNum = System::Convert::ToUInt16(this->textBox_SerialNumChange->Text);
- }
- catch(...){
- label_AdvancedError->Text = System::String::Concat(label_AdvancedError->Text, " Ser#");
- label_AdvancedError->Visible = true;
- }
- try{
- ucHwVersion = System::Convert::ToByte(this->textBox_HardwareVerChange->Text);
- }
- catch(...){
- label_AdvancedError->Text = System::String::Concat(label_AdvancedError->Text, " HWVr");
- label_AdvancedError->Visible = true;
- }
- try{
- ucSwVersion = System::Convert::ToByte(this->textBox_SoftwareVerChange->Text);
- }
- catch(...){
- label_AdvancedError->Text = System::String::Concat(label_AdvancedError->Text, " SWVr");
- label_AdvancedError->Visible = true;
- }
- try{
- ucModelNum = System::Convert::ToByte(this->textBox_ModelNumChange->Text);
- }
- catch(...){
- label_AdvancedError->Text = System::String::Concat(label_AdvancedError->Text, " Mdl#");
- label_AdvancedError->Visible = true;
- }
- }
- System::Void BikeCadenceSensor::button_UpdateTime_Click(System::Object^ sender, System::EventArgs^ e)
- {
- ULONG ulCumulativeTime = 0;
- label_AdvancedError->Visible = false;
- try
- {
- ulCumulativeTime = System::Convert::ToUInt32(this->textBox_ElpTimeChange->Text);
- if(ulCumulativeTime > 33554430)
- throw "Cumulative operating time exceeds rollover value";
- ulElapsedTime2 = ulCumulativeTime >> 1;
-
- label_ElapsedSecsDisplay->Text = ((ulElapsedTime2 & 0x00FFFFFF) << 1).ToString();
- }
- catch(...)
- {
- label_AdvancedError->Text = "Error: Time";
- label_AdvancedError->Visible = true;
- }
- }
- System::Void BikeCadenceSensor::numericUpDown_Sim_CurCadence_ValueChanged(System::Object^ sender, System::EventArgs^ e)
- {
-
-
- if(this->numericUpDown_Sim_CurrentCadence->Enabled)
- {
- ucCurCadence = (UCHAR)this->numericUpDown_Sim_CurrentCadence->Value;
- ForceUpdate();
- }
- }
- System::Void BikeCadenceSensor::numericUpDown_Sim_MinMaxCadence_ValueChanged(System::Object^ sender, System::EventArgs^ e)
- {
- UCHAR ucPrevCadence = ucCurCadence;
-
-
- if(this->numericUpDown_Sim_MinCadence->Value < this->numericUpDown_Sim_MaxCadence->Value)
- {
- ucMinCadence = (UCHAR)this->numericUpDown_Sim_MinCadence->Value;
- ucMaxCadence = (UCHAR)this->numericUpDown_Sim_MaxCadence->Value;
- if(ucCurCadence > ucMaxCadence)
- ucCurCadence = ucMaxCadence;
- else if(ucCurCadence < ucMinCadence)
- ucCurCadence = ucMinCadence;
- if(ucCurCadence != ucPrevCadence)
- {
- this->numericUpDown_Sim_CurrentCadence->Value = ucCurCadence;
- ForceUpdate();
- }
- }
- else
- {
-
- this->numericUpDown_Sim_MinCadence->Value = ucMinCadence;
- this->numericUpDown_Sim_MaxCadence->Value = ucMaxCadence;
- }
- }
-
-
- System::Void BikeCadenceSensor::checkBox_Legacy_CheckedChanged(System::Object^ sender, System::EventArgs^ e)
- {
-
- if(checkBox_Legacy->Checked)
- {
- System::Windows::Forms::DialogResult result = MessageBox::Show(L"This option is available for backward compatibility testing.\nIt should not be used as a reference in the development of new sensors.", L"Warning", MessageBoxButtons::OKCancel);
- if( result == ::DialogResult::OK )
- {
- bLegacy = TRUE;
-
-
-
- srand(ulElapsedTime2);
- ucReserved = (UCHAR) ((rand() % 240) + 16);
- checkBox_SendBasicPage->Checked = FALSE;
- checkBox_SendBasicPage->Enabled = FALSE;
- }
- else
- {
- bLegacy = FALSE;
- ucReserved = BC_RESERVED;
- checkBox_Legacy->Checked = FALSE;
- checkBox_SendBasicPage->Enabled = TRUE;
- }
- }
- else
- {
- bLegacy = FALSE;
- ucReserved = BC_RESERVED;
- checkBox_SendBasicPage->Enabled = TRUE;
- }
- }
- System::Void BikeCadenceSensor::checkBox_SendBasicPage_CheckedChanged(System::Object^ sender, System::EventArgs^ e)
- {
- if(checkBox_SendBasicPage->Checked)
- {
- bTxMinimum = TRUE;
- ucNextBackgroundPage = BC_PAGE2;
- ucBackgroundCount = 0;
- }
- else
- {
- bTxMinimum = FALSE;
- ucNextBackgroundPage = BC_PAGE1;
- ucBackgroundCount = 0;
- }
- }
- void BikeCadenceSensor::ForceUpdate()
- {
- timerHandle->Interval = 250;
- }
|