From 08f4542954d538b35ddf5683ea02000173dc1cc1 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Wed, 6 Apr 2016 11:35:00 -0700 Subject: [PATCH] Marginal attempt to simplify stream write loop. --- firmware/application/audio_thread.hpp | 40 +++++++++++++++++---------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/firmware/application/audio_thread.hpp b/firmware/application/audio_thread.hpp index 6e4a0a3bd..2a8cac18e 100644 --- a/firmware/application/audio_thread.hpp +++ b/firmware/application/audio_thread.hpp @@ -106,7 +106,8 @@ private: return; } - while( !chThdShouldTerminate() ) { + bool success = true; + while( success && !chThdShouldTerminate() ) { chEvtWaitAny(EVT_FIFO_HIGHWATER); auto fifo = reinterpret_cast*>(shared_memory.FIFO_HACK); @@ -116,25 +117,34 @@ private: StreamOutput stream { fifo }; - while( stream.available() >= write_buffer->size() ) { - led_usb.on(); - - const auto bytes_to_write = stream.read(write_buffer->data(), write_buffer->size()); - - if( bytes_to_write == write_buffer->size() ) { - if( !file.write(write_buffer->data(), write_buffer->size()) ) { - led_tx.on(); - break; - } - } else { - break; - } - led_usb.off(); + while( success && (stream.available() >= write_buffer->size()) ) { + success = transfer(stream, write_buffer.get()); } } + if( !success ) { + led_tx.on(); + } + file.close(); } + + bool transfer(StreamOutput& stream, std::array* const write_buffer) { + bool success = false; + + led_usb.on(); + + const auto bytes_to_write = stream.read(write_buffer->data(), write_buffer->size()); + if( bytes_to_write == write_buffer->size() ) { + if( file.write(write_buffer->data(), write_buffer->size()) ) { + success = true; + } + } + + led_usb.off(); + + return success; + } }; #endif/*__AUDIO_THREAD_H__*/