129 lines
3.0 KiB
C
129 lines
3.0 KiB
C
|
|
#include "SerialService.h"
|
||
|
|
#include "Pinout.h"
|
||
|
|
#include "Utils.h"
|
||
|
|
#include "IComponenteCAN.h"
|
||
|
|
|
||
|
|
#ifndef ReleModel_v2
|
||
|
|
#define ReleModel_v2
|
||
|
|
|
||
|
|
class Rele_v2 : public ComponenteCAN {
|
||
|
|
public:
|
||
|
|
String ID;
|
||
|
|
uint8_t ID_Num = 0;
|
||
|
|
PinoModel _pino;
|
||
|
|
bool Iniciado = false;
|
||
|
|
|
||
|
|
Rele_v2() {}
|
||
|
|
|
||
|
|
Rele_v2(
|
||
|
|
const String& id,
|
||
|
|
uint8_t idNum,
|
||
|
|
bool atuacaoNivelAlto,
|
||
|
|
int pino,
|
||
|
|
TiposBarramentos tipoBarramento
|
||
|
|
) :
|
||
|
|
ID(id),
|
||
|
|
ID_Num(idNum),
|
||
|
|
_pino(PinoModel(pino, tipoBarramento, _OUTPUT)),
|
||
|
|
AtuacaoNivelAlto(atuacaoNivelAlto)
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
void Inicializar() {
|
||
|
|
if (Iniciado) {
|
||
|
|
MostrarLog("Rele ja inicializado");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (_pino.Definido()) {
|
||
|
|
bool nivelInicial = AtuacaoNivelAlto ? false : true;
|
||
|
|
_pino.Conectar(nivelInicial);
|
||
|
|
_Status = Estado::Desligado;
|
||
|
|
Iniciado = true;
|
||
|
|
MostrarLog("Rele iniciado");
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
MostrarLog("Rele nao iniciado");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void Desligar() {
|
||
|
|
if (!Iniciado) {
|
||
|
|
MostrarLog("Rele nao esta inicializado");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
AtualizarStatus(Estado::Desligado);
|
||
|
|
_pino.Desconectar();
|
||
|
|
Iniciado = false;
|
||
|
|
MostrarLog("Rele desligado");
|
||
|
|
}
|
||
|
|
|
||
|
|
void RequisitarDados() {
|
||
|
|
if (!Iniciado) return;
|
||
|
|
AtualizarLeitura();
|
||
|
|
}
|
||
|
|
|
||
|
|
void ProcessarComando(std::vector<uint8_t>& data) {
|
||
|
|
if (!Iniciado) return;
|
||
|
|
if (data.size() < 3) return;
|
||
|
|
CanMessagePosicaoDados posicao = (CanMessagePosicaoDados)data[0];
|
||
|
|
uint8_t idNum = data[1];
|
||
|
|
if (idNum != ID_Num) return;
|
||
|
|
switch (posicao) {
|
||
|
|
case CanMessagePosicaoDados::Command1: {
|
||
|
|
Estado novoEstado = (Estado)data[2];
|
||
|
|
AtualizarStatus(novoEstado);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
std::vector<uint8_t> MontarMensagemCAN(CanMessagePosicaoDados posicao) {
|
||
|
|
std::vector<uint8_t> data;
|
||
|
|
data.push_back(static_cast<uint8_t>(posicao));
|
||
|
|
data.push_back(ID_Num);
|
||
|
|
|
||
|
|
switch (posicao) {
|
||
|
|
case CanMessagePosicaoDados::Status: {
|
||
|
|
data.push_back(Iniciado ? 1 : 0);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
case CanMessagePosicaoDados::Dados1: {
|
||
|
|
data.push_back(static_cast<uint8_t>(_Status));
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return data;
|
||
|
|
}
|
||
|
|
|
||
|
|
private:
|
||
|
|
bool DebugMode = true;
|
||
|
|
|
||
|
|
bool AtuacaoNivelAlto = true;
|
||
|
|
Estado _Status = Desligado;
|
||
|
|
|
||
|
|
void AtualizarStatus(Estado novoEstado) {
|
||
|
|
bool ligado = (novoEstado == Estado::Ligado);
|
||
|
|
bool nivelPino = AtuacaoNivelAlto ? ligado : !ligado;
|
||
|
|
MostrarLog("Atualizando status do rele de " + String(_Status) + " para " + String(novoEstado));
|
||
|
|
_pino.set(nivelPino);
|
||
|
|
_Status = novoEstado;
|
||
|
|
}
|
||
|
|
|
||
|
|
void AtualizarLeitura() {
|
||
|
|
bool nivelPino = _pino.get();
|
||
|
|
bool ligado = AtuacaoNivelAlto ? nivelPino : !nivelPino;
|
||
|
|
_Status = ligado ? Estado::Ligado : Estado::Desligado;
|
||
|
|
}
|
||
|
|
|
||
|
|
void MostrarLog(String mensagem) {
|
||
|
|
if (DebugMode) {
|
||
|
|
PrintTela("[RLE " + ID + "] " + mensagem);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
};
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
#endif
|