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.

120 lines
3.3 KiB

12 months ago
12 months ago
12 months ago
  1. #include "hmp110.hpp"
  2. #include "stm32components/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. static_assert(sizeof(hmp110_sensordata_t) == 14 * 2, "sizeof(hmp110_sensordata_t) != 14*2");
  62. uint16_t buf[14];
  63. bool suc = readReg03Muti(m_id, 0x0100, buf, 14, 300);
  64. if (!suc) {
  65. return false;
  66. }
  67. memset(sensordata, 0, sizeof(hmp110_sensordata_t));
  68. memcpy(sensordata, buf, sizeof(hmp110_sensordata_t));
  69. // sensordata->rh = buf[0];
  70. // sensordata->temp = buf[1];
  71. // sensordata->df_ptemp = buf[2];
  72. // sensordata->ah = buf[3];
  73. // sensordata->mr = buf[4];
  74. // sensordata->wet_bulb_temp = buf[5];
  75. // sensordata->enthalpy = buf[6];
  76. return true;
  77. }
  78. void HMP110::updateSensorDataAndErrorcode() {
  79. bool suc = read_sensor_data(&m_rdbuf);
  80. osDelay(100);
  81. int32_t errorcode = read_errorcode();
  82. if (errorcode != 0) {
  83. ZLOGE(TAG, "errorcode: %d", errorcode);
  84. }
  85. if (!suc) {
  86. ZLOGE(TAG, "read_sensor_data failed");
  87. errorcode = -1;
  88. }
  89. {
  90. zlock_guard lck(m_cache_lock);
  91. m_cachedata = m_rdbuf;
  92. m_cache_errorcode = errorcode;
  93. }
  94. }
  95. int32_t HMP110::read_cache_errorcode() {
  96. zlock_guard lck(m_cache_lock);
  97. return m_cache_errorcode;
  98. }
  99. bool HMP110::read_cache_sensor_data(hmp110_sensordata_t* sensordata) {
  100. zlock_guard lck(m_cache_lock);
  101. *sensordata = m_cachedata;
  102. return true;
  103. }