#include "Enuns.h" #include "SerialService.h" #include #include #ifndef Pinout #define Pinout Adafruit_MCP23X17 mcp; bool mcpInicializado = false; class PinoModel { public: int num; TiposBarramentos barramento; TiposPinos tipo; TiposLeituras leitura; PinoModel(int numero = -1, TiposBarramentos barr = CONTROLADOR, TiposPinos tp = _INPUT, TiposLeituras lt = DIGITAL) : num(numero), barramento(barr), tipo(tp), leitura(lt) { } bool Definido() { return num > -1; } void Conectar(bool statusInicial = false) { if (Definido()) { if (barramento == CONTROLADOR) { if (tipo == _INPUT) { pinMode(num, INPUT); } else if (tipo == _OUTPUT) { pinMode(num, OUTPUT); set(statusInicial); } } else if (barramento == EXPANSORI2C) { if (tipo == _INPUT) { mcp.pinMode(num, INPUT); } else if (tipo == _OUTPUT) { mcp.pinMode(num, OUTPUT); set(statusInicial); } } } } void Desconectar() { if (Definido()) { if (barramento == CONTROLADOR) { if (tipo == _PWM) { ledcDetachPin(num); } pinMode(num, INPUT); } else if (barramento == EXPANSORI2C) { mcp.pinMode(num, INPUT); } } } int get() { if (Definido()) { if (barramento == CONTROLADOR) { if (leitura == DIGITAL) { return digitalRead(num); } else if (leitura == ANALOGICO) { return analogRead(num); } } else if (barramento == EXPANSORI2C) { return mcp.digitalRead(num); } } return -1; } void set(bool status) { if (Definido()) { if (barramento == CONTROLADOR) { digitalWrite(num, status ? HIGH : LOW); } else if (barramento == EXPANSORI2C) { mcp.digitalWrite(num, status ? HIGH : LOW); } } } }; void iniciarMCP(T_Code Modulo, int sda, int scl, byte endereco, bool Mandatorio = false) { Serial.println("Iniciando MCP23X17..."); if (!mcpInicializado) { Wire.begin(sda, scl); if (!mcp.begin_I2C(endereco, &Wire)) { mcpInicializado = false; Serial.println("Erro ao iniciar MCP23X17!"); if (Mandatorio) { Serial.println("Conexão obrigatória, tentando novamente em 5 segundos..."); EnviarDadosSerial(MontarProtocoloErro(Modulo)); delay(5000); iniciarMCP(Modulo, sda, scl, endereco, Mandatorio); } } else { Serial.println("MCP23X17 iniciado com sucesso!"); mcpInicializado = true; } } } #endif