Fix fileselect0r (no files, down-scroll)

This commit is contained in:
Stefan `Sec` Zehl 2011-08-01 23:51:08 +02:00
parent 42fd4f116c
commit 4ed86ed5bc
1 changed files with 56 additions and 34 deletions

View File

@ -13,29 +13,30 @@ int getFiles(char files[][FLEN], uint8_t count, uint16_t skip, char *ext)
DIR dir; /* Directory object */ DIR dir; /* Directory object */
FILINFO Finfo; FILINFO Finfo;
FRESULT res; FRESULT res;
int ctr;
int pos = 0; int pos = 0;
int extlen = strlen(ext);
res = f_opendir(&dir, "0:"); res = f_opendir(&dir, "0:");
if(res){ if(res){
//lcdPrint("OpenDir:"); lcdPrintln(f_get_rc_string(res)); lcdRefresh(); //lcdPrint("OpenDir:"); lcdPrintln(f_get_rc_string(res)); lcdRefresh();
return 0; return 0;
}; };
ctr=0; while(f_readdir(&dir, &Finfo) == FR_OK && Finfo.fname[0]){
while(1){
res = f_readdir(&dir, &Finfo);
if ((res != FR_OK) || !Finfo.fname[0])
break;
int len=strlen(Finfo.fname); int len=strlen(Finfo.fname);
int extlen = strlen(ext);
if(len<extlen)
continue;
if( strcmp(Finfo.fname+len-extlen, ext) != 0) if( strcmp(Finfo.fname+len-extlen, ext) != 0)
continue; continue;
if (Finfo.fattrib & AM_DIR) if (Finfo.fattrib & AM_DIR)
continue; continue;
if( ctr++ < skip ) if( skip>0 ){
skip--;
continue; continue;
};
strcpy(files[pos++],Finfo.fname); strcpy(files[pos++],Finfo.fname);
if( pos == count ) if( pos == count )
break; break;
@ -43,6 +44,7 @@ int getFiles(char files[][FLEN], uint8_t count, uint16_t skip, char *ext)
return pos; return pos;
} }
#define PERPAGE 7
int selectFile(char *filename, char *extension) int selectFile(char *filename, char *extension)
{ {
int skip = 0; int skip = 0;
@ -50,13 +52,29 @@ int selectFile(char *filename, char *extension)
int selected = 0; int selected = 0;
font=&Font_7x8; font=&Font_7x8;
while(1){ while(1){
char files[7][FLEN]; char files[PERPAGE][FLEN];
int count = getFiles(files, 7, skip, extension); int count = getFiles(files, PERPAGE, skip, extension);
if(!count){
lcdPrintln("No Files?");
lcdRefresh();
getInputWait();
getInputWaitRelease();
return -1;
};
if(count<PERPAGE && selected==count){
skip--;
continue;
};
redraw: redraw:
if( count )
lcdClear(); lcdClear();
lcdPrintln("Select file:"); lcdPrint("Select file:");
lcdSetCrsrX(40);
lcdPrint(IntToStr(skip,1,0));
lcdPrint("/");
lcdPrint(IntToStr(selected,1,0));
lcdNl();
for(int i=0; i<count; i++){ for(int i=0; i<count; i++){
if( selected == i ) if( selected == i )
lcdPrint("*"); lcdPrint("*");
@ -67,14 +85,16 @@ int selectFile(char *filename, char *extension)
lcdRefresh(); lcdRefresh();
key=getInputWait(); key=getInputWait();
getInputWaitRelease(); getInputWaitRelease();
if( key==BTN_DOWN ){ switch(key){
case BTN_DOWN:
if( selected < count-1 ){ if( selected < count-1 ){
selected++; selected++;
goto redraw; goto redraw;
}else{ }else{
skip++; skip++;
} }
}else if( key==BTN_UP ){ break;
case BTN_UP:
if( selected > 0 ){ if( selected > 0 ){
selected--; selected--;
goto redraw; goto redraw;
@ -83,9 +103,11 @@ int selectFile(char *filename, char *extension)
skip--; skip--;
} }
} }
}else if( key==BTN_LEFT ){ break;
return 1; case BTN_LEFT:
}else if( key==BTN_RIGHT ){ return -1;
case BTN_ENTER:
case BTN_RIGHT:
strcpy(filename, files[selected]); strcpy(filename, files[selected]);
return 0; return 0;
} }