Thread UART
Dependencies: fsl_phy_mcr20a fsl_smac mbed-rtos mbed
Fork of mcr20_wireless_uart by
circular_buffer.cpp@27:1eb29717bfd9, 2015-06-29 (annotated)
- Committer:
- andreikovacs
- Date:
- Mon Jun 29 05:50:47 2015 +0000
- Revision:
- 27:1eb29717bfd9
Changed app to behave like the initial Wireless UART demo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andreikovacs | 27:1eb29717bfd9 | 1 | #include "circular_buffer.h" |
andreikovacs | 27:1eb29717bfd9 | 2 | |
andreikovacs | 27:1eb29717bfd9 | 3 | CircularBuffer::CircularBuffer() |
andreikovacs | 27:1eb29717bfd9 | 4 | { |
andreikovacs | 27:1eb29717bfd9 | 5 | size = gCircularBufferSize_c; |
andreikovacs | 27:1eb29717bfd9 | 6 | readIndex = 0; |
andreikovacs | 27:1eb29717bfd9 | 7 | writeIndex = 0; |
andreikovacs | 27:1eb29717bfd9 | 8 | count = 0; |
andreikovacs | 27:1eb29717bfd9 | 9 | MEM_Init(); |
andreikovacs | 27:1eb29717bfd9 | 10 | buffer = (uint8_t *) MEM_BufferAlloc(size * sizeof(uint8_t)); |
andreikovacs | 27:1eb29717bfd9 | 11 | if ( NULL == buffer ) |
andreikovacs | 27:1eb29717bfd9 | 12 | { |
andreikovacs | 27:1eb29717bfd9 | 13 | /*if buffer alloc fails stop the program execution*/ |
andreikovacs | 27:1eb29717bfd9 | 14 | while(1); |
andreikovacs | 27:1eb29717bfd9 | 15 | } |
andreikovacs | 27:1eb29717bfd9 | 16 | } |
andreikovacs | 27:1eb29717bfd9 | 17 | |
andreikovacs | 27:1eb29717bfd9 | 18 | CircularBuffer::CircularBuffer(uint32_t sz) |
andreikovacs | 27:1eb29717bfd9 | 19 | { |
andreikovacs | 27:1eb29717bfd9 | 20 | size = sz; |
andreikovacs | 27:1eb29717bfd9 | 21 | readIndex = 0; |
andreikovacs | 27:1eb29717bfd9 | 22 | writeIndex = 0; |
andreikovacs | 27:1eb29717bfd9 | 23 | count = 0; |
andreikovacs | 27:1eb29717bfd9 | 24 | MEM_Init(); |
andreikovacs | 27:1eb29717bfd9 | 25 | buffer = (uint8_t *) MEM_BufferAlloc(size * sizeof(uint8_t)); |
andreikovacs | 27:1eb29717bfd9 | 26 | if ( NULL == buffer ) |
andreikovacs | 27:1eb29717bfd9 | 27 | { |
andreikovacs | 27:1eb29717bfd9 | 28 | /*if buffer alloc fails stop the program execution*/ |
andreikovacs | 27:1eb29717bfd9 | 29 | while(1); |
andreikovacs | 27:1eb29717bfd9 | 30 | } |
andreikovacs | 27:1eb29717bfd9 | 31 | } |
andreikovacs | 27:1eb29717bfd9 | 32 | |
andreikovacs | 27:1eb29717bfd9 | 33 | CircularBuffer::~CircularBuffer() |
andreikovacs | 27:1eb29717bfd9 | 34 | { |
andreikovacs | 27:1eb29717bfd9 | 35 | size = 0; |
andreikovacs | 27:1eb29717bfd9 | 36 | readIndex = 0; |
andreikovacs | 27:1eb29717bfd9 | 37 | writeIndex = 0; |
andreikovacs | 27:1eb29717bfd9 | 38 | count = 0; |
andreikovacs | 27:1eb29717bfd9 | 39 | MEM_BufferFree(buffer); |
andreikovacs | 27:1eb29717bfd9 | 40 | } |
andreikovacs | 27:1eb29717bfd9 | 41 | |
andreikovacs | 27:1eb29717bfd9 | 42 | bufferStatus_t CircularBuffer :: addToBuffer (uint8_t c) |
andreikovacs | 27:1eb29717bfd9 | 43 | { |
andreikovacs | 27:1eb29717bfd9 | 44 | buffer[writeIndex] = c; |
andreikovacs | 27:1eb29717bfd9 | 45 | writeIndex++; |
andreikovacs | 27:1eb29717bfd9 | 46 | if (writeIndex >= size) |
andreikovacs | 27:1eb29717bfd9 | 47 | { |
andreikovacs | 27:1eb29717bfd9 | 48 | writeIndex = 0; |
andreikovacs | 27:1eb29717bfd9 | 49 | } |
andreikovacs | 27:1eb29717bfd9 | 50 | count++; |
andreikovacs | 27:1eb29717bfd9 | 51 | if (count >= size) |
andreikovacs | 27:1eb29717bfd9 | 52 | { |
andreikovacs | 27:1eb29717bfd9 | 53 | return buffer_Full_c; |
andreikovacs | 27:1eb29717bfd9 | 54 | } |
andreikovacs | 27:1eb29717bfd9 | 55 | return buffer_Ok_c; |
andreikovacs | 27:1eb29717bfd9 | 56 | } |
andreikovacs | 27:1eb29717bfd9 | 57 | |
andreikovacs | 27:1eb29717bfd9 | 58 | bufferStatus_t CircularBuffer :: getFromBuffer (uint8_t *c) |
andreikovacs | 27:1eb29717bfd9 | 59 | { |
andreikovacs | 27:1eb29717bfd9 | 60 | if ( 0 == count ) |
andreikovacs | 27:1eb29717bfd9 | 61 | { |
andreikovacs | 27:1eb29717bfd9 | 62 | return buffer_Empty_c; |
andreikovacs | 27:1eb29717bfd9 | 63 | } |
andreikovacs | 27:1eb29717bfd9 | 64 | (*c) = buffer[readIndex]; |
andreikovacs | 27:1eb29717bfd9 | 65 | readIndex++; |
andreikovacs | 27:1eb29717bfd9 | 66 | if (readIndex >= size) |
andreikovacs | 27:1eb29717bfd9 | 67 | { |
andreikovacs | 27:1eb29717bfd9 | 68 | readIndex = 0; |
andreikovacs | 27:1eb29717bfd9 | 69 | } |
andreikovacs | 27:1eb29717bfd9 | 70 | count--; |
andreikovacs | 27:1eb29717bfd9 | 71 | return buffer_Ok_c; |
andreikovacs | 27:1eb29717bfd9 | 72 | } |
andreikovacs | 27:1eb29717bfd9 | 73 | |
andreikovacs | 27:1eb29717bfd9 | 74 | uint32_t CircularBuffer :: getCount() |
andreikovacs | 27:1eb29717bfd9 | 75 | { |
andreikovacs | 27:1eb29717bfd9 | 76 | return count; |
andreikovacs | 27:1eb29717bfd9 | 77 | } |