ESP32_ChinaDieselHeater_Con.../Arduino/SenderTrial2/TxManage.cpp

107 lines
2.1 KiB
C++
Raw Normal View History

#include <Arduino.h>
#include "TxManage.h"
extern void SerialReport(const char* hdr, const unsigned char* pData, const char* ftr);
CTxManage::CTxManage(int TxEnbPin, USARTClass& serial) :
m_Serial(serial),
2018-09-23 09:15:49 +00:00
m_Frame(CProtocol::CtrlMode)
{
m_bOnReq = false;
m_bOffReq = false;
m_bTxPending = false;
m_nStartTime = 0;
m_nTxEnbPin = TxEnbPin;
}
void CTxManage::begin()
{
pinMode(m_nTxEnbPin, OUTPUT);
digitalWrite(m_nTxEnbPin, LOW);
}
void
CTxManage::RequestOn()
{
m_bOnReq = true;
}
void
CTxManage::RequestOff()
{
m_bOffReq = true;
}
2018-09-23 08:59:19 +00:00
void
2018-09-23 09:15:49 +00:00
CTxManage::Start(const CProtocol& ref, unsigned long timenow, bool self)
{
m_Frame = ref;
2018-09-23 08:59:19 +00:00
// 0x78 prevents the controller showing bum information when we parrot the OEM controller
// heater is happy either way, the OEM controller has set the max/min stuff already
m_Frame.Data[0] = self ? 0x76 : 0x78;
if(timenow == 0)
timenow++;
m_nStartTime = timenow;
m_bTxPending = true;
}
2018-09-23 08:59:19 +00:00
bool
CTxManage::CheckTx(unsigned long timenow)
{
if(m_nStartTime) {
long diff = timenow - m_nStartTime;
if(diff > m_nStartDelay) {
// begin front porch of Tx gating pulse
digitalWrite(m_nTxEnbPin, HIGH);
}
if(m_bTxPending && (diff > (m_nStartDelay + m_nFrontPorch))) {
// begin serial transmission
m_bTxPending = false;
_send();
}
if(diff > (m_nStartDelay + m_nFrameTime)) {
// conclude Tx gating
m_nStartTime = 0;
digitalWrite(m_nTxEnbPin, LOW);
}
}
2018-09-23 08:59:19 +00:00
return m_nStartTime == 0;
}
void
CTxManage::_send()
{
2018-09-23 08:59:19 +00:00
// install on/off commands if required
if(m_bOnReq) {
m_bOnReq = false;
2018-09-23 08:59:19 +00:00
m_Frame.Controller.Command = 0xa0;
}
else if(m_bOffReq) {
m_bOffReq = false;
2018-09-23 08:59:19 +00:00
m_Frame.Controller.Command = 0x05;
}
else {
2018-09-23 08:59:19 +00:00
m_Frame.Controller.Command = 0x00;
}
2018-09-23 08:59:19 +00:00
// ensure CRC valid
m_Frame.setCRC();
// send to heater - using binary
for(int i=0; i<24; i++) {
m_Serial.write(m_Frame.Data[i]); // write native binary values
}
Report();
}
void
CTxManage::Report()
{
SerialReport("Self ", m_Frame.Data, " ");
}