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

#include "hmp110.hpp"
#include "stm32components/modbus/modbus_block_host.hpp"
using namespace iflytop;
#define TAG "HMP110"
void HMP110::init(ModbusBlockHost* modbusBlockHost) {
m_modbusBlockHost = modbusBlockHost;
m_id = 240;
m_cache_lock.init();
m_chlock.init();
}
bool HMP110::readReg03(uint8_t slaveAddr, uint16_t regAddr, uint16_t* regVal, int overtimems) { //
zlock_guard lck(m_chlock);
// ZLOGI(TAG, "=============== readReg03 %d %d", slaveAddr, regAddr);
bool ret = m_modbusBlockHost->readReg03(slaveAddr, regAddr, regVal, overtimems);
// ZLOGI(TAG, "=============== END%d", ret);
return ret;
}
bool HMP110::readReg03Muti(uint8_t slaveAddr, uint16_t regAddr, uint16_t* regVal, int regNum, int overtimems) { //
zlock_guard lck(m_chlock);
// ZLOGI(TAG, "=============== readReg03Muti %d %d %d", slaveAddr, regAddr, regNum);
bool ret = m_modbusBlockHost->readReg03Muti(slaveAddr, regAddr, regVal, regNum, overtimems);
// ZLOGI(TAG, "=============== END%d", ret);
return ret;
}
bool HMP110::ping(int id) {
int16_t val = 0;
return readReg03(id, 0x1F00, (uint16_t*)&val, 300);
}
void HMP110::setid(int32_t id) { m_id = id; }
void HMP110::read_calibration_date(int32_t* year, int32_t* month, int32_t* day) { // TODO
*year = 1;
*month = 2;
*day = 3;
}
int32_t HMP110::read_errorcode() {
int16_t val = 0;
bool suc = readReg03(m_id, 0x0200, (uint16_t*)&val, 300);
if (!suc) {
return -1;
}
if (val == 1) {
return 0;
}
uint16_t ecodebuf[2];
suc = readReg03Muti(m_id, 0x0203, ecodebuf, 2, 300);
uint32_t ecode = ecodebuf[0] | (((uint32_t)ecodebuf[1]) << 16);
return ecode;
}
bool HMP110::read_reg(int32_t add, uint16_t* val, size_t len) {
// ZLOGI(TAG, "read_reg %x %d", add, len);
bool suc = readReg03Muti(m_id, add, val, len, 300);
// if (suc)
// for (size_t i = 0; i < len; i++) {
// ZLOGI(TAG, "val[%d] = %d", i, val[i]);
// }
// else
// ZLOGE(TAG, "read_reg failed");
return suc;
}
bool HMP110::read_sensor_data(hmp110_sensordata_t* sensordata) {
static_assert(sizeof(hmp110_sensordata_t) == 14 * 2, "sizeof(hmp110_sensordata_t) != 14*2");
uint16_t buf[14];
bool suc = readReg03Muti(m_id, 0x0100, buf, 14, 300);
if (!suc) {
return false;
}
memset(sensordata, 0, sizeof(hmp110_sensordata_t));
memcpy(sensordata, buf, sizeof(hmp110_sensordata_t));
// sensordata->rh = buf[0];
// sensordata->temp = buf[1];
// sensordata->df_ptemp = buf[2];
// sensordata->ah = buf[3];
// sensordata->mr = buf[4];
// sensordata->wet_bulb_temp = buf[5];
// sensordata->enthalpy = buf[6];
return true;
}
void HMP110::updateSensorDataAndErrorcode() {
bool suc = read_sensor_data(&m_rdbuf);
osDelay(100);
int32_t errorcode = read_errorcode();
if (errorcode != 0) {
ZLOGE(TAG, "errorcode: %d", errorcode);
}
if (!suc) {
ZLOGE(TAG, "read_sensor_data failed");
errorcode = -1;
}
{
zlock_guard lck(m_cache_lock);
m_cachedata = m_rdbuf;
m_cache_errorcode = errorcode;
}
}
int32_t HMP110::read_cache_errorcode() {
zlock_guard lck(m_cache_lock);
return m_cache_errorcode;
}
bool HMP110::read_cache_sensor_data(hmp110_sensordata_t* sensordata) {
zlock_guard lck(m_cache_lock);
*sensordata = m_cachedata;
return true;
}