#include "SerialService.h" #include "Pinout.h" #include "Utils.h" #include #ifndef SensorCorrenteModel #define SensorCorrenteModel class SensorCorrente { public: static void InicializarLista(std::vector& lista, const String& parametros, const String& Mod_ID); SensorCorrente(String _modID, String _id) { Mod_ID = _modID; _ID = _id; } // Variáveis Adafruit_INA219 sINA219; // Definições String Mod_ID = ""; String _ID; byte _Endereco; double _A_Shunt; bool Iniciado = false; float busVoltage = 0; float shuntVoltage = 0; float current = 0; float power = 0; void Inicializar() { if (Iniciado) { PrintTela(_ID + " ja inicializado"); return; } if (_Endereco > 0x00) { Wire.beginTransmission(_Endereco); byte error = Wire.endTransmission(); if (error == 0) { sINA219 = Adafruit_INA219(_Endereco); Iniciado = sINA219.begin(); if (Iniciado) { if (_A_Shunt == 3.2) { sINA219.setCalibration_32V_2A(); } else if (_A_Shunt == 50) { sINA219.setCalibration_50A_75mV(); } else if (_A_Shunt == 100) { sINA219.setCalibration_100A_75mV(); } } } else { Iniciado = false; PrintTela(_ID + " nao iniciado, endereco " + _Endereco + " nao encontrado!"); } } PrintTela(_ID + " iniciado"); } void Desligar() { if (!Iniciado) { PrintTela(_ID + " nao esta inicializado"); return; } PrintTela(_ID + " Desligado"); Iniciado = false; } void AferirCorrente() { busVoltage = sINA219.getBusVoltage_V(); shuntVoltage = sINA219.getShuntVoltage_mV(); current = sINA219.getCurrent_mA(); power = sINA219.getPower_mW(); } void RequisitarDados() { AferirCorrente(); } }; // Função para inicializar os sensores de corrente com IDs e pinos específicos void SensorCorrente::InicializarLista(std::vector& lista, const String& parametros, const String& Mod_ID) { // Verifica se há sensores já na lista if (!lista.empty()) { PrintTela("Limpando lista de sensores de corrente existente..."); for (auto* sensor : lista) { sensor->Desligar(); // Desliga cada sensor antes de remover PrintTela("Sensor de Corrente " + sensor->_ID + " desligado."); delete sensor; } lista.clear(); // Limpa a lista após desligar todos os itens } if (parametros.length() > 0) { std::vector _config = SplitString(parametros, SplitConfig); // Divide a configuração por vírgulas for (const String& config : _config) { std::vector partes = SplitString(config, SplitSubParams); // Divide cada configuração pelo '-' if (partes.size() >= 6) { String id = partes[0]; bool conectar = partes[1] == "1"; String _endereco = partes[4]; int _a_max = partes[5].toDouble(); if (conectar) { SensorCorrente* sensor = new SensorCorrente(Mod_ID, id); sensor->_Endereco = stringToByte(_endereco); sensor->_A_Shunt = _a_max; sensor->Inicializar(); lista.push_back(sensor); PrintTela("Sensor de corrente " + id + " inicializado no endereco " + _endereco); } } else { PrintTela("Configuração inválida para sensor de corrente: " + config); } } } else { PrintTela("Configuração de sensor de corrente não encontrada."); } } #endif