From 4613fce3706a3faceea3175672a064c550b5ed62 Mon Sep 17 00:00:00 2001 From: RedHawk Date: Tue, 13 Jun 2023 19:27:00 +0300 Subject: [PATCH] temp: [#61] Communication with sensor. * Up/Down state. * Raw temperature data. * Can't believe it actually worked. --- .../threads/temperature/SensorTempSHT20.cpp | 49 ++++++++++++++++++- .../src/threads/temperature/SensorTempSHT20.h | 22 +++++---- .../src/threads/temperature/Temperature.cpp | 6 ++- 3 files changed, 64 insertions(+), 13 deletions(-) diff --git a/source/shoh/src/threads/temperature/SensorTempSHT20.cpp b/source/shoh/src/threads/temperature/SensorTempSHT20.cpp index b3cc327..73be084 100644 --- a/source/shoh/src/threads/temperature/SensorTempSHT20.cpp +++ b/source/shoh/src/threads/temperature/SensorTempSHT20.cpp @@ -28,11 +28,56 @@ 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() {} + +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; diff --git a/source/shoh/src/threads/temperature/SensorTempSHT20.h b/source/shoh/src/threads/temperature/SensorTempSHT20.h index 021e1a5..1dcd5c2 100644 --- a/source/shoh/src/threads/temperature/SensorTempSHT20.h +++ b/source/shoh/src/threads/temperature/SensorTempSHT20.h @@ -12,17 +12,21 @@ class SensorTempSHT20 { public: - SensorTempSHT20(I2C* pi2c); - virtual ~SensorTempSHT20(); + SensorTempSHT20(I2C* pi2c); + virtual ~SensorTempSHT20(); + int8_t getTemperature(); + bool is_up(); 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; - const uint8_t _dev_addr; - bool _up_flag; + I2C* _pi2c; + const uint8_t _dev_addr; + 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_ */ diff --git a/source/shoh/src/threads/temperature/Temperature.cpp b/source/shoh/src/threads/temperature/Temperature.cpp index 877e583..3e49259 100644 --- a/source/shoh/src/threads/temperature/Temperature.cpp +++ b/source/shoh/src/threads/temperature/Temperature.cpp @@ -5,7 +5,8 @@ */ #include "Temperature.h" -#include "SensorTempTC74.h" +//#include "SensorTempTC74.h" +#include "SensorTempSHT20.h" #include "Event.h" #include "Log.h" @@ -15,7 +16,8 @@ Temperature::~Temperature() {} 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); int8_t temp_value = -128; _qm->send(ThreadCommon::QueueManager::master_event_all, &t, 0);