temp: [#61] Communication with sensor.

* Up/Down state.
* Raw temperature data.
* Can't believe it actually worked.
This commit is contained in:
RedHawk 2023-06-13 19:27:00 +03:00
parent 7cc658bc2e
commit 4613fce370
3 changed files with 64 additions and 13 deletions

View File

@ -28,11 +28,56 @@
SensorTempSHT20::SensorTempSHT20(I2C* pi2c) SensorTempSHT20::SensorTempSHT20(I2C* pi2c)
: _pi2c(pi2c), _dev_addr(0x40) : _pi2c(pi2c), _dev_addr(0x40), _up_flag(false),
{} _com_read_hold(0xe3), _com_read_nohold(0xf3),
_com_write_ur(0xe6), _com_read_ur(0xe7),
_com_soft_reset(0xfe)
{
this->read();
}
SensorTempSHT20::~SensorTempSHT20() SensorTempSHT20::~SensorTempSHT20()
{} {}
int8_t SensorTempSHT20::getTemperature()
{
uint16_t raw_temp = this->read();
bool err_bit = raw_temp & 0x1;
if (err_bit)
return -128;
//TODO: Temperature parsing has to be done here.
return raw_temp >> 9;
}
bool SensorTempSHT20::is_up()
{
this->read();
return this->_up_flag;
}
uint16_t SensorTempSHT20::read()
{
uint8_t tbuf = this->_com_read_hold;
uint8_t rbuf[3] = {0x0, 0x2, 0x0};
uint8_t crc = 0x0;
uint16_t raw_temp = 0;
this->_up_flag = this->_pi2c->transaction(this->_dev_addr, &tbuf, 1, rbuf, 3);
//Sensor changes this bit to 0 on temp measurement.
if (rbuf[1] & 0x2)
return 0x1;
raw_temp |= (rbuf[0] << 8);
raw_temp |= (rbuf[1] & 0xfc);
crc = rbuf[2];
LOG_WARNING("Raw data: %04x; CRC: %x", raw_temp, crc);
//TODO: crc check here.
return raw_temp;
}
/* /*
const int16_t POLYNOMIAL = 0x131; const int16_t POLYNOMIAL = 0x131;

View File

@ -14,15 +14,19 @@ class SensorTempSHT20 {
public: public:
SensorTempSHT20(I2C* pi2c); SensorTempSHT20(I2C* pi2c);
virtual ~SensorTempSHT20(); virtual ~SensorTempSHT20();
int8_t getTemperature();
bool is_up();
private: private:
uint16_t read(); uint16_t read();
bool write_com(uint8_t com, uint8_t *trdata, const uint16_t size);
bool read_com(uint8_t com, uint8_t *rdata, uint16_t size);
I2C* _pi2c; I2C* _pi2c;
const uint8_t _dev_addr; const uint8_t _dev_addr;
bool _up_flag; bool _up_flag;
const uint8_t _com_read_hold;
const uint8_t _com_read_nohold;
const uint8_t _com_write_ur; //user register
const uint8_t _com_read_ur; //user register
const uint8_t _com_soft_reset;
}; };
#endif /* THREADS_TEMPERATURE_SENSORTEMPSHT20_H_ */ #endif /* THREADS_TEMPERATURE_SENSORTEMPSHT20_H_ */

View File

@ -5,7 +5,8 @@
*/ */
#include "Temperature.h" #include "Temperature.h"
#include "SensorTempTC74.h" //#include "SensorTempTC74.h"
#include "SensorTempSHT20.h"
#include "Event.h" #include "Event.h"
#include "Log.h" #include "Log.h"
@ -15,7 +16,8 @@ Temperature::~Temperature() {}
void Temperature::taskFunction() void Temperature::taskFunction()
{ {
SensorTempTC74 ext_temp_sensor(this->_pi2c, 0x4a); //SensorTempTC74 ext_temp_sensor(this->_pi2c, 0x4a);
SensorTempSHT20 ext_temp_sensor(this->_pi2c);
Event t (Event::ExternalTemp, -128); Event t (Event::ExternalTemp, -128);
int8_t temp_value = -128; int8_t temp_value = -128;
_qm->send<Event>(ThreadCommon::QueueManager::master_event_all, &t, 0); _qm->send<Event>(ThreadCommon::QueueManager::master_event_all, &t, 0);