UTFT String - end of string final character strange behaviour

Hello everyone

I trust that you all are well today. I’m running into a strange occurrence that I can’t figure out. Please see my attached photo (Sorry I had resize my photo to get the size small enough to be able to upload). What happens is that I have navigation keys in the bottom right corner. The navigation keys allows me to navigate within the string and then insert additional characters where I want characters.

If I insert characters within the string then I get the garbled pixels at the end of the string. If I don’t insert characters into the string and just add a character to the end of the string every time then this garbled pixels after the string doesn’t appear.

I’m hoping that you guys and gals will be able to give me some suggestions as to what to look out for. Thank you in advance.

Regards
Dirk

Code below:

void updateStr(int val, int Trigger, int ASCII_DEC)
{
  myGLCD.setFont(Grotesk16x32);
  myGLCD.setBackColor(VGA_WHITE);
  myGLCD.setColor(Button_Text_Color);     
  myGLCD.printNumI((stPosition * -1) ,100, 35);

  //************************************************************************************************************************************************************************************************************************************************************
    
  if ( stPosition < 0 )               // Used for navigation in the string using the arrow keys and only triggered if we try to enter a char into the string whil stPosition != 0
  { 
    if (stCurrentLen < 41)
    {
      if (stCurrentLen < 42)          // Have to have this section before the incrementation of the string length otherwise the previous character indication line will not be erased.
      {
        myGLCD.setColor(VGA_WHITE);
        myGLCD.fillRect(Button_X_Begin + 12 + (16 * (stCurrentLen + stPosition)),Button_Y_Begin - 15, Button_X_Begin + 12 + (16 * (stCurrentLen + stPosition)) + 16,Button_Y_Begin - 13);
        myGLCD.setColor(Button_Text_Color);      
      }
      
      stCurrentLen++;                // Increment the length before entering into the for statement. We need to move all the characters one to the right so we first need to increment the string length.
      
      for( int i = 0; i < (stPosition * -1); i++ )      //  For statement used to move the characters to the right so that we don't write over them
      {
        stCurrent[stCurrentLen - i ] = stCurrent[stCurrentLen - 1 - i ];     //  Move each character from it's current cell to the next cell.

        if(stSpecTrig[stCurrentLen - i] == 1)                 //    Check whether a special character has been selected or not. If so change the font so as to print the special character. If not leave the font to the normal texxt font.
        {
          myGLCD.setFont(Various_Symbols_16x32);
        }
        
        else
        {
          myGLCD.setFont(Grotesk16x32);
        }

        myGLCD.printChar(stCurrent[stCurrentLen - i],Button_X_Begin + 12 + (16 * (stCurrentLen - i)),Button_Y_Begin - 49);      //used to print the moved characters
      }

      stCurrent[stCurrentLen+1]='\0';   // If we are moving within the string then we need to move the null termination character as well.
      //myGLCD.printChar(' ',Button_X_Begin + 12 + (16 * (stCurrentLen)),Button_Y_Begin - 49);
      
      stCurrent[stCurrentLen + stPosition]= val;                     //  Now that all the characters have been shifted forward by one position store the new character into the current cell.
      stSpecTrig[stCurrentLen + stPosition] = Trigger;

      if(stSpecTrig[stCurrentLen + stPosition] == 1)                 //    Check whether a special character has been selected or not. If so change the font so as to print the special character. If not leave the font to the normal texxt font.
      {
        myGLCD.setFont(Various_Symbols_16x32);
      }
        
      else
      {
        myGLCD.setFont(Grotesk16x32);
      }

      myGLCD.printChar(stCurrent[stCurrentLen + stPosition],Button_X_Begin + 12 + (16 * (stCurrentLen + stPosition)),Button_Y_Begin - 49);     // Now we can print the newly inserted character into the inside of the string somewhere.
    }
  }
    
  //************************************************************************************************************************************************************************************************************************************************************

  else         // Normal operation i.e. no navigation within the string    // If there are no navigation in the string then the characters are added as per normal to the end of the array.
  { 
    if (stCurrentLen < 42)
    {  
      stCurrent[stCurrentLen]= val;
      stCurrent[stCurrentLen+1]='\0';
      stSpecTrig[stCurrentLen] = Trigger;

      if (stCurrentLen < 42)     //  Removes previous character indication line
      {
        myGLCD.setColor(VGA_WHITE);
        myGLCD.fillRect(Button_X_Begin + 12 + (16 * stCurrentLen),Button_Y_Begin - 15, Button_X_Begin + 12 + (16 * stCurrentLen) + 16,Button_Y_Begin - 13);
        myGLCD.setColor(Button_Text_Color); 
      }
          
      if(stSpecTrig[stCurrentLen] == 1)                 //    Check whether a special character has been selected or not. If so change the font so as to print the special character. If not leave the font to the normal texxt font.
      {
        myGLCD.setFont(Various_Symbols_16x32);
      }
      else
      {
        myGLCD.setFont(Grotesk16x32);
      }

      myGLCD.printChar(stCurrent[stCurrentLen],Button_X_Begin + 12 + (16 * stCurrentLen),Button_Y_Begin - 49);  // Print the last and newest character added to the string.
      stCurrentLen++;  // Increment the string length.
    }
  }

  if (stCurrentLen < 42)
  {
    myGLCD.fillRect(Button_X_Begin + 12 + (16 * (stCurrentLen + stPosition)),Button_Y_Begin - 15, Button_X_Begin + 12 + (16 * (stCurrentLen + stPosition)) + 16,Button_Y_Begin - 13); // Insert a new character indication line
  }
    
  x = 0;
  y = 0;

  delay(100);
    
}

Forgot to mention. I'm using 5" TFT LCD display with the TFT/SD Shield for an Arduino Due on an Arduino DUE. Let me know if there are any questions.

Regards Dirk

No help on the problem as I’m not familiar with the library.

A quick look at the code gives

  if ( stPosition < 0 )               // Used for navigation in the string using the arrow keys and only triggered if we try to enter a char into the string whil stPosition != 0
  {
    if (stCurrentLen < 41)
    {
      if (stCurrentLen < 42)          // Have to have this section before the incrementation of the string length otherwise the previous character indication line will not be erased.
      {

The last if is redundant; you test first if stCurrentLen is less than 41 and hence it will always be less than 42 :wink:

===

Would it not be easier to use a memmove to move all characters (including nul character) one position in one go (instead of one-by-one)?

Hey Sterretje

Thanks for the input. I've tried to curb the problem by printing a blank character over the garbled pixels. It does work but I get a flicker off the garbled pixels just before my blank character draws over it and the blank character prints over my text field frame if I reach the final character.

 //myGLCD.printChar(' ',Button_X_Begin + 12 + (16 * (stCurrentLen)),Button_Y_Begin - 49);

So I implemented the first if statement to prevent the blank character from drawing over the frame of the text field. It is temporary until I can remove the problem of the garbled pixels.

I wasn't aware of the memmove function. However it is working by moving the characters one by one - sort off - so I don't see that I want to break the code by trying to implement new functions now :)

bump to the top

I figured this out, my for statement should have started counting at 1 instead of at 0. This resolved the problem for me.