#include "Enuns.h" #include "SerialService.h" #include "I2CService.h" #ifndef Pinout #define Pinout #define PINO_INVALIDO 0xFF 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 Definido() { bool barramentoIniciado = (barramento == TiposBarramentos::CONTROLADOR) || (barramento == TiposBarramentos::EXPANSOR_ADS && I2CService::AdsIniciado) || (barramento == TiposBarramentos::EXPANSOR_GPIO && I2CService::McpIniciado); return num != PINO_INVALIDO; } void Conectar(bool statusInicial = false) { Conectado = false; if (Definido()) { if (barramento == TiposBarramentos::CONTROLADOR) { Conectado = true; if (tipo == _INPUT) { pinMode(num, INPUT); } else if (tipo == _OUTPUT) { pinMode(num, OUTPUT); set(statusInicial); } } else if (barramento == TiposBarramentos::EXPANSOR_GPIO) { if (I2CService::McpIniciado) { Conectado = true; I2CService::SolicitarAcessoI2C(idSensor, _CanalMUX); if (tipo == _INPUT) { I2CService::mcp.pinMode(num, INPUT); } else if (tipo == _OUTPUT) { I2CService::mcp.pinMode(num, OUTPUT); set(statusInicial); } 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) { I2CService::SolicitarAcessoI2C(idSensor, _CanalMUX); I2CService::mcp.pinMode(num, INPUT); I2CService::LiberarAcessoI2C(idSensor); } } } Conectado = false; } int get(int leituras = 1) { if (Conectado) { if (barramento == TiposBarramentos::CONTROLADOR) { if (leitura == TiposLeituras::DIGITAL) { return digitalRead(num); } else if (leitura == TiposLeituras::ANALOGICO) { double l = 0; for (int i = 0; i < leituras; i++) { vTaskDelay(1); int leitura = analogRead(num); MostrarLog("Leitura Controlador: " + String(leitura)); l += leitura; } return l / leituras; } } else if (barramento == TiposBarramentos::EXPANSOR_GPIO) { if (I2CService::McpIniciado) { I2CService::SolicitarAcessoI2C(idSensor, _CanalMUX); int leitura = I2CService::mcp.digitalRead(num); I2CService::LiberarAcessoI2C(idSensor); return leitura; } } else if (barramento == TiposBarramentos::EXPANSOR_ADS) { return I2CService::RealizarLeituraADS(num, leituras, idSensor, _CanalMUX); } } return -1; } void set(bool status) { if (Conectado) { if (barramento == CONTROLADOR) { digitalWrite(num, status ? HIGH : LOW); } else if (barramento == EXPANSOR_GPIO) { if (I2CService::McpIniciado) { I2CService::SolicitarAcessoI2C(idSensor, _CanalMUX); I2CService::mcp.digitalWrite(num, status ? HIGH : LOW); I2CService::LiberarAcessoI2C(idSensor); } } } } private: bool DebugMode = false; void MostrarLog(String mensagem) { if (DebugMode) { PrintTela("[PINOUT] " + mensagem); } } }; #endif