简体   繁体   中英

Reading and selecting files using dirent.h in C

I'm new into programming microcontrollers, and I have a problem.

I'm trying to make a device that plays music from USB. I can read from USB, but I have no idea how to choose a certain file. I'm using dirent .

My code so far:

while (true) {
    USBHostMSD msd("usb");
     //setup PWM hardware for a Class D style audio output
    PWMout.period(1.0/400000.0);

    // wait until connected to a USB device
    while(!msd.connect()) {
        Thread::wait(500);
    }

    FILE *wave_file;
    lcd.cls();
    lcd.locate(0,3);


    DIR *dir;
    struct dirent *ent;
    int i=0;
    int stevilo_datotek =0;


    dir = opendir ("/usb/");
    if (dir != NULL) 
    {


        while ((ent = readdir (dir)) != NULL) 
        {   
              lcd.printf ("%s\n", ent->d_name[0]);  
        }
}

Now this code displays what's on USB. How can I navigate through files on USB using the buttons on the device? I'd like to know if there's a way to assign certain song a certain number so I can navigate. I've studied dirent.h file for so long, and I can't find where dirent saves the file order (if it does).

You may be confusing the purpose of dirent.h In short it may be difficult to see the forest for the trees.

You can read the information (ent->d_name, note that ent->d_name is a pointer to an array of chars, often called a 'string') into a data structure (like an array or a list), then use that struct with code that detects button presses to move an index into the array information either up or down (to a greater or lesser index, make sure to check that your index doesn't go outside the range or your structure). Or you can create code where your while loop waits on a button press and only reads the file name then (using seekdir to go backwards).

UPDATE (to response to comment): Keep in mind that filesystems are tree structures like so:

 / (root) +--- dir1 ----------------------------+- dir1.1 ---- dir2 (empty) -- file1.1 ---- dir3 ---- dir3.1 +--- file3.1 ---- file1 ---- file3.2 ---- file2 

You have to decide how to handle that, are you going to only support one directory (make them put all their music files in one place), allow them to navigate directories, or look through all directories selecting only files you know how to play?

There is no inherit order to the files (or sub-directories) and in some systems files can be added or deleted at any time.

Here is a very simple-minded example of one way to keep the list of directory entries:

char *names[400]; // make space for 400 names
int ix;
ix = 0;
if (dir != NULL) 
{
     while ((ent = readdir (dir)) != NULL) 
     {   
           lcd.printf ("%s\n", ent->d_name[0]);  
           // allocate memory to store the name
           names[ix] = (char*) malloc(strlen(ent->d_name)); // strlen from string.h 
                                                            // malloc from stdlib.h
           // copy the name from the directory entry
           strcpy(names[ix], ent->d_name); // strcpy from string.h
           ix++;
           if (ix >= 400) 
           {
               // do something because your array isn't big enough
           }
     }
}

Now you have your names in the array 'names' and can address them by index. The value 'ix-1' is your last name, 0 is your first name. Your button presses can increment/decrement the index into your name array which identifies the name you want. Keep in mind that some of those names might be directory names rather than file names.

Admittedly this is simply-minded, you might want to allocation the array rather than use a fixed value (in fact you have to if you want to pass the 'names' array to a calling function), there are "secure" versions of strcpy (meant to help prevent memory overflow corruption), etc. but it should give you an idea about how you can keep the names in memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM