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