180 lines
5.0 KiB
C
180 lines
5.0 KiB
C
|
|
#ifndef SensorLuminosidadeModel
|
||
|
|
#define SensorLuminosidadeModel
|
||
|
|
|
||
|
|
#include "SerialService.h"
|
||
|
|
#include "I2CService.h"
|
||
|
|
#include "Utils.h"
|
||
|
|
#include <vector>
|
||
|
|
#include <Adafruit_TSL2591.h>
|
||
|
|
|
||
|
|
class SensorLuminosidade {
|
||
|
|
public:
|
||
|
|
SensorLuminosidade(String _modID, String _id) {
|
||
|
|
Mod_ID = _modID;
|
||
|
|
_ID = _id;
|
||
|
|
}
|
||
|
|
|
||
|
|
String Mod_ID = "";
|
||
|
|
String _ID;
|
||
|
|
int ID_Num;
|
||
|
|
byte _Endereco;
|
||
|
|
int _CanalMUX;
|
||
|
|
|
||
|
|
bool Iniciado = false;
|
||
|
|
float lux = 0;
|
||
|
|
float infra = 0;
|
||
|
|
float total = 0;
|
||
|
|
|
||
|
|
void Inicializar() {
|
||
|
|
if (Iniciado) {
|
||
|
|
MostrarLog("Sensor ja inicializado");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!I2CService::SolicitarAcessoI2C(ID_Num, _CanalMUX)) return;
|
||
|
|
|
||
|
|
bool encontrado = I2CService::VerificaEnderecoBarramento(_Endereco);
|
||
|
|
if (encontrado) {
|
||
|
|
tsl = Adafruit_TSL2591(ID_Num);
|
||
|
|
Iniciado = tsl.begin();
|
||
|
|
if (Iniciado) {
|
||
|
|
tsl.setGain(TSL2591_GAIN_MED); // GAIN_LOW, MED, HIGH, MAX
|
||
|
|
tsl.setTiming(TSL2591_INTEGRATIONTIME_100MS); // tempo de integração
|
||
|
|
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;
|
||
|
|
AferirLuminosidade();
|
||
|
|
}
|
||
|
|
|
||
|
|
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: {
|
||
|
|
uint32_t lux100 = lux * 100;
|
||
|
|
data.push_back((lux100 >> 24) & 0xFF);
|
||
|
|
data.push_back((lux100 >> 16) & 0xFF);
|
||
|
|
data.push_back((lux100 >> 8) & 0xFF);
|
||
|
|
data.push_back(lux100 & 0xFF);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return data;
|
||
|
|
}
|
||
|
|
|
||
|
|
static std::vector<uint8_t> ConfigurarSensor(std::vector<SensorLuminosidade*>& 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](SensorLuminosidade* s) { return s->ID_Num == idNum; });
|
||
|
|
bool jaExiste = it != lista.end();
|
||
|
|
SensorLuminosidade* sensor;
|
||
|
|
|
||
|
|
switch (posicao) {
|
||
|
|
case CanMessagePosicaoDados::Config1: {
|
||
|
|
if (data.size() < 6) return status;
|
||
|
|
bool conectar = data[3] == 1;
|
||
|
|
uint8_t endereco = data[4];
|
||
|
|
int canalMux = (data[5] == 255) ? -1 : data[5];
|
||
|
|
|
||
|
|
if (conectar) {
|
||
|
|
if (!jaExiste) {
|
||
|
|
sensor = new SensorLuminosidade(Mod_ID, "sLUZ_" + String(idNum));
|
||
|
|
} else {
|
||
|
|
sensor = *it;
|
||
|
|
}
|
||
|
|
if (!sensor->Iniciado) {
|
||
|
|
sensor->ID_Num = idNum;
|
||
|
|
sensor->_Endereco = endereco;
|
||
|
|
sensor->_CanalMUX = canalMux;
|
||
|
|
sensor->Inicializar();
|
||
|
|
if (!jaExiste) {
|
||
|
|
lista.push_back(sensor);
|
||
|
|
sensor->MostrarLog("Sensor de luminosidade adicionado via CAN: sLUZ_" + String(idNum));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status);
|
||
|
|
} else {
|
||
|
|
if (jaExiste) {
|
||
|
|
sensor = *it;
|
||
|
|
sensor->Desligar();
|
||
|
|
status = sensor->MontarMensagemCAN(CanMessagePosicaoDados::Status);
|
||
|
|
sensor->MostrarLog("Sensor de luminosidade removido via CAN: sLUZ_" + String(idNum));
|
||
|
|
delete sensor;
|
||
|
|
lista.erase(it);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return status;
|
||
|
|
}
|
||
|
|
|
||
|
|
private:
|
||
|
|
|
||
|
|
bool DebugMode = true;
|
||
|
|
void MostrarLog(String mensagem) {
|
||
|
|
if (DebugMode) {
|
||
|
|
PrintTela("[LUZ " + _ID + "] " + mensagem);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Adafruit_TSL2591 tsl = Adafruit_TSL2591();
|
||
|
|
|
||
|
|
void AferirLuminosidade() {
|
||
|
|
if (!Iniciado) return;
|
||
|
|
|
||
|
|
if (!I2CService::SolicitarAcessoI2C(ID_Num, _CanalMUX)) return;
|
||
|
|
|
||
|
|
uint32_t lum = tsl.getFullLuminosity();
|
||
|
|
uint16_t ir = lum >> 16;
|
||
|
|
uint16_t full = lum & 0xFFFF;
|
||
|
|
float _lux = tsl.calculateLux(full, ir);
|
||
|
|
|
||
|
|
lux = _lux;
|
||
|
|
infra = ir;
|
||
|
|
total = full;
|
||
|
|
|
||
|
|
MostrarLog("Lux: " + String(lux) + ", Infra: " + String(infra) + ", Total: " + String(total));
|
||
|
|
|
||
|
|
I2CService::LiberarAcessoI2C(ID_Num);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
#endif
|