agrobot_base/Firmware/Modulos/Pinout.h

144 lines
5.3 KiB
C
Raw Normal View History

2024-04-15 10:57:37 +00:00
#include "Enuns.h"
#include "SerialService.h"
2025-04-25 20:28:38 +00:00
#include "I2CService.h"
2024-04-15 10:57:37 +00:00
#ifndef Pinout
#define Pinout
2025-04-09 21:11:40 +00:00
#define PINO_INVALIDO 0xFF
2024-04-15 10:57:37 +00:00
class PinoModel {
public:
int num;
TiposBarramentos barramento;
TiposPinos tipo;
TiposLeituras leitura;
2025-05-08 12:29:29 +00:00
bool Conectado = false;
2025-05-15 20:20:46 +00:00
int idSensor;
int _CanalMUX;
2024-04-15 10:57:37 +00:00
2025-05-15 20:20:46 +00:00
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) {
2024-04-15 10:57:37 +00:00
}
bool Definido() {
2025-05-15 20:20:46 +00:00
bool barramentoIniciado =
(barramento == TiposBarramentos::CONTROLADOR) ||
(barramento == TiposBarramentos::EXPANSOR_ADS && I2CService::AdsIniciado) ||
(barramento == TiposBarramentos::EXPANSOR_GPIO && I2CService::McpIniciado);
2025-04-09 21:11:40 +00:00
return num != PINO_INVALIDO;
2024-04-15 10:57:37 +00:00
}
void Conectar(bool statusInicial = false) {
2025-05-15 20:20:46 +00:00
Conectado = false;
2024-04-15 10:57:37 +00:00
if (Definido()) {
2025-05-08 12:29:29 +00:00
if (barramento == TiposBarramentos::CONTROLADOR) {
Conectado = true;
2024-04-15 10:57:37 +00:00
if (tipo == _INPUT) {
pinMode(num, INPUT);
} else if (tipo == _OUTPUT) {
pinMode(num, OUTPUT);
set(statusInicial);
}
}
2025-05-08 12:29:29 +00:00
else if (barramento == TiposBarramentos::EXPANSOR_GPIO) {
2025-04-25 20:28:38 +00:00
if (I2CService::McpIniciado) {
Conectado = true;
2025-05-15 20:20:46 +00:00
I2CService::SolicitarAcessoI2C(idSensor, _CanalMUX);
2025-03-26 13:04:52 +00:00
if (tipo == _INPUT) {
2025-04-25 20:28:38 +00:00
I2CService::mcp.pinMode(num, INPUT);
2025-03-26 13:04:52 +00:00
} else if (tipo == _OUTPUT) {
2025-04-25 20:28:38 +00:00
I2CService::mcp.pinMode(num, OUTPUT);
2025-03-26 13:04:52 +00:00
set(statusInicial);
}
2025-05-15 20:20:46 +00:00
I2CService::LiberarAcessoI2C(idSensor);
2025-05-08 12:29:29 +00:00
}
}
else if (barramento == TiposBarramentos::EXPANSOR_ADS) {
2025-05-15 20:20:46 +00:00
/*if (I2CService::AdsIniciado) {
2025-05-08 12:29:29 +00:00
Conectado = true;
2025-05-15 20:20:46 +00:00
}*/
Conectado = true;
2024-04-15 10:57:37 +00:00
}
}
2025-05-08 12:29:29 +00:00
MostrarLog("Pino " + String(num) + " no barramento " + String(barramento) + " conectado: " + Conectado);
2024-04-15 10:57:37 +00:00
}
void Desconectar() {
if (Definido()) {
if (barramento == CONTROLADOR) {
if (tipo == _PWM) {
ledcDetachPin(num);
}
pinMode(num, INPUT);
}
2025-03-24 18:36:15 +00:00
else if (barramento == EXPANSOR_GPIO) {
2025-04-25 20:28:38 +00:00
if (I2CService::McpIniciado) {
2025-05-15 20:20:46 +00:00
I2CService::SolicitarAcessoI2C(idSensor, _CanalMUX);
2025-04-25 20:28:38 +00:00
I2CService::mcp.pinMode(num, INPUT);
2025-05-15 20:20:46 +00:00
I2CService::LiberarAcessoI2C(idSensor);
2025-03-26 13:04:52 +00:00
}
2024-04-15 10:57:37 +00:00
}
}
2025-05-08 12:29:29 +00:00
Conectado = false;
2024-04-15 10:57:37 +00:00
}
2025-03-26 13:04:52 +00:00
int get(int leituras = 1) {
2025-05-08 12:29:29 +00:00
if (Conectado) {
if (barramento == TiposBarramentos::CONTROLADOR) {
if (leitura == TiposLeituras::DIGITAL) {
2024-04-15 10:57:37 +00:00
return digitalRead(num);
}
2025-05-08 12:29:29 +00:00
else if (leitura == TiposLeituras::ANALOGICO) {
2025-02-20 20:01:28 +00:00
double l = 0;
for (int i = 0; i < leituras; i++) {
2025-05-08 12:29:29 +00:00
vTaskDelay(1);
int leitura = analogRead(num);
MostrarLog("Leitura Controlador: " + String(leitura));
l += leitura;
2025-02-20 20:01:28 +00:00
}
return l / leituras;
2024-04-15 10:57:37 +00:00
}
}
2025-05-08 12:29:29 +00:00
else if (barramento == TiposBarramentos::EXPANSOR_GPIO) {
2025-04-25 20:28:38 +00:00
if (I2CService::McpIniciado) {
2025-05-15 20:20:46 +00:00
I2CService::SolicitarAcessoI2C(idSensor, _CanalMUX);
2025-04-25 20:28:38 +00:00
int leitura = I2CService::mcp.digitalRead(num);
2025-05-15 20:20:46 +00:00
I2CService::LiberarAcessoI2C(idSensor);
2025-04-25 20:28:38 +00:00
return leitura;
2025-03-26 13:04:52 +00:00
}
2024-04-15 10:57:37 +00:00
}
2025-05-08 12:29:29 +00:00
else if (barramento == TiposBarramentos::EXPANSOR_ADS) {
2025-05-15 20:20:46 +00:00
return I2CService::RealizarLeituraADS(num, leituras, idSensor, _CanalMUX);
2025-03-24 18:36:15 +00:00
}
2024-04-15 10:57:37 +00:00
}
return -1;
}
void set(bool status) {
2025-05-08 12:29:29 +00:00
if (Conectado) {
2024-04-15 10:57:37 +00:00
if (barramento == CONTROLADOR) {
digitalWrite(num, status ? HIGH : LOW);
}
2025-03-24 18:36:15 +00:00
else if (barramento == EXPANSOR_GPIO) {
2025-04-25 20:28:38 +00:00
if (I2CService::McpIniciado) {
2025-05-15 20:20:46 +00:00
I2CService::SolicitarAcessoI2C(idSensor, _CanalMUX);
2025-04-25 20:28:38 +00:00
I2CService::mcp.digitalWrite(num, status ? HIGH : LOW);
2025-05-15 20:20:46 +00:00
I2CService::LiberarAcessoI2C(idSensor);
2025-03-26 13:04:52 +00:00
}
2024-04-15 10:57:37 +00:00
}
}
}
2025-05-08 12:29:29 +00:00
private:
bool DebugMode = false;
void MostrarLog(String mensagem) {
if (DebugMode) {
PrintTela("[PINOUT] " + mensagem);
}
}
2025-04-25 20:28:38 +00:00
};
2025-03-24 18:36:15 +00:00
2024-04-15 10:57:37 +00:00
#endif