agrobot_base/Firmware/Modulos/Pinout.h

122 lines
3.8 KiB
C
Raw Normal View History

2024-04-15 10:57:37 +00:00
#include "Enuns.h"
#include "SerialService.h"
#include <Wire.h>
#include <Adafruit_MCP23X17.h>
#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);
}
}
}
2025-02-20 20:01:28 +00:00
int get(int leituras = 0) {
2024-04-15 10:57:37 +00:00
if (Definido()) {
if (barramento == CONTROLADOR) {
if (leitura == DIGITAL) {
return digitalRead(num);
}
else if (leitura == ANALOGICO) {
2025-02-20 20:01:28 +00:00
double l = 0;
for (int i = 0; i < leituras; i++) {
l += analogRead(num);
delay(1);
}
return l / leituras;
2024-04-15 10:57:37 +00:00
}
}
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);
}
}
}
};
2024-06-06 11:52:32 +00:00
void iniciarMCP(T_Code Modulo, String Mod_ID, int sda, int scl, byte endereco, bool Mandatorio = false) {
PrintTela("Iniciando MCP23X17...");
2024-04-15 10:57:37 +00:00
if (!mcpInicializado) {
bool init = Wire.begin(sda, scl);
String str = "SDA " + (String)sda + " SCL " + (String)scl + " Res " + init;
PrintTela(str, false);
2024-04-15 10:57:37 +00:00
if (!mcp.begin_I2C(endereco, &Wire)) {
mcpInicializado = false;
PrintTela("Erro ao iniciar MCP23X17!");
2024-04-15 10:57:37 +00:00
if (Mandatorio) {
PrintTela("Conexão obrigatória, tentando novamente em 5 segundos...");
//EnviarDadosSerial(Mod_ID, MontarProtocoloErro(Modulo));
2024-04-15 10:57:37 +00:00
delay(5000);
2024-06-06 11:52:32 +00:00
iniciarMCP(Modulo, Mod_ID, sda, scl, endereco, Mandatorio);
2024-04-15 10:57:37 +00:00
}
else {
PrintTela("MCP nao mandatorio, continuando o fluxo...");
}
2024-04-15 10:57:37 +00:00
}
else {
PrintTela("MCP23X17 iniciado com sucesso!");
2024-04-15 10:57:37 +00:00
mcpInicializado = true;
}
}
}
#endif