#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; PinoModel(int numero = PINO_INVALIDO, TiposBarramentos barr = CONTROLADOR, TiposPinos tp = _INPUT, TiposLeituras lt = DIGITAL) : num(numero), barramento(barr), tipo(tp), leitura(lt) { } bool Definido() { return num != PINO_INVALIDO; } void Conectar(bool statusInicial = false) { Conectado - false; if (Definido()) { if (barramento == TiposBarramentos::CONTROLADOR) { if (tipo == _INPUT) { pinMode(num, INPUT); } else if (tipo == _OUTPUT) { pinMode(num, OUTPUT); set(statusInicial); } Conectado = true; } else if (barramento == TiposBarramentos::EXPANSOR_GPIO) { if (I2CService::McpIniciado) { I2CService::SolicitarAcessoI2C(); if (tipo == _INPUT) { I2CService::mcp.pinMode(num, INPUT); } else if (tipo == _OUTPUT) { I2CService::mcp.pinMode(num, OUTPUT); set(statusInicial); } I2CService::LiberarAcessoI2C(); Conectado = true; } } else if (barramento == TiposBarramentos::EXPANSOR_ADS) { if (I2CService::AdsIniciado) { 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(); I2CService::mcp.pinMode(num, INPUT); I2CService::LiberarAcessoI2C(); } } } 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(); int leitura = I2CService::mcp.digitalRead(num); I2CService::LiberarAcessoI2C(); return leitura; } } else if (barramento == TiposBarramentos::EXPANSOR_ADS) { if (I2CService::AdsIniciado) { I2CService::SolicitarAcessoI2C(); uint16_t l = 0; for (int i = 0; i < leituras; i++) { vTaskDelay(1); int leituraADC = (int)(I2CService::ads.readADC_SingleEnded(num)); int leitura12bits = map(leituraADC, 0, 32767, 0, 4095); MostrarLog("Leitura ADS: " + String(leituraADC) + ", Leitura 12 bits: " + String(leitura12bits)); l += leitura12bits; } I2CService::LiberarAcessoI2C(); return (int)(l / leituras); } } } 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(); I2CService::mcp.digitalWrite(num, status ? HIGH : LOW); I2CService::LiberarAcessoI2C(); } } } } private: bool DebugMode = false; void MostrarLog(String mensagem) { if (DebugMode) { PrintTela("[PINOUT] " + mensagem); } } }; #endif