95 lines
2.4 KiB
C++
95 lines
2.4 KiB
C++
#include "SerialService.h"
|
|
#include "Pinout.h"
|
|
#include "Utils.h"
|
|
#include <Adafruit_INA219.h>
|
|
|
|
#ifndef SensorCorrenteModel
|
|
#define SensorCorrenteModel
|
|
|
|
class SensorCorrente {
|
|
public:
|
|
|
|
SensorCorrente(String _modID, String _id) {
|
|
Mod_ID = _modID;
|
|
_ID = _id;
|
|
}
|
|
|
|
// Variáveis
|
|
Adafruit_INA219 sINA219;
|
|
|
|
// Definições
|
|
String Mod_ID = "";
|
|
String _ID;
|
|
S_Code _componente = sCOR;
|
|
byte _Endereco;
|
|
double _A_Shunt;
|
|
|
|
bool Iniciado = false;
|
|
float busVoltage = 0;
|
|
float shuntVoltage = 0;
|
|
float current = 0;
|
|
float power = 0;
|
|
|
|
void Inicializar() {
|
|
if (Iniciado) {
|
|
//EnviarDadosSerial(_ID + " ja inicializado");
|
|
EnviarDadosSerial(Mod_ID, MontarProtocoloSensor(sCFG, _ID, Iniciado ? "1" : "0"));
|
|
return;
|
|
}
|
|
if (_Endereco > 0x00) {
|
|
Wire.beginTransmission(_Endereco);
|
|
byte error = Wire.endTransmission();
|
|
if (error == 0) {
|
|
sINA219 = Adafruit_INA219(_Endereco);
|
|
Iniciado = sINA219.begin();
|
|
if (Iniciado) {
|
|
if (_A_Shunt == 3.2) {
|
|
sINA219.setCalibration_32V_2A();
|
|
}
|
|
else if (_A_Shunt == 50) {
|
|
sINA219.setCalibration_50A_75mV();
|
|
}
|
|
else if (_A_Shunt == 100) {
|
|
sINA219.setCalibration_100A_75mV();
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
Iniciado = false;
|
|
EnviarDadosSerial(Mod_ID, _ID + " nao iniciado, endereco " + _Endereco + " nao encontrado!");
|
|
}
|
|
}
|
|
|
|
EnviarDadosSerial(Mod_ID, MontarProtocoloSensor(sCFG, _ID, Iniciado ? "1" : "0"));
|
|
}
|
|
|
|
void Desligar() {
|
|
if (!Iniciado) {
|
|
//EnviarDadosSerial(_ID + " nao esta inicializado");
|
|
EnviarDadosSerial(Mod_ID, MontarProtocoloSensor(sCFG, _ID, Iniciado ? "1" : "0"));
|
|
return;
|
|
}
|
|
|
|
//EnviarDadosSerial(_ID + " Desligado");
|
|
|
|
Iniciado = false;
|
|
|
|
EnviarDadosSerial(Mod_ID, MontarProtocoloSensor(sCFG, _ID, Iniciado ? "1" : "0"));
|
|
}
|
|
|
|
void AferirCorrente() {
|
|
busVoltage = sINA219.getBusVoltage_V();
|
|
shuntVoltage = sINA219.getShuntVoltage_mV();
|
|
current = sINA219.getCurrent_mA();
|
|
power = sINA219.getPower_mW();
|
|
}
|
|
|
|
void RequisitarDados() {
|
|
AferirCorrente();
|
|
|
|
EnviarDadosSerial(Mod_ID, MontarProtocoloSensor(sCOR, _ID, MontarDadosProtocoloINA226(busVoltage, shuntVoltage, current, power)));
|
|
}
|
|
|
|
};
|
|
|
|
#endif |