temp: [#61] Temperature parsing and documentation.

This commit is contained in:
RedHawk 2023-06-13 23:52:14 +03:00
parent cb64ae7e0f
commit 88a7bac525

View File

@ -15,17 +15,12 @@
//U9H - Sensirion undecodable crap
//Address: 0x40
//Trigger T measurement | hold master | 1110 0011 - 0xe3
//Trigger T measurement | hold master | 1110 0011 - 0xe3 (implemented)
//Trigger T measurement | no hold master | 1111 0011 - 0xf3
//Write user register | | 1110 0110 - 0xe6
//Read user register | | 1110 0111 - 0xe7
//Soft reset | | 1111 1110 - 0xfe
//Use hold master - read(com = 0xe3)
//Reading is done via 3 bytes.
//WRITE: I2C address + write | read command
//READ: Data (MSB) | Data (LSB) + Stat. | Checksum
SensorTempSHT20::SensorTempSHT20(I2C* pi2c)
: _pi2c(pi2c), _dev_addr(0x40), _up_flag(false),
@ -33,29 +28,59 @@ _com_read_hold(0xe3), _com_read_nohold(0xf3),
_com_write_ur(0xe6), _com_read_ur(0xe7),
_com_soft_reset(0xfe), _polynominal(0x131)
{
//Read sensor during the initialisation to get the "UP" state earlier.
this->read();
}
SensorTempSHT20::~SensorTempSHT20()
{}
/**
* @brief Gets temperature from SHT20 sensor.
*
* @return int8_t temperature value trimmed from -128 to 127.
*/
int8_t SensorTempSHT20::getTemperature()
{
uint16_t raw_temp = this->read();
bool err_bit = raw_temp & 0x1;
int temp = 0;
if (err_bit)
return -128;
//TODO: Temperature parsing has to be done here.
return raw_temp >> 9;
//Formula: (St / 2 ^ 16) * 175.72 - 46.85
temp = ((double)raw_temp / 65536) * 175.72 - 46.85;
if(temp > 127)
temp = 127;
if (temp < -128)
temp = -128;
return temp;
}
/**
* @brief Makes sure that the sensor is up.
*
* @return true It is.
* @return false It is not.
*/
bool SensorTempSHT20::is_up()
{
this->read();
return this->_up_flag;
}
//Use hold master - read(com = 0xe3)
//Reading is done via 3 bytes.
//WRITE: I2C address + write | read command
//READ: Data (MSB) | Data (LSB) + Stat. | Checksum
/**
* @brief Gets raw temperature measurement data from sensor
* using hold master mode.
*
* @return uint16_t Raw temperature measurement data.
*/
uint16_t SensorTempSHT20::read()
{
uint8_t tbuf = this->_com_read_hold;
@ -78,6 +103,15 @@ uint16_t SensorTempSHT20::read()
return raw_temp;
}
/**
* @brief Checks if checksum is correct.
*
* @param data array of raw data to check.
* @param nbrOfBytes size of an array.
* @param checksum received checksum to compare calculated crc to.
* @return true - checksum is incorrect.
* @return false - checksum is the same as calculated.
*/
bool SensorTempSHT20::crc_check(uint8_t * data, uint8_t nbrOfBytes, uint8_t checksum)
{
uint8_t crc = 0;