Skip to content

Commit

Permalink
Updating examples. Adding example code into examples folder by request.
Browse files Browse the repository at this point in the history
  • Loading branch information
NachtRaveVL committed Mar 20, 2017
1 parent daf566b commit 8e580b1
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 138 deletions.
282 changes: 144 additions & 138 deletions README.md
Expand Up @@ -32,153 +32,158 @@ In BY8X01-16P.h:

Make sure to flip RX/TX lines when plugging into device from MCU. If running a 5v Arduino board, put a 1k Ohm resistor between the MCU's TX and device's RX pin (not required if on a 3.3v device). Also, remove A, B, and C resistors on device (factory default is a resistor on A and C, while B is left open), which puts the device into the recommended 1-1-1 mode used for MCU serial control. Busy pin returns a 2.8v signal when playback is active (just enough for 5v boards to register as logic level HIGH), and is optional for library usage.

## Example Usage

Below are several examples of library usage.

### Simple Example
```Arduino
#include "BY8X01-16P.h"
BY8X0116P audioController; // Library using default Serial1 UART and no busy pin hookup
void setup() {
Serial1.begin(9600); // Serial1 must be started first - only supported UART baud rate is 9600
audioController.init(); // Initializes module
audioController.setVolume(20); // Sets player volume to 20 (out of 30 max)
audioController.play(); // Starts playback of loaded tracks
}
```

### Combination Playback Example

In this example, files are loaded onto the MicroSD and queued using the special playFileIndex method, which allows up to 10 songs to be queued for playback. Index is prescribed by the FAT file system, and is generally in the order that the files were copied to the flash drive, but not guaranteed. Indexing runs across all files in every subfolder. A file sorter software program (such as "DriveSort" or "FAT32 Sorter") should be used if specific file index order for playback is required.

```Arduino
#include "BY8X01-16P.h"
BY8X0116P audioController; // Library using default Serial1 UART and no busy pin hookup
void setup() {
Serial.begin(115200);
Serial1.begin(9600); // Serial1 must be started first - only supported UART baud rate is 9600
audioController.init(); // Initializes module
audioController.setEqualizerProfile(BY8X0116P_EqualizerProfile_Rock); // Sets player equalizer profile to Rock
audioController.playFileIndex(0); // Queues first file on MicroSD card for playback
audioController.playFileIndex(1); // Queues second file on MicroSD card for playback
audioController.playFileIndex(2); // Queues third file on MicroSD card for playback
audioController.playFileIndex(3); // Queues fourth file on MicroSD card for playback
audioController.playFileIndex(4); // Queues fifth file on MicroSD card for playback
audioController.waitPlaybackFinished(); // Blocking call that waits until all songs have completed
Serial.println("All done!");
}
```

### Indexed Playback Example

In this example, folders are named "00" through "99" and files inside them are named "001.mp3" (or .wav) through "255.mp3" (or .wav). The folder and file index passed into playFolderFileIndex will play that specific file. Note that combination play here is not supported so waiting between track plays is required. Having a busy pin connected here will allow for tighter timing control between file playbacks.

```Arduino
#include "BY8X01-16P.h"
## Example Usage

Below are several examples of library usage.

### Simple Example
```Arduino
#include "BY8X01-16P.h"
BY8X0116P audioController; // Library using default Serial1 UART and no busy pin hookup
void setup() {
Serial1.begin(9600); // Serial1 must be started first - only supported UART baud rate is 9600
audioController.init(); // Initializes module
audioController.setVolume(20); // Sets player volume to 20 (out of 30 max)
audioController.play(); // Starts playback of loaded tracks
}
```

### Combination Playback Example

In this example, files are loaded onto the MicroSD and queued using the special playFileIndex method, which allows up to 10 songs to be queued for playback. Index is prescribed by the FAT file system, and is generally in the order that the files were copied to the flash drive, but not guaranteed. Indexing runs across all files in every subfolder. A file sorter software program (such as "DriveSort" or "FAT32 Sorter") should be used if specific file index order for playback is required.

```Arduino
#include "BY8X01-16P.h"
BY8X0116P audioController; // Library using default Serial1 UART and no busy pin hookup
void setup() {
Serial.begin(115200);
Serial1.begin(9600); // Serial1 must be started first - only supported UART baud rate is 9600
audioController.init(); // Initializes module
audioController.setEqualizerProfile(BY8X0116P_EqualizerProfile_Rock); // Sets player equalizer profile to Rock
audioController.playFileIndex(0); // Queues first file on MicroSD card for playback
audioController.playFileIndex(1); // Queues second file on MicroSD card for playback
audioController.playFileIndex(2); // Queues third file on MicroSD card for playback
audioController.playFileIndex(3); // Queues fourth file on MicroSD card for playback
audioController.playFileIndex(4); // Queues fifth file on MicroSD card for playback
audioController.waitPlaybackFinished(); // Blocking call that waits until all songs have completed
Serial.println("All done!");
}
```

### Indexed Playback Example

In this example, folders are named "00" through "99" and files inside them are named "001.mp3" (or .wav) through "255.mp3" (or .wav). The folder and file index passed into playFolderFileIndex will play that specific file. Note that combination play here is not supported so waiting between track plays is required. Having a busy pin connected here will allow for tighter timing control between file playbacks.

```Arduino
#include "BY8X01-16P.h"
const byte busyPin = 22;
BY8X0116P audioController(Serial1, busyPin); // Library using Serial1 UART and busy pin input D22
void setup() {
Serial.begin(115200);
Serial1.begin(9600); // Serial1 must be started first - only supported UART baud rate is 9600
audioController.init(); // Initializes module
audioController.playFolderFileIndex(0, 1); // Plays "00\001.mp3"
int numTracks = getNumberOfTracksInCurrentFolder(); // Gets number of tracks in current folder
Serial.println(numTracks); // Should display number of tracks in the "00" folder
char buffer[12];
audioController.getCurrentTrackFilename(buffer); // Gets current filename (in 8.3 format) and places it into buffer
Serial.println(buffer); // Should display "001.mp3"
audioController.waitBusy(); // Blocking call that waits until all songs have completed
// Plays all the remaining songs in the entire folder, printing out their file name upon playback
for (int i = 2; i <= numTracks; ++i) {
audioController.playFolderFileIndex(0, i);
audioController.getCurrentTrackFilename(buffer);
Serial.println(buffer);
audioController.waitBusy();
}
Serial.println("All done!");
}
```

### SoftwareSerial Example

In this example, we use SoftwareSerial to replicate a hardware serial line.

```Arduino
#include "BY8X01-16P.h"
BY8X0116P audioController(Serial1, busyPin); // Library using Serial1 UART and busy pin input D22
void setup() {
Serial.begin(115200);
Serial1.begin(9600); // Serial1 must be started first - only supported UART baud rate is 9600
audioController.init(); // Initializes module
audioController.playFolderFileIndex(0, 1); // Plays "00\001.mp3"
int numTracks = getNumberOfTracksInCurrentFolder(); // Gets number of tracks in current folder
Serial.println(numTracks); // Should display number of tracks in the "00" folder
char buffer[12];
audioController.getCurrentTrackFilename(buffer); // Gets current filename (in 8.3 format) and places it into buffer
Serial.println(buffer); // Should display "001.mp3"
audioController.waitBusy(); // Blocking call that waits until all songs have completed
// Plays all the remaining songs in the entire folder, printing out their file name upon playback
for (int i = 2; i <= numTracks; ++i) {
audioController.playFolderFileIndex(0, i);
audioController.getCurrentTrackFilename(buffer);
Serial.println(buffer);
audioController.waitBusy();
}
Serial.println("All done!");
}
```

### SoftwareSerial Example

In this example, we use SoftwareSerial to replicate a hardware serial line.

```Arduino
#include "BY8X01-16P.h"
#include "SoftwareSerial.h"
const byte rxPin = 2;
const byte txPin = 3;
SoftwareSerial swSerial(rxPin, txPin); // SoftwareSerial using RX pin D2 and TX pin D3
SoftwareSerial swSerial(rxPin, txPin); // SoftwareSerial using RX pin D2 and TX pin D3
BY8X0116P audioController(swSerial); // Library using SoftwareSerial and no busy pin hookup
BY8X0116P audioController(swSerial); // Library using SoftwareSerial and no busy pin hookup
void setup() {
Serial.begin(115200);
pinMode(rxPin, INPUT); // Must manually setup pin modes for RX/TX pins
pinMode(txPin, OUTPUT);
swSerial.begin(9600); // swSerial must be started first - only supported UART baud rate is 9600
audioController.init(); // Initializes module
audioController.play(); // Starts playback of loaded tracks
}
```

## Module Info

If one uncomments the BY8X0116P_ENABLE_DEBUG_OUTPUT define in the libraries main header file (thus enabling debug output) the printModuleInfo() method becomes available, which will display information about the module itself, including initalized states, register values, current settings, etc. All calls being made will display internal debug information about the structure of the call itself. An example of this output is shown here:

In BY8X01-16P.h:
```Arduino
pinMode(txPin, OUTPUT);
swSerial.begin(9600); // swSerial must be started first - only supported UART baud rate is 9600
audioController.init(); // Initializes module
audioController.play(); // Starts playback of loaded tracks
}
```

## Module Info

If one uncomments the BY8X0116P_ENABLE_DEBUG_OUTPUT define in the libraries main header file (thus enabling debug output) the printModuleInfo() method becomes available, which will display information about the module itself, including initalized states, register values, current settings, etc. All calls being made will display internal debug information about the structure of the call itself. An example of this output is shown here:

In BY8X01-16P.h:
```Arduino
// Uncomment this define to enable debug output.
#define BY8X0116P_ENABLE_DEBUG_OUTPUT 1
```

In main sketch:
```Arduino
BY8X0116P audioController;
void setup() {
// ...
audioController.printModuleInfo();
}
```

In serial monitor:
```
#define BY8X0116P_ENABLE_DEBUG_OUTPUT 1
```

In main sketch:
```Arduino
#include "BY8X01-16P.h"
BY8X0116P audioController;
void setup() {
Serial.begin(115200);
audioController.printModuleInfo();
}
```

In serial monitor:
```
~~~ BY8X0116P Module Info ~~~
Busy Pin:
Expand Down Expand Up @@ -252,5 +257,6 @@ Playback Device:
BY8X0116P::getPlaybackDevice
BY8X0116P::writeRequest Cmd: 0x18, Chk: 0x1B
BY8X0116P::readResponse respData[4]: 0001
1: BY8X0116P_PlaybackDevice_MicroSD
1: BY8X0116P_PlaybackDevice_MicroSD
```
31 changes: 31 additions & 0 deletions examples/CombinationPlaybackExample/CombinationPlaybackExample.ino
@@ -0,0 +1,31 @@
// BY8X01-16P-Arduino Combination Playback Example
// In this example, files are loaded onto the MicroSD and queued using the special
// playFileIndex method, which allows up to 10 songs to be queued for playback. Index is
// prescribed by the FAT file system, and is generally in the order that the files were
// copied to the flash drive, but not guaranteed. Indexing runs across all files in every
// subfolder. A file sorter software program (such as "DriveSort" or "FAT32 Sorter")
// should be used if specific file index order for playback is required.

#include "BY8X01-16P.h"

BY8X0116P audioController; // Library using default Serial1 UART and no busy pin hookup

void setup() {
Serial.begin(115200);

Serial1.begin(9600); // Serial1 must be started first - only supported UART baud rate is 9600

audioController.init(); // Initializes module

audioController.setEqualizerProfile(BY8X0116P_EqualizerProfile_Rock); // Sets player equalizer profile to Rock

audioController.playFileIndex(0); // Queues first file on MicroSD card for playback
audioController.playFileIndex(1); // Queues second file on MicroSD card for playback
audioController.playFileIndex(2); // Queues third file on MicroSD card for playback
audioController.playFileIndex(3); // Queues fourth file on MicroSD card for playback
audioController.playFileIndex(4); // Queues fifth file on MicroSD card for playback

audioController.waitPlaybackFinished(); // Blocking call that waits until all songs have completed

Serial.println("All done!");
}
43 changes: 43 additions & 0 deletions examples/IndexedPlaybackExample/IndexedPlaybackExample.ino
@@ -0,0 +1,43 @@
// BY8X01-16P-Arduino Indexed Playback Example
// In this example, folders are named "00" through "99" and files inside them are named
// "001.mp3" (or .wav) through "255.mp3" (or .wav). The folder and file index passed into
// playFolderFileIndex will play that specific file. Note that combination play here is
// not supported so waiting between track plays is required. Having a busy pin connected
// here will allow for tighter timing control between file playbacks.

#include "BY8X01-16P.h"

const byte busyPin = 22;
BY8X0116P audioController(Serial1, busyPin); // Library using Serial1 UART and busy pin input D22

void setup() {
Serial.begin(115200);

Serial1.begin(9600); // Serial1 must be started first - only supported UART baud rate is 9600

audioController.init(); // Initializes module

audioController.playFolderFileIndex(0, 1); // Plays "00\001.mp3"

int numTracks = getNumberOfTracksInCurrentFolder(); // Gets number of tracks in current folder
Serial.println(numTracks); // Should display number of tracks in the "00" folder

char buffer[12];
audioController.getCurrentTrackFilename(buffer); // Gets current filename (in 8.3 format) and places it into buffer

Serial.println(buffer); // Should display "001.mp3"

audioController.waitBusy(); // Blocking call that waits until all songs have completed

// Plays all the remaining songs in the entire folder, printing out their file name upon playback
for (int i = 2; i <= numTracks; ++i) {
audioController.playFolderFileIndex(0, i);

audioController.getCurrentTrackFilename(buffer);
Serial.println(buffer);

audioController.waitBusy();
}

Serial.println("All done!");
}
19 changes: 19 additions & 0 deletions examples/ModuleInfo/ModuleInfo.ino
@@ -0,0 +1,19 @@
// BY8X01-16P-Arduino Module Info
// If one uncomments the BY8X0116P_ENABLE_DEBUG_OUTPUT define in the libraries main
// header file (thus enabling debug output) the printModuleInfo() method becomes
// available, which will display information about the module itself, including
// initalized states, register values, current settings, etc. All calls being made will
// display internal debug information about the structure of the call itself.

// Uncomment this define to enable debug output.
#define BY8X0116P_ENABLE_DEBUG_OUTPUT 1

#include "BY8X01-16P.h"

BY8X0116P audioController;

void setup() {
Serial.begin(115200);

audioController.printModuleInfo();
}

0 comments on commit 8e580b1

Please sign in to comment.