Changed driver to measure float

This commit is contained in:
Robert Jaakke 2020-06-07 16:33:28 +02:00
parent 2da09526ba
commit d3a59fd65c
3 changed files with 60 additions and 61 deletions

View File

@ -181,7 +181,7 @@ int16_t LOLIN_HP303B::standby(void)
* -2 if the object initialization failed * -2 if the object initialization failed
* -1 on other fail * -1 on other fail
*/ */
int16_t LOLIN_HP303B::measureTempOnce(int32_t &result) int16_t LOLIN_HP303B::measureTempOnce(float &result)
{ {
return measureTempOnce(result, m_tempOsr); return measureTempOnce(result, m_tempOsr);
} }
@ -202,7 +202,7 @@ int16_t LOLIN_HP303B::measureTempOnce(int32_t &result)
* -2 if the object initialization failed * -2 if the object initialization failed
* -1 on other fail * -1 on other fail
*/ */
int16_t LOLIN_HP303B::measureTempOnce(int32_t &result, uint8_t oversamplingRate) int16_t LOLIN_HP303B::measureTempOnce(float &result, uint8_t oversamplingRate)
{ {
//Start measurement //Start measurement
int16_t ret = startMeasureTempOnce(oversamplingRate); int16_t ret = startMeasureTempOnce(oversamplingRate);
@ -286,7 +286,7 @@ int16_t LOLIN_HP303B::startMeasureTempOnce(uint8_t oversamplingRate)
* -2 if the object initialization failed * -2 if the object initialization failed
* -1 on other fail * -1 on other fail
*/ */
int16_t LOLIN_HP303B::measurePressureOnce(int32_t &result) int16_t LOLIN_HP303B::measurePressureOnce(float &result)
{ {
return measurePressureOnce(result, m_prsOsr); return measurePressureOnce(result, m_prsOsr);
} }
@ -307,7 +307,7 @@ int16_t LOLIN_HP303B::measurePressureOnce(int32_t &result)
* -2 if the object initialization failed * -2 if the object initialization failed
* -1 on other fail * -1 on other fail
*/ */
int16_t LOLIN_HP303B::measurePressureOnce(int32_t &result, uint8_t oversamplingRate) int16_t LOLIN_HP303B::measurePressureOnce(float &result, uint8_t oversamplingRate)
{ {
//start the measurement //start the measurement
int16_t ret = startMeasurePressureOnce(oversamplingRate); int16_t ret = startMeasurePressureOnce(oversamplingRate);
@ -388,7 +388,7 @@ int16_t LOLIN_HP303B::startMeasurePressureOnce(uint8_t oversamplingRate)
* -2 if the object initialization failed * -2 if the object initialization failed
* -1 on other fail * -1 on other fail
*/ */
int16_t LOLIN_HP303B::getSingleResult(int32_t &result) int16_t LOLIN_HP303B::getSingleResult(float &result)
{ {
//abort if initialization failed //abort if initialization failed
if(m_initFail) if(m_initFail)
@ -636,9 +636,9 @@ int16_t LOLIN_HP303B::startMeasureBothCont(uint8_t tempMr,
* -2 if the object initialization failed * -2 if the object initialization failed
* -1 on other fail * -1 on other fail
*/ */
int16_t LOLIN_HP303B::getContResults(int32_t *tempBuffer, int16_t LOLIN_HP303B::getContResults(float *tempBuffer,
uint8_t &tempCount, uint8_t &tempCount,
int32_t *prsBuffer, float *prsBuffer,
uint8_t &prsCount) uint8_t &prsCount)
{ {
if(m_initFail) if(m_initFail)
@ -660,7 +660,7 @@ int16_t LOLIN_HP303B::getContResults(int32_t *tempBuffer,
//while FIFO is not empty //while FIFO is not empty
while(readByteBitfield(HP303B__REG_INFO_FIFO_EMPTY) == 0) while(readByteBitfield(HP303B__REG_INFO_FIFO_EMPTY) == 0)
{ {
int32_t result; float result;
//read next result from FIFO //read next result from FIFO
int16_t type = getFIFOvalue(&result); int16_t type = getFIFOvalue(&result);
switch(type) switch(type)
@ -824,7 +824,7 @@ int16_t LOLIN_HP303B::correctTemp(void)
//perform a first temperature measurement (again) //perform a first temperature measurement (again)
//the most recent temperature will be saved internally //the most recent temperature will be saved internally
//and used for compensation when calculating pressure //and used for compensation when calculating pressure
int32_t trash; float trash;
measureTempOnce(trash); measureTempOnce(trash);
return HP303B__SUCCEEDED; return HP303B__SUCCEEDED;
@ -892,7 +892,7 @@ void LOLIN_HP303B::init(void)
//perform a first temperature measurement //perform a first temperature measurement
//the most recent temperature will be saved internally //the most recent temperature will be saved internally
//and used for compensation when calculating pressure //and used for compensation when calculating pressure
int32_t trash; float trash;
measureTempOnce(trash); measureTempOnce(trash);
//make sure the HP303B is in standby after initialization //make sure the HP303B is in standby after initialization
@ -1192,9 +1192,9 @@ uint16_t LOLIN_HP303B::calcBusyTime(uint16_t mr, uint16_t osr)
* returns: 0 on success * returns: 0 on success
* -1 on fail; * -1 on fail;
*/ */
int16_t LOLIN_HP303B::getTemp(int32_t *result) int16_t LOLIN_HP303B::getTemp(float *result)
{ {
uint8_t buffer[3] = {0}; unsigned char buffer[3] = {0};
//read raw pressure data to buffer //read raw pressure data to buffer
int16_t i = readBlock(HP303B__REG_ADR_TEMP, int16_t i = readBlock(HP303B__REG_ADR_TEMP,
@ -1207,14 +1207,14 @@ int16_t LOLIN_HP303B::getTemp(int32_t *result)
} }
//compose raw temperature value from buffer //compose raw temperature value from buffer
int32_t temp = (uint32_t)buffer[0] << 16 float temp = buffer[0] << 16
| (uint32_t)buffer[1] << 8 | buffer[1] << 8
| (uint32_t)buffer[2]; | buffer[2];
//recognize non-32-bit negative numbers //recognize non-32-bit negative numbers
//and convert them to 32-bit negative numbers using 2's complement //and convert them to 32-bit negative numbers using 2's complement
if(temp & ((uint32_t)1 << 23)) if(temp > 0x7FFFFF)
{ {
temp -= (uint32_t)1 << 24; temp = temp - 0x1000000;
} }
//return temperature //return temperature
@ -1229,9 +1229,9 @@ int16_t LOLIN_HP303B::getTemp(int32_t *result)
* returns: 0 on success * returns: 0 on success
* -1 on fail; * -1 on fail;
*/ */
int16_t LOLIN_HP303B::getPressure(int32_t *result) int16_t LOLIN_HP303B::getPressure(float *result)
{ {
uint8_t buffer[3] = {0}; unsigned char buffer[3] = {0};
//read raw pressure data to buffer //read raw pressure data to buffer
int16_t i = readBlock(HP303B__REG_ADR_PRS, int16_t i = readBlock(HP303B__REG_ADR_PRS,
HP303B__REG_LEN_PRS, HP303B__REG_LEN_PRS,
@ -1244,14 +1244,12 @@ int16_t LOLIN_HP303B::getPressure(int32_t *result)
} }
//compose raw pressure value from buffer //compose raw pressure value from buffer
int32_t prs = (uint32_t)buffer[0] << 16 float prs = buffer[0] << 16 | buffer[1] << 8 | buffer[2];
| (uint32_t)buffer[1] << 8
| (uint32_t)buffer[2];
//recognize non-32-bit negative numbers //recognize non-32-bit negative numbers
//and convert them to 32-bit negative numbers using 2's complement //and convert them to 32-bit negative numbers using 2's complement
if(prs & ((uint32_t)1 << 23)) if(prs > 0x7FFFFF)
{ {
prs -= (uint32_t)1 << 24; prs = prs - 0x1000000;
} }
*result = calcPressure(prs); *result = calcPressure(prs);
@ -1266,7 +1264,7 @@ int16_t LOLIN_HP303B::getPressure(int32_t *result)
* 0 if result is a temperature raw value * 0 if result is a temperature raw value
* 1 if result is a pressure raw value * 1 if result is a pressure raw value
*/ */
int16_t LOLIN_HP303B::getFIFOvalue(int32_t* value) int16_t LOLIN_HP303B::getFIFOvalue(float *value)
{ {
//abort on invalid argument //abort on invalid argument
if(value == NULL) if(value == NULL)
@ -1274,7 +1272,7 @@ int16_t LOLIN_HP303B::getFIFOvalue(int32_t* value)
return HP303B__FAIL_UNKNOWN; return HP303B__FAIL_UNKNOWN;
} }
uint8_t buffer[HP303B__REG_LEN_PRS] = {0}; unsigned char buffer[HP303B__REG_LEN_PRS] = {0};
//always read from pressure raw value register //always read from pressure raw value register
int16_t i = readBlock(HP303B__REG_ADR_PRS, int16_t i = readBlock(HP303B__REG_ADR_PRS,
HP303B__REG_LEN_PRS, HP303B__REG_LEN_PRS,
@ -1286,14 +1284,14 @@ int16_t LOLIN_HP303B::getFIFOvalue(int32_t* value)
return HP303B__FAIL_UNKNOWN; return HP303B__FAIL_UNKNOWN;
} }
//compose raw pressure value from buffer //compose raw pressure value from buffer
*value = (uint32_t)buffer[0] << 16 *value = buffer[0] << 16
| (uint32_t)buffer[1] << 8 | buffer[1] << 8
| (uint32_t)buffer[2]; | buffer[2];
//recognize non-32-bit negative numbers //recognize non-32-bit negative numbers
//and convert them to 32-bit negative numbers using 2's complement //and convert them to 32-bit negative numbers using 2's complement
if(*value & ((uint32_t)1 << 23)) if(*value > 0x7FFFFF)
{ {
*value -= (uint32_t)1 << 24; *value = *value - 0x1000000;
} }
//least significant bit shows measurement type //least significant bit shows measurement type
@ -1305,7 +1303,7 @@ int16_t LOLIN_HP303B::getFIFOvalue(int32_t* value)
* raw: raw temperature value read from HP303B * raw: raw temperature value read from HP303B
* returns: temperature value in °C * returns: temperature value in °C
*/ */
int32_t LOLIN_HP303B::calcTemp(int32_t raw) float LOLIN_HP303B::calcTemp(float raw)
{ {
double temp = raw; double temp = raw;
@ -1320,7 +1318,7 @@ int32_t LOLIN_HP303B::calcTemp(int32_t raw)
temp = m_c0Half + m_c1 * temp; temp = m_c0Half + m_c1 * temp;
//return temperature //return temperature
return (int32_t)temp; return (float)temp;
} }
/** /**
@ -1328,7 +1326,7 @@ int32_t LOLIN_HP303B::calcTemp(int32_t raw)
* raw: raw pressure value read from HP303B * raw: raw pressure value read from HP303B
* returns: pressure value in Pa * returns: pressure value in Pa
*/ */
int32_t LOLIN_HP303B::calcPressure(int32_t raw) float LOLIN_HP303B::calcPressure(float raw)
{ {
double prs = raw; double prs = raw;
@ -1341,7 +1339,7 @@ int32_t LOLIN_HP303B::calcPressure(int32_t raw)
+ m_lastTempScal * (m_c01 + prs * (m_c11 + prs * m_c21)); + m_lastTempScal * (m_c01 + prs * (m_c11 + prs * m_c21));
//return pressure //return pressure
return (int32_t)prs; return (float)prs;
} }
/** /**

View File

@ -34,21 +34,21 @@ public:
int16_t standby(void); int16_t standby(void);
//Command Mode //Command Mode
int16_t measureTempOnce(int32_t &result); int16_t measureTempOnce(float &result);
int16_t measureTempOnce(int32_t &result, uint8_t oversamplingRate); int16_t measureTempOnce(float &result, uint8_t oversamplingRate);
int16_t startMeasureTempOnce(void); int16_t startMeasureTempOnce(void);
int16_t startMeasureTempOnce(uint8_t oversamplingRate); int16_t startMeasureTempOnce(uint8_t oversamplingRate);
int16_t measurePressureOnce(int32_t &result); int16_t measurePressureOnce(float &result);
int16_t measurePressureOnce(int32_t &result, uint8_t oversamplingRate); int16_t measurePressureOnce(float &result, uint8_t oversamplingRate);
int16_t startMeasurePressureOnce(void); int16_t startMeasurePressureOnce(void);
int16_t startMeasurePressureOnce(uint8_t oversamplingRate); int16_t startMeasurePressureOnce(uint8_t oversamplingRate);
int16_t getSingleResult(int32_t &result); int16_t getSingleResult(float &result);
//Background Mode //Background Mode
int16_t startMeasureTempCont(uint8_t measureRate, uint8_t oversamplingRate); int16_t startMeasureTempCont(uint8_t measureRate, uint8_t oversamplingRate);
int16_t startMeasurePressureCont(uint8_t measureRate, uint8_t oversamplingRate); int16_t startMeasurePressureCont(uint8_t measureRate, uint8_t oversamplingRate);
int16_t startMeasureBothCont(uint8_t tempMr, uint8_t tempOsr, uint8_t prsMr, uint8_t prsOsr); int16_t startMeasureBothCont(uint8_t tempMr, uint8_t tempOsr, uint8_t prsMr, uint8_t prsOsr);
int16_t getContResults(int32_t *tempBuffer, uint8_t &tempCount, int32_t *prsBuffer, uint8_t &prsCount); int16_t getContResults(float *tempBuffer, uint8_t &tempCount, float *prsBuffer, uint8_t &prsCount);
//Interrupt Control //Interrupt Control
int16_t setInterruptPolarity(uint8_t polarity); int16_t setInterruptPolarity(uint8_t polarity);
@ -122,11 +122,11 @@ private:
int16_t configTemp(uint8_t temp_mr, uint8_t temp_osr); int16_t configTemp(uint8_t temp_mr, uint8_t temp_osr);
int16_t configPressure(uint8_t prs_mr, uint8_t prs_osr); int16_t configPressure(uint8_t prs_mr, uint8_t prs_osr);
uint16_t calcBusyTime(uint16_t temp_rate, uint16_t temp_osr); uint16_t calcBusyTime(uint16_t temp_rate, uint16_t temp_osr);
int16_t getTemp(int32_t *result); int16_t getTemp(float *result);
int16_t getPressure(int32_t *result); int16_t getPressure(float *result);
int16_t getFIFOvalue(int32_t *value); int16_t getFIFOvalue(float *value);
int32_t calcTemp(int32_t raw); float calcTemp(float raw);
int32_t calcPressure(int32_t raw); float calcPressure(float raw);
//bus specific //bus specific
int16_t readByte(uint8_t regAddress); int16_t readByte(uint8_t regAddress);

View File

@ -51,8 +51,8 @@ bool HP303B_Read(float &temperature, float &pressure, uint8_t hp303b_address)
{ {
HP303BSensor.begin(hp303b_address); HP303BSensor.begin(hp303b_address);
int32_t t; float t;
int32_t p; float p;
int16_t ret; int16_t ret;
ret = HP303BSensor.measureTempOnce(t, oversampling); ret = HP303BSensor.measureTempOnce(t, oversampling);
@ -96,9 +96,15 @@ void HP303B_Show(bool json)
if (HP303B_Read(temperature, pressure, address)) if (HP303B_Read(temperature, pressure, address))
{ {
char str_temperature[33];
dtostrfd(temperature, Settings.flag2.temperature_resolution, str_temperature);
char str_pressure[33];
dtostrfd(pressure, Settings.flag2.pressure_resolution, str_pressure);
if (json) if (json)
{ {
ResponseAppend_P(PSTR(",\"HP303B\":{\"" D_JSON_TEMPERATURE "\":%d,\"" D_JSON_PRESSURE "\":%d"), temperature, pressure); ResponseAppend_P(PSTR(",\"HP303B\":{\"" D_JSON_TEMPERATURE "\":%s,\"" D_JSON_PRESSURE "\":%s"), str_temperature, str_pressure);
ResponseJsonEnd();
#ifdef USE_DOMOTICZ #ifdef USE_DOMOTICZ
if (0 == tele_period) if (0 == tele_period)
{ {
@ -109,11 +115,6 @@ void HP303B_Show(bool json)
} }
else else
{ {
char str_temperature[33];
dtostrfd(temperature, Settings.flag2.temperature_resolution, str_temperature);
char str_pressure[33];
dtostrfd(pressure, Settings.flag2.pressure_resolution, str_pressure);
WSContentSend_PD(HTTP_SNS_TEMP, "HP303B", str_temperature, TempUnit()); WSContentSend_PD(HTTP_SNS_TEMP, "HP303B", str_temperature, TempUnit());
WSContentSend_PD(HTTP_SNS_PRESSURE, "HP303B", str_pressure, PressureUnit().c_str()); WSContentSend_PD(HTTP_SNS_PRESSURE, "HP303B", str_pressure, PressureUnit().c_str());
#endif // USE_WEBSERVER #endif // USE_WEBSERVER