Announcement

Collapse
No announcement yet.

4DUINO - Toggle Switch (via Touchscreen)

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

  • 4DUINO - Toggle Switch (via Touchscreen)

    Can someone send me some code to show a toggle switch with touch function to make it go high on the first touch, low on the next touch, high on the next touch etc...

    Thanks,
    Steve

  • #2
    Hi Steve,

    Here's the modified code from the one you sent via our support ticketing system.


    Code:
      state = Display.touch_Get(TOUCH_STATUS);           // get touchscreen status
      n = Display.img_Touched(hndl,-1) ;
    
      if(state == TOUCH_PRESSED)                        // if there's a press
          {
    
          if(n == i4Dbutton1 )
            {
            if (button == 0)
              {
                Display.img_SetWord(hndl, i4Dbutton1, IMAGE_INDEX, 1); // 4Dbutton1 where state is 0 for up and 1 for down
                Display.img_Show(hndl,i4Dbutton1) ;  // 4Dbutton1
                button = 1;
              }
    
            else if (button == 1)
              {
                Display.img_SetWord(hndl, i4Dbutton1, IMAGE_INDEX, 0); // 4Dbutton1 where state is 0 for up and 1 for down
                Display.img_Show(hndl,i4Dbutton1) ;  // 4Dbutton1
                button = 0;
              }
            }
          }

    Previously this was:


    Code:
      state = Display.touch_Get(TOUCH_STATUS);           // get touchscreen status
      n = Display.img_Touched(hndl,-1) ;
    
      if(state == TOUCH_PRESSED)                        // if there's a press
          {
    
          if(n == i4Dbutton1 )
            {
            if (button == 0)
              {
                Display.img_SetWord(hndl, i4Dbutton1, IMAGE_INDEX, 1); // 4Dbutton1 where state is 0 for up and 1 for down
                Display.img_Show(hndl,i4Dbutton1) ;  // 4Dbutton1
                button = 1;
              }
    
            if (button == 1)
              {
                Display.img_SetWord(hndl, i4Dbutton1, IMAGE_INDEX, 0); // 4Dbutton1 where state is 0 for up and 1 for down
                Display.img_Show(hndl,i4Dbutton1) ;  // 4Dbutton1
                button = 0;
              }
            }
          }

    The difference is that after checking if the touched object was i4Dbutton1,

    if(n == i4Dbutton1)

    the button is always toggled back and forth:

    Here's what happens:

    Code:
            if (button == 0)
              {
                Display.img_SetWord(hndl, i4Dbutton1, IMAGE_INDEX, 1); // 4Dbutton1 where state is 0 for up and 1 for down
                Display.img_Show(hndl,i4Dbutton1) ;  // 4Dbutton1
                button = 1;
              }

    In here, button state is turned to 1. Then going to the next if statement,


    Code:
            if (button == 1)
              {
                Display.img_SetWord(hndl, i4Dbutton1, IMAGE_INDEX, 0); // 4Dbutton1 where state is 0 for up and 1 for down
                Display.img_Show(hndl,i4Dbutton1) ;  // 4Dbutton1
                button = 0;
              }

    button will always be 1 and therefore will always be turned to 0 after this part.

    Therefore, the button switches from On to Off so fast everytime you press. This may not be noticable since it happens so fast.

    Using an "else if" instead of "if" will fix this:

    Code:
            if (button == 0)
              {
                Display.img_SetWord(hndl, i4Dbutton1, IMAGE_INDEX, 1); // 4Dbutton1 where state is 0 for up and 1 for down
                Display.img_Show(hndl,i4Dbutton1) ;  // 4Dbutton1
                button = 1;
              }
    
            else if (button == 1)
              {
                Display.img_SetWord(hndl, i4Dbutton1, IMAGE_INDEX, 0); // 4Dbutton1 where state is 0 for up and 1 for down
                Display.img_Show(hndl,i4Dbutton1) ;  // 4Dbutton1
                button = 0;
              }

    I hope this helped.

    Best Regards,
    Juniel Cruz

    Comment


    • #3
      Thanks - I can get the switch to work when it is on the screen by itself, however if there is other graphics and digital displays working on the same screen the toggle seems to stop. Is the system not capable of multiple tasks running at a time?

      Comment


      • #4
        Hi Steve,

        Please post your code or an example.
        You can have many things on the screen at once, all interacting with the atmega32u4, so there must be something not quite right with the way you are doing it.
        Post what you have and we can try and spot it.

        Regards
        James

        Comment


        • #5
          Code:
          % % Display % % .DefineResetLine ; // *Replaced* at compile time with define for reset line connected to the display
          % % Display % % .DefineDisplaySerialx ; // *Replaced* at compile time with define the Serial Port connected to the display
          // uncomment to log SPE errors messages to Serial (serial monitor)
          //#define LOG_MESSAGES
          
          % % Display % % .IncludeSerial_4DLib ; // *Replaced* at compile time with an Include the Serial Library relevant to the display
          #include "%%Display%%.Processor_LedDigitsDisplay.h"
          #include "%%Display%%.Processor_PrintDisk.h"
          % % Display % % .IncludeSerial_Const4D ; // *Replaced* at compile time with an Include the Constants file relevant to the display
          
          % % Display % % .AssignDisplaySerialtoLibrary ; // *Replaced* at compile time with an Assign of the correct Serial port to the correct library
          
          
          #include "TI_SENSOR_TOUCH_BUTTONConst.h"
          
          int ldrVal = 0;
          int loops = 0 ;
          String ATresponse ; // response from last AT command, does not include "OK" or "ERROR" and will be partial if command timed out
          word hndl ;
          int16_t x, y ;
          int button ;
          int state, n, posn ;
          
          void mycallback(int ErrCode, unsigned char Errorbyte)
          {
          #ifdef LOG_MESSAGES
            const char *Error4DText[] = {"OK\0", "Timeout\0", "NAK\0", "Length\0", "Invalid\0"} ;
            LOG_MESSAGES.print(F("Serial 4D Library reports error ")) ;
            LOG_MESSAGES.print(Error4DText[ErrCode]) ;
            if (ErrCode == Err4D_NAK)
            {
              LOG_MESSAGES.print(F(" returned data= ")) ;
              LOG_MESSAGES.println(Errorbyte) ;
            }
            else
              LOG_MESSAGES.println(F("")) ;
            while (1) ; // you can return here, or you can loop
          #else
            // Pin 13 has an LED connected on most Arduino boards. Just give it a name
          #define led 13
            while (1)
            {
              digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
              delay(200); // wait for a second
              digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
              delay(200); // wait for a second
            }
          #endif
          }
          
          void setup()
          {
            pinMode(RESETLINE, OUTPUT); // Display reset pin
            % % Display % % .Toggle_Reset_On ; // *Replaced* at compile time with correct rest on logic for the attached display
            delay(100); // wait for it to be recognised
            % % Display % % .Toggle_Reset_Off ; // *Replaced* at compile time with correct rest off logic for the attached display
            // Uncomment when using ESP8266
            // pinMode(ESPRESET, OUTPUT); // ESP reset pin
            // digitalWrite(ESPRESET, 1); // Reset ESP
            // delay(100); // wait for it t
            // digitalWrite(ESPRESET, 0); // Release ESP reset
            delay(4000) ; // give display time to startup
            % % Display % % .DisplaySerial.Begin_Speed ; // *Replaced* at compile time with command to start the serial port at the correct speed
            Display.TimeLimit4D = 5000 ; // 5 second timeout on all commands
            Display.Callback4D = mycallback ;
            Display.gfx_ScreenMode(PORTRAIT) ; // change manually if orientation change
          
          
            Display.putstr("Mounting...\n");
            if (!(Display.file_Mount()))
            {
              while (!(Display.file_Mount()))
              {
                Display.putstr("Drive not mounted...");
                delay(200);
                Display.gfx_Cls();
                delay(200);
              }
            }
          
            //hFontn = Display.file_LoadImageControl("NoName3.dnn", "NoName3.gnn", 1); // Open handle to access uSD fonts, uncomment if required and change nn to font number
            //hstrings = Display.file_Open("TI_SEN~1.txf", 'r') ; // Open handle to access uSD strings, uncomment if required
            hndl = Display.file_LoadImageControl("TI_SEN~1.dat", "TI_SEN~1.gci", 1);
          
            // put your setup code here, to run once:
            Display.gfx_Cls();
            pinMode(A0, INPUT);
          
            Display.img_Show(hndl, iForm1) ; // Border1
            Display.img_Show(hndl, iImage1) ; // Image1
            Display.gfx_LinePattern(LPDASHDOT) ;
          
            Display.gfx_LinePattern(LPDASHDOT) ;
            Display.gfx_Line(1, 160, 150, 160, SLATEGRAY) ; // Line1
            Display.gfx_LinePattern(LPSOLID) ;
            Display.gfx_LinePattern(LPDASHDOT) ;
            Display.gfx_Line(1, 220, 150, 220, SLATEGRAY) ; // Line2
            Display.gfx_LinePattern(LPSOLID) ;
          
            Display.img_Show(hndl, iStatictext1) ; // Statictext1
            Display.img_Show(hndl, iStatictext2) ; // Statictext2
            Display.img_Show(hndl, iStatictext3) ; // Statictext3
            Display.img_Show(hndl, iStatictext4) ; // Statictext4
          
            Display.img_Show(hndl, iUserled1) ; // Userled1 show initialy, if required
            Display.img_Show(hndl, iUserled2) ; // Userled2 show initialy, if required
            Display.img_Show(hndl, iUserled3) ; // Userled3 show initialy, if required
            Display.img_Show(hndl, iLeddigits1); // Leddigits1 show all digits at 0, only do this once
          
          
            Display.touch_Set(TOUCH_ENABLE); // Enable the touchscreen
          
            Display.img_ClearAttributes(hndl, i4Dbutton1, I_TOUCH_DISABLE); // 4Dbutton1 set to enable touch, only need to do this once
            Display.img_Show(hndl, i4Dbutton1); // 4Dbutton1 show button, only do this once
          
          } // end Setup **do not alter, remove or duplicate this line**
          
          
          void loop()
          {
          
            checktouchscreen();
            analogread();
            LedDigitsDisplay(Display, hndl, ldrVal, iLeddigits1 + 1, 12, 3, 1, 33, 0) ; // Leddigits1
            Display.img_SetWord(hndl, iThermometer1, IMAGE_INDEX, ldrVal) ; // where frame is 0 to 100 (for a displayed -1 to -1)
            Display.img_Show(hndl, iThermometer1) ; // Thermometer1
            checktouchscreen();
            Display.img_SetWord(hndl, iUserled1, IMAGE_INDEX, 0) ; // Userled1 where numx is 0 (Off) or 1 (On)
            Display.img_Show(hndl, iUserled1) ; // Userled1
            Display.img_SetWord(hndl, iUserled2, IMAGE_INDEX, 1) ; // Userled2 where numx is 0 (Off) or 1 (On)
            Display.img_Show(hndl, iUserled2) ; // Userled2
            Display.img_SetWord(hndl, iUserled3, IMAGE_INDEX, 1) ; // Userled3 where numx is 0 (Off) or 1 (On)
            Display.img_Show(hndl, iUserled3) ; // Userled3
          
          }
          
          
          
          void analogread()
          {
            ldrVal = analogRead(A0);
            ldrVal = map(ldrVal, 0, 1024, 0, 100) ;
          }
          
          
          
          void checktouchscreen()
          {
            // put your main code here, to run repeatedly:
          
          
            state = Display.touch_Get(TOUCH_STATUS); // get touchscreen status
            n = Display.img_Touched(hndl, -1) ;
          
            if (state == TOUCH_RELEASED) // if there's a press
            {
              if (n == i4Dbutton1 )
          
              {
                if (button == 0)
                {
                  Display.img_SetWord(hndl, i4Dbutton1, IMAGE_INDEX, button); // 4Dbutton1 where state is 0 for up and 1 for down
                  Display.img_Show(hndl, i4Dbutton1) ; // 4Dbutton1
                  button = 2;
                }
          
                else if (button == 2)
                {
                  Display.img_SetWord(hndl, i4Dbutton1, IMAGE_INDEX, button); // 4Dbutton1 where state is 0 for up and 1 for down
                  Display.img_Show(hndl, i4Dbutton1) ; // 4Dbutton1
                  button = 0;
                }
              }
            }
          }
          Last edited by James_4DSystems; 27th September 2016, 07:48 AM. Reason: Edited into a code box so its readable

          Comment


          • #6
            Hi Steve,

            In the setup part of your code, I added a line that disables touch on the form object (border).

            Since you are using a border:

            Code:
               Display.img_Show(hndl, iForm1) ; // Border1
            Workshop treats the whole form as a touchable object. Therefore, you need to disable touch for it during setup.

            Code:
             void setup()
            {
               pinMode(RESETLINE, OUTPUT);       // Display reset pin
            %%Display%%.Toggle_Reset_On ;       // *Replaced* at compile time with correct rest on logic for the attached display
              delay(100);                       // wait for it to be recognised
            %%Display%%.Toggle_Reset_Off ;      // *Replaced* at compile time with correct rest off logic for the attached display
            // Uncomment when using ESP8266
            //  pinMode(ESPRESET, OUTPUT);        // ESP reset pin
            //  digitalWrite(ESPRESET, 1);        // Reset ESP
            //  delay(100);                       // wait for it t
            //  digitalWrite(ESPRESET, 0);        // Release ESP reset
              delay(4000) ;                     // give display time to startup
              %%Display%%.DisplaySerial.Begin_Speed ; // *Replaced* at compile time with command to start the serial port at the correct speed
              Display.TimeLimit4D = 5000 ;       // 5 second timeout on all commands
                Display.Callback4D = mycallback ;
              Display.gfx_ScreenMode(PORTRAIT) ; // change manually if orientation change
            
            
              Display.putstr("Mounting...\n");
              if (!(Display.file_Mount()))
              {
                while(!(Display.file_Mount()))
                {
                  Display.putstr("Drive not mounted...");
                  delay(200);
                  Display.gfx_Cls();
                  delay(200);
                }
              }
            
            //hFontn = Display.file_LoadImageControl("NoName3.dnn", "NoName3.gnn", 1); // Open handle to access uSD fonts, uncomment if required and change nn to font number
            //hstrings = Display.file_Open("TI_SEN~1.txf", 'r') ;                            // Open handle to access uSD strings, uncomment if required
              hndl = Display.file_LoadImageControl("TI_SEN~1.dat", "TI_SEN~1.gci", 1);
            
              // put your setup code here, to run once:
              Display.gfx_Cls();
              pinMode(A0,INPUT);
            
              Display.img_Show(hndl,iForm1) ;  // Border1
              Display.img_Show(hndl,iImage1) ;  // Image1
              Display.gfx_LinePattern(LPDASHDOT) ;
            
              Display.gfx_LinePattern(LPDASHDOT) ;
              Display.gfx_Line(1, 160, 150, 160, SLATEGRAY) ;  // Line1
              Display.gfx_LinePattern(LPSOLID) ;
              Display.gfx_LinePattern(LPDASHDOT) ;
              Display.gfx_Line(1, 220, 150, 220, SLATEGRAY) ;  // Line2
              Display.gfx_LinePattern(LPSOLID) ;
            
              Display.img_Show(hndl,iStatictext1) ;  // Statictext1
              Display.img_Show(hndl,iStatictext2) ;  // Statictext2
              Display.img_Show(hndl,iStatictext3) ;  // Statictext3
              Display.img_Show(hndl,iStatictext4) ;  // Statictext4
            
              Display.img_Show(hndl,iUserled1) ;  // Userled1 show initialy, if required
              Display.img_Show(hndl,iUserled2) ;  // Userled2 show initialy, if required
              Display.img_Show(hndl,iUserled3) ;  // Userled3 show initialy, if required
              Display.img_Show(hndl, iLeddigits1);  // Leddigits1 show all digits at 0, only do this once
            
            
              Display.touch_Set(TOUCH_ENABLE);  // Enable the touchscreen
            
              Display.img_SetAttributes(hndl, iForm1, I_TOUCH_DISABLE); // iForm1 set to disable touch, only need to do this once
              Display.img_ClearAttributes(hndl, i4Dbutton1, I_TOUCH_DISABLE); // 4Dbutton1 set to enable touch, only need to do this once
              Display.img_Show(hndl, i4Dbutton1);                             // 4Dbutton1 show button, only do this once
            
            } // end Setup **do not alter, remove or duplicate this line**
            Here's the line that disables touch:

            Code:
              Display.img_SetAttributes(hndl, iForm1, I_TOUCH_DISABLE); // iForm1 set to disable touch, only need to do this once

            That should fix the problem.

            Best Regards,

            Juniel Cruz
            Last edited by Juniel Cruz; 27th September 2016, 07:46 PM.
            Juniel Cruz

            Comment

            Working...
            X