antplus_temperature.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. #pragma once
  9. #include "types.h"
  10. #define Temperature_REV ALPHA 1 // Device Profile Revision Number
  11. public ref class Temperature
  12. {
  13. public:
  14. // Channel Parameters
  15. static const UCHAR DEVICE_TYPE = 25;
  16. static const UCHAR TX_TYPE_SENSOR = 5;
  17. static const UCHAR TX_TYPE_DISPLAY = 0;
  18. static const UCHAR RF_CHANNEL = 57;
  19. static const USHORT MESG_4HZ_PERIOD = 8192; // 4Hz
  20. static const USHORT MESG_P5HZ_PERIOD = 65535; // 0.5Hz
  21. static const UCHAR MESG_4HZ_TIMEOUT = (4 * 30); // 4Hz * 30secs
  22. static const UCHAR MESG_REQ_RESP_ACK = 0x81;
  23. static const UCHAR MESG_REQ_RESP_BROADCAST = 0x01;
  24. static const UCHAR MESG_WAIT_REQ = 5;
  25. // Data Pages
  26. static const UCHAR PAGE_0 = 0;
  27. static const UCHAR PAGE_1 = 1;
  28. // Reserved/invalid/special values
  29. static const UCHAR RESERVED = 0xFF;
  30. static const UCHAR BACKGROUND_INTERVAL = 129;
  31. static const UCHAR TXINFO_4HZ = 0x01; // 4.0 Hz transmission rate
  32. static const UCHAR TXINFO_P5HZ = 0x00; // 0.5 Hz transmission rate
  33. static const short HIGHLOW_INVALID = 0xF800; // Sign extended 0x800 invalid value
  34. static const UCHAR SUPPORTED_PAGES_0 = 0x01; // page 0 supported
  35. static const UCHAR SUPPORTED_PAGES_1 = 0x02; // page 1 supported
  36. static const UCHAR SUPPORTED_PAGES_0_1 = 0x03; // pages 0 and 1 supported
  37. static const UCHAR UTC_TIME_NOT_SUPPORTED = 0x00; // UTC Time values for TxInfo
  38. static const UCHAR UTC_TIME_NOT_SET = 0x04;
  39. static const UCHAR UTC_TIME_SET = 0x08;
  40. static const UCHAR LOCAL_TIME_NOT_SUPPORTED = 0x00; // LOCAL Time values for TxInfo
  41. static const UCHAR LOCAL_TIME_NOT_SET = 0x10;
  42. static const UCHAR LOCAL_TIME_SET = 0x20;
  43. static const UCHAR DATA_REQUEST = 0x01;
  44. // Bit Masks
  45. static const USHORT BYTE_MASK = 0x00FF;
  46. static const USHORT UPPER_BYTE_MASK = 0xFF00;
  47. static const USHORT UNIBBLE_UBYTE_SIGNXTND = 0xF000;
  48. static const UCHAR UPPER_NIBBLE_MASK = 0xF0;
  49. static const UCHAR LOWER_NIBBLE_MASK = 0x0F;
  50. static const UCHAR PERIOD_MASK = 0x03;
  51. static const UCHAR UTC_TIME_MASK = 0x0C;
  52. static const UCHAR LOCAL_TIME_MASK = 0x30;
  53. // Error handling
  54. // Flags indicate errors causing the exception
  55. ref class Error : public System::Exception{
  56. public:
  57. BOOL bBadReserved; // Invalid values on reserved fields
  58. BOOL bUndefPage; // Undefined page
  59. enum class Code : UCHAR // Error code definitions
  60. {
  61. INVALID_RESERVED, // Invalid value in reserved field
  62. UNDEF_PAGE, // Undefined data page
  63. };
  64. Error()
  65. {
  66. ClearFlags();
  67. }
  68. Error(Code eCode1_)
  69. {
  70. ClearFlags();
  71. SetFlags(eCode1_);
  72. }
  73. Error(Code eCode1_, Code eCode2_)
  74. {
  75. ClearFlags();
  76. SetFlags(eCode1_);
  77. SetFlags(eCode2_);
  78. }
  79. private:
  80. void ClearFlags()
  81. {
  82. bBadReserved = FALSE;
  83. bUndefPage = FALSE;
  84. }
  85. void SetFlags(Code eCode_)
  86. {
  87. switch(eCode_)
  88. {
  89. case Code::INVALID_RESERVED:
  90. bBadReserved = TRUE;
  91. break;
  92. case Code::UNDEF_PAGE:
  93. bUndefPage = TRUE;
  94. break;
  95. default:
  96. break;
  97. }
  98. }
  99. };
  100. public:
  101. USHORT usMessagePeriod; // Dynamic Message Period (0.5Hz Beacon, 4Hz when Communicating w/ Display)
  102. // Temperature Data Page 1
  103. UCHAR ucTxInfo; // Transmission Info
  104. UCHAR ucEventCount;
  105. UCHAR ucSupportedPages;
  106. short s24HrLow; // 24 Hour Low (hundredths of deg C)
  107. short s24HrHigh; // 24 Hour High (hundredths of deg C)
  108. short sCurrentTemp; // Current Temperature (hundredths of deg C)
  109. // Period Switching
  110. BOOL b4HzRotation; // Begin 4Hz Page Rotation
  111. // Temperature Data Request
  112. BOOL bPageRequest; // True if slave has requested a page
  113. UCHAR ucRequestedPageNum; // Requested Page Number
  114. public:
  115. Temperature()
  116. {
  117. }
  118. ~Temperature()
  119. {
  120. }
  121. public:
  122. /**************************************************************************
  123. * Temperature::Decode
  124. *
  125. * Decodes all main data
  126. *
  127. * pucRxBuffer_: pointer to the buffer containing the received data
  128. *
  129. * returns: N/A
  130. *
  131. *************************************************************************/
  132. void Decode(UCHAR* pucRxBuffer_)
  133. {
  134. UCHAR ucPageNum = pucRxBuffer_[0];
  135. switch(ucPageNum)
  136. {
  137. case PAGE_0 :
  138. ucTxInfo = pucRxBuffer_[3];
  139. ucSupportedPages = pucRxBuffer_[4];
  140. break;
  141. case PAGE_1:
  142. ucEventCount = pucRxBuffer_[2];
  143. s24HrLow = pucRxBuffer_[4] & UPPER_NIBBLE_MASK;
  144. s24HrLow = (s24HrLow << 4) | pucRxBuffer_[3];
  145. // sign extend
  146. if(s24HrLow >> 11)
  147. s24HrLow = s24HrLow | UNIBBLE_UBYTE_SIGNXTND;
  148. s24HrHigh = pucRxBuffer_[5];
  149. s24HrHigh = (s24HrHigh << 4) | (pucRxBuffer_[4] & LOWER_NIBBLE_MASK);
  150. // sign extend
  151. if(s24HrHigh >> 11)
  152. s24HrHigh = s24HrHigh | UNIBBLE_UBYTE_SIGNXTND;
  153. sCurrentTemp = pucRxBuffer_[7];
  154. sCurrentTemp = (sCurrentTemp << 8) | pucRxBuffer_[6];
  155. break;
  156. default:
  157. break;
  158. }
  159. }
  160. /**************************************************************************
  161. * Temperature::Encode
  162. *
  163. * Encodes the temperature into the transmission buffer:
  164. *
  165. * ucPageNum_: number of page to encode (nothing happens if != 1)
  166. * pucTxBuffer_: pointer to the buffer that will store the encoded data
  167. *
  168. * returns: N/A
  169. *
  170. **************************************************************************/
  171. void Encode(UCHAR ucPageNum_, UCHAR* pucTxBuffer_)
  172. {
  173. switch(ucPageNum_)
  174. {
  175. case 0:
  176. pucTxBuffer_[0] = ucPageNum_;
  177. pucTxBuffer_[1] = RESERVED;
  178. pucTxBuffer_[2] = RESERVED;
  179. pucTxBuffer_[3] = ucTxInfo;
  180. pucTxBuffer_[4] = SUPPORTED_PAGES_0_1; //Supported pages LSB
  181. pucTxBuffer_[5] = 0x00; //Supported pages second LSB
  182. pucTxBuffer_[6] = 0x00; //Supported pages second MSB
  183. pucTxBuffer_[7] = 0x00; //Supported pages MSB
  184. break;
  185. case 1:
  186. pucTxBuffer_[0] = ucPageNum_;
  187. pucTxBuffer_[1] = RESERVED;
  188. pucTxBuffer_[2] = ucEventCount;
  189. pucTxBuffer_[3] = (s24HrLow & BYTE_MASK); // 24 Hour Low LSB
  190. // for pucTxBuffer_[4] bits 4:7 = 24 Hour Low MSN, bits 0:3 = 24 Hour High LSN
  191. pucTxBuffer_[4] =((s24HrLow>>4) & UPPER_NIBBLE_MASK) | (s24HrHigh & LOWER_NIBBLE_MASK);
  192. pucTxBuffer_[5] = (s24HrHigh>>4) & BYTE_MASK; // 24 Hour High MSB
  193. pucTxBuffer_[6] = System::Convert::ToByte(sCurrentTemp & BYTE_MASK); // Current Temp LSB
  194. pucTxBuffer_[7] = System::Convert::ToByte((sCurrentTemp>>8) & BYTE_MASK); // Current Temp MSB
  195. break;
  196. default:
  197. break;
  198. }
  199. }
  200. };