172 lines
4.8 KiB
C++
172 lines
4.8 KiB
C++
#ifndef SensorTemperaturaSHTModel
|
|
#define SensorTemperaturaSHTModel
|
|
|
|
#include "SerialService.h"
|
|
#include "I2CService.h"
|
|
#include "Utils.h"
|
|
#include <vector>
|
|
#include <Adafruit_SHT4x.h>
|
|
|
|
class SensorTemperaturaSHT {
|
|
public:
|
|
SensorTemperaturaSHT(String _modID, String _id) {
|
|
Mod_ID = _modID;
|
|
_ID = _id;
|
|
}
|
|
|
|
String Mod_ID = "";
|
|
String _ID;
|
|
int ID_Num;
|
|
byte _Endereco = 0x44;
|
|
int _CanalMUX;
|
|
|
|
bool Iniciado = false;
|
|
float temperatura = 0;
|
|
float umidade = 0;
|
|
|
|
void Inicializar() {
|
|
if (Iniciado) {
|
|
MostrarLog("Sensor ja inicializado");
|
|
return;
|
|
}
|
|
|
|
if (!I2CService::SolicitarAcessoI2C(ID_Num, _CanalMUX)) return;
|
|
|
|
bool encontrado = I2CService::VerificaEnderecoBarramento(_Endereco);
|
|
if (encontrado) {
|
|
Iniciado = sht41.begin();
|
|
if (Iniciado) {
|
|
sht41.setPrecision(SHT4X_HIGH_PRECISION);
|
|
sht41.setHeater(SHT4X_NO_HEATER);
|
|
MostrarLog("Sensor iniciado no canal " + String(_CanalMUX));
|
|
}
|
|
else {
|
|
PrintTela("Sensor nao iniciado!");
|
|
}
|
|
} else {
|
|
Iniciado = false;
|
|
MostrarLog("Sensor nao iniciado, endereco " + String(_Endereco) + " nao encontrado!");
|
|
}
|
|
|
|
I2CService::LiberarAcessoI2C(ID_Num);
|
|
}
|
|
|
|
void Desligar() {
|
|
if (!Iniciado) {
|
|
MostrarLog("Sensor nao esta inicializado");
|
|
return;
|
|
}
|
|
|
|
Iniciado = false;
|
|
|
|
if (I2CService::QuemEstaUsando() == ID_Num) {
|
|
while (I2CService::QuemEstaUsando() == ID_Num) {
|
|
vTaskDelay(10);
|
|
}
|
|
}
|
|
|
|
MostrarLog("Sensor Desligado");
|
|
}
|
|
|
|
void RequisitarDados() {
|
|
if (!Iniciado) return;
|
|
AferirTemperatura();
|
|
}
|
|
|
|
std::vector<uint8_t> MontarMensagemCAN(CanMessagePosicaoDados posicao) {
|
|
std::vector<uint8_t> data;
|
|
data.push_back(static_cast<uint8_t>(posicao));
|
|
data.push_back(ID_Num);
|
|
|
|
switch (posicao) {
|
|
case CanMessagePosicaoDados::Status: {
|
|
data.push_back(Iniciado ? 1 : 0);
|
|
break;
|
|
}
|
|
case CanMessagePosicaoDados::Dados1: {
|
|
uint16_t tmp = temperatura * 100;
|
|
uint16_t umd = umidade * 100;
|
|
data.push_back(tmp >> 8); data.push_back(tmp & 0xFF);
|
|
data.push_back(umd >> 8); data.push_back(umd & 0xFF);
|
|
break;
|
|
}
|
|
}
|
|
return data;
|
|
}
|
|
|
|
static std::vector<uint8_t> ConfigurarSensor(std::vector<SensorTemperaturaSHT*>& lista, std::vector<uint8_t>& data, const String& Mod_ID) {
|
|
std::vector<uint8_t> status;
|
|
if (data.size() < 2) return status;
|
|
CanMessagePosicaoDados posicao = (CanMessagePosicaoDados)data[0];
|
|
uint8_t idNum = data[1];
|
|
auto it = std::find_if(lista.begin(), lista.end(), [idNum](SensorTemperaturaSHT* s) { return s->ID_Num == idNum; });
|
|
bool jaExiste = it != lista.end();
|
|
SensorTemperaturaSHT* sensor;
|
|
|
|
switch (posicao) {
|
|
case CanMessagePosicaoDados::Config1: {
|
|
if (data.size() < 5) return status;
|
|
bool conectar = data[3] == 1;
|
|
int canalMux = (data[4] == 255) ? -1 : data[4];
|
|
|
|
if (conectar) {
|
|
if (!jaExiste) {
|
|
sensor = new SensorTemperaturaSHT(Mod_ID, "sSHT_" + String(idNum));
|
|
} else {
|
|
sensor = *it;
|
|
}
|
|
if (!sensor->Iniciado) {
|
|
sensor->ID_Num = idNum;
|
|
sensor->_CanalMUX = canalMux;
|
|
sensor->Inicializar();
|
|
if (!jaExiste) {
|
|
lista.push_back(sensor);
|
|
sensor->MostrarLog("Sensor de temperatura e umidade adicionado via CAN: sSHT_" + String(idNum));
|
|
}
|
|
}
|
|
status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status);
|
|
} else {
|
|
if (jaExiste) {
|
|
sensor = *it;
|
|
sensor->Desligar();
|
|
status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status);
|
|
sensor->MostrarLog("Sensor de temperatura e umidade removido via CAN: sSHT_" + String(idNum));
|
|
delete sensor;
|
|
lista.erase(it);
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
return status;
|
|
}
|
|
|
|
private:
|
|
|
|
bool DebugMode = true;
|
|
void MostrarLog(String mensagem) {
|
|
if (DebugMode) {
|
|
PrintTela("[SHT " + _ID + "] " + mensagem);
|
|
}
|
|
}
|
|
|
|
Adafruit_SHT4x sht41;
|
|
|
|
void AferirTemperatura() {
|
|
if (!Iniciado) return;
|
|
|
|
if (!I2CService::SolicitarAcessoI2C(ID_Num, _CanalMUX)) return;
|
|
|
|
sensors_event_t temp, hum;
|
|
sht41.getEvent(&hum, &temp);
|
|
|
|
temperatura = temp.temperature;
|
|
umidade = hum.relative_humidity;
|
|
|
|
MostrarLog("Temperatura: " + String(temperatura) + ", Umidade: " + String(umidade));
|
|
|
|
I2CService::LiberarAcessoI2C(ID_Num);
|
|
}
|
|
};
|
|
|
|
#endif |