Skip to content

Commit

Permalink
powermeter refactor: SML lib: replace double by float
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
schlimmchen committed Jun 26, 2024
1 parent 6108d24 commit 75c07c1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
2 changes: 1 addition & 1 deletion include/PowerMeterSml.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PowerMeterSml : public PowerMeterProvider {

typedef struct {
uint8_t const OBIS[6];
void (*decoder)(double&);
void (*decoder)(float&);
float* target;
char const* name;
} OBISHandler;
Expand Down
10 changes: 5 additions & 5 deletions lib/SMLParser/sml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
while (scaler++) {
Expand Down Expand Up @@ -372,31 +372,31 @@ 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;
smlOBISByUnit(val, sc, SML_WATT_HOUR);
wh = val;
smlPow(wh, sc);
}

void smlOBISW(double &w)
void smlOBISW(float &w)
{
long long int val;
smlOBISByUnit(val, sc, SML_WATT);
w = val;
smlPow(w, sc);
}

void smlOBISVolt(double &v)
void smlOBISVolt(float &v)
{
long long int val;
smlOBISByUnit(val, sc, SML_VOLT);
v = val;
smlPow(v, sc);
}

void smlOBISAmpere(double &a)
void smlOBISAmpere(float &a)
{
long long int val;
smlOBISByUnit(val, sc, SML_AMPERE);
Expand Down
9 changes: 4 additions & 5 deletions lib/SMLParser/sml.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,9 @@ bool smlOBISCheck(const unsigned char *obis);
void smlOBISManufacturer(unsigned char *str, int maxSize);
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(double &wh);
void smlOBISW(double &w);
void smlOBISVolt(double &v);
void smlOBISAmpere(double &a);
void smlOBISWh(float &wh);
void smlOBISW(float &w);
void smlOBISVolt(float &v);
void smlOBISAmpere(float &a);

#endif
13 changes: 6 additions & 7 deletions src/PowerMeterSml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ void PowerMeterSml::processSmlByte(uint8_t byte)
for (auto& handler: smlHandlerList) {
if (!smlOBISCheck(handler.OBIS)) { continue; }

double helper;
handler.decoder(helper);
gotUpdate();

std::lock_guard<std::mutex> l(_mutex);
*handler.target = helper;
gotUpdate();
handler.decoder(*handler.target);

if (!_verboseLogging) { continue; }
MessageOutput.printf("[PowerMeterSml] decoded %s to %.2f\r\n",
handler.name, helper);
if (_verboseLogging) {
MessageOutput.printf("[PowerMeterSml] decoded %s to %.2f\r\n",
handler.name, *handler.target);
}
}
break;
default:
Expand Down

0 comments on commit 75c07c1

Please sign in to comment.