From 444a2b5d0379db3e58a303b1132b6992592fcc35 Mon Sep 17 00:00:00 2001 From: fvanroie Date: Thu, 9 Apr 2020 18:36:23 +0200 Subject: [PATCH] Fix PWM bug --- src/hasp.cpp | 33 ++++++++++++++------ src/hasp_gui.cpp | 74 ++++++++++++++++++++++++++++++++++----------- src/hasp_http.cpp | 2 +- src/hasp_mdns.cpp | 7 +++-- src/hasp_mqtt.cpp | 39 ++++++++++++++++++------ src/hasp_telnet.cpp | 28 +++++++++++------ src/hasp_wifi.cpp | 2 +- 7 files changed, 135 insertions(+), 50 deletions(-) diff --git a/src/hasp.cpp b/src/hasp.cpp index 5ce6f8ac..6ad81537 100644 --- a/src/hasp.cpp +++ b/src/hasp.cpp @@ -359,8 +359,8 @@ void haspSetup() { guiSetDim(haspStartDim); - // lv_coord_t hres = lv_disp_get_hor_res(NULL); - // lv_coord_t vres = lv_disp_get_ver_res(NULL); + // lv_coord_t hres = lv_disp_get_hor_res(NULL); + // lv_coord_t vres = lv_disp_get_ver_res(NULL); // static lv_font_t * // my_font = (lv_font_t *)lv_mem_alloc(sizeof(lv_font_t)); @@ -967,15 +967,28 @@ void haspLoadPage(String pages) bool haspGetConfig(const JsonObject & settings) { - settings[FPSTR(F_CONFIG_STARTPAGE)] = haspStartPage; - settings[FPSTR(F_CONFIG_STARTDIM)] = haspStartDim; - settings[FPSTR(F_CONFIG_THEME)] = haspThemeId; - settings[FPSTR(F_CONFIG_HUE)] = haspThemeHue; - settings[FPSTR(F_CONFIG_ZIFONT)] = haspZiFontPath; - settings[FPSTR(F_CONFIG_PAGES)] = haspPagesPath; + bool changed = false; - configOutput(settings); - return true; + if(haspStartPage != settings[FPSTR(F_CONFIG_STARTPAGE)].as()) changed = true; + settings[FPSTR(F_CONFIG_STARTPAGE)] = haspStartPage; + + if(haspStartDim != settings[FPSTR(F_CONFIG_STARTDIM)].as()) changed = true; + settings[FPSTR(F_CONFIG_STARTDIM)] = haspStartDim; + + if(haspThemeId != settings[FPSTR(F_CONFIG_THEME)].as()) changed = true; + settings[FPSTR(F_CONFIG_THEME)] = haspThemeId; + + if(haspThemeHue != settings[FPSTR(F_CONFIG_HUE)].as()) changed = true; + settings[FPSTR(F_CONFIG_HUE)] = haspThemeHue; + + if(strcmp(haspZiFontPath, settings[FPSTR(F_CONFIG_ZIFONT)].as().c_str()) != 0) changed = true; + settings[FPSTR(F_CONFIG_ZIFONT)] = haspZiFontPath; + + if(strcmp(haspPagesPath, settings[FPSTR(F_CONFIG_PAGES)].as().c_str()) != 0) changed = true; + settings[FPSTR(F_CONFIG_PAGES)] = haspPagesPath; + + if(changed) configOutput(settings); + return changed; } /** Set HASP Configuration. diff --git a/src/hasp_gui.cpp b/src/hasp_gui.cpp index 6e4369bc..60b7ea72 100644 --- a/src/hasp_gui.cpp +++ b/src/hasp_gui.cpp @@ -30,10 +30,11 @@ #include // Include the SPIFFS library #endif +#define BACKLIGHT_CHANNEL 15 // pwm channek 0-15 + /* ---------- Screenshot Variables ---------- */ File pFileOut; uint8_t guiSnapshot = 0; -size_t guiVDBsize = 0; #if defined(ARDUINO_ARCH_ESP8266) #include @@ -385,8 +386,19 @@ boolean Touch_getXY(uint16_t * x, uint16_t * y, boolean showTouch) { static const int coords[] = {3800, 500, 300, 3800}; // portrait - left, right, top, bottom static const int XP = 27, XM = 15, YP = 4, YM = 14; // default ESP32 Uno touchscreen pins - static TouchScreen ts = TouchScreen(XP, aYP, aXM, YM, 300); + static TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300); TSPoint p = ts.getPoint(); + int z1 = analogRead(aXM); + int z2 = analogRead(aYP); + Serial.print(p.x); + Serial.print(" - "); + Serial.print(p.y); + Serial.print(" - "); + Serial.print(p.z); + Serial.print(" - "); + Serial.print(z1); + Serial.print(" - "); + Serial.println(z2); pinMode(aYP, OUTPUT); // restore shared pins pinMode(aXM, OUTPUT); @@ -397,6 +409,7 @@ boolean Touch_getXY(uint16_t * x, uint16_t * y, boolean showTouch) #define MAXPRESSURE 1000 bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE); if(pressed) { + switch(guiRotation) { case 0: // portrait *x = map(p.x, coords[0], coords[1], 0, tft.width()); @@ -494,7 +507,9 @@ void guiSetup() #endif /* Initialize the Virtual Device Buffers */ + size_t guiVDBsize = 0; lv_init(); + #if defined(ARDUINO_ARCH_ESP32) /* allocate on iram (or psram ?) */ guiVDBsize = 16 * 1024u; // 32 KBytes * 2 @@ -531,11 +546,11 @@ void guiSetup() Log.verbose(F("LVGL: Backlight: Pin %d"), guiBacklightPin); #if defined(ARDUINO_ARCH_ESP32) + // pinMode(guiBacklightPin, OUTPUT); // configure LED PWM functionalitites - ledcSetup(100, 1000, 10); + ledcSetup(BACKLIGHT_CHANNEL, 1000, 10); // attach the channel to the GPIO to be controlled - pinMode(guiBacklightPin, OUTPUT); - ledcAttachPin(guiBacklightPin, 99); + ledcAttachPin(guiBacklightPin, BACKLIGHT_CHANNEL); #else pinMode(guiBacklightPin, OUTPUT); #endif @@ -578,7 +593,7 @@ void guiSetup() indev_drv.read_cb = my_touchpad_read; lv_indev_t * mouse_indev = lv_indev_drv_register(&indev_drv); - if(guiShowPointer || true) { + if(guiShowPointer) { lv_obj_t * label = lv_label_create(lv_layer_sys(), NULL); lv_label_set_text(label, "<"); lv_indev_set_cursor(mouse_indev, label); // connect the object to the driver @@ -646,7 +661,7 @@ void guiSetBacklight(bool lighton) if(guiBacklightPin >= 0) { #if defined(ARDUINO_ARCH_ESP32) - ledcWrite(99, lighton ? map(guiDimLevel, 0, 100, 0, 1023) : 0); // ledChannel and value + ledcWrite(BACKLIGHT_CHANNEL, lighton ? map(guiDimLevel, 0, 100, 0, 1023) : 0); // ledChannel and value #else analogWrite(guiBacklightPin, lighton ? map(guiDimLevel, 0, 100, 0, 1023) : 0); #endif @@ -664,7 +679,7 @@ void guiSetDim(int8_t level) if(guiBacklightIsOn) { // The backlight is ON #if defined(ARDUINO_ARCH_ESP32) - ledcWrite(99, map(guiDimLevel, 0, 100, 0, 1023)); // ledChannel and value + ledcWrite(BACKLIGHT_CHANNEL, map(guiDimLevel, 0, 100, 0, 1023)); // ledChannel and value #else analogWrite(guiBacklightPin, map(guiDimLevel, 0, 100, 0, 1023)); #endif @@ -683,20 +698,46 @@ int8_t guiGetDim() //////////////////////////////////////////////////////////////////////////////////////////////////// bool guiGetConfig(const JsonObject & settings) { - settings[FPSTR(F_GUI_TICKPERIOD)] = guiTickPeriod; - settings[FPSTR(F_GUI_IDLEPERIOD1)] = guiSleepTime1; - settings[FPSTR(F_GUI_IDLEPERIOD2)] = guiSleepTime2; - settings[FPSTR(F_GUI_BACKLIGHTPIN)] = guiBacklightPin; - settings[FPSTR(F_GUI_ROTATION)] = guiRotation; - settings[FPSTR(F_GUI_POINTER)] = guiShowPointer; + bool changed = false; + if(guiTickPeriod != settings[FPSTR(F_GUI_TICKPERIOD)].as()) changed = true; + settings[FPSTR(F_GUI_TICKPERIOD)] = guiTickPeriod; + + if(guiSleepTime1 != settings[FPSTR(F_GUI_IDLEPERIOD1)].as()) changed = true; + settings[FPSTR(F_GUI_IDLEPERIOD1)] = guiSleepTime1; + + if(guiSleepTime2 != settings[FPSTR(F_GUI_IDLEPERIOD2)].as()) changed = true; + settings[FPSTR(F_GUI_IDLEPERIOD2)] = guiSleepTime2; + + if(guiBacklightPin != settings[FPSTR(F_GUI_BACKLIGHTPIN)].as()) changed = true; + settings[FPSTR(F_GUI_BACKLIGHTPIN)] = guiBacklightPin; + + if(guiRotation != settings[FPSTR(F_GUI_ROTATION)].as()) changed = true; + settings[FPSTR(F_GUI_ROTATION)] = guiRotation; + + if(guiShowPointer != settings[FPSTR(F_GUI_POINTER)].as()) changed = true; + settings[FPSTR(F_GUI_POINTER)] = guiShowPointer; + + /* Check CalData array has changed */ JsonArray array = settings[FPSTR(F_GUI_CALIBRATION)].to(); + uint8_t i = 0; + for(JsonVariant v : array) { + Log.verbose(F("GUI CONF: %d: %d <=> %d"), i, calData[i], v.as()); + if(i < 5) { + if(calData[i] != v.as()) changed = true; + } else { + changed = true; + } + i++; + } + + /* Build new CalData array */ for(uint8_t i = 0; i < 5; i++) { array.add(calData[i]); } - configOutput(settings); - return true; + if(changed) configOutput(settings); + return changed; } /** Set GUI Configuration. @@ -772,7 +813,6 @@ static void guiSendBmpHeader() { uint8_t buffer[128]; memset(buffer, 0, sizeof(buffer)); - int32_t res; lv_disp_t * disp = lv_disp_get_default(); buffer[0] = 0x42; // B diff --git a/src/hasp_http.cpp b/src/hasp_http.cpp index f080032b..33ee80d2 100644 --- a/src/hasp_http.cpp +++ b/src/hasp_http.cpp @@ -1910,7 +1910,7 @@ bool httpGetConfig(const JsonObject & settings) if(strcmp(httpPassword, settings[FPSTR(F_CONFIG_PASS)].as().c_str()) != 0) changed = true; settings[FPSTR(F_CONFIG_PASS)] = httpPassword; - configOutput(settings); + if(changed) configOutput(settings); return changed; } diff --git a/src/hasp_mdns.cpp b/src/hasp_mdns.cpp index cfd6419d..5e4b97bd 100644 --- a/src/hasp_mdns.cpp +++ b/src/hasp_mdns.cpp @@ -75,10 +75,13 @@ void mdnsStop() bool mdnsGetConfig(const JsonObject & settings) { + bool changed = false; + + if(mdnsEnabled != settings[FPSTR(F_CONFIG_ENABLE)].as()) changed = true; settings[FPSTR(F_CONFIG_ENABLE)] = mdnsEnabled; - configOutput(settings); - return true; + if(changed) configOutput(settings); + return changed; } /** Set MDNS Configuration. diff --git a/src/hasp_mqtt.cpp b/src/hasp_mqtt.cpp index abadb37d..c344ef6d 100644 --- a/src/hasp_mqtt.cpp +++ b/src/hasp_mqtt.cpp @@ -123,7 +123,7 @@ void IRAM_ATTR mqtt_send_state(const __FlashStringHelper * subtopic, const char if(mqttIsConnected()) { char topic[64]; - snprintf_P(topic, sizeof(topic), PSTR("%sstate/%S"), mqttNodeTopic, subtopic); + snprintf_P(topic, sizeof(topic), PSTR("%sstate/%s"), mqttNodeTopic, subtopic); mqttClient.publish(topic, payload); } else { return mqtt_log_no_connection(); @@ -466,7 +466,7 @@ String mqttGetNodename() void mqttStop() { - if(mqttClient.connected()) { + if(mqttEnabled && mqttClient.connected()) { char topicBuffer[128]; snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%sstatus"), mqttNodeTopic); @@ -482,15 +482,28 @@ void mqttStop() bool mqttGetConfig(const JsonObject & settings) { - settings[FPSTR(F_CONFIG_NAME)] = mqttNodeName; - settings[FPSTR(F_CONFIG_GROUP)] = mqttGroupName; - settings[FPSTR(F_CONFIG_HOST)] = mqttServer; - settings[FPSTR(F_CONFIG_PORT)] = mqttPort; - settings[FPSTR(F_CONFIG_USER)] = mqttUser; - settings[FPSTR(F_CONFIG_PASS)] = mqttPassword; + bool changed = false; - configOutput(settings); - return true; + if(strcmp(mqttNodeName, settings[FPSTR(F_CONFIG_NAME)].as().c_str()) != 0) changed = true; + settings[FPSTR(F_CONFIG_NAME)] = mqttNodeName; + + if(strcmp(mqttGroupName, settings[FPSTR(F_CONFIG_GROUP)].as().c_str()) != 0) changed = true; + settings[FPSTR(F_CONFIG_GROUP)] = mqttGroupName; + + if(strcmp(mqttServer, settings[FPSTR(F_CONFIG_HOST)].as().c_str()) != 0) changed = true; + settings[FPSTR(F_CONFIG_HOST)] = mqttServer; + + if(mqttPort != settings[FPSTR(F_CONFIG_PORT)].as()) changed = true; + settings[FPSTR(F_CONFIG_PORT)] = mqttPort; + + if(strcmp(mqttUser, settings[FPSTR(F_CONFIG_USER)].as().c_str()) != 0) changed = true; + settings[FPSTR(F_CONFIG_USER)] = mqttUser; + + if(strcmp(mqttPassword, settings[FPSTR(F_CONFIG_PASS)].as().c_str()) != 0) changed = true; + settings[FPSTR(F_CONFIG_PASS)] = mqttPassword; + + if(changed) configOutput(settings); + return changed; } /** Set MQTT Configuration. @@ -517,6 +530,7 @@ bool mqttSetConfig(const JsonObject & settings) String mac = wifiGetMacAddress(3, ""); mac.toLowerCase(); snprintf_P(mqttNodeName, sizeof(mqttNodeName), PSTR("plate_%s"), mac.c_str()); + changed = true; } if(!settings[FPSTR(F_CONFIG_GROUP)].isNull()) { @@ -524,6 +538,11 @@ bool mqttSetConfig(const JsonObject & settings) strncpy(mqttGroupName, settings[FPSTR(F_CONFIG_GROUP)], sizeof(mqttGroupName)); } + if(strlen(mqttGroupName) == 0) { + strcpy_P(mqttGroupName, PSTR("plates")); + changed = true; + } + if(!settings[FPSTR(F_CONFIG_HOST)].isNull()) { changed |= strcmp(mqttServer, settings[FPSTR(F_CONFIG_HOST)]) != 0; strncpy(mqttServer, settings[FPSTR(F_CONFIG_HOST)], sizeof(mqttServer)); diff --git a/src/hasp_telnet.cpp b/src/hasp_telnet.cpp index 11622ecf..cb5136bd 100644 --- a/src/hasp_telnet.cpp +++ b/src/hasp_telnet.cpp @@ -26,8 +26,9 @@ extern char httpUser[32]; extern char httpPassword[32]; uint8_t telnetLoginState = TELNET_UNAUTHENTICATED; -static WiFiServer * telnetServer; //= new WiFiServer(23); WiFiClient telnetClient; +static WiFiServer * telnetServer; +uint16_t telnetPort = 23; bool telnetInCommandMode = false; uint8_t telnetEnabled = true; // Enable telnet debug output uint8_t telnetLoginAttempt = 0; // Initial attempt @@ -173,17 +174,17 @@ void telnetSetup() // telnetSetConfig(settings); if(telnetEnabled) { // Setup telnet server for remote debug output - if(!telnetServer) telnetServer = new WiFiServer(23); + if(!telnetServer) telnetServer = new WiFiServer(telnetPort); if(telnetServer) { telnetServer->setNoDelay(true); telnetServer->begin(); // if(!telnetClient) telnetClient = new WiFiClient; - if(!telnetClient) { - Log.error(F("Failed to start telnet client")); - } else { - telnetClient.setNoDelay(true); - } + // if(!telnetClient) { + // Log.error(F("Failed to start telnet client")); + //} else { + telnetClient.setNoDelay(true); + //} Log.notice(F("Debug telnet console started")); } else { @@ -213,10 +214,16 @@ void IRAM_ATTR telnetLoop() bool telnetGetConfig(const JsonObject & settings) { + bool changed = false; + + if(telnetEnabled != settings[FPSTR(F_CONFIG_ENABLE)].as()) changed = true; settings[FPSTR(F_CONFIG_ENABLE)] = telnetEnabled; - configOutput(settings); - return true; + if(telnetPort != settings[FPSTR(F_CONFIG_PORT)].as()) changed = true; + settings[FPSTR(F_CONFIG_PORT)] = telnetPort; + + if(changed) configOutput(settings); + return changed; } /** Set TELNET Configuration. @@ -232,6 +239,9 @@ bool telnetSetConfig(const JsonObject & settings) configOutput(settings); bool changed = false; + changed |= configSet(telnetEnabled, settings[FPSTR(F_CONFIG_ENABLE)], PSTR("telnetEnabled")); + changed |= configSet(telnetPort, settings[FPSTR(F_CONFIG_PORT)], PSTR("telnetPort")); + return changed; } diff --git a/src/hasp_wifi.cpp b/src/hasp_wifi.cpp index a497da56..70725e9a 100644 --- a/src/hasp_wifi.cpp +++ b/src/hasp_wifi.cpp @@ -208,7 +208,7 @@ bool wifiGetConfig(const JsonObject & settings) if(strcmp(wifiPassword, settings[FPSTR(F_CONFIG_PASS)].as().c_str()) != 0) changed = true; settings[FPSTR(F_CONFIG_PASS)] = wifiPassword; - configOutput(settings); + if(changed) configOutput(settings); return changed; }