
SD leseing til array
Dependencies: SDFileSystem mbed
Fork of SDFileSystem_HelloWorld by
main.cpp@3:5a08f9ecfb16, 2017-04-24 (annotated)
- Committer:
- mathimat
- Date:
- Mon Apr 24 10:23:10 2017 +0000
- Revision:
- 3:5a08f9ecfb16
- Parent:
- 2:57156358b223
test;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:bdbd3d6fc5d5 | 1 | #include "mbed.h" |
mathimat | 3:5a08f9ecfb16 | 2 | #include <stdio.h> |
mathimat | 3:5a08f9ecfb16 | 3 | #include <string.h> |
mbed_official | 0:bdbd3d6fc5d5 | 4 | #include "SDFileSystem.h" |
mathimat | 3:5a08f9ecfb16 | 5 | #include "wavheader.h" |
mathimat | 1:d84eab100439 | 6 | |
mathimat | 1:d84eab100439 | 7 | SDFileSystem sd(p5, p6, p7, p20, "sd"); // the pinout on the mbed Cool Components workshop board |
mathimat | 3:5a08f9ecfb16 | 8 | SPI dac(p11,p12,p13); |
mathimat | 3:5a08f9ecfb16 | 9 | DigitalOut LR(p14); |
mathimat | 3:5a08f9ecfb16 | 10 | DigitalOut LED(LED1); |
mathimat | 3:5a08f9ecfb16 | 11 | DigitalOut LEDn(LED1); |
mathimat | 3:5a08f9ecfb16 | 12 | Ticker sampler; |
mathimat | 3:5a08f9ecfb16 | 13 | |
mathimat | 1:d84eab100439 | 14 | |
mathimat | 1:d84eab100439 | 15 | char filer[64][64]; |
mathimat | 3:5a08f9ecfb16 | 16 | int numberOfFiles = 0; //files in filer array |
mathimat | 3:5a08f9ecfb16 | 17 | int chuncksize = 0; |
mathimat | 3:5a08f9ecfb16 | 18 | int readern = 0; |
mathimat | 3:5a08f9ecfb16 | 19 | int playern = 0; |
mathimat | 3:5a08f9ecfb16 | 20 | int16_t databuf[1024]; |
mathimat | 3:5a08f9ecfb16 | 21 | int16_t sbuffer[1]; |
mathimat | 3:5a08f9ecfb16 | 22 | |
mathimat | 3:5a08f9ecfb16 | 23 | struct WAV_HEADER wavHeader; |
mathimat | 1:d84eab100439 | 24 | |
mathimat | 1:d84eab100439 | 25 | |
mathimat | 2:57156358b223 | 26 | void sdToArray(char directory[64]="/sd", char type[5] = ".wav") |
mathimat | 2:57156358b223 | 27 | { |
mathimat | 2:57156358b223 | 28 | DIR *d; |
mathimat | 1:d84eab100439 | 29 | struct dirent *dir; |
mathimat | 2:57156358b223 | 30 | d = opendir(directory); |
mathimat | 1:d84eab100439 | 31 | char buffer[64]; |
mathimat | 1:d84eab100439 | 32 | if (d) { |
mathimat | 1:d84eab100439 | 33 | while ((dir = readdir(d)) != NULL) { |
mathimat | 2:57156358b223 | 34 | //leser alle filnavn fra sd kort og leger de i en buffer |
mathimat | 1:d84eab100439 | 35 | sprintf(buffer, "%s", dir->d_name); |
mathimat | 2:57156358b223 | 36 | //sjekker at filen ikke starter med [ . ] og slutter med valgt type (wav). |
mathimat | 2:57156358b223 | 37 | //legger så disse i filer arrayet. |
mathimat | 2:57156358b223 | 38 | if (!strcmp(strrchr(buffer, '\0') - 4, type)) { |
mathimat | 1:d84eab100439 | 39 | if (strncmp(buffer, ".",1)) { |
mathimat | 2:57156358b223 | 40 | strcpy(filer[numberOfFiles], buffer); |
mathimat | 2:57156358b223 | 41 | numberOfFiles++; |
mathimat | 1:d84eab100439 | 42 | } |
mathimat | 1:d84eab100439 | 43 | } |
mathimat | 1:d84eab100439 | 44 | } |
mathimat | 1:d84eab100439 | 45 | |
mathimat | 1:d84eab100439 | 46 | closedir(d); |
mbed_official | 0:bdbd3d6fc5d5 | 47 | } |
mathimat | 2:57156358b223 | 48 | } |
mathimat | 1:d84eab100439 | 49 | |
mathimat | 3:5a08f9ecfb16 | 50 | void analyzeWAV(char filnavn[64]) |
mathimat | 3:5a08f9ecfb16 | 51 | { |
mathimat | 3:5a08f9ecfb16 | 52 | char dir[128] = "/sd/"; |
mathimat | 3:5a08f9ecfb16 | 53 | strcat(dir,filnavn); |
mathimat | 3:5a08f9ecfb16 | 54 | FILE *fil = fopen(dir, "rb"); |
mathimat | 3:5a08f9ecfb16 | 55 | if(fil!=NULL) { |
mathimat | 3:5a08f9ecfb16 | 56 | fread(&wavHeader,sizeof(wavHeader),1,fil); |
mathimat | 3:5a08f9ecfb16 | 57 | fclose(fil); |
mathimat | 3:5a08f9ecfb16 | 58 | printf("riff: %s \r\n", wavHeader.RIFF); |
mathimat | 3:5a08f9ecfb16 | 59 | printf("wave: %s \r\n", wavHeader.WAVE); |
mathimat | 3:5a08f9ecfb16 | 60 | printf("Samples: %d \r\n", wavHeader.SamplesPerSec); |
mathimat | 3:5a08f9ecfb16 | 61 | printf("bitprsamp: %d \r\n", wavHeader.bitsPerSample); |
mathimat | 3:5a08f9ecfb16 | 62 | printf("channels: %d \r\n", wavHeader.NumOfChan); |
mathimat | 3:5a08f9ecfb16 | 63 | } |
mathimat | 3:5a08f9ecfb16 | 64 | printf("funcedn"); |
mathimat | 3:5a08f9ecfb16 | 65 | fclose(fil); |
mathimat | 3:5a08f9ecfb16 | 66 | } |
mathimat | 3:5a08f9ecfb16 | 67 | |
mathimat | 3:5a08f9ecfb16 | 68 | void play() |
mathimat | 3:5a08f9ecfb16 | 69 | { |
mathimat | 3:5a08f9ecfb16 | 70 | if (playern<readern) { |
mathimat | 3:5a08f9ecfb16 | 71 | LR = 0; |
mathimat | 3:5a08f9ecfb16 | 72 | dac.write(sbuffer[1]); |
mathimat | 3:5a08f9ecfb16 | 73 | LR = 1; |
mathimat | 3:5a08f9ecfb16 | 74 | dac.write(sbuffer[1]); |
mathimat | 3:5a08f9ecfb16 | 75 | LEDn != LEDn; |
mathimat | 3:5a08f9ecfb16 | 76 | } |
mathimat | 3:5a08f9ecfb16 | 77 | } |
mathimat | 2:57156358b223 | 78 | int main() |
mathimat | 2:57156358b223 | 79 | { |
mathimat | 3:5a08f9ecfb16 | 80 | numberOfFiles=0; |
mathimat | 2:57156358b223 | 81 | sdToArray(); |
mathimat | 2:57156358b223 | 82 | printf("WAV filer:\r\n"); |
mathimat | 3:5a08f9ecfb16 | 83 | for(int i = 0; i < numberOfFiles; i++) { |
mathimat | 2:57156358b223 | 84 | printf("%d | %s \r\n",i,filer[i]); |
mathimat | 1:d84eab100439 | 85 | } |
mathimat | 3:5a08f9ecfb16 | 86 | printf("Skriv filnummer:"); |
mathimat | 3:5a08f9ecfb16 | 87 | int nummer; |
mathimat | 3:5a08f9ecfb16 | 88 | scanf("%d", &nummer); |
mathimat | 3:5a08f9ecfb16 | 89 | printf("%d - %s \r\n", nummer, filer[nummer]); |
mathimat | 3:5a08f9ecfb16 | 90 | printf("\r\n END!! \r"); |
mathimat | 3:5a08f9ecfb16 | 91 | |
mathimat | 3:5a08f9ecfb16 | 92 | // |
mathimat | 3:5a08f9ecfb16 | 93 | float frekvensplay = 1.0/wavHeader.SamplesPerSec; |
mathimat | 3:5a08f9ecfb16 | 94 | printf("attacher-"); |
mathimat | 3:5a08f9ecfb16 | 95 | sampler.attach(&play,frekvensplay); |
mathimat | 3:5a08f9ecfb16 | 96 | printf("spiller-"); |
mathimat | 1:d84eab100439 | 97 | |
mathimat | 3:5a08f9ecfb16 | 98 | char dir[128] = "/sd/"; |
mathimat | 3:5a08f9ecfb16 | 99 | strcat(dir,filer[nummer]); |
mathimat | 3:5a08f9ecfb16 | 100 | FILE *fip = fopen(dir, "rb"); |
mathimat | 3:5a08f9ecfb16 | 101 | if(fip==NULL) { |
mathimat | 3:5a08f9ecfb16 | 102 | printf("error opening wav %c - \r\n", dir); |
mathimat | 3:5a08f9ecfb16 | 103 | } |
mathimat | 3:5a08f9ecfb16 | 104 | fseek(fip, SEEK_SET, 0); |
mathimat | 3:5a08f9ecfb16 | 105 | while(1) { |
mathimat | 3:5a08f9ecfb16 | 106 | fread(sbuffer, 2,1,fip); |
mathimat | 3:5a08f9ecfb16 | 107 | } |
mbed_official | 0:bdbd3d6fc5d5 | 108 | } |