Hoymiles Lib: Migrate byteAssign array to std::list

This commit is contained in:
Thomas Basler 2023-01-29 11:46:50 +01:00
parent 7b9d870cce
commit 6b36369b06
10 changed files with 45 additions and 73 deletions

View File

@ -32,12 +32,7 @@ String HM_1CH::typeName()
return F("HM-300, HM-350, HM-400"); return F("HM-300, HM-350, HM-400");
} }
const byteAssign_t* HM_1CH::getByteAssignment() const std::list<byteAssign_t>* HM_1CH::getByteAssignment()
{ {
return byteAssignment; return &byteAssignment;
}
uint8_t HM_1CH::getAssignmentCount()
{
return sizeof(byteAssignment) / sizeof(byteAssign_t);
} }

View File

@ -2,17 +2,17 @@
#pragma once #pragma once
#include "HM_Abstract.h" #include "HM_Abstract.h"
#include <list>
class HM_1CH : public HM_Abstract { class HM_1CH : public HM_Abstract {
public: public:
explicit HM_1CH(uint64_t serial); explicit HM_1CH(uint64_t serial);
static bool isValidSerial(uint64_t serial); static bool isValidSerial(uint64_t serial);
String typeName(); String typeName();
const byteAssign_t* getByteAssignment(); const std::list<byteAssign_t>* getByteAssignment();
uint8_t getAssignmentCount();
private: private:
const byteAssign_t byteAssignment[18] = { const std::list<byteAssign_t> byteAssignment = {
{ CH1, FLD_UDC, UNIT_V, 2, 2, 10, false, 1 }, { CH1, FLD_UDC, UNIT_V, 2, 2, 10, false, 1 },
{ CH1, FLD_IDC, UNIT_A, 4, 2, 100, false, 2 }, { CH1, FLD_IDC, UNIT_A, 4, 2, 100, false, 2 },
{ CH1, FLD_PDC, UNIT_W, 6, 2, 10, false, 1 }, { CH1, FLD_PDC, UNIT_W, 6, 2, 10, false, 1 },

View File

@ -33,12 +33,7 @@ String HM_2CH::typeName()
return F("HM-600, HM-700, HM-800"); return F("HM-600, HM-700, HM-800");
} }
const byteAssign_t* HM_2CH::getByteAssignment() const std::list<byteAssign_t>* HM_2CH::getByteAssignment()
{ {
return byteAssignment; return &byteAssignment;
}
uint8_t HM_2CH::getAssignmentCount()
{
return sizeof(byteAssignment) / sizeof(byteAssign_t);
} }

View File

@ -8,11 +8,10 @@ public:
explicit HM_2CH(uint64_t serial); explicit HM_2CH(uint64_t serial);
static bool isValidSerial(uint64_t serial); static bool isValidSerial(uint64_t serial);
String typeName(); String typeName();
const byteAssign_t* getByteAssignment(); const std::list<byteAssign_t>* getByteAssignment();
uint8_t getAssignmentCount();
private: private:
const byteAssign_t byteAssignment[24] = { const std::list<byteAssign_t> byteAssignment = {
{ CH1, FLD_UDC, UNIT_V, 2, 2, 10, false, 1 }, { CH1, FLD_UDC, UNIT_V, 2, 2, 10, false, 1 },
{ CH1, FLD_IDC, UNIT_A, 4, 2, 100, false, 2 }, { CH1, FLD_IDC, UNIT_A, 4, 2, 100, false, 2 },
{ CH1, FLD_PDC, UNIT_W, 6, 2, 10, false, 1 }, { CH1, FLD_PDC, UNIT_W, 6, 2, 10, false, 1 },

View File

@ -32,12 +32,7 @@ String HM_4CH::typeName()
return F("HM-1000, HM-1200, HM-1500"); return F("HM-1000, HM-1200, HM-1500");
} }
const byteAssign_t* HM_4CH::getByteAssignment() const std::list<byteAssign_t>* HM_4CH::getByteAssignment()
{ {
return byteAssignment; return &byteAssignment;
}
uint8_t HM_4CH::getAssignmentCount()
{
return sizeof(byteAssignment) / sizeof(byteAssign_t);
} }

View File

@ -8,11 +8,10 @@ public:
explicit HM_4CH(uint64_t serial); explicit HM_4CH(uint64_t serial);
static bool isValidSerial(uint64_t serial); static bool isValidSerial(uint64_t serial);
String typeName(); String typeName();
const byteAssign_t* getByteAssignment(); const std::list<byteAssign_t>* getByteAssignment();
uint8_t getAssignmentCount();
private: private:
const byteAssign_t byteAssignment[36] = { const std::list<byteAssign_t> byteAssignment = {
{ CH1, FLD_UDC, UNIT_V, 2, 2, 10, false, 1 }, { CH1, FLD_UDC, UNIT_V, 2, 2, 10, false, 1 },
{ CH1, FLD_IDC, UNIT_A, 4, 2, 100, false, 2 }, { CH1, FLD_IDC, UNIT_A, 4, 2, 100, false, 2 },
{ CH1, FLD_PDC, UNIT_W, 8, 2, 10, false, 1 }, { CH1, FLD_PDC, UNIT_W, 8, 2, 10, false, 1 },

View File

@ -30,7 +30,7 @@ void InverterAbstract::init()
// Not possible in constructor --> virtual function // Not possible in constructor --> virtual function
// Not possible in verifyAllFragments --> Because no data if nothing is ever received // Not possible in verifyAllFragments --> Because no data if nothing is ever received
// It has to be executed because otherwise the getChannelCount method in stats always returns 0 // It has to be executed because otherwise the getChannelCount method in stats always returns 0
_statisticsParser.get()->setByteAssignment(getByteAssignment(), getAssignmentCount()); _statisticsParser.get()->setByteAssignment(getByteAssignment());
} }
uint64_t InverterAbstract::serial() uint64_t InverterAbstract::serial()

View File

@ -11,6 +11,7 @@
#include "types.h" #include "types.h"
#include <Arduino.h> #include <Arduino.h>
#include <cstdint> #include <cstdint>
#include <list>
#define MAX_NAME_LENGTH 32 #define MAX_NAME_LENGTH 32
@ -38,8 +39,7 @@ public:
void setName(const char* name); void setName(const char* name);
const char* name(); const char* name();
virtual String typeName() = 0; virtual String typeName() = 0;
virtual const byteAssign_t* getByteAssignment() = 0; virtual const std::list<byteAssign_t>* getByteAssignment() = 0;
virtual uint8_t getAssignmentCount() = 0;
bool isProducing(); bool isProducing();
bool isReachable(); bool isReachable();

View File

@ -28,10 +28,9 @@ const calcFunc_t calcFunctions[] = {
{ CALC_IRR_CH, &calcIrradiation } { CALC_IRR_CH, &calcIrradiation }
}; };
void StatisticsParser::setByteAssignment(const byteAssign_t* byteAssignment, const uint8_t count) void StatisticsParser::setByteAssignment(const std::list<byteAssign_t>* byteAssignment)
{ {
_byteAssignment = byteAssignment; _byteAssignment = byteAssignment;
_byteAssignmentCount = count;
} }
void StatisticsParser::clearBuffer() void StatisticsParser::clearBuffer()
@ -50,31 +49,26 @@ void StatisticsParser::appendFragment(uint8_t offset, uint8_t* payload, uint8_t
_statisticLength += len; _statisticLength += len;
} }
uint8_t StatisticsParser::getAssignIdxByChannelField(uint8_t channel, uint8_t fieldId) const byteAssign_t* StatisticsParser::getAssignmentByChannelField(uint8_t channel, uint8_t fieldId)
{ {
const byteAssign_t* b = _byteAssignment; for (auto const& i : *_byteAssignment) {
if (i.ch == channel && i.fieldId == fieldId) {
uint8_t pos; return &i;
for (pos = 0; pos < _byteAssignmentCount; pos++) {
if (b[pos].ch == channel && b[pos].fieldId == fieldId) {
return pos;
} }
} }
return 0xff; return NULL;
} }
float StatisticsParser::getChannelFieldValue(uint8_t channel, uint8_t fieldId) float StatisticsParser::getChannelFieldValue(uint8_t channel, uint8_t fieldId)
{ {
uint8_t pos = getAssignIdxByChannelField(channel, fieldId); const byteAssign_t* pos = getAssignmentByChannelField(channel, fieldId);
if (pos == 0xff) { if (pos == NULL) {
return 0; return 0;
} }
const byteAssign_t* b = _byteAssignment; uint8_t ptr = pos->start;
uint8_t end = ptr + pos->num;
uint8_t ptr = b[pos].start; uint16_t div = pos->div;
uint8_t end = ptr + b[pos].num;
uint16_t div = b[pos].div;
if (CMD_CALC != div) { if (CMD_CALC != div) {
// Value is a static value // Value is a static value
@ -85,9 +79,9 @@ float StatisticsParser::getChannelFieldValue(uint8_t channel, uint8_t fieldId)
} while (++ptr != end); } while (++ptr != end);
float result; float result;
if (b[pos].isSigned && b[pos].num == 2) { if (pos->isSigned && pos->num == 2) {
result = static_cast<float>(static_cast<int16_t>(val)); result = static_cast<float>(static_cast<int16_t>(val));
} else if (b[pos].isSigned && b[pos].num == 4) { } else if (pos->isSigned && pos->num == 4) {
result = static_cast<float>(static_cast<int32_t>(val)); result = static_cast<float>(static_cast<int32_t>(val));
} else { } else {
result = static_cast<float>(val); result = static_cast<float>(val);
@ -97,7 +91,7 @@ float StatisticsParser::getChannelFieldValue(uint8_t channel, uint8_t fieldId)
return result; return result;
} else { } else {
// Value has to be calculated // Value has to be calculated
return calcFunctions[b[pos].start].func(this, b[pos].num); return calcFunctions[pos->start].func(this, pos->num);
} }
return 0; return 0;
@ -105,39 +99,34 @@ float StatisticsParser::getChannelFieldValue(uint8_t channel, uint8_t fieldId)
bool StatisticsParser::hasChannelFieldValue(uint8_t channel, uint8_t fieldId) bool StatisticsParser::hasChannelFieldValue(uint8_t channel, uint8_t fieldId)
{ {
uint8_t pos = getAssignIdxByChannelField(channel, fieldId); const byteAssign_t* pos = getAssignmentByChannelField(channel, fieldId);
return pos != 0xff; return pos != NULL;
} }
const char* StatisticsParser::getChannelFieldUnit(uint8_t channel, uint8_t fieldId) const char* StatisticsParser::getChannelFieldUnit(uint8_t channel, uint8_t fieldId)
{ {
uint8_t pos = getAssignIdxByChannelField(channel, fieldId); const byteAssign_t* pos = getAssignmentByChannelField(channel, fieldId);
const byteAssign_t* b = _byteAssignment; return units[pos->unitId];
return units[b[pos].unitId];
} }
const char* StatisticsParser::getChannelFieldName(uint8_t channel, uint8_t fieldId) const char* StatisticsParser::getChannelFieldName(uint8_t channel, uint8_t fieldId)
{ {
uint8_t pos = getAssignIdxByChannelField(channel, fieldId); const byteAssign_t* pos = getAssignmentByChannelField(channel, fieldId);
const byteAssign_t* b = _byteAssignment; return fields[pos->fieldId];
return fields[b[pos].fieldId];
} }
uint8_t StatisticsParser::getChannelFieldDigits(uint8_t channel, uint8_t fieldId) uint8_t StatisticsParser::getChannelFieldDigits(uint8_t channel, uint8_t fieldId)
{ {
uint8_t pos = getAssignIdxByChannelField(channel, fieldId); const byteAssign_t* pos = getAssignmentByChannelField(channel, fieldId);
return _byteAssignment[pos].digits; return pos->digits;
} }
uint8_t StatisticsParser::getChannelCount() uint8_t StatisticsParser::getChannelCount()
{ {
const byteAssign_t* b = _byteAssignment;
uint8_t cnt = 0; uint8_t cnt = 0;
for (uint8_t pos = 0; pos < _byteAssignmentCount; pos++) { for (auto const &b: *_byteAssignment) {
if (b[pos].ch > cnt) { if (b.ch > cnt) {
cnt = b[pos].ch; cnt = b.ch;
} }
} }

View File

@ -3,6 +3,7 @@
#include "Parser.h" #include "Parser.h"
#include <Arduino.h> #include <Arduino.h>
#include <cstdint> #include <cstdint>
#include <list>
#define STATISTIC_PACKET_SIZE (4 * 16) #define STATISTIC_PACKET_SIZE (4 * 16)
@ -78,9 +79,9 @@ public:
void clearBuffer(); void clearBuffer();
void appendFragment(uint8_t offset, uint8_t* payload, uint8_t len); void appendFragment(uint8_t offset, uint8_t* payload, uint8_t len);
void setByteAssignment(const byteAssign_t* byteAssignment, const uint8_t count); void setByteAssignment(const std::list<byteAssign_t>* byteAssignment);
uint8_t getAssignIdxByChannelField(uint8_t channel, uint8_t fieldId); const byteAssign_t* getAssignmentByChannelField(uint8_t channel, uint8_t fieldId);
float getChannelFieldValue(uint8_t channel, uint8_t fieldId); float getChannelFieldValue(uint8_t channel, uint8_t fieldId);
bool hasChannelFieldValue(uint8_t channel, uint8_t fieldId); bool hasChannelFieldValue(uint8_t channel, uint8_t fieldId);
const char* getChannelFieldUnit(uint8_t channel, uint8_t fieldId); const char* getChannelFieldUnit(uint8_t channel, uint8_t fieldId);
@ -101,8 +102,7 @@ private:
uint8_t _statisticLength = 0; uint8_t _statisticLength = 0;
uint16_t _chanMaxPower[CH4]; uint16_t _chanMaxPower[CH4];
const byteAssign_t* _byteAssignment; const std::list<byteAssign_t>* _byteAssignment;
uint8_t _byteAssignmentCount;
uint32_t _rxFailureCount = 0; uint32_t _rxFailureCount = 0;
}; };