temperature: Temperature sensor handling.

This commit is contained in:
RedHawk 2023-05-17 14:16:23 +03:00
parent f578fd2854
commit fc2a05a9d8
4 changed files with 125 additions and 7 deletions

View File

@ -121,7 +121,7 @@ void thread_master(void* pvParams) {
LOG_INFO("Master is creating tasks"); LOG_INFO("Master is creating tasks");
manager->tm->createTask(thread_manager, "manager", manager->tm->createTask(thread_manager, "manager",
configMINIMAL_STACK_SIZE * 15,tskIDLE_PRIORITY + 1UL, configMINIMAL_STACK_SIZE * 14,tskIDLE_PRIORITY + 1UL,
static_cast<void*>(manager)); static_cast<void*>(manager));
manager->tm->createTask(thread_rotary, "rotary", manager->tm->createTask(thread_rotary, "rotary",
configMINIMAL_STACK_SIZE * 9,tskIDLE_PRIORITY + 1UL, configMINIMAL_STACK_SIZE * 9,tskIDLE_PRIORITY + 1UL,
@ -133,7 +133,7 @@ void thread_master(void* pvParams) {
configMINIMAL_STACK_SIZE * 9,tskIDLE_PRIORITY + 1UL, configMINIMAL_STACK_SIZE * 9,tskIDLE_PRIORITY + 1UL,
static_cast<void*>(manager)); static_cast<void*>(manager));
manager->tm->createTask(thread_temperature, "temperature", manager->tm->createTask(thread_temperature, "temperature",
configMINIMAL_STACK_SIZE * 10,tskIDLE_PRIORITY + 1UL, configMINIMAL_STACK_SIZE * 9,tskIDLE_PRIORITY + 1UL,
static_cast<void*>(manager)); static_cast<void*>(manager));
LOG_INFO("Master created tasks"); LOG_INFO("Master created tasks");
m.taskFunction(); m.taskFunction();

View File

@ -5,7 +5,99 @@
*/ */
#include "SensorTempTC74.h" #include "SensorTempTC74.h"
#include "FreeRTOS.h"
#include "task.h"
#include "Log.h"
SensorTempTC74::SensorTempTC74(I2C* pi2c) : _pi2c(pi2c) {} SensorTempTC74::SensorTempTC74(I2C* pi2c, const uint8_t dev_addr)
: _pi2c(pi2c), _dev_addr(dev_addr), _temp_reg(0x00), _ctrl_reg(0x01),
_ready_bit(0x40), _standby_bit(0x80), _up_flag(false)
{}
SensorTempTC74::~SensorTempTC74() {} SensorTempTC74::~SensorTempTC74()
{}
int8_t SensorTempTC74::getTemperature()
{
return static_cast<int8_t>(this->read());
}
bool SensorTempTC74::is_up()
{
this->on_standby();
if (!this->_up_flag)
LOG_WARNING("Unable to reach temperature sensor [%d]", this->_dev_addr);
return this->_up_flag;
}
bool SensorTempTC74::on_standby()
{
uint8_t data = 0x00;
this->_up_flag = this->read_reg(this->_ctrl_reg, &data, 1);
return data & this->_standby_bit;
}
bool SensorTempTC74::is_ready()
{
uint8_t data = 0x00;
this->_up_flag = this->read_reg(this->_ctrl_reg, &data, 1);
return data & this->_ready_bit;
}
uint8_t SensorTempTC74::read()
{
uint8_t data = 0x80; // b(1000 0000)
//if on standby - remove standby and wait for 1 ms.
if (this->on_standby())
{
this->remove_standby();
vTaskDelay(1);
}
//if ready and up - read
if (this->is_ready() && this->_up_flag)
{
this->read_reg(this->_temp_reg, &data, 1);
LOG_DEBUG("Temperature sensor returned: %x", data);
}
else
LOG_WARNING("Unable to read temperature sensor [%d] value.", this->_dev_addr);
//set standy.
this->set_standby();
return data;
}
void SensorTempTC74::remove_standby()
{
uint8_t data = 0x00;
this->_up_flag = this->write_reg(this->_ctrl_reg, &data, 1);
if(!this->_up_flag)
LOG_WARNING("Unable to remove standby for temperature sensor [%d].", this->_dev_addr);
}
void SensorTempTC74::set_standby()
{
uint8_t data = this->_standby_bit;
this->_up_flag = this->write_reg(this->_ctrl_reg, &data, 1);
if(!this->_up_flag)
LOG_WARNING("Unable to set standby for temperature sensor [%d].", this->_dev_addr);
}
bool SensorTempTC74::write_reg(uint8_t com, uint8_t *trdata, const uint16_t size)
{
vTaskDelay(1); //Not sure if it is needed.
uint8_t arr[size + 1];
arr[0] = com;
for (unsigned int i = 1; i < (unsigned int)size + 1; i++) {
arr[i] = trdata[i - 1];
}
return this->_pi2c->write(this->_dev_addr, arr, size + 1);
}
bool SensorTempTC74::read_reg(uint8_t com, uint8_t *rdata, uint16_t size)
{
vTaskDelay(1); //Not sure if it is needed.
return this->_pi2c->transaction(this->_dev_addr, &com, 1, rdata, size);
}

View File

@ -8,14 +8,32 @@
#define THREADS_TEMPERATURE_SENSORTEMPTC74_H_ #define THREADS_TEMPERATURE_SENSORTEMPTC74_H_
#include "I2C.h" #include "I2C.h"
#include "chip.h"
class SensorTempTC74 { class SensorTempTC74 {
public: public:
SensorTempTC74(I2C* pi2c); SensorTempTC74(I2C* pi2c, const uint8_t dev_addr);
virtual ~SensorTempTC74(); virtual ~SensorTempTC74();
bool read(); int8_t getTemperature();
bool is_up();
private: private:
uint8_t read();
bool on_standby();
bool is_ready();
void remove_standby();
void set_standby();
bool write_reg(uint8_t com, uint8_t *trdata, const uint16_t size);
bool read_reg(uint8_t com, uint8_t *rdata, uint16_t size);
I2C* _pi2c; I2C* _pi2c;
const uint8_t _dev_addr;
const uint8_t _temp_reg;
const uint8_t _ctrl_reg;
const uint8_t _ready_bit;
uint8_t _standby_bit;
bool _up_flag;
}; };
#endif /* THREADS_TEMPERATURE_SENSORTEMPTC74_H_ */ #endif /* THREADS_TEMPERATURE_SENSORTEMPTC74_H_ */

View File

@ -14,9 +14,17 @@ Temperature::~Temperature() {}
void Temperature::taskFunction() void Temperature::taskFunction()
{ {
SensorTempTC74 ext_temp_sensor(this->_pi2c); SensorTempTC74 ext_temp_sensor(this->_pi2c, 0x4a);
int8_t temp_value = 0;
for (;;) for (;;)
{ {
if (ext_temp_sensor.is_up())
temp_value = ext_temp_sensor.getTemperature();
LOG_DEBUG("External temperature is: %d", temp_value);
//Send temperature on queue. As event. :(
vTaskDelay(5000); vTaskDelay(5000);
} }