Skip to content

Commit

Permalink
[hmc] Add pidint event support
Browse files Browse the repository at this point in the history
  • Loading branch information
CapnBry committed Feb 23, 2021
1 parent d346e02 commit f869dbb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
38 changes: 37 additions & 1 deletion arduino/libraries/HeaterMeterClient/src/HeaterMeterClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,24 @@ void HeaterMeterClientProbe::clear(void)
HasTemperatureDph = false;
}

void HeaterMeterClientPidInternals::clear(void)
{
LastUpdate = 0;

P = 0.0f;
I = 0.0f;
D = 0.0f;
dT = 0.0f;
}

void HeaterMeterClientPidOutput::clear(void)
{
Enabled = false;
Current = 0;
Fan = 0;
Servo = 0;

Internals.clear();
}

void HeaterMeterClientPid::clear(void)
Expand Down Expand Up @@ -154,20 +167,43 @@ void HeaterMeterClient::handleHmStatus(char* data)
DeserializationError err = deserializeJson(doc, data);
if (err)
{
Serial.print(F("Could not deserialize JSON event: "));
Serial.print(F("Could not deserialize hmstatus event: "));
Serial.println(err.c_str());
return;
}

updateProxyFromJson(doc);
}

void HeaterMeterClient::handlePidInt(char* data)
{
StaticJsonDocument<128> doc;
DeserializationError err = deserializeJson(doc, data);
if (err)
{
Serial.print(F("Could not deserialize pidint event: "));
Serial.println(err.c_str());
return;
}

state.Output.Internals.LastUpdate = millis();
state.Output.Internals.P = doc["p"];
state.Output.Internals.I = doc["i"];
state.Output.Internals.D = doc["d"];
state.Output.Internals.dT = doc["t"];

if (onPidInt)
onPidInt();
}

void HeaterMeterClient::handleServerSentEvent(char* data)
{
//Serial.print(_eventType); Serial.write('='); Serial.print(data);

if (strcmp(_eventType, "hmstatus") == 0)
handleHmStatus(data);
else if (strcmp(_eventType, "pidint") == 0)
handlePidInt(data);
else
{
//Serial.print(F("Unhandled server-sent event type: "));
Expand Down
15 changes: 15 additions & 0 deletions arduino/libraries/HeaterMeterClient/src/HeaterMeterClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,25 @@ struct HeaterMeterClientProbe
void clear(void);
};

struct HeaterMeterClientPidInternals
{
uint32_t LastUpdate;
float P;
float I;
float D;
float dT;

void clear();
bool valid() { return ((LastUpdate != 0) && (millis() - LastUpdate < 3000)); }
};

struct HeaterMeterClientPidOutput
{
bool Enabled; // false if "Off"
uint8_t Current;
uint8_t Fan;
uint8_t Servo;
struct HeaterMeterClientPidInternals Internals;

void clear(void);
};
Expand Down Expand Up @@ -63,6 +76,7 @@ class HeaterMeterClient
std::function<void(void)> onConnect;
std::function<void(void)> onDisconnect;
std::function<void(void)> onHmStatus;
std::function<void(void)> onPidInt;
std::function<void(err_t)> onError;
std::function<void(HmclientProtocolState)> onProtocolStateChange;

Expand All @@ -80,6 +94,7 @@ class HeaterMeterClient
bool readLine(char** pos, size_t* len);
void updateProxyFromJson(JsonDocument& doc);
void handleHmStatus(char* data);
void handlePidInt(char* data);
void handleServerSentEvent(char* data);
void handleServerSentLine(void);
void clientConnect(void);
Expand Down

0 comments on commit f869dbb

Please sign in to comment.