Semaphore based wait and sync for the mp3 play
Dependents: RTOS-VS1053b-mp3_semaphore
VS1053t.cpp@0:d421471bef92, 2012-03-19 (annotated)
- Committer:
- takashikojo
- Date:
- Mon Mar 19 12:08:44 2012 +0000
- Revision:
- 0:d421471bef92
VS1053 driver with Semaphore
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
takashikojo | 0:d421471bef92 | 1 | /* mbed VLSI VS1053t library |
takashikojo | 0:d421471bef92 | 2 | * |
takashikojo | 0:d421471bef92 | 3 | * Thread based VS1053 class inherited by VS1053b |
takashikojo | 0:d421471bef92 | 4 | * |
takashikojo | 0:d421471bef92 | 5 | */ |
takashikojo | 0:d421471bef92 | 6 | |
takashikojo | 0:d421471bef92 | 7 | |
takashikojo | 0:d421471bef92 | 8 | #include "mbed.h" |
takashikojo | 0:d421471bef92 | 9 | #include "rtos.h" |
takashikojo | 0:d421471bef92 | 10 | |
takashikojo | 0:d421471bef92 | 11 | #include "defines.h" |
takashikojo | 0:d421471bef92 | 12 | #include "VS1053t.h" |
takashikojo | 0:d421471bef92 | 13 | |
takashikojo | 0:d421471bef92 | 14 | VS1053t :: VS1053t( |
takashikojo | 0:d421471bef92 | 15 | PinName mosi, PinName miso, PinName sck, |
takashikojo | 0:d421471bef92 | 16 | PinName cs, PinName rst, PinName dreq, PinName dcs, |
takashikojo | 0:d421471bef92 | 17 | char* buffer, int buffer_size, bool thread |
takashikojo | 0:d421471bef92 | 18 | ): |
takashikojo | 0:d421471bef92 | 19 | |
takashikojo | 0:d421471bef92 | 20 | VS1053( mosi, miso, sck, cs, rst, dreq, dcs, |
takashikojo | 0:d421471bef92 | 21 | buffer, buffer_size) |
takashikojo | 0:d421471bef92 | 22 | { } |
takashikojo | 0:d421471bef92 | 23 | |
takashikojo | 0:d421471bef92 | 24 | Semaphore lock_buff(1) ; |
takashikojo | 0:d421471bef92 | 25 | |
takashikojo | 0:d421471bef92 | 26 | unsigned int VS1053t::waitBuffer(unsigned int size, uint32_t time) { |
takashikojo | 0:d421471bef92 | 27 | int n ; |
takashikojo | 0:d421471bef92 | 28 | WaitingSize = size ; |
takashikojo | 0:d421471bef92 | 29 | while ((n = VS1053t::bufferFree()) < size) { |
takashikojo | 0:d421471bef92 | 30 | if(thread) |
takashikojo | 0:d421471bef92 | 31 | lock_buff.wait(time) ; |
takashikojo | 0:d421471bef92 | 32 | else |
takashikojo | 0:d421471bef92 | 33 | wait_ms(100) ; |
takashikojo | 0:d421471bef92 | 34 | } |
takashikojo | 0:d421471bef92 | 35 | // if(n < size) { time out |
takashikojo | 0:d421471bef92 | 36 | return(n) ; |
takashikojo | 0:d421471bef92 | 37 | } |
takashikojo | 0:d421471bef92 | 38 | |
takashikojo | 0:d421471bef92 | 39 | unsigned char VS1053t::bufferGetByte(void) { |
takashikojo | 0:d421471bef92 | 40 | unsigned char retVal = 0x00; |
takashikojo | 0:d421471bef92 | 41 | if (bufferCount() > 0x00) { |
takashikojo | 0:d421471bef92 | 42 | retVal = *_bufferReadPointer++; |
takashikojo | 0:d421471bef92 | 43 | if (_bufferReadPointer >= _buffer + BUFFER_SIZE) { |
takashikojo | 0:d421471bef92 | 44 | _bufferReadPointer = _buffer; |
takashikojo | 0:d421471bef92 | 45 | } |
takashikojo | 0:d421471bef92 | 46 | } |
takashikojo | 0:d421471bef92 | 47 | if (thread && (VS1053t::bufferFree() >= WaitingSize)) |
takashikojo | 0:d421471bef92 | 48 | lock_buff.release() ; |
takashikojo | 0:d421471bef92 | 49 | return retVal; |
takashikojo | 0:d421471bef92 | 50 | } |