TFT vertically scrolling menu/list

hi guys,

The code I have works, but I feel like there’s a better way (with modulus division?) to “wrap” my list back around to itself.

I have an array with a max of 128 items. The menu displays 10 items at a time. Each time the position is higher than 10th item listed, it scrolls down. Works for Up as well.

A nice to have would be to have the list wrap around itself. so if I press up when I’m at 1, it would list 120 through 1.

I garauntee there’s a better way than how I’m doing it. So, fair warning, my code is noob stuff probably.

//  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;
}
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;      
          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;
        drawFilelist();
      }
      else
      {
        filePOS--;
        drawFilelist();
      }


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