#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(int leituras = 0) { if (Definido()) { if (barramento == CONTROLADOR) { if (leitura == DIGITAL) { return digitalRead(num); } else if (leitura == ANALOGICO) { double l = 0; for (int i = 0; i < leituras; i++) { l += analogRead(num); delay(1); } return l / leituras; } } 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, String Mod_ID, int sda, int scl, byte endereco, bool Mandatorio = false) { PrintTela("Iniciando MCP23X17..."); if (!mcpInicializado) { bool init = Wire.begin(sda, scl); String str = "SDA " + (String)sda + " SCL " + (String)scl + " Res " + init; PrintTela(str, false); if (!mcp.begin_I2C(endereco, &Wire)) { mcpInicializado = false; PrintTela("Erro ao iniciar MCP23X17!"); if (Mandatorio) { PrintTela("Conexão obrigatória, tentando novamente em 5 segundos..."); //EnviarDadosSerial(Mod_ID, MontarProtocoloErro(Modulo)); delay(5000); iniciarMCP(Modulo, Mod_ID, sda, scl, endereco, Mandatorio); } else { PrintTela("MCP nao mandatorio, continuando o fluxo..."); } } else { PrintTela("MCP23X17 iniciado com sucesso!"); mcpInicializado = true; } } } #endif