1

I Use ELM Chan fatfs library for file system. This application reads a USB memory MSC. I want to read all the MP3 files in root directory in a sorted manner but I can not use an array to save all the file paths, Number of files exceeds ram memory of my microcontroller. I have the bellow idea.

  1. function that save the index of the MP3 files in an (array of indexes)
  2. A function that gets file name based on the f_readdir index that was saved in (array of indexes)
  3. Sort (array of indexes) based on the bubble sort.
  4. read file based on this sorted indexes array

Is This a viable solution? I think this method is really slow

Nima Aghayan
  • 109
  • 1
  • 7
  • Even for couple thousands elements bubble sort should be fast enough. You could also consider insertion sort or heap sort for in place sorting. – Kamil.S Dec 10 '22 at 11:49
  • In this suggestion I need to call f_readdir for example 10 times to read a file name. – Nima Aghayan Dec 10 '22 at 12:29
  • If you can't use a static array why not use heap and strdup the names? if you really haven't got enough RAM for that then you could create an temporary index file on the medium itself and keep indicies as you suggest. It could be marginally faster on a drive with hundreds of files, in exchange for many times more complicated software. – Tom V Dec 10 '22 at 16:16
  • @TomV If the system does no have enough RAM for a static array, how could it provide the space on the heap, which is in RAM, too? – the busybee Dec 11 '22 at 16:38
  • 1
    If you cannot save the filenames in RAM, you need to do this somewhere else. And this is most simply the file system. Yes, you will need many reads of files or directories, then. Is your RAM big enough to store -let's say- up to 10 integers per file? Then some of the suggested sort methods work with indexes. – the busybee Dec 11 '22 at 16:42
  • @thebusybee obviously you can't make memory from nothing but doing it that way is potentially a lot more efficient. If you use a 2D array you need number of names * (maximum length+1), but if the maximum file name length of a name is triple the average file name length then you need one third as much memory in total. – Tom V Dec 11 '22 at 22:12

0 Answers0