#include "Enuns.h" #include "SerialService.h" #include "I2CService.h" #ifndef Pinout #define Pinout #define PINO_INVALIDO 0xFF enum class FormatoLeituraAnalogica : uint8_t { Bruta, Milivolts }; class PinoModel { public: int num; TiposBarramentos barramento; TiposPinos tipo; TiposLeituras leitura; bool Conectado = false; int idSensor; int _CanalMUX; PinoModel(int numero = PINO_INVALIDO, TiposBarramentos barr = CONTROLADOR, TiposPinos tp = _INPUT, TiposLeituras lt = DIGITAL, int _idSensor = 0, int canalMux = -1) : num(numero), barramento(barr), tipo(tp), leitura(lt), idSensor(_idSensor), _CanalMUX(canalMux) { } bool ConfigurarLeituraAnalogica(uint8_t resolucaoBits = 12, adc_attenuation_t atenuacao = ADC_11db) { if (!Conectado || barramento != TiposBarramentos::CONTROLADOR || leitura != TiposLeituras::ANALOGICO) { return false; } if (resolucaoBits < 9 || resolucaoBits > 12) { MostrarLog( "Resolucao ADC invalida no GPIO " + String(num) + ": " + String(resolucaoBits) + " bits" ); return false; } analogReadResolution(resolucaoBits); analogSetPinAttenuation(num, atenuacao); MostrarLog( "ADC configurado | GPIO: " + String(num) + " | Resolucao: " + String(resolucaoBits) + " bits | Atenuacao: " + String(static_cast(atenuacao)) ); return true; } bool Definido() { bool barramentoIniciado = (barramento == TiposBarramentos::CONTROLADOR) || (barramento == TiposBarramentos::EXPANSOR_ADS && I2CService::AdsIniciado) || (barramento == TiposBarramentos::EXPANSOR_GPIO && I2CService::McpIniciado); return num != PINO_INVALIDO && barramentoIniciado; } void Conectar(bool statusInicial = false) { Conectado = false; if (Definido()) { if (barramento == TiposBarramentos::CONTROLADOR) { Conectado = true; if (tipo == _INPUT) { pinMode(num, INPUT); } else if (tipo == _INPUT_PULLUP) { pinMode(num, INPUT_PULLUP); } else if (tipo == _OUTPUT) { pinMode(num, OUTPUT); set(statusInicial); } } else if (barramento == TiposBarramentos::EXPANSOR_GPIO) { if (I2CService::McpIniciado) { if (!I2CService::SolicitarAcessoI2C(idSensor, _CanalMUX)) { return; } if (tipo == _INPUT) { I2CService::mcp.pinMode(num, INPUT); } else if (tipo == _INPUT_PULLUP) { I2CService::mcp.pinMode(num, INPUT_PULLUP); } else if (tipo == _OUTPUT) { I2CService::mcp.pinMode(num, OUTPUT); set(statusInicial, false); } Conectado = true; I2CService::LiberarAcessoI2C(idSensor); } } else if (barramento == TiposBarramentos::EXPANSOR_ADS) { /*if (I2CService::AdsIniciado) { Conectado = true; }*/ Conectado = true; } } MostrarLog("Pino " + String(num) + " no barramento " + String(barramento) + " conectado: " + Conectado); } void Desconectar() { if (Definido()) { if (barramento == CONTROLADOR) { if (tipo == _PWM) { ledcDetachPin(num); } pinMode(num, INPUT); } else if (barramento == EXPANSOR_GPIO) { if (I2CService::McpIniciado) { if (!I2CService::SolicitarAcessoI2C(idSensor, _CanalMUX)) { return; } I2CService::mcp.pinMode(num, INPUT); I2CService::LiberarAcessoI2C(idSensor); } } } Conectado = false; } int get(int quantidadeLeituras = 1, FormatoLeituraAnalogica formato = FormatoLeituraAnalogica::Bruta) { if (!Conectado || quantidadeLeituras <= 0) { return -1; } bool retornarMilivolts = formato == FormatoLeituraAnalogica::Milivolts; switch (barramento) { case TiposBarramentos::CONTROLADOR: { if (leitura == TiposLeituras::DIGITAL) { if (retornarMilivolts) { MostrarLog("Leitura em mV solicitada para pino digital: " + String(num)); return -1; } return digitalRead(num); } if (leitura == TiposLeituras::ANALOGICO) { uint64_t soma = 0; for (int i = 0; i < quantidadeLeituras; i++) { if (i > 0) { vTaskDelay(pdMS_TO_TICKS(1)); } const uint32_t valor = retornarMilivolts ? analogReadMilliVolts(num) : analogRead(num); soma += valor; } const uint32_t media = static_cast(soma / static_cast(quantidadeLeituras)); MostrarLog( "Leitura Controlador | GPIO: " + String(num) + " | Valor: " + String(media) + (retornarMilivolts ? " mV" : " raw") + " | Amostras: " + String(quantidadeLeituras) ); return media <= static_cast(INT_MAX) ? static_cast(media) : -1; } return -1; } case TiposBarramentos::EXPANSOR_GPIO: { if (retornarMilivolts) { MostrarLog("Leitura em mV nao suportada pelo expansor GPIO"); return -1; } if (!I2CService::McpIniciado) { return -1; } if (!I2CService::SolicitarAcessoI2C(idSensor, _CanalMUX)) { return -1; } const int valor = I2CService::mcp.digitalRead(num); I2CService::LiberarAcessoI2C(idSensor); return valor; } case TiposBarramentos::EXPANSOR_ADS: { if (retornarMilivolts) { MostrarLog("Modo milivolts ainda nao definido para o ADS"); return -1; } return I2CService::RealizarLeituraADS(num, quantidadeLeituras, idSensor, _CanalMUX); } default: return -1; } } void set(bool status, bool reqMutex = true) { if (Conectado) { if (barramento == CONTROLADOR) { digitalWrite(num, status ? HIGH : LOW); } else if (barramento == EXPANSOR_GPIO) { if (I2CService::McpIniciado) { if (reqMutex && !I2CService::SolicitarAcessoI2C(idSensor, _CanalMUX)) { return; } I2CService::mcp.digitalWrite(num, status ? HIGH : LOW); if (reqMutex) { I2CService::LiberarAcessoI2C(idSensor); } } } } } private: bool DebugMode = false; void MostrarLog(String mensagem) { if (DebugMode) { PrintTela("[PINOUT] " + mensagem); } } }; #endif