From 2b785e5b0c511c419f0da4bcbbe6af86c79b845b Mon Sep 17 00:00:00 2001 From: RedHawk Date: Fri, 9 Jun 2023 11:10:21 +0300 Subject: [PATCH 1/8] prototype: Change pins to the prototype ones. --- source/shoh/src/threads/relay/Relay.h | 4 ++-- source/shoh/src/threads/rotary/Rotary.h | 6 +++--- .../src/threads/user_interface/UserInterface.cpp | 16 ++++++++-------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/source/shoh/src/threads/relay/Relay.h b/source/shoh/src/threads/relay/Relay.h index c626ff7..417b721 100644 --- a/source/shoh/src/threads/relay/Relay.h +++ b/source/shoh/src/threads/relay/Relay.h @@ -48,8 +48,8 @@ public: void utilizeEventData(); private: ThreadCommon::QueueManager* _qm; - RelayDevice relays [2] = {{0, 24, 0}, - {0, 26, 1}}; + RelayDevice relays [2] = {{0, 11, 0}, + {0, 23, 1}}; void parseEvent(Event * e); int8_t setpoint, ext_temp; diff --git a/source/shoh/src/threads/rotary/Rotary.h b/source/shoh/src/threads/rotary/Rotary.h index dafe910..61ccdc0 100644 --- a/source/shoh/src/threads/rotary/Rotary.h +++ b/source/shoh/src/threads/rotary/Rotary.h @@ -20,9 +20,9 @@ public: private: Event* message; ThreadCommon::QueueManager* _qm; - DigitalIoPin signal[3] = { { 0, 1, true, true, false, true, PIN_INT0_IRQn}, //SW1 - { 0, 16, true, true, false, true, PIN_INT1_IRQn}, //SW2 - { 1, 8, true, false, false, true, PIN_INT2_IRQn} }; + DigitalIoPin signal[3] = { { 1, 18, true, true, false, true, PIN_INT0_IRQn}, //SW1 //Right //0 1 + { 2, 12, true, true, false, true, PIN_INT1_IRQn}, //SW2 //Left //0 16 + { 2, 11, true, false, false, true, PIN_INT2_IRQn} }; //Press //1 8 }; void thread_rotary(void* pvParams); diff --git a/source/shoh/src/threads/user_interface/UserInterface.cpp b/source/shoh/src/threads/user_interface/UserInterface.cpp index 4ea61c0..9bcc0fd 100644 --- a/source/shoh/src/threads/user_interface/UserInterface.cpp +++ b/source/shoh/src/threads/user_interface/UserInterface.cpp @@ -75,19 +75,19 @@ void UserInterface::handleLCD(LiquidCrystal *lcd, const char *str) void UserInterface::initLCD1() { - this->lcd1_rs = new DigitalIoPin(1, 9, false); - this->lcd1_en = new DigitalIoPin(0, 14, false); - this->lcd1_d4 = new DigitalIoPin(0, 13, false); - this->lcd1_d5 = new DigitalIoPin(0, 12, false); - this->lcd1_d6 = new DigitalIoPin(0, 23, false); - this->lcd1_d7 = new DigitalIoPin(0, 11, false); + this->lcd1_rs = new DigitalIoPin(1, 24, false);//(1, 18, false); + this->lcd1_en = new DigitalIoPin(1, 26, false);//(1, 24, false); + this->lcd1_d4 = new DigitalIoPin(1, 27, false);//(1, 19, false); + this->lcd1_d5 = new DigitalIoPin(1, 25, false);//(1, 26, false); + this->lcd1_d6 = new DigitalIoPin(1, 28, false);//(1, 27, false); + this->lcd1_d7 = new DigitalIoPin(2, 3, false);//(1, 25, false); this->lcd1_rs->write(false); this->lcd1_en->write(false); this->lcd1_d4->write(false); this->lcd1_d5->write(false); - this->lcd1_d6->write(false); - this->lcd1_d7->write(false); + this->lcd1_d6->write(false); + this->lcd1_d7->write(false); // LCD init. this->lcd1 = new LiquidCrystal(this->lcd1_rs, this->lcd1_en, this->lcd1_d4, this->lcd1_d5, this->lcd1_d6, this->lcd1_d7); From 38b24f5998b0616ea89aef5f0f2f7419e87492a9 Mon Sep 17 00:00:00 2001 From: RedHawk Date: Sun, 11 Jun 2023 13:48:24 +0300 Subject: [PATCH 2/8] rotary: [#62] Changed interrupts behaviour. * Button on the rotary is still inactive. Demands further investigation with logic analizer. --- source/shoh/src/threads/rotary/Rotary.cpp | 19 ++++++++++++++----- source/shoh/src/threads/rotary/Rotary.h | 6 +++--- .../src/threads/temperature/Temperature.cpp | 1 + 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/source/shoh/src/threads/rotary/Rotary.cpp b/source/shoh/src/threads/rotary/Rotary.cpp index fcdce48..4ea786d 100644 --- a/source/shoh/src/threads/rotary/Rotary.cpp +++ b/source/shoh/src/threads/rotary/Rotary.cpp @@ -12,6 +12,8 @@ static QueueHandle_t * p_rotary_isr_q; +static DigitalIoPin * p_sigB; + extern "C" { void @@ -19,11 +21,17 @@ extern "C" { Chip_PININT_ClearIntStatus (LPC_PININT, PININTCH (PIN_INT0_IRQn)); portBASE_TYPE xHigherPriorityWoken = pdFALSE; - uint8_t data = ThreadCommon::RotaryAction::Right; + uint8_t data; + + if (p_sigB->read()) + data = ThreadCommon::RotaryAction::Left; + else + data = ThreadCommon::RotaryAction::Right; + xQueueSendFromISR (*p_rotary_isr_q, &data, &xHigherPriorityWoken); portEND_SWITCHING_ISR(xHigherPriorityWoken); } - +/* void PIN_INT1_IRQHandler (void) { @@ -33,11 +41,11 @@ extern "C" xQueueSendFromISR (*p_rotary_isr_q, &data, &xHigherPriorityWoken); portEND_SWITCHING_ISR(xHigherPriorityWoken); } - +*/ void - PIN_INT2_IRQHandler (void) + PIN_INT1_IRQHandler (void) { - Chip_PININT_ClearIntStatus (LPC_PININT, PININTCH (PIN_INT2_IRQn)); + Chip_PININT_ClearIntStatus (LPC_PININT, PININTCH (PIN_INT1_IRQn)); portBASE_TYPE xHigherPriorityWoken = pdFALSE; uint8_t data = ThreadCommon::RotaryAction::Press; xQueueSendFromISR (*p_rotary_isr_q, &data, &xHigherPriorityWoken); @@ -48,6 +56,7 @@ extern "C" Rotary::Rotary(ThreadCommon::QueueManager* qm) : _qm(qm) { LOG_DEBUG("Creating Rotary"); + p_sigB = &(this->signal[1]); } Rotary::~Rotary() diff --git a/source/shoh/src/threads/rotary/Rotary.h b/source/shoh/src/threads/rotary/Rotary.h index 61ccdc0..79a4201 100644 --- a/source/shoh/src/threads/rotary/Rotary.h +++ b/source/shoh/src/threads/rotary/Rotary.h @@ -20,9 +20,9 @@ public: private: Event* message; ThreadCommon::QueueManager* _qm; - DigitalIoPin signal[3] = { { 1, 18, true, true, false, true, PIN_INT0_IRQn}, //SW1 //Right //0 1 - { 2, 12, true, true, false, true, PIN_INT1_IRQn}, //SW2 //Left //0 16 - { 2, 11, true, false, false, true, PIN_INT2_IRQn} }; //Press //1 8 + DigitalIoPin signal[3] = { { 1, 18, true, true, false, true, PIN_INT0_IRQn}, //sigA + { 2, 12, true, true, false}, //sigB + { 0, 1, true, true, false, true, PIN_INT1_IRQn} }; //Press, sw //2 11 }; void thread_rotary(void* pvParams); diff --git a/source/shoh/src/threads/temperature/Temperature.cpp b/source/shoh/src/threads/temperature/Temperature.cpp index 7929184..877e583 100644 --- a/source/shoh/src/threads/temperature/Temperature.cpp +++ b/source/shoh/src/threads/temperature/Temperature.cpp @@ -27,6 +27,7 @@ void Temperature::taskFunction() if(temp_value == -128) { LOG_ERROR("Failed to get temperature."); + vTaskDelay(10000); continue; } From 34618d640da7c936d4a16af0cf4f80df51e94670 Mon Sep 17 00:00:00 2001 From: RedHawk Date: Tue, 13 Jun 2023 16:50:10 +0300 Subject: [PATCH 3/8] rotary: [#62] Change pin for rotary press. * Connected button to ground. * It seems that initially chosen pin has no interrupts! * That's a way to spend several hours... --- source/shoh/src/threads/rotary/Rotary.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/shoh/src/threads/rotary/Rotary.h b/source/shoh/src/threads/rotary/Rotary.h index 79a4201..e21077b 100644 --- a/source/shoh/src/threads/rotary/Rotary.h +++ b/source/shoh/src/threads/rotary/Rotary.h @@ -22,7 +22,7 @@ private: ThreadCommon::QueueManager* _qm; DigitalIoPin signal[3] = { { 1, 18, true, true, false, true, PIN_INT0_IRQn}, //sigA { 2, 12, true, true, false}, //sigB - { 0, 1, true, true, false, true, PIN_INT1_IRQn} }; //Press, sw //2 11 + { 0, 2, true, true, false, true, PIN_INT1_IRQn} }; //Press, sw //2 11 (pin without interrupt) //0 1 (board button) }; void thread_rotary(void* pvParams); From 7cc658bc2e1f314ed85ed3cc55aa44fe424ecfda Mon Sep 17 00:00:00 2001 From: RedHawk Date: Sun, 11 Jun 2023 21:45:22 +0300 Subject: [PATCH 4/8] temp sensor: [#61] Added placeholder for the sensor interface. --- .../threads/temperature/SensorTempSHT20.cpp | 57 +++++++++++++++++++ .../src/threads/temperature/SensorTempSHT20.h | 28 +++++++++ 2 files changed, 85 insertions(+) create mode 100644 source/shoh/src/threads/temperature/SensorTempSHT20.cpp create mode 100644 source/shoh/src/threads/temperature/SensorTempSHT20.h diff --git a/source/shoh/src/threads/temperature/SensorTempSHT20.cpp b/source/shoh/src/threads/temperature/SensorTempSHT20.cpp new file mode 100644 index 0000000..b3cc327 --- /dev/null +++ b/source/shoh/src/threads/temperature/SensorTempSHT20.cpp @@ -0,0 +1,57 @@ +/* + * SensorTempSHT20.cpp + * + * Created on: 16 May 2023 + */ + +#include "SensorTempSHT20.h" +#include "FreeRTOS.h" +#include "task.h" +#include "Log.h" + +//D6U9H: +//D - Digital (i2c) +//6 - 2016 year of production +//U9H - Sensirion undecodable crap +//Address: 0x40 + +//Trigger T measurement | hold master | 1110 0011 - 0xe3 +//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) +{} + +SensorTempSHT20::~SensorTempSHT20() +{} +/* +const int16_t POLYNOMIAL = 0x131; + +int8_t SHT2x_CheckCrc(int8_t data[], int8_t nbrOfBytes, int8_t checksum) +{ + int8_t crc = 0; + int8_t bit; + int8_t byteCtr; + //calculates 8-Bit checksum with given polynomial + for (byteCtr = 0; byteCtr < nbrOfBytes; ++byteCtr) + { + crc ^= (data[byteCtr]); + for ( bit = 8; bit > 0; --bit) + { + if (crc & 0x80) crc = (crc << 1) ^ POLYNOMIAL; + else crc = (crc << 1); + } + } + if (crc != checksum) return 1; + else return 0; +} +*/ diff --git a/source/shoh/src/threads/temperature/SensorTempSHT20.h b/source/shoh/src/threads/temperature/SensorTempSHT20.h new file mode 100644 index 0000000..021e1a5 --- /dev/null +++ b/source/shoh/src/threads/temperature/SensorTempSHT20.h @@ -0,0 +1,28 @@ +/* + * SensorTempSHT20.h + * + * Created on: 16 May 2023 + */ + +#ifndef THREADS_TEMPERATURE_SENSORTEMPTC74_H_ +#define THREADS_TEMPERATURE_SENSORTEMPTC74_H_ + +#include "I2C.h" +#include "chip.h" + +class SensorTempSHT20 { +public: + SensorTempSHT20(I2C* pi2c); + virtual ~SensorTempSHT20(); +private: + 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; +}; + +#endif /* THREADS_TEMPERATURE_SENSORTEMPSHT20_H_ */ From 4613fce3706a3faceea3175672a064c550b5ed62 Mon Sep 17 00:00:00 2001 From: RedHawk Date: Tue, 13 Jun 2023 19:27:00 +0300 Subject: [PATCH 5/8] 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); From cb64ae7e0f3fd45d61706d74b5a03fe7bcb0f037 Mon Sep 17 00:00:00 2001 From: RedHawk Date: Tue, 13 Jun 2023 19:52:53 +0300 Subject: [PATCH 6/8] temp: [#61] CRC check. --- .../threads/temperature/SensorTempSHT20.cpp | 40 ++++++++----------- .../src/threads/temperature/SensorTempSHT20.h | 2 + 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/source/shoh/src/threads/temperature/SensorTempSHT20.cpp b/source/shoh/src/threads/temperature/SensorTempSHT20.cpp index 73be084..db72284 100644 --- a/source/shoh/src/threads/temperature/SensorTempSHT20.cpp +++ b/source/shoh/src/threads/temperature/SensorTempSHT20.cpp @@ -31,7 +31,7 @@ SensorTempSHT20::SensorTempSHT20(I2C* pi2c) : _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) +_com_soft_reset(0xfe), _polynominal(0x131) { this->read(); } @@ -71,32 +71,24 @@ uint16_t SensorTempSHT20::read() 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. + if (this->crc_check(rbuf, 2, crc)) + LOG_WARNING("Temperature sensor reported crc mismatch.\nRaw data: %04x; CRC: %x", raw_temp, crc); + return raw_temp; } -/* -const int16_t POLYNOMIAL = 0x131; - -int8_t SHT2x_CheckCrc(int8_t data[], int8_t nbrOfBytes, int8_t checksum) +bool SensorTempSHT20::crc_check(uint8_t * data, uint8_t nbrOfBytes, uint8_t checksum) { - int8_t crc = 0; - int8_t bit; - int8_t byteCtr; - //calculates 8-Bit checksum with given polynomial - for (byteCtr = 0; byteCtr < nbrOfBytes; ++byteCtr) - { - crc ^= (data[byteCtr]); - for ( bit = 8; bit > 0; --bit) - { - if (crc & 0x80) crc = (crc << 1) ^ POLYNOMIAL; - else crc = (crc << 1); - } - } - if (crc != checksum) return 1; - else return 0; + uint8_t crc = 0; + uint8_t bit; + uint8_t byteCtr; + //Calculates 8-Bit checksum with given _polynomial + for (byteCtr = 0; byteCtr < nbrOfBytes; ++byteCtr) + { + crc ^= (data[byteCtr]); + for ( bit = 8; bit > 0; --bit) + crc = (crc & 0x80) ? ((crc << 1) ^ this->_polynominal) : (crc << 1); + } + return crc != checksum; } -*/ diff --git a/source/shoh/src/threads/temperature/SensorTempSHT20.h b/source/shoh/src/threads/temperature/SensorTempSHT20.h index 1dcd5c2..6006bc3 100644 --- a/source/shoh/src/threads/temperature/SensorTempSHT20.h +++ b/source/shoh/src/threads/temperature/SensorTempSHT20.h @@ -18,6 +18,7 @@ public: bool is_up(); private: uint16_t read(); + bool crc_check(uint8_t * data, uint8_t nbrOfBytes, uint8_t checksum); I2C* _pi2c; const uint8_t _dev_addr; @@ -27,6 +28,7 @@ private: const uint8_t _com_write_ur; //user register const uint8_t _com_read_ur; //user register const uint8_t _com_soft_reset; + const uint16_t _polynominal; }; #endif /* THREADS_TEMPERATURE_SENSORTEMPSHT20_H_ */ From 88a7bac525d3b3a1e940488ca0ca680879169876 Mon Sep 17 00:00:00 2001 From: RedHawk Date: Tue, 13 Jun 2023 23:52:14 +0300 Subject: [PATCH 7/8] temp: [#61] Temperature parsing and documentation. --- .../threads/temperature/SensorTempSHT20.cpp | 52 +++++++++++++++---- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/source/shoh/src/threads/temperature/SensorTempSHT20.cpp b/source/shoh/src/threads/temperature/SensorTempSHT20.cpp index db72284..fe1110a 100644 --- a/source/shoh/src/threads/temperature/SensorTempSHT20.cpp +++ b/source/shoh/src/threads/temperature/SensorTempSHT20.cpp @@ -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; From 7a25e14a8fb337708c3c67a7f63c8b39f9641b40 Mon Sep 17 00:00:00 2001 From: RedHawk Date: Wed, 14 Jun 2023 20:36:26 +0300 Subject: [PATCH 8/8] temp: [#61] Make things a little clearer. * Newline in log isn't nice. * I managed to confuse even myself with this 0x4a. It's actually not used anywhere currently. --- source/shoh/src/threads/temperature/SensorTempSHT20.cpp | 2 +- source/shoh/src/threads/temperature/Temperature.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/source/shoh/src/threads/temperature/SensorTempSHT20.cpp b/source/shoh/src/threads/temperature/SensorTempSHT20.cpp index fe1110a..25ad4b0 100644 --- a/source/shoh/src/threads/temperature/SensorTempSHT20.cpp +++ b/source/shoh/src/threads/temperature/SensorTempSHT20.cpp @@ -98,7 +98,7 @@ uint16_t SensorTempSHT20::read() crc = rbuf[2]; if (this->crc_check(rbuf, 2, crc)) - LOG_WARNING("Temperature sensor reported crc mismatch.\nRaw data: %04x; CRC: %x", raw_temp, crc); + LOG_WARNING("Temperature sensor reported crc mismatch. Raw data: %04x; CRC: %x", raw_temp, crc); return raw_temp; } diff --git a/source/shoh/src/threads/temperature/Temperature.cpp b/source/shoh/src/threads/temperature/Temperature.cpp index 3e49259..45efd81 100644 --- a/source/shoh/src/threads/temperature/Temperature.cpp +++ b/source/shoh/src/threads/temperature/Temperature.cpp @@ -43,7 +43,7 @@ void Temperature::taskFunction() void thread_temperature(void* pvParams) { ThreadCommon::CommonManagers * manager = static_cast(pvParams); - I2C_config conf{0x4a, 100000}; + I2C_config conf{0, 100000}; I2C i2c(conf); Temperature t(manager->qm, &i2c); t.taskFunction();