Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add non-blocking playback method #98

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/VS1053.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,30 @@ void VS1053::sdi_send_buffer(uint8_t *data, size_t len) {
data_mode_off();
}

size_t VS1053::sdi_send_buffer_non_blocking(const uint8_t *data, size_t limit) {
size_t iocount = 0;

data_mode_on();
while (digitalRead(dreq_pin) && limit > 0) // More to do?
{
size_t chunk_length = limit;
if (chunk_length > vs1053_chunk_size) {
chunk_length = vs1053_chunk_size;
}

spi_write_bytes(data, chunk_length);

data += chunk_length;
limit -= chunk_length;
iocount += chunk_length;

yield();
}
data_mode_off();

return iocount;
}

void VS1053::sdi_send_fillers(size_t len) {
size_t chunk_length; // Length of chunk 32 byte or shorter

Expand Down Expand Up @@ -244,6 +268,10 @@ void VS1053::playChunk(uint8_t *data, size_t len) {
sdi_send_buffer(data, len);
}

size_t VS1053::playNonBlocking(const uint8_t *data, size_t limit) {
return sdi_send_buffer_non_blocking(data, limit);
}

void VS1053::stopSong() {
uint16_t modereg; // Read from mode register
int i; // Loop control
Expand Down
6 changes: 6 additions & 0 deletions src/VS1053.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ class VS1053 {

void sdi_send_buffer(uint8_t *data, size_t len);

size_t sdi_send_buffer_non_blocking(const uint8_t *data, size_t limit);

void sdi_send_fillers(size_t length);

void wram_write(uint16_t address, uint16_t data);
Expand All @@ -136,6 +138,10 @@ class VS1053 {
// Play a chunk of data. Copies the data to the chip. Blocks until complete
void playChunk(uint8_t *data, size_t len);

// Writes the up to limit bytes from the given buffer to the VS1053 audio buffer. Tries to write as many bytes
// as possible with out blocking. Returns the number of bytes written.
size_t playNonBlocking(const uint8_t *data, size_t limit);

// Finish playing a song. Call this after the last playChunk call
void stopSong();

Expand Down