TFT scrolling menu wrap question

Hi guys,

I have this question posted in the Programming section (tried to delete it but can’t) also. I have an Adafruit 1.8 TFT w/SD card + Mega and I’ve *modified some code I found to create a menu system. One of the menu items brings up a list of files on the SD card. Hitting Down or Up causes the list to scroll appropriately.

However, it is possible to have the list of files ‘wrap’ around itself when it hits the top number in the array? Eventually, I have it coded to do nothing if up is pressed and the list is at #1, and do nothing if down is pressed and its at 127.

just curious. I’ll attach the entire menu structure sketch but post only the relevant code:
edit: a video link, showing the menu Arduino TFT menu - YouTube

the code here shows how I setup the array of files on the SD card and also how I draw the list of 10 files at a time. I feel like there’s a better way to do this.

// Check the joystick position
int CheckJoystick()
{
  int joystickState = analogRead(3);

  if (joystickState < 50) return Down;
  if (joystickState < 150) return Right;
  if (joystickState < 250) return Press;
  if (joystickState < 500) return Up;
  if (joystickState < 650) return Left;
  return Neutral;
} 

enum COLORS { 	
  BLACK = 0x0000, BLUE = 0x001F, RED = 0xF800, ORANGE = 0xFA60, GREEN = 0x07E0,
  CYAN = 0x07FF, MAGENTA = 0xF81F, YELLOW = 0xFFE0, GRAY = 0xCCCC, WHITE = 0xFFFF
};

enum arrows { 
  uarr = 0x18, darr = 0x19, larr = 0x1b, rarr = 0x1a, upyr = 0x1e, dpyr = 0x1f, lpyr = 0x11, rpyr = 0x10 };


////////////////////////////////////////////////////////////////////////////////
//  size 1 template:	12345678901234567890123456 <-- if last char is ON 26, \n not req'd; driver inserts it
//static char version[]={"simpleMenu.ino 2013SEP12"};
char textx[64];  // Made buffer big enough to hold two lines of text
////////////////////////////////////////////////////////////////////////////////
//  Static menu manager
#define MAX_SELECTIONS 5
#define MAX_PROMPT_LEN 20

char  nameFiles[128][20];
char name[20];
String strname = "";
SdFile root;
int filePOS = 0;
int fileIndex = 0;
int listStart = 1;
int maxmidicount;
/////////////////////////
//////////////
void clearFilelist(){
  tft.drawRect(0, 60, 128, 100, BLACK);
  tft.fillRect(0, 60 , 128, 100, BLACK); 
}

int topfile(int pos, int start, int index){
  
  int result;
  pos = filePOS;
  start = listStart;
  index = fileIndex;
  
if (filePOS>120){
  result = 7;
}
else
{
  result = 10; 
}
  return result;
}


//int maxfile = topfile(filePOS, listStart, fileIndex);
void drawFilelist(){
  clearFilelist();
  char msg[64];
  sprintf(msg, "listStart: %d\n filePOS: %d\n fileIndex: %d\n", listStart,  filePOS, fileIndex);
  Serial.print(msg);

  tft.setCursor(0,60);
   int maxfile = topfile(filePOS, listStart, fileIndex);
    Serial.println(maxfile);
//for(fileIndex = listStart; fileIndex < (listStart+10); fileIndex++){
     for(fileIndex = listStart; fileIndex < (listStart+maxfile); fileIndex++){
    if (fileIndex == filePOS) {
      tft.setTextColor(GREEN);
    }
    else
    {
      tft.setTextColor(WHITE);
    }


    tft.print(fileIndex);  
    tft.print(" ");
    tft.println(nameFiles[fileIndex]) ;
  }
}

void IndexFileNames(){
  //char test1[20];
  SD.vwd()->rewind();
  while (root.openNext(SD.vwd(), O_READ)) {
    root.getFilename(name);
    // Serial.println(name);
    root.close();
    strname = name;

    if (strname.endsWith(".MID")    ) {
      fileIndex++;          
      for (int i = 0; i<20; i++){

        if (name[i] != 0x00){
          nameFiles[fileIndex][i] = name[i];

        }

      }   
    }     
  }

  tft.setCursor(0,60);
maxmidicount = fileIndex;
}

this code shows what actually happens when I press up or down.

void select_midi(int key){

  tft.setCursor(0,60);

  drawFilelist();

  for( uint8_t b = CheckJoystick(); b != Press; b = CheckJoystick() ) {
    if (b == Down){
      /////////////////////
     if (filePOS<127){
        if (filePOS>=(listStart+9) ){
          filePOS++;
          //listStart = filePOS;   
       listStart = listStart+1   ;
          drawFilelist();
          Serial.println("new page");
        }
        else
        {
          filePOS++;
          drawFilelist();
          Serial.println("goin down");
        }

     }


    }
    //////////////////////////
    else  if (b==Up){
if (filePOS>1){
      if (filePOS<=(listStart)){
        filePOS--;
       // listStart = filePOS-9;
       listStart = listStart-1;
        drawFilelist();
      }
      else
      {
        filePOS--;
        drawFilelist();
      }


    }}
    //
  }
  cls();
  draw_menu();
}

menu_structs.ino (15.7 KB)