Skip to content

Commit

Permalink
sk
Browse files Browse the repository at this point in the history
  • Loading branch information
helgeerbe committed Aug 19, 2022
1 parent b8ffa37 commit 7140574
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 33 deletions.
52 changes: 28 additions & 24 deletions lib/VeDirectFrameHandler/VeDirectFrameHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ char MODULE[] = "VE.Frame"; // Victron seems to use this to find out where loggi
// The name of the record that contains the checksum.
static constexpr char checksumTagName[] = "CHECKSUM";

HardwareSerial VedirectSerial(1);

VeDirectFrameHandler VeDirect;

VeDirectFrameHandler::VeDirectFrameHandler() :
Expand All @@ -51,32 +53,35 @@ VeDirectFrameHandler::VeDirectFrameHandler() :
veEnd(0),
mState(IDLE),
mChecksum(0),
mTextPointer(0),
idx(0),
tempName(),
tempValue()
tempValue(),
_pollInterval(5),
_lastPoll(0)
{
}

void VeDirectFrameHandler::init()
{
Serial2.begin(19200, SERIAL_8N1, VICTRON_PIN_RX, VICTRON_PIN_TX);
Serial2.flush();
VedirectSerial.begin(19200, SERIAL_8N1, VICTRON_PIN_RX, VICTRON_PIN_TX);
VedirectSerial.flush();
}

void VeDirectFrameHandler::setPollInterval(uint32_t interval)
void VeDirectFrameHandler::setPollInterval(unsigned long interval)
{
_pollInterval = interval;
}

void VeDirectFrameHandler::loop()
{
unsigned long now = millis();
_frameEnd = false;
polltime = _pollInterval;

if ((millis() - getLastUpdate()) > (_pollInterval * 1000)) {
while ( Serial2.available() && (!_frameEnd) && (millis() - now < 500)) {
rxData(Serial2.read());
}
if ((millis() - getLastUpdate()) < _pollInterval * 1000) {
return;
}

while ( VedirectSerial.available()) {
rxData(VedirectSerial.read());
}
}

Expand Down Expand Up @@ -109,29 +114,29 @@ void VeDirectFrameHandler::rxData(uint8_t inbyte)
}
break;
case RECORD_BEGIN:
mTextPointer = mName;
*mTextPointer++ = inbyte;
idx = 0;
mName[idx++] = inbyte;
mState = RECORD_NAME;
break;
case RECORD_NAME:
// The record name is being received, terminated by a \t
switch(inbyte) {
case '\t':
// the Checksum record indicates a EOR
if ( mTextPointer < (mName + sizeof(mName)) ) {
*mTextPointer = 0; /* Zero terminate */
if ( idx < sizeof(mName) ) {
mName[idx] = 0; /* Zero terminate */
if (strcmp(mName, checksumTagName) == 0) {
mState = CHECKSUM;
break;
}
}
mTextPointer = mValue; /* Reset value pointer */
idx = 0; /* Reset value pointer */
mState = RECORD_VALUE;
break;
default:
// add byte to name, but do no overflow
if ( mTextPointer < (mName + sizeof(mName)) )
*mTextPointer++ = inbyte;
if ( idx < sizeof(mName) )
mName[idx++] = inbyte;
break;
}
break;
Expand All @@ -140,8 +145,8 @@ void VeDirectFrameHandler::rxData(uint8_t inbyte)
switch(inbyte) {
case '\n':
// forward record, only if it could be stored completely
if ( mTextPointer < (mValue + sizeof(mValue)) ) {
*mTextPointer = 0; // make zero ended
if ( idx < sizeof(mValue) ) {
mValue[idx] = 0; // make zero ended
textRxEvent(mName, mValue);
}
mState = RECORD_BEGIN;
Expand All @@ -150,8 +155,8 @@ void VeDirectFrameHandler::rxData(uint8_t inbyte)
break;
default:
// add byte to value, but do no overflow
if ( mTextPointer < (mValue + sizeof(mValue)) )
*mTextPointer++ = inbyte;
if ( idx < sizeof(mValue) )
mValue[idx++] = inbyte;
break;
}
break;
Expand Down Expand Up @@ -213,7 +218,6 @@ void VeDirectFrameHandler::frameEndEvent(bool valid) {
setLastUpdate();
}
frameIndex = 0; // reset frame
_frameEnd = true;
}

/*
Expand All @@ -236,7 +240,7 @@ bool VeDirectFrameHandler::hexRxEvent(uint8_t inbyte) {
return true; // stubbed out for future
}

uint32_t VeDirectFrameHandler::getLastUpdate()
unsigned long VeDirectFrameHandler::getLastUpdate()
{
return _lastPoll;
}
Expand Down
14 changes: 8 additions & 6 deletions lib/VeDirectFrameHandler/VeDirectFrameHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ const byte buffLen = 40; // Maximum number of lines possi
#define VICTRON_PIN_RX 22
#endif



class VeDirectFrameHandler {

public:

VeDirectFrameHandler();
void init();
void setPollInterval(uint32_t interval);
void setPollInterval(unsigned long interval);
void loop();
uint32_t getLastUpdate();
unsigned long getLastUpdate();
void setLastUpdate();
String getPidAsString(const char* pid);
String getCsAsString(const char* pid);
Expand All @@ -45,6 +47,7 @@ class VeDirectFrameHandler {

int frameIndex; // which line of the frame are we on
int veEnd; // current size (end) of the public buffer
unsigned long polltime=0;

private:
//bool mStop; // not sure what Victron uses this for, not using
Expand All @@ -62,7 +65,7 @@ class VeDirectFrameHandler {

uint8_t mChecksum; // checksum value

char * mTextPointer; // pointer to the private buffer we're writing to, name or value
int idx; // index to the private buffer we're writing to, name or value

char mName[9]; // buffer for the field name
char mValue[33]; // buffer for the field value
Expand All @@ -74,9 +77,8 @@ class VeDirectFrameHandler {
void frameEndEvent(bool);
void logE(const char *, const char *);
bool hexRxEvent(uint8_t);
uint32_t _pollInterval;
uint32_t _lastPoll = 0;
bool _frameEnd = false;
unsigned long _pollInterval;
unsigned long _lastPoll;
};

extern VeDirectFrameHandler VeDirect;
Expand Down
7 changes: 4 additions & 3 deletions src/WebApi_ws_vedirect_live.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void WebApiWsVedirectLiveClass::loop()
// Update on ve.direct change or at least after 10 seconds
if (millis() - _lastWsPublish > (10 * 1000) || (maxTimeStamp != _newestVedirectTimestamp)) {

DynamicJsonDocument root(40960);
DynamicJsonDocument root(1024);
JsonVariant var = root;
generateJsonResponse(var);

Expand Down Expand Up @@ -111,7 +111,8 @@ void WebApiWsVedirectLiveClass::generateJsonResponse(JsonVariant& root)
root[F(VeDirect.veName[i])] = VeDirect.veValue[i];
}
}

root[F("polltime")] = VeDirect.polltime;
root[F("VE.end")] = VeDirect.veEnd;
if (VeDirect.getLastUpdate() > _newestVedirectTimestamp) {
_newestVedirectTimestamp = VeDirect.getLastUpdate();
}
Expand All @@ -132,7 +133,7 @@ void WebApiWsVedirectLiveClass::onWebsocketEvent(AsyncWebSocket* server, AsyncWe

void WebApiWsVedirectLiveClass::onLivedataStatus(AsyncWebServerRequest* request)
{
AsyncJsonResponse* response = new AsyncJsonResponse(false, 40960U);
AsyncJsonResponse* response = new AsyncJsonResponse(false, 1024U);
JsonVariant root = response->getRoot().as<JsonVariant>();
generateJsonResponse(root);

Expand Down

0 comments on commit 7140574

Please sign in to comment.