Hieromon / PageBuilder

An arduino library to create html string in the sketch for ESP8266/ESP32 WebServer.
MIT License
193 stars 40 forks source link

using PageBuilder in an own library? #17

Closed kokospalme closed 4 years ago

kokospalme commented 4 years ago

Hello Hieromon, I really like your PageBuilder library and want to use it in my own class, which should contain the pageBuilder and pageElement objects to get things going further than in the basic *.ino-file. But now I'm stuck since 2 days... I have copied the code from DynamicPage.ino-example, but with this code in the WebUI.cpp

/*
 * WebUI.cpp
 *
 *      Author: Chris
 */

#include "WebUI.h"

WebUI::WebUI(){

}

void WebUI::begin(){    //runs in void setup()
    Root_p.exitCanHandle(handleAcs);    // Handles for all requests.
    Root_p.insert(Server);     // Add root page

    Server.begin();
}

void WebUI::run(){      //runs in void loop()
    Server.handleClient();
}

// This function creates dynamic web page by each request.
// It is called twice at one time URI request that caused by the structure
// of ESP8266WebServer class.
bool WebUI::handleAcs(HTTPMethod method, String uri){
    if(uri == currentUri){  //page is prepared
        return true;
    }else{
        currentUri = uri;   //set uri so its prepared next time
        Root_p.clearElement();
        Root_p.addElement(Root_elm);

        if(uri == "/"){
              Root_p.setUri(uri.c_str());
              Root_elm.setMold(PSTR(
                "<html>"
                "<body>"
                "<h2>{{ROOT}}</h2>"
                "</body>"
                "</html>"));
              Root_elm.addToken("ROOT", rootPage);
              return true;

        }else if(uri == "/raw"){

            return true;
        }else{
            return false;
        }
    }
    return false;
}

String WebUI::rootPage(PageArgument& args) {
  return String(F("This is root page."));
}

and this code in WebUI.c

class WebUI{
public:
    WebUI();
    void begin();
    void run();
    bool handleAcs(HTTPMethod method, String uri);
private:

    PageElement Root_elm;
    PageBuilder Root_p;

    String currentUri;
    String rootPage(PageArgument& args);
};

I get this error:

"/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/tools/xtensa-esp32-elf-gcc/1.22.0-80-g6c4433a-5.2.0/bin/xtensa-esp32-elf-g++" -DESP_PLATFORM -DMBEDTLS_CONFIG_FILE="mbedtls/esp_config.h" -DHAVE_CONFIG_H -DGCC_NOT_5_2_0=0 -DWITH_POSIX "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/config" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/app_trace" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/app_update" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/asio" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/bootloader_support" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/bt" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/coap" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/console" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/driver" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/esp-tls" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/esp32" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/esp_adc_cal" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/esp_event" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/esp_http_client" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/esp_http_server" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/esp_https_ota" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/esp_ringbuf" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/ethernet" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/expat" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/fatfs" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/freemodbus" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/freertos" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/heap" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/idf_test" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/jsmn" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/json" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/libsodium" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/log" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/lwip" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/mbedtls" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/mdns" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/micro-ecc" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/mqtt" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/newlib" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/nghttp" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/nvs_flash" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/openssl" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/protobuf-c" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/protocomm" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/pthread" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/sdmmc" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/smartconfig_ack" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/soc" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/spi_flash" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/spiffs" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/tcp_transport" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/tcpip_adapter" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/ulp" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/vfs" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/wear_levelling" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/wifi_provisioning" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/wpa_supplicant" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/xtensa-debug-module" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/esp-face" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/esp32-camera" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/esp-face" "-I/Applications/sloeber.app/Contents/Eclipse//arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/tools/sdk/include/fb_gfx" -std=gnu++11 -Os -g3 -Wpointer-arith -fexceptions -fstack-protector -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -mlongcalls -nostdlib -Wall -Werror=all -Wextra -Wno-error=maybe-uninitialized -Wno-error=unused-function -Wno-error=unused-but-set-variable -Wno-error=unused-variable -Wno-error=deprecated-declarations -Wno-unused-parameter -Wno-unused-but-set-parameter -Wno-missing-field-initializers -Wno-sign-compare -fno-rtti -MMD -c -DF_CPU=240000000L -DARDUINO=10802 -DARDUINO_ESP32_DEV -DARDUINO_ARCH_ESP32 -DARDUINO_BOARD="ESP32_DEV" -DARDUINO_VARIANT="esp32"  -DESP32 -DCORE_DEBUG_LEVEL=0 -DBOARD_HAS_PSRAM -mfix-esp32-psram-cache-issue   -I"/Applications/sloeber.app/Contents/Eclipse/arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/cores/esp32" -I"/Applications/sloeber.app/Contents/Eclipse/arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/variants/esp32" -I"/Applications/sloeber.app/Contents/Eclipse/arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/libraries/WebServer/src" -I"/Applications/sloeber.app/Contents/Eclipse/arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/libraries/WiFi/src" -I"/Applications/sloeber.app/Contents/Eclipse/arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/libraries/FS/src" -I"/Applications/sloeber.app/Contents/Eclipse/arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/libraries/SPIFFS/src" -I"/Users/Chris/Documents/Arduino/libraries/ArtLight" -I"/Users/Chris/Documents/Arduino/libraries/FastLED" -I"/Users/Chris/Documents/Arduino/libraries/Lumos" -I"/Applications/sloeber.app/Contents/Eclipse/arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/libraries/ESPmDNS/src" -I"/Users/Chris/Documents/Arduino/libraries/WebUI/src" -I"/Users/Chris/Documents/Arduino/libraries/PageBuilder/src" -MMD -MP -MF"libraries/WebUI/src/WebUI.cpp.d" -MT"libraries/WebUI/src/WebUI.cpp.o" -D__IN_ECLIPSE__=1 -x c++ "/Users/Chris/Documents/Arduino/libraries/WebUI/src/WebUI.cpp"  -o  "libraries/WebUI/src/WebUI.cpp.o"
/Users/Chris/Documents/Arduino/libraries/WebUI/src/WebUI.cpp: In member function 'void WebUI::begin()':
/Users/Chris/Documents/Arduino/libraries/WebUI/src/WebUI.cpp:21:32: error: invalid use of non-static member function
  Root_p.exitCanHandle(handleAcs);    // Handles for all requests.
                                ^
/Users/Chris/Documents/Arduino/libraries/WebUI/src/WebUI.cpp: In member function 'bool WebUI::handleAcs(HTTPMethod, String)':
/Users/Chris/Documents/Arduino/libraries/WebUI/src/WebUI.cpp:50:43: error: invalid use of non-static member function
         Root_elm.addToken("ROOT", rootPage);
                                           ^
make: *** [libraries/WebUI/src/WebUI.cpp.o] Error 1

what is the problem about using Root_elm.addToken("ROOT", rootPage) ? rootPage is a non-static member of WebUI, which is used by a non-static member function?!

Can you guys help me out with this?

Hieromon commented 4 years ago

@kokospalme Thank you for using PageBuilder.

what is the problem about using Root_elm.addToken("ROOT", rootPage) ? rootPage is a non-static member of WebUI, which is used by a non-static member function?!

The problem is that the rootPage is a non-static member. That's exactly an error. Your code is ambiguous about the instance passing to the addToken. You must explicitly specify the instance if you pass a non-static member function as a callback. It is a standard for Callable function objects of C++. In this case, you need to bind the addToken to its instance with this identity. Refer to: https://stackoverflow.com/questions/37636373/how-stdbind-works-with-member-functions https://en.cppreference.com/w/cpp/utility/functional/bind

WebUI.cpp

#include <functional>
#include "WebUI.h"
...
void WebUI::begin() {              // runs in void setup()
  Root_p.exitCanHandle(std::bind(&WebUI::handleAcs, this, std::placeholders::_1, std::placeholders::_2)); // Handles for all requests.
  ...
}

bool WebUI::handleAcs(HTTPMethod method, String uri) {
...
      Root_elm.setMold(PSTR("<html>"
                            "<body>"
                            "<h2>{{ROOT}}</h2>"
                            "</body>"
                            "</html>"));
      Root_elm.addToken(String("ROOT"), std::bind(&WebUI::rootPage, this, std::placeholders::_1));
      return true;
...
}
kokospalme commented 4 years ago

thanks for your reply. I'll give it a try... :)

Hieromon commented 4 years ago

Closed due to no activity after providing the solution. Post the new comment to reopen.