Skip to content

Commit 31a9070

Browse files
committed
Cleaning up example file.
1 parent eee2c29 commit 31a9070

File tree

1 file changed

+21
-31
lines changed

1 file changed

+21
-31
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* GIGA R1 - Audio Playback
33
* 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.
45
*/
56

67
#include <USBHostMbed5.h>
@@ -14,55 +15,44 @@ AdvancedDAC dac1(A12);
1415
USBHostMSD msd;
1516
mbed::FATFileSystem usb("USB_DRIVE_NAME");
1617

17-
void setup() {
18+
void setup()
19+
{
1820
Serial.begin(115200);
1921
while (!Serial);
2022

2123
pinMode(PA_15, OUTPUT);
2224
digitalWrite(PA_15, HIGH);
2325

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 ...");
2627

27-
while (!msd.connect()) {
28+
while (!msd.connect())
2829
delay(1000);
29-
}
3030

3131
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+
{
3435
Serial.print("Error mounting USB device ");
35-
Serial.println(err);
36-
while (1);
36+
Serial.println(rc_mount);
37+
return;
3738
}
3839

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..");
4641

4742
// 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+
}
4950

5051
// Crucial (from mBed)
5152
wav_play_rl(f, dac1, false);
5253

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);
6656
}
6757

68-
void loop() {}
58+
void loop() {}

0 commit comments

Comments
 (0)