Skip to content

Commit

Permalink
VE.Direct: process frame end event only for valid frames
Browse files Browse the repository at this point in the history
save a parameters, save a level of indention, save a function call for
invalid frames.
  • Loading branch information
schlimmchen committed Oct 16, 2023
1 parent 8ab7a41 commit d7db1d9
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 34 deletions.
2 changes: 1 addition & 1 deletion lib/VeDirectFrameHandler/VeDirectFrameHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ void VeDirectFrameHandler::rxData(uint8_t inbyte)
if (_verboseLogging) { dumpDebugBuffer(); }
_checksum = 0;
_state = IDLE;
frameEndEvent(valid);
if (valid) { frameValidEvent(); }
break;
}
case RECORD_HEX:
Expand Down
2 changes: 1 addition & 1 deletion lib/VeDirectFrameHandler/VeDirectFrameHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class VeDirectFrameHandler {
void dumpDebugBuffer();
void rxData(uint8_t inbyte); // byte of serial data
virtual void textRxEvent(char *, char *) = 0;
virtual void frameEndEvent(bool) = 0; // copy temp struct to public struct
virtual void frameValidEvent() = 0;
int hexRxEvent(uint8_t);

std::unique_ptr<HardwareSerial> _vedirectSerial;
Expand Down
36 changes: 16 additions & 20 deletions lib/VeDirectFrameHandler/VeDirectMpptController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,30 +64,26 @@ void VeDirectMpptController::textRxEvent(char* name, char* value)
}

/*
* frameEndEvent
* This function is called at the end of the received frame. If the checksum is valid, the temp buffer is read line by line.
* If the name exists in the public buffer, the new value is copied to the public buffer. If not, a new name/value entry
* is created in the public buffer.
* frameValidEvent
* This function is called at the end of the received frame.
*/
void VeDirectMpptController::frameEndEvent(bool valid) {
if (valid) {
_tmpFrame.P = _tmpFrame.V * _tmpFrame.I;
void VeDirectMpptController::frameValidEvent() {
_tmpFrame.P = _tmpFrame.V * _tmpFrame.I;

_tmpFrame.IPV = 0;
if (_tmpFrame.VPV > 0) {
_tmpFrame.IPV = _tmpFrame.PPV / _tmpFrame.VPV;
}

_tmpFrame.E = 0;
if ( _tmpFrame.PPV > 0) {
_efficiency.addNumber(static_cast<double>(_tmpFrame.P * 100) / _tmpFrame.PPV);
_tmpFrame.E = _efficiency.getAverage();
}
_tmpFrame.IPV = 0;
if (_tmpFrame.VPV > 0) {
_tmpFrame.IPV = _tmpFrame.PPV / _tmpFrame.VPV;
}

_spData = std::make_shared<veMpptStruct>(_tmpFrame);
_tmpFrame = {};
_lastUpdate = millis();
_tmpFrame.E = 0;
if ( _tmpFrame.PPV > 0) {
_efficiency.addNumber(static_cast<double>(_tmpFrame.P * 100) / _tmpFrame.PPV);
_tmpFrame.E = _efficiency.getAverage();
}

_spData = std::make_shared<veMpptStruct>(_tmpFrame);
_tmpFrame = {};
_lastUpdate = millis();
}

/*
Expand Down
2 changes: 1 addition & 1 deletion lib/VeDirectFrameHandler/VeDirectMpptController.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class VeDirectMpptController : public VeDirectFrameHandler {

private:
void textRxEvent(char* name, char* value) final;
void frameEndEvent(bool) final;
void frameValidEvent() final;
spData_t _spData = nullptr;
veMpptStruct _tmpFrame{}; // private struct for received name and value pairs
MovingAverage<double, 5> _efficiency;
Expand Down
18 changes: 8 additions & 10 deletions lib/VeDirectFrameHandler/VeDirectShuntController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,16 @@ void VeDirectShuntController::textRxEvent(char* name, char* value)
}

/*
* frameEndEvent
* This function is called at the end of the received frame. If the checksum is valid, the temp buffer is read line by line.
* If the name exists in the public buffer, the new value is copied to the public buffer. If not, a new name/value entry
* is created in the public buffer.
* frameValidEvent
* This function is called at the end of the received frame.
*/
void VeDirectShuntController::frameEndEvent(bool valid) {
void VeDirectShuntController::frameValidEvent() {
// other than in the MPPT controller, the SmartShunt seems to split all data
// into two seperate messagesas. Thus we update veFrame only every second message
// after a value for PID has been received
if (valid && _tmpFrame.PID != 0) {
veFrame = _tmpFrame;
_tmpFrame = {};
_lastUpdate = millis();
}
if (_tmpFrame.PID == 0) { return; }

veFrame = _tmpFrame;
_tmpFrame = {};
_lastUpdate = millis();
}
2 changes: 1 addition & 1 deletion lib/VeDirectFrameHandler/VeDirectShuntController.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class VeDirectShuntController : public VeDirectFrameHandler {

private:
void textRxEvent(char * name, char * value) final;
void frameEndEvent(bool) final; // copy temp struct to public struct
void frameValidEvent() final;
veShuntStruct _tmpFrame{}; // private struct for received name and value pairs
};

Expand Down

0 comments on commit d7db1d9

Please sign in to comment.