SD leseing til array

Dependencies:   SDFileSystem mbed

Fork of SDFileSystem_HelloWorld by mbed official

Committer:
mathimat
Date:
Tue Apr 18 08:32:29 2017 +0000
Revision:
2:57156358b223
Parent:
1:d84eab100439
Child:
3:5a08f9ecfb16
n? med listing av filer;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:bdbd3d6fc5d5 1 #include "mbed.h"
mbed_official 0:bdbd3d6fc5d5 2 #include "SDFileSystem.h"
mathimat 1:d84eab100439 3
mathimat 1:d84eab100439 4 SDFileSystem sd(p5, p6, p7, p20, "sd"); // the pinout on the mbed Cool Components workshop board
mathimat 1:d84eab100439 5
mathimat 1:d84eab100439 6 char filer[64][64];
mathimat 2:57156358b223 7 int numberOfFiles = 0;
mathimat 1:d84eab100439 8
mathimat 1:d84eab100439 9
mathimat 2:57156358b223 10 void sdToArray(char directory[64]="/sd", char type[5] = ".wav")
mathimat 2:57156358b223 11 {
mathimat 2:57156358b223 12 DIR *d;
mathimat 1:d84eab100439 13 struct dirent *dir;
mathimat 2:57156358b223 14 d = opendir(directory);
mathimat 1:d84eab100439 15 char buffer[64];
mathimat 1:d84eab100439 16 if (d) {
mathimat 1:d84eab100439 17 while ((dir = readdir(d)) != NULL) {
mathimat 2:57156358b223 18 //leser alle filnavn fra sd kort og leger de i en buffer
mathimat 1:d84eab100439 19 sprintf(buffer, "%s", dir->d_name);
mathimat 2:57156358b223 20 //sjekker at filen ikke starter med [ . ] og slutter med valgt type (wav).
mathimat 2:57156358b223 21 //legger så disse i filer arrayet.
mathimat 2:57156358b223 22 if (!strcmp(strrchr(buffer, '\0') - 4, type)) {
mathimat 1:d84eab100439 23 if (strncmp(buffer, ".",1)) {
mathimat 2:57156358b223 24 strcpy(filer[numberOfFiles], buffer);
mathimat 2:57156358b223 25 numberOfFiles++;
mathimat 1:d84eab100439 26 }
mathimat 1:d84eab100439 27 }
mathimat 1:d84eab100439 28 }
mathimat 1:d84eab100439 29
mathimat 1:d84eab100439 30 closedir(d);
mbed_official 0:bdbd3d6fc5d5 31 }
mathimat 2:57156358b223 32 }
mathimat 1:d84eab100439 33
mathimat 2:57156358b223 34 int main()
mathimat 2:57156358b223 35 {
mathimat 2:57156358b223 36 sdToArray();
mathimat 2:57156358b223 37 printf("WAV filer:\r\n");
mathimat 2:57156358b223 38 for(int i =0; i < numberOfFiles;i++) {
mathimat 2:57156358b223 39 printf("%d | %s \r\n",i,filer[i]);
mathimat 1:d84eab100439 40 }
mathimat 1:d84eab100439 41 return(0);
mathimat 1:d84eab100439 42
mathimat 1:d84eab100439 43
mbed_official 0:bdbd3d6fc5d5 44 }