UI enhancement in PWM Fan usermod.

This commit is contained in:
Blaz Kristan 2022-08-19 21:25:44 +02:00
parent 44a4b11d36
commit 5927332a5f

View File

@ -103,6 +103,7 @@ class PWMFanUsermod : public Usermod {
// https://randomnerdtutorials.com/esp32-pwm-arduino-ide/ // https://randomnerdtutorials.com/esp32-pwm-arduino-ide/
void initPWMfan(void) { void initPWMfan(void) {
if (pwmPin < 0 || !pinManager.allocatePin(pwmPin, true, PinOwner::UM_Unspecified)) { if (pwmPin < 0 || !pinManager.allocatePin(pwmPin, true, PinOwner::UM_Unspecified)) {
enabled = false;
pwmPin = -1; pwmPin = -1;
return; return;
} }
@ -218,11 +219,23 @@ class PWMFanUsermod : public Usermod {
* Below it is shown how this could be used for e.g. a light sensor * Below it is shown how this could be used for e.g. a light sensor
*/ */
void addToJsonInfo(JsonObject& root) { void addToJsonInfo(JsonObject& root) {
JsonObject user = root["u"];
if (user.isNull()) user = root.createNestedObject("u");
JsonArray infoArr = user.createNestedArray(FPSTR(_name));
String uiDomString = F("<button class=\"btn btn-xs\" onclick=\"requestJson({'");
uiDomString += FPSTR(_name);
uiDomString += F("':{'");
uiDomString += FPSTR(_enabled);
uiDomString += F("':");
uiDomString += enabled ? "false" : "true";
uiDomString += F("}});\"><i class=\"icons ");
uiDomString += enabled ? "on" : "off";
uiDomString += F("\">&#xe08f;</i></button>");
infoArr.add(uiDomString);
if (enabled) { if (enabled) {
JsonObject user = root["u"]; JsonArray infoArr = user.createNestedArray(F("Manual"));
if (user.isNull()) user = root.createNestedObject("u");
// if (!tempUM) {
JsonArray infoArr = user.createNestedArray(F("Fan speed [%]"));
String uiDomString = F("<div class=\"slider\"><div class=\"sliderwrap il\"><input class=\"noslide\" onchange=\"requestJson({'"); String uiDomString = F("<div class=\"slider\"><div class=\"sliderwrap il\"><input class=\"noslide\" onchange=\"requestJson({'");
uiDomString += FPSTR(_name); uiDomString += FPSTR(_name);
uiDomString += F("':{'"); uiDomString += F("':{'");
@ -231,13 +244,12 @@ class PWMFanUsermod : public Usermod {
uiDomString += pwmValuePct; uiDomString += pwmValuePct;
uiDomString += F(" /><div class=\"sliderdisplay\"></div></div></div>"); //<output class=\"sliderbubble\"></output> uiDomString += F(" /><div class=\"sliderdisplay\"></div></div></div>"); //<output class=\"sliderbubble\"></output>
infoArr.add(uiDomString); infoArr.add(uiDomString);
// }
JsonArray data = user.createNestedArray(F("Speed"));
if (tachoPin >= 0) { if (tachoPin >= 0) {
JsonArray data = user.createNestedArray(FPSTR(_name));
data.add(last_rpm); data.add(last_rpm);
data.add(F("rpm")); data.add(F("rpm"));
} else { } else {
JsonArray data = user.createNestedArray(FPSTR(_name));
if (lockFan) data.add(F("locked")); if (lockFan) data.add(F("locked"));
else data.add(F("auto")); else data.add(F("auto"));
} }
@ -256,14 +268,19 @@ class PWMFanUsermod : public Usermod {
* Values in the state object may be modified by connected clients * Values in the state object may be modified by connected clients
*/ */
void readFromJsonState(JsonObject& root) { void readFromJsonState(JsonObject& root) {
if (!initDone || !enabled) return; // prevent crash on boot applyPreset() if (!initDone) return; // prevent crash on boot applyPreset()
JsonObject usermod = root[FPSTR(_name)]; JsonObject usermod = root[FPSTR(_name)];
if (!usermod.isNull()) { if (!usermod.isNull()) {
if (!usermod[FPSTR(_speed)].isNull() && usermod[FPSTR(_speed)].is<int>()) { if (usermod[FPSTR(_enabled)].is<bool>()) {
int pwmValuePct = usermod[FPSTR(_speed)].as<int>(); enabled = usermod[FPSTR(_enabled)].as<bool>();
updateFanSpeed((MAX(0,MIN(100,pwmValuePct)) * 255) / 100); if (!enabled) updateFanSpeed(0);
} }
if (!usermod[FPSTR(_lock)].isNull() && usermod[FPSTR(_lock)].is<bool>()) { if (enabled && !usermod[FPSTR(_speed)].isNull() && usermod[FPSTR(_speed)].is<int>()) {
pwmValuePct = usermod[FPSTR(_speed)].as<int>();
updateFanSpeed((constrain(pwmValuePct,0,100) * 255) / 100);
if (pwmValuePct) lockFan = true;
}
if (enabled && !usermod[FPSTR(_lock)].isNull() && usermod[FPSTR(_lock)].is<bool>()) {
lockFan = usermod[FPSTR(_lock)].as<bool>(); lockFan = usermod[FPSTR(_lock)].as<bool>();
} }
} }