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