powermeter refactor: SML lib: replace double by float

avoid additional conversions and avoid double for the fact that
calculations on type double are implemented in software, whereas
float is handled in hardware on ESP32.
This commit is contained in:
Bernhard Kirchen 2024-05-09 21:46:59 +02:00
parent 6108d24795
commit 75c07c17f2
4 changed files with 17 additions and 19 deletions

View File

@ -27,7 +27,7 @@ private:
typedef struct { typedef struct {
uint8_t const OBIS[6]; uint8_t const OBIS[6];
void (*decoder)(double&); void (*decoder)(float&);
float* target; float* target;
char const* name; char const* name;
} OBISHandler; } OBISHandler;

View File

@ -317,7 +317,7 @@ void smlOBISManufacturer(unsigned char *str, int maxSize)
} }
} }
void smlPow(double &val, signed char &scaler) void smlPow(float &val, signed char &scaler)
{ {
if (scaler < 0) { if (scaler < 0) {
while (scaler++) { while (scaler++) {
@ -372,7 +372,7 @@ void smlOBISByUnit(long long int &val, signed char &scaler, sml_units_t unit)
} }
} }
void smlOBISWh(double &wh) void smlOBISWh(float &wh)
{ {
long long int val; long long int val;
smlOBISByUnit(val, sc, SML_WATT_HOUR); smlOBISByUnit(val, sc, SML_WATT_HOUR);
@ -380,7 +380,7 @@ void smlOBISWh(double &wh)
smlPow(wh, sc); smlPow(wh, sc);
} }
void smlOBISW(double &w) void smlOBISW(float &w)
{ {
long long int val; long long int val;
smlOBISByUnit(val, sc, SML_WATT); smlOBISByUnit(val, sc, SML_WATT);
@ -388,7 +388,7 @@ void smlOBISW(double &w)
smlPow(w, sc); smlPow(w, sc);
} }
void smlOBISVolt(double &v) void smlOBISVolt(float &v)
{ {
long long int val; long long int val;
smlOBISByUnit(val, sc, SML_VOLT); smlOBISByUnit(val, sc, SML_VOLT);
@ -396,7 +396,7 @@ void smlOBISVolt(double &v)
smlPow(v, sc); smlPow(v, sc);
} }
void smlOBISAmpere(double &a) void smlOBISAmpere(float &a)
{ {
long long int val; long long int val;
smlOBISByUnit(val, sc, SML_AMPERE); smlOBISByUnit(val, sc, SML_AMPERE);

View File

@ -97,10 +97,9 @@ bool smlOBISCheck(const unsigned char *obis);
void smlOBISManufacturer(unsigned char *str, int maxSize); void smlOBISManufacturer(unsigned char *str, int maxSize);
void smlOBISByUnit(long long int &wh, signed char &scaler, sml_units_t unit); void smlOBISByUnit(long long int &wh, signed char &scaler, sml_units_t unit);
// Be aware that double on Arduino UNO is just 32 bit void smlOBISWh(float &wh);
void smlOBISWh(double &wh); void smlOBISW(float &w);
void smlOBISW(double &w); void smlOBISVolt(float &v);
void smlOBISVolt(double &v); void smlOBISAmpere(float &a);
void smlOBISAmpere(double &a);
#endif #endif

View File

@ -22,16 +22,15 @@ void PowerMeterSml::processSmlByte(uint8_t byte)
for (auto& handler: smlHandlerList) { for (auto& handler: smlHandlerList) {
if (!smlOBISCheck(handler.OBIS)) { continue; } if (!smlOBISCheck(handler.OBIS)) { continue; }
double helper;
handler.decoder(helper);
std::lock_guard<std::mutex> l(_mutex);
*handler.target = helper;
gotUpdate(); gotUpdate();
if (!_verboseLogging) { continue; } std::lock_guard<std::mutex> l(_mutex);
handler.decoder(*handler.target);
if (_verboseLogging) {
MessageOutput.printf("[PowerMeterSml] decoded %s to %.2f\r\n", MessageOutput.printf("[PowerMeterSml] decoded %s to %.2f\r\n",
handler.name, helper); handler.name, *handler.target);
}
} }
break; break;
default: default: