You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.1 KiB

12 months ago
12 months ago
12 months ago
  1. #include "hmp110.hpp"
  2. #include "stm32basic/modbus/modbus_block_host.hpp"
  3. using namespace iflytop;
  4. #define TAG "HMP110"
  5. void HMP110::init(ModbusBlockHost* modbusBlockHost) {
  6. m_modbusBlockHost = modbusBlockHost;
  7. m_id = 240;
  8. m_cache_lock.init();
  9. m_chlock.init();
  10. }
  11. bool HMP110::readReg03(uint8_t slaveAddr, uint16_t regAddr, uint16_t* regVal, int overtimems) { //
  12. zlock_guard lck(m_chlock);
  13. // ZLOGI(TAG, "=============== readReg03 %d %d", slaveAddr, regAddr);
  14. bool ret = m_modbusBlockHost->readReg03(slaveAddr, regAddr, regVal, overtimems);
  15. // ZLOGI(TAG, "=============== END%d", ret);
  16. return ret;
  17. }
  18. bool HMP110::readReg03Muti(uint8_t slaveAddr, uint16_t regAddr, uint16_t* regVal, int regNum, int overtimems) { //
  19. zlock_guard lck(m_chlock);
  20. // ZLOGI(TAG, "=============== readReg03Muti %d %d %d", slaveAddr, regAddr, regNum);
  21. bool ret = m_modbusBlockHost->readReg03Muti(slaveAddr, regAddr, regVal, regNum, overtimems);
  22. // ZLOGI(TAG, "=============== END%d", ret);
  23. return ret;
  24. }
  25. bool HMP110::ping(int id) {
  26. int16_t val = 0;
  27. return readReg03(id, 0x1F00, (uint16_t*)&val, 300);
  28. }
  29. void HMP110::setid(int32_t id) { m_id = id; }
  30. void HMP110::read_calibration_date(int32_t* year, int32_t* month, int32_t* day) { // TODO
  31. *year = 1;
  32. *month = 2;
  33. *day = 3;
  34. }
  35. int32_t HMP110::read_errorcode() {
  36. int16_t val = 0;
  37. bool suc = readReg03(m_id, 0x0200, (uint16_t*)&val, 300);
  38. if (!suc) {
  39. return -1;
  40. }
  41. if (val == 1) {
  42. return 0;
  43. }
  44. uint16_t ecodebuf[2];
  45. suc = readReg03Muti(m_id, 0x0203, ecodebuf, 2, 300);
  46. uint32_t ecode = ecodebuf[0] | (((uint32_t)ecodebuf[1]) << 16);
  47. return ecode;
  48. }
  49. bool HMP110::read_reg(int32_t add, uint16_t* val, size_t len) {
  50. // ZLOGI(TAG, "read_reg %x %d", add, len);
  51. bool suc = readReg03Muti(m_id, add, val, len, 300);
  52. // if (suc)
  53. // for (size_t i = 0; i < len; i++) {
  54. // ZLOGI(TAG, "val[%d] = %d", i, val[i]);
  55. // }
  56. // else
  57. // ZLOGE(TAG, "read_reg failed");
  58. return suc;
  59. }
  60. bool HMP110::read_sensor_data(hmp110_sensordata_t* sensordata) {
  61. uint16_t buf[7];
  62. bool suc = readReg03Muti(m_id, 0x0100, buf, 7, 300);
  63. if (!suc) {
  64. return false;
  65. }
  66. sensordata->rh = buf[0];
  67. sensordata->temp = buf[1];
  68. sensordata->df_ptemp = buf[2];
  69. sensordata->ah = buf[3];
  70. sensordata->mr = buf[4];
  71. sensordata->wet_bulb_temp = buf[5];
  72. sensordata->enthalpy = buf[6];
  73. return true;
  74. }
  75. void HMP110::updateSensorDataAndErrorcode() {
  76. bool suc = read_sensor_data(&m_rdbuf);
  77. osDelay(100);
  78. int32_t errorcode = read_errorcode();
  79. if (errorcode != 0) {
  80. ZLOGE(TAG, "errorcode: %d", errorcode);
  81. }
  82. if (!suc) {
  83. ZLOGE(TAG, "read_sensor_data failed");
  84. errorcode = -1;
  85. }
  86. {
  87. zlock_guard lck(m_cache_lock);
  88. m_cachedata = m_rdbuf;
  89. m_cache_errorcode = errorcode;
  90. }
  91. }
  92. int32_t HMP110::read_cache_errorcode() {
  93. zlock_guard lck(m_cache_lock);
  94. return m_cache_errorcode;
  95. }
  96. bool HMP110::read_cache_sensor_data(hmp110_sensordata_t* sensordata) {
  97. zlock_guard lck(m_cache_lock);
  98. *sensordata = m_cachedata;
  99. return true;
  100. }