BrickUp API Service for Docker version.

TH02_dev.cpp 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * TH02_dev.cpp
  3. * Driver for DIGITAL I2C HUMIDITY AND TEMPERATURE SENSOR
  4. *
  5. * Copyright (c) 2014 seeed technology inc.
  6. * Website : www.seeed.cc
  7. * Author : Oliver Wang
  8. * Create Time: April 2014
  9. * Change Log :
  10. *
  11. * The MIT License (MIT)
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in
  21. * all copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29. * THE SOFTWARE.
  30. */
  31. /****************************************************************************/
  32. /*** Include files ***/
  33. /****************************************************************************/
  34. #include "TH02_dev.h"
  35. #include <Wire.h>
  36. #include <Arduino.h>
  37. /* Use Serial IIC */
  38. #ifdef SERIAL_IIC
  39. #endif
  40. TH02_dev TH02;
  41. /****************************************************************************/
  42. /*** Local Variable ***/
  43. /****************************************************************************/
  44. /****************************************************************************/
  45. /*** Class member Functions ***/
  46. /****************************************************************************/
  47. void TH02_dev::begin(void)
  48. {
  49. /* Start IIC */
  50. Wire.begin();
  51. /* TH02 don't need to software reset */
  52. }
  53. float TH02_dev::ReadTemperature(void)
  54. {
  55. /* Start a new temperature conversion */
  56. TH02_IIC_WriteReg(REG_CONFIG, CMD_MEASURE_TEMP);
  57. //delay(100);
  58. /* Wait until conversion is done */
  59. while(!isAvailable());
  60. uint16_t value = TH02_IIC_ReadData();
  61. value = value >> 2;
  62. /*
  63. Formula:
  64. Temperature(C) = (Value/32) - 50
  65. */
  66. float temper = (value/32.0)-50.0;
  67. return temper;
  68. }
  69. float TH02_dev::ReadHumidity(void)
  70. {
  71. /* Start a new humility conversion */
  72. TH02_IIC_WriteReg(REG_CONFIG, CMD_MEASURE_HUMI);
  73. /* Wait until conversion is done */
  74. //delay(100);
  75. while(!isAvailable());
  76. uint16_t value = TH02_IIC_ReadData();
  77. value = value >> 4;
  78. /*
  79. Formula:
  80. Humidity(%) = (Value/16) - 24
  81. */
  82. float humility = (value/16.0)-24.0;
  83. return humility;
  84. }
  85. /****************************************************************************/
  86. /*** Local Functions ***/
  87. /****************************************************************************/
  88. uint8_t TH02_dev::isAvailable()
  89. {
  90. uint8_t status = TH02_IIC_ReadReg(REG_STATUS);
  91. if(status & STATUS_RDY_MASK)
  92. {
  93. return 0; //ready
  94. }
  95. else
  96. {
  97. return 1; //not ready yet
  98. }
  99. }
  100. void TH02_dev::TH02_IIC_WriteCmd(uint8_t u8Cmd)
  101. {
  102. /* Port to arduino */
  103. Wire.beginTransmission(TH02_I2C_DEV_ID);
  104. Wire.write(u8Cmd);
  105. Wire.endTransmission();
  106. }
  107. uint8_t TH02_dev::TH02_IIC_ReadReg(uint8_t u8Reg)
  108. {
  109. /* Port to arduino */
  110. uint8_t Temp = 0;
  111. /* Send a register reading command */
  112. TH02_IIC_WriteCmd(u8Reg);
  113. Wire.requestFrom(TH02_I2C_DEV_ID, 1);
  114. while(Wire.available())
  115. {
  116. Temp = Wire.read();
  117. }
  118. return Temp;
  119. }
  120. void TH02_dev::TH02_IIC_WriteReg(uint8_t u8Reg,uint8_t u8Data)
  121. {
  122. Wire.beginTransmission(TH02_I2C_DEV_ID);
  123. Wire.write(u8Reg);
  124. Wire.write(u8Data);
  125. Wire.endTransmission();
  126. }
  127. uint16_t TH02_dev::TH02_IIC_ReadData(void)
  128. {
  129. /* Port to arduino */
  130. uint16_t Temp = TH02_IIC_ReadData2byte();
  131. return Temp;
  132. }
  133. uint16_t TH02_dev::TH02_IIC_ReadData2byte()
  134. {
  135. uint16_t TempData = 0;
  136. uint16_t tmpArray[3]={0};
  137. int cnt = 0;
  138. TH02_IIC_WriteCmd(REG_DATA_H);
  139. Wire.requestFrom(TH02_I2C_DEV_ID, 3);
  140. while(Wire.available())
  141. {
  142. tmpArray[cnt] = (uint16_t)Wire.read();
  143. cnt++;
  144. }
  145. /* MSB */
  146. TempData = (tmpArray[1]<<8)|(tmpArray[2]);
  147. return TempData;
  148. }