163 lines
5.4 KiB
C++
163 lines
5.4 KiB
C++
#include "Enuns.h"
|
|
#include "SerialService.h"
|
|
#include <Wire.h>
|
|
#include <Adafruit_MCP23X17.h>
|
|
#include <Adafruit_ADS1X15.h>
|
|
|
|
#ifndef Pinout
|
|
#define Pinout
|
|
|
|
Adafruit_MCP23X17 mcp;
|
|
Adafruit_ADS1115 ads;
|
|
bool mcpInicializado = false;
|
|
bool adsInicializado = 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 == EXPANSOR_GPIO) {
|
|
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 == EXPANSOR_GPIO) {
|
|
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 == EXPANSOR_GPIO) {
|
|
return mcp.digitalRead(num);
|
|
}
|
|
else if (barramento == EXPANSOR_ADS) {
|
|
double l = 0;
|
|
for (int i = 0; i < leituras; i++) {
|
|
int leituraADC = (int)(ads.readADC_SingleEnded(num));
|
|
int leitura12bits = map(leituraADC, 0, 32767, 0, 4095);
|
|
l += leitura12bits;
|
|
delay(1);
|
|
}
|
|
return l / leituras;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
void set(bool status) {
|
|
if (Definido()) {
|
|
if (barramento == CONTROLADOR) {
|
|
digitalWrite(num, status ? HIGH : LOW);
|
|
}
|
|
else if (barramento == EXPANSOR_GPIO) {
|
|
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("MCP23X17 nao mandatorio, continuando o fluxo...");
|
|
}
|
|
}
|
|
else {
|
|
PrintTela("MCP23X17 iniciado com sucesso!");
|
|
mcpInicializado = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
void iniciarADS(T_Code Modulo, String Mod_ID, int sda, int scl, byte endereco, bool Mandatorio = false) {
|
|
PrintTela("Iniciando ADS1115...");
|
|
if (!adsInicializado) {
|
|
bool init = Wire.begin(sda, scl);
|
|
String str = "SDA " + (String)sda + " SCL " + (String)scl + " Res " + init;
|
|
PrintTela(str, false);
|
|
|
|
if (!ads.begin(endereco, &Wire)) {
|
|
adsInicializado = false;
|
|
PrintTela("Erro ao iniciar ADS1115!");
|
|
if (Mandatorio) {
|
|
PrintTela("Conexão obrigatória, tentando novamente em 5 segundos...");
|
|
//EnviarDadosSerial(Mod_ID, MontarProtocoloErro(Modulo));
|
|
delay(5000);
|
|
iniciarADS(Modulo, Mod_ID, sda, scl, endereco, Mandatorio);
|
|
}
|
|
else {
|
|
PrintTela("ADS1115 nao mandatorio, continuando o fluxo...");
|
|
}
|
|
}
|
|
else {
|
|
ads.setGain(GAIN_ONE);
|
|
PrintTela("ADS1115 iniciado com sucesso!");
|
|
adsInicializado = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
#endif |