CustomSensor.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 "CustomSensor.h"
  10. /**************************************************************************
  11. * CustomSensor::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 CustomSensor::ANT_eventNotification(UCHAR ucEventCode_, UCHAR* pucEventBuffer_)
  23. {
  24. switch(ucEventCode_)
  25. {
  26. case EVENT_TX:
  27. HandleTransmit((UCHAR*) pucEventBuffer_);
  28. break;
  29. default:
  30. break;
  31. }
  32. }
  33. /**************************************************************************
  34. * CustomSensor::InitializeSim
  35. *
  36. * Initializes simulator variables
  37. *
  38. * returns: N/A
  39. *
  40. **************************************************************************/
  41. void CustomSensor::InitializeSim()
  42. {
  43. ulEventCounter = 0;
  44. bSendHex = FALSE;
  45. ValidateInput();
  46. }
  47. /**************************************************************************
  48. * CustomSensor::HandleTransmit
  49. *
  50. * Encode data generated by simulator for transmission
  51. *
  52. * pucTxBuffer_: pointer to the transmit buffer
  53. *
  54. * returns: N/A
  55. *
  56. **************************************************************************/
  57. void CustomSensor::HandleTransmit(UCHAR* pucTxBuffer_)
  58. {
  59. UCHAR i;
  60. // Copy user data into transmit buffer
  61. for(i=0; i<8; i++)
  62. {
  63. pucTxBuffer_[i] = arrayUserData[i];
  64. }
  65. }
  66. /**************************************************************************
  67. * CustomSensor::onTimerTock
  68. *
  69. * Simulates a device event, updating simulator data based on this event
  70. *
  71. * usEventTime_: current time (ms)
  72. *
  73. * returns: N/A
  74. *
  75. **************************************************************************/
  76. void CustomSensor::onTimerTock(USHORT usEventTime_)
  77. {
  78. ++ulEventCounter;
  79. label_EventCount->Text = ulEventCounter.ToString();
  80. }
  81. /**************************************************************************
  82. * CustomSensor::button_UpdateData_Click
  83. *
  84. * Validates user input and updates data to transmit
  85. *
  86. * returns: N/A
  87. *
  88. **************************************************************************/
  89. System::Void CustomSensor::button_UpdateData_Click(System::Object^ sender, System::EventArgs^ e)
  90. {
  91. ValidateInput();
  92. }
  93. /**************************************************************************
  94. * CustomSensor::radioButton_TranslateSelect_CheckedChanged
  95. *
  96. * Selects format of input box for message to send: Hex/Char
  97. *
  98. * pucRxBuffer_: pointer to the buffer containing the received data
  99. *
  100. * returns: N/A
  101. *
  102. **************************************************************************/
  103. System::Void CustomSensor::radioButton_TranslateSelect_CheckedChanged(System::Object^ sender, System::EventArgs^ e) {
  104. if(this->radioButton_asChar->Checked){
  105. this->textBox_customTxData->Text = "Max8Char";
  106. this->textBox_customTxData->MaxLength = 8;
  107. bSendHex = FALSE;
  108. }
  109. else if(this->radioButton_asHex->Checked){
  110. this->textBox_customTxData->Text = "00,00,00,00,00,00,00,00";
  111. this->textBox_customTxData->MaxLength = 23;
  112. bSendHex = TRUE;
  113. }
  114. }
  115. /**************************************************************************
  116. * CustomSensor::button_SendAck_Click
  117. *
  118. * Sends user data as an acknowledged messaage
  119. *
  120. * returns: N/A
  121. *
  122. **************************************************************************/
  123. System::Void CustomSensor::button_SendAck_Click(System::Object^ sender, System::EventArgs^ e)
  124. {
  125. UCHAR i;
  126. UCHAR aucTxBuffer[8] = {0,0,0,0,0,0,0,0};
  127. ValidateInput();
  128. // Copy user data into transmit buffer
  129. for(i=0; i<8; i++)
  130. {
  131. aucTxBuffer[i] = arrayUserData[i];
  132. }
  133. // Send acknowledged message
  134. requestAckMsg(aucTxBuffer);
  135. }
  136. /**************************************************************************
  137. * CustomSensor::ValidateInput
  138. *
  139. * Validates user input (text box) and parses into an array
  140. *
  141. * returns: N/A
  142. *
  143. **************************************************************************/
  144. void CustomSensor::ValidateInput()
  145. {
  146. UCHAR i;
  147. // Validate empty string
  148. if(System::String::IsNullOrEmpty(this->textBox_customTxData->Text))
  149. {
  150. this->label_InputError->Text = "Error: Empty String";
  151. return;
  152. }
  153. // Check format of char string is valid
  154. if(!bSendHex)
  155. {
  156. UCHAR textLength = textBox_customTxData->Text->Length;
  157. for(i=0; i < textLength; ++i)
  158. arrayUserData[i] = (UCHAR)textBox_customTxData->Text[i];
  159. while(i<8)
  160. arrayUserData[i++] = 0x00;
  161. this->label_InputError->Text = "";
  162. this->label_DataTxd->Text = this->textBox_customTxData->Text;
  163. }
  164. // Check format of hex couplets is valid
  165. else
  166. {
  167. array<System::String^>^ hexes;
  168. hexes = this->textBox_customTxData->Text->Split(',');
  169. try
  170. {
  171. int hexLength = hexes->Length;
  172. if(hexLength >8)
  173. throw "Too many commas";
  174. for(i=0; i<hexLength; ++i)
  175. {
  176. if(hexes[i]->Length >2)
  177. throw "Too many letters between commas";
  178. arrayUserData[i] = Byte::Parse(hexes[i],System::Globalization::NumberStyles::HexNumber);
  179. }
  180. while(i<8)
  181. arrayUserData[i++] = 0x00;
  182. this->label_InputError->Text = "";
  183. this->label_DataTxd->Text = "Hex";
  184. }
  185. catch(...)
  186. {
  187. this->label_InputError->Visible = true;
  188. this->label_InputError->Text = "Error: Invalid Input";
  189. for(i=0; i<8; ++i)
  190. arrayUserData[i] = 0x00;
  191. }
  192. }
  193. }