Announcement

Collapse
No announcement yet.

Multiple Forms

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Multiple Forms

    HI
    I am trying to use 2 or more forms but not working, all the buttons are on the first form even though i have created 2nd form.
    here is the code. I would like a button to switch to form 2 and on form 2 a button to switch back. Thanks for any help. I had a look at another thread on here couldnt get it working.

    #include "ESP8266WiFi.h"
    #include "GFX4d.h"
    #include "PubSubClient.h"

    GFX4d gfx = GFX4d();

    #include "xmas_lightConst.h" // Note. This file will not be created if there are no generated graphics

    int state;
    //const char* ssid = "***********";
    //const char* password = "*************";
    const char* ssid = "*****************";
    const char* password = "*************";

    const char* mqtt_server = "192.168.0.0";
    const char* mqtt_user = "user";
    const char* mqtt_password = "passl";

    WiFiClient espClient;
    PubSubClient client(espClient);
    long lastMsg = 0;
    char msg[50];
    int value = 0;
    int pressed = -1;
    void setup_wifi() {
    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
    delay(500);

    }
    }

    void setup()
    {
    gfx.begin();
    gfx.Cls();
    gfx.ScrollEnable(false);
    gfx.BacklightOn(true);
    gfx.Orientation(PORTRAIT);
    gfx.SmoothScrollSpeed(5);
    gfx.TextColor(WHITE, BLACK); gfx.Font(2); gfx.TextSize(1);
    gfx.Open4dGFX("XMAS_L~1"); // Opens DAT and GCI files for read using filename without extension. Note! Workshop generates files with Short filenames

    gfx.UserImages(iWinbutton1, 0); // Winbutton1 where state is 0 for up and 1 for down
    gfx.imageTouchEnable(iWinbutton1, true) ;
    gfx.UserImages(iWinbutton2, 0); // Winbutton2 where state is 0 for up and 1 for down
    gfx.imageTouchEnable(iWinbutton2, true) ;

    gfx.imageTouchEnable(iWinbutton3, true) ; // Winbutton3 show initialy, if required
    gfx.UserImages(iWinbutton3,0) ; // Winbutton3 show initialy, if required
    gfx.imageTouchEnable(iWinbutton4, true) ; // Winbutton4 show initialy, if required
    gfx.UserImages(iWinbutton4,0) ;// Winbutton4 show initialy, if required

    gfx.imageTouchEnable(iWinbutton5, true) ; // Winbutton5 show initialy, if required
    gfx.UserImages(iWinbutton5,0) ; // Winbutton5 show initialy, if required

    gfx.imageTouchEnable(iWinbutton6, true) ; // Winbutton6 show initialy, if required
    gfx.UserImages(iWinbutton6,0) ; // Winbutton6 show initialy, if required

    gfx.imageTouchEnable(iWinbutton7, true) ; // Winbutton7 show initialy, if required
    gfx.UserImages(iWinbutton7,0) ; // Winbutton7 show initialy, if required


    gfx.touch_Set(TOUCH_ENABLE);


    WiFi.mode(WIFI_STA);
    WiFi.disconnect();
    setup_wifi();
    client.setServer(mqtt_server, 1883);
    delay(100);


    } // end Setup **do not alter, remove or duplicate this line**

    void reconnect() {
    // Loop until we're reconnected
    while (!client.connected()) {
    if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
    } else {

    delay(5000);
    }
    }
    }
    void loop()
    {

    if (!client.connected()) {
    reconnect();
    }
    client.loop();

    long now = millis();
    int status;


    if (now - lastMsg > 500) {
    lastMsg = now;

    gfx.touch_Update();
    state = gfx.touch_GetPen();
    if(state == TOUCH_PRESSED){

    if(gfx.imageTouched() == iWinbutton1){
    gfx.UserImages(iWinbutton1, 1);
    String msg= "ON";
    char message[58];
    msg.toCharArray(message,58);
    // Serial.println(message);
    //publish sensor data to MQTT broker
    client.publish("wled/door", message);
    delay(1000);
    gfx.touch_Update();
    state = gfx.touch_GetPen();
    if(state == TOUCH_PRESSED){
    if(gfx.imageTouched() == iWinbutton1){
    gfx.UserImages(iWinbutton3, 0);
    }
    }
    }
    if(gfx.imageTouched() == iWinbutton2){
    gfx.UserImages(iWinbutton4, state);
    String msg= "OFF";
    char message[58];
    msg.toCharArray(message,58);
    Serial.println(message);
    //publish sensor data to MQTT broker
    client.publish("wled/door", message);
    delay(3000);
    }
    if(gfx.imageTouched() == iWinbutton3){
    gfx.UserImages(iWinbutton3, 1);
    String msg= "FX=44";
    char message[58];
    msg.toCharArray(message,58);
    Serial.println(message);
    //publish sensor data to MQTT broker
    client.publish("wled/door/api", message);
    delay(3000);

    }
    if(gfx.imageTouched() == iWinbutton4){
    gfx.UserImages(iWinbutton4, state);
    String msg= "FX=04";
    char message[58];
    msg.toCharArray(message,58);
    Serial.println(message);
    //publish sensor data to MQTT broker
    client.publish("wled/door/api", message);
    delay(3000);

    }
    if(gfx.imageTouched() == iWinbutton5){
    gfx.UserImages(iWinbutton5, state);
    String msg= "FX=79";
    char message[58];
    msg.toCharArray(message,58);
    Serial.println(message);
    //publish sensor data to MQTT broker
    client.publish("wled/door/api", message);
    delay(3000);
    }
    if(gfx.imageTouched() == iWinbutton6){
    gfx.UserImages(iWinbutton6, state);
    showForm2();

    }
    if(gfx.imageTouched() == iWinbutton7){
    gfx.UserImages(iWinbutton7, state);
    showForm1();
    }


    }




    // put your main code here, to run repeatedly:
    yield(); // Required for ESP

    }
    }



    void showForm1(){
    gfx.Cls();
    gfx.imageTouchEnable(iWinbutton6, true) ; // Winbutton1 show initialy, if required
    gfx.UserImages(iWinbutton6,0) ; // Winbutton1 show initialy, if required
    gfx.imageTouchEnable(iWinbutton7, false) ;
    }



    void showForm2(){
    gfx.Cls();
    gfx.imageTouchEnable(iWinbutton7, true) ; // Winbutton1 show initialy, if required
    gfx.UserImages(iWinbutton7,0) ; // Winbutton1 show initialy, if required
    gfx.imageTouchEnable(iWinbutton6, false) ;
    }
    Last edited by Jynx; 3 days ago.

  • #2
    Pic of display
    Attached Files

    Comment


    • #3
      Hi,

      Would it be possible to send me your complete project so I can test it here. You can zip your project by clicking on File then Zip project and send the zip from your project file to me.

      Best regards

      Paul

      Comment


      • #4
        xmas_light.zip

        file attached thanks for the fast response

        Comment


        • #5
          Hi,

          I commented out this line

          if (now - lastMsg > 500) {

          and the } before the yield and we get a response to touch. It looks like touch will only be checked every 500ms and really it needs to be checked more often. It would be better, if possible, to check for touch outside of this condition every loop cycle and also make the yield(); as the last command before the end of the loop.

          I will look for a way to do that in your code.

          Best regards

          Paul

          Comment


          • #6
            Ok thanks Paul.

            Comment


            • #7
              Hi,

              I have attached a version of your code which uses

              status = gfx.ImageTouchedAuto(DRAW_UPDOWN);

              It is a recent addition in the latest GFX4d library and it cuts out the need for all the touch checking and simply returns a value of the button pressed or -1 if nothing pressed. Again, this needs to be checked often, every loop cycle, but it will make it easier to finish your project..

              If your GFX4d library is up to date then it will work as is, if not you can download it here.

              https://github.com/4dsystems/GFX4d

              Then it will be just a matter of adding your own MQTT control code again.

              The way you have functions to change form is correct. You may find it easier to disable all buttons in the function using

              imageTouchEnable(-1, false);

              which will disable all buttons and then enable the buttons that you need.

              I hope this helps

              Best regards

              Paul
              Attached Files

              Comment


              • #8
                Hi thanks Paul. I have run your code but this still does not work. I still get both 6&7 buttons on same page. See pic
                regards
                Wayne
                Attached Files

                Comment


                • #9
                  Hi Wayne,

                  I didn't do that part in the code, I just showed another way of handling touch which might simplify things.

                  So I understand correctly, Winbutton 6 is the button you want shown on the first form with Winbuttons 1 to 5, and then Winbutton 7 is the button to return to form 1

                  Paul

                  Comment


                  • #10
                    Hi Wayne,

                    Can you try this code as an example of changing forms.

                    Code:
                    #include "ESP8266WiFi.h"
                    #include "GFX4d.h"
                    #include "PubSubClient.h"
                    
                    GFX4d gfx = GFX4d();
                    
                    #include "xmas_light3Const.h"    // Note. This file will not be created if there are no generated graphics
                    
                    int state;
                    const char* ssid = "SKYE7FB5-2.4";
                    const char* password =  "PYMYQVCWWM";
                    //const char* ssid = "SKY93627";
                    //const char* password =  "SBSPFFXX";
                    
                    const char* mqtt_server = "192.168.0.23"; const char* mqtt_user = "wayne"; const char* mqtt_password = "tropical";
                    
                    WiFiClient espClient;
                    PubSubClient client(espClient);
                    long lastMsg = 0;
                    char msg[50];
                    int value = 0;
                    int pressed = -1;
                    long now;
                    void setup_wifi() {
                       WiFi.begin(ssid, password);
                    
                     while (WiFi.status() != WL_CONNECTED) {
                        delay(500);
                    
                        }
                    }
                    
                    void setup()
                    {
                      gfx.begin();
                      gfx.Cls();
                      gfx.ScrollEnable(false);
                      gfx.BacklightOn(true);
                      gfx.Orientation(PORTRAIT);
                      gfx.SmoothScrollSpeed(5);
                      gfx.TextColor(WHITE, BLACK); gfx.Font(2);  gfx.TextSize(1);
                      gfx.Open4dGFX("XMAS_L~1"); // Opens DAT and GCI files for read using filename without extension. Note! Workshop generates files with Short filenames
                    
                      showForm1();
                    
                    
                      gfx.touch_Set(TOUCH_ENABLE);
                    
                    
                      WiFi.mode(WIFI_STA);
                      WiFi.disconnect();
                      setup_wifi();
                      client.setServer(mqtt_server, 1883);
                      delay(100);
                      now = millis();
                    
                     } // end Setup **do not alter, remove or duplicate this line**
                    
                    void reconnect() {
                      // Loop until we're reconnected
                      //while (!client.connected()) {
                      //if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
                      // } else {
                    
                      // delay(5000);
                      //  }
                      //}
                    }
                    void loop()
                    {
                    
                     if (!client.connected()) {
                        //reconnect();
                      }
                      //client.loop();
                    
                    //long now = millis();
                      int status;
                    
                    
                      status = gfx.ImageTouchedAuto(DRAW_UPDOWN);
                      gfx.MoveTo(100, 190);
                      //if(status != -1)gfx.print(status);
                      if(status == iWinbutton6) showForm2();
                      if(status == iWinbutton7) showForm1();
                      // put your main code here, to run repeatedly:
                      yield(); // Required for ESP
                    
                     //}
                    }
                    
                    
                    
                    void showForm1(){
                      //pressed = -1;
                      gfx.Cls();
                      gfx.imageTouchEnable(-1, false) ;
                      gfx.UserImages(iWinbutton1, 0); // Winbutton1 where state is 0 for up and 1 for down
                      gfx.imageTouchEnable(iWinbutton1, true) ;
                      gfx.UserImages(iWinbutton2, 0); // Winbutton2 where state is 0 for up and 1 for down
                      gfx.imageTouchEnable(iWinbutton2, true) ;
                      gfx.imageTouchEnable(iWinbutton3, true) ;   // Winbutton3 show initialy, if required
                      gfx.UserImages(iWinbutton3,0) ;             // Winbutton3 show initialy, if required
                      gfx.imageTouchEnable(iWinbutton4, true) ;   // Winbutton4 show initialy, if required
                      gfx.UserImages(iWinbutton4,0) ;// Winbutton4 show initialy, if required
                      gfx.imageTouchEnable(iWinbutton5, true) ;   // Winbutton5 show initialy, if required
                      gfx.UserImages(iWinbutton5,0) ;             // Winbutton5 show initialy, if required
                      gfx.imageTouchEnable(iWinbutton6, true) ;   // Winbutton6 show initialy, if required
                      gfx.UserImages(iWinbutton6,0) ;             // Winbutton6 show initialy, if required
                    }
                    
                    
                    void showForm2(){
                      //pressed = -1;
                      gfx.Cls();
                      gfx.imageTouchEnable(-1, false) ;
                      gfx.imageTouchEnable(iWinbutton7, true) ;   // Winbutton1 show initialy, if required
                      gfx.UserImages(iWinbutton7,0) ;             // Winbutton1 show initialy, if required
                    }

                    Comment

                    Working...
                    X