1
1
/*
2
2
* GIGA R1 - Audio Playback
3
3
* Simple wav format audio playback via 12-Bit DAC output by reading from a USB drive.
4
+ * In order for this sketch to work you need to rename 'USB_DRIVE_NAME' to the name of your USB stick drive.
4
5
*/
5
6
6
7
#include < USBHostMbed5.h>
@@ -14,55 +15,44 @@ AdvancedDAC dac1(A12);
14
15
USBHostMSD msd;
15
16
mbed::FATFileSystem usb (" USB_DRIVE_NAME" );
16
17
17
- void setup () {
18
+ void setup ()
19
+ {
18
20
Serial.begin (115200 );
19
21
while (!Serial);
20
22
21
23
pinMode (PA_15, OUTPUT);
22
24
digitalWrite (PA_15, HIGH);
23
25
24
- delay (2500 );
25
- Serial.println (" Starting USB File Read example..." );
26
+ Serial.println (" Please connect a USB stick to the GIGA's USB port ..." );
26
27
27
- while (!msd.connect ()) {
28
+ while (!msd.connect ())
28
29
delay (1000 );
29
- }
30
30
31
31
Serial.println (" Mounting USB device..." );
32
- int err = usb.mount (&msd);
33
- if (err) {
32
+ int const rc_mount = usb.mount (&msd);
33
+ if (rc_mount)
34
+ {
34
35
Serial.print (" Error mounting USB device " );
35
- Serial.println (err );
36
- while ( 1 ) ;
36
+ Serial.println (rc_mount );
37
+ return ;
37
38
}
38
39
39
- // Read
40
- Serial.print (" read done " );
41
- mbed::fs_file_t file;
42
- struct dirent *ent;
43
- int dirIndex = 0 ;
44
- int res = 0 ;
45
- Serial.println (" Open file.." );
40
+ Serial.println (" Opening audio file.." );
46
41
47
42
// 16-bit PCM Mono 16kHz realigned noise reduction
48
- FILE *f = fopen (" /USB_DRIVE_NAME/AUDIO_SAMPLE.wav" , " r+" );
43
+ FILE * f = fopen (" /USB_DRIVE_NAME/AUDIO_SAMPLE.wav" , " r+" );
44
+ if (f == nullptr )
45
+ {
46
+ Serial.print (" Error opening audio file: " );
47
+ Serial.println (strerror (errno));
48
+ return ;
49
+ }
49
50
50
51
// Crucial (from mBed)
51
52
wav_play_rl (f, dac1, false );
52
53
53
- // Close the file
54
- Serial.println (" File closing" );
55
- fflush (stdout);
56
- err = fclose (f);
57
- if (err < 0 ) {
58
- Serial.print (" fclose error: " );
59
- Serial.print (strerror (errno));
60
- Serial.print (" (" );
61
- Serial.print (-errno);
62
- Serial.print (" )" );
63
- } else {
64
- Serial.println (" File closed!" );
65
- }
54
+ // Cleanup.
55
+ fclose (f);
66
56
}
67
57
68
- void loop () {}
58
+ void loop () {}
0 commit comments