Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed BMP280 ELEC350-Practicals-FZ429_2
sample_buffer.cpp@1:e274a5cc021d, 2018-12-16 (annotated)
- Committer:
- ChrisHayes
- Date:
- Sun Dec 16 15:25:04 2018 +0000
- Revision:
- 1:e274a5cc021d
this is my code up two 16/12/2018
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ChrisHayes | 1:e274a5cc021d | 1 | #include "sample_buffer.hpp" |
| ChrisHayes | 1:e274a5cc021d | 2 | #include "sample_hardware.hpp" |
| ChrisHayes | 1:e274a5cc021d | 3 | |
| ChrisHayes | 1:e274a5cc021d | 4 | //Thread sychronisation primatives |
| ChrisHayes | 1:e274a5cc021d | 5 | //Semaphore spaceAvailable(BUFFERSIZE); |
| ChrisHayes | 1:e274a5cc021d | 6 | Semaphore samplesInBuffer(0); |
| ChrisHayes | 1:e274a5cc021d | 7 | Mutex bufferLock; |
| ChrisHayes | 1:e274a5cc021d | 8 | int NInBuffer[4]; |
| ChrisHayes | 1:e274a5cc021d | 9 | |
| ChrisHayes | 1:e274a5cc021d | 10 | //Output buffer |
| ChrisHayes | 1:e274a5cc021d | 11 | float buffer[4][BUFFERSIZE]; |
| ChrisHayes | 1:e274a5cc021d | 12 | unsigned int newestIndex[4] ={ BUFFERSIZE-1 }; //First time it is incremented, it will be 0 |
| ChrisHayes | 1:e274a5cc021d | 13 | unsigned int oldestIndex[4] ={ BUFFERSIZE-1 }; |
| ChrisHayes | 1:e274a5cc021d | 14 | int32_t Nspace = BUFFERSIZE; |
| ChrisHayes | 1:e274a5cc021d | 15 | |
| ChrisHayes | 1:e274a5cc021d | 16 | //Producer |
| ChrisHayes | 1:e274a5cc021d | 17 | void addDataToBuffer(float data, int row) |
| ChrisHayes | 1:e274a5cc021d | 18 | { |
| ChrisHayes | 1:e274a5cc021d | 19 | //Is there space? |
| ChrisHayes | 1:e274a5cc021d | 20 | // int32_t Nspaces = spaceAvailable.wait(); |
| ChrisHayes | 1:e274a5cc021d | 21 | |
| ChrisHayes | 1:e274a5cc021d | 22 | |
| ChrisHayes | 1:e274a5cc021d | 23 | |
| ChrisHayes | 1:e274a5cc021d | 24 | //Ok, there is space - take the lock |
| ChrisHayes | 1:e274a5cc021d | 25 | bufferLock.lock(); |
| ChrisHayes | 1:e274a5cc021d | 26 | // redLED = 1; |
| ChrisHayes | 1:e274a5cc021d | 27 | |
| ChrisHayes | 1:e274a5cc021d | 28 | if (NInBuffer[row] < 120) |
| ChrisHayes | 1:e274a5cc021d | 29 | { |
| ChrisHayes | 1:e274a5cc021d | 30 | NInBuffer[row] = NInBuffer[row] +1; |
| ChrisHayes | 1:e274a5cc021d | 31 | } |
| ChrisHayes | 1:e274a5cc021d | 32 | |
| ChrisHayes | 1:e274a5cc021d | 33 | if (NInBuffer[row] == 120) |
| ChrisHayes | 1:e274a5cc021d | 34 | { |
| ChrisHayes | 1:e274a5cc021d | 35 | oldestIndex[row] = (oldestIndex[row]+1) % BUFFERSIZE; |
| ChrisHayes | 1:e274a5cc021d | 36 | } |
| ChrisHayes | 1:e274a5cc021d | 37 | |
| ChrisHayes | 1:e274a5cc021d | 38 | |
| ChrisHayes | 1:e274a5cc021d | 39 | //Update buffer |
| ChrisHayes | 1:e274a5cc021d | 40 | newestIndex[row] = (newestIndex[row]+1) % BUFFERSIZE; |
| ChrisHayes | 1:e274a5cc021d | 41 | |
| ChrisHayes | 1:e274a5cc021d | 42 | buffer[row][newestIndex[row]] = data; |
| ChrisHayes | 1:e274a5cc021d | 43 | // printf("NInBuffer = %d", NInBuffer); |
| ChrisHayes | 1:e274a5cc021d | 44 | // printf("oldestIndex= %d", oldestIndex); |
| ChrisHayes | 1:e274a5cc021d | 45 | printf("\tAdded data: %5.1f to buffer \r\n", data); |
| ChrisHayes | 1:e274a5cc021d | 46 | |
| ChrisHayes | 1:e274a5cc021d | 47 | //Release lock |
| ChrisHayes | 1:e274a5cc021d | 48 | bufferLock.unlock(); |
| ChrisHayes | 1:e274a5cc021d | 49 | // redLED = 0; |
| ChrisHayes | 1:e274a5cc021d | 50 | |
| ChrisHayes | 1:e274a5cc021d | 51 | //Signal that a sample has been added |
| ChrisHayes | 1:e274a5cc021d | 52 | samplesInBuffer.release(); |
| ChrisHayes | 1:e274a5cc021d | 53 | } |
| ChrisHayes | 1:e274a5cc021d | 54 | |
| ChrisHayes | 1:e274a5cc021d | 55 | //Consumer |
| ChrisHayes | 1:e274a5cc021d | 56 | float takeDataFromBuffer(int row) |
| ChrisHayes | 1:e274a5cc021d | 57 | { |
| ChrisHayes | 1:e274a5cc021d | 58 | if(NInBuffer[row] >0) |
| ChrisHayes | 1:e274a5cc021d | 59 | { |
| ChrisHayes | 1:e274a5cc021d | 60 | NInBuffer[row] = NInBuffer[row] -1; |
| ChrisHayes | 1:e274a5cc021d | 61 | } |
| ChrisHayes | 1:e274a5cc021d | 62 | |
| ChrisHayes | 1:e274a5cc021d | 63 | //Are thre any samples in the buffer |
| ChrisHayes | 1:e274a5cc021d | 64 | int32_t Nsamples = samplesInBuffer.wait(); |
| ChrisHayes | 1:e274a5cc021d | 65 | |
| ChrisHayes | 1:e274a5cc021d | 66 | //Ok, there are samples - take the lock |
| ChrisHayes | 1:e274a5cc021d | 67 | bufferLock.lock(); |
| ChrisHayes | 1:e274a5cc021d | 68 | // yellowLED = 1; |
| ChrisHayes | 1:e274a5cc021d | 69 | |
| ChrisHayes | 1:e274a5cc021d | 70 | //Update buffer - remove oldest |
| ChrisHayes | 1:e274a5cc021d | 71 | oldestIndex[row] = (oldestIndex[row]+1) % BUFFERSIZE; |
| ChrisHayes | 1:e274a5cc021d | 72 | float data = buffer[row][oldestIndex[row]]; |
| ChrisHayes | 1:e274a5cc021d | 73 | // printf("NInBuffer = %d", NInBuffer); |
| ChrisHayes | 1:e274a5cc021d | 74 | // printf("\t\tTaking data: %5.1f from buffer\r\n", data); |
| ChrisHayes | 1:e274a5cc021d | 75 | |
| ChrisHayes | 1:e274a5cc021d | 76 | //Release lock |
| ChrisHayes | 1:e274a5cc021d | 77 | bufferLock.unlock(); |
| ChrisHayes | 1:e274a5cc021d | 78 | // yellowLED = 0; |
| ChrisHayes | 1:e274a5cc021d | 79 | |
| ChrisHayes | 1:e274a5cc021d | 80 | //Signal there is space in the buffer |
| ChrisHayes | 1:e274a5cc021d | 81 | // spaceAvailable.release(); |
| ChrisHayes | 1:e274a5cc021d | 82 | |
| ChrisHayes | 1:e274a5cc021d | 83 | //return a copy of the result |
| ChrisHayes | 1:e274a5cc021d | 84 | return data; |
| ChrisHayes | 1:e274a5cc021d | 85 | } |
| ChrisHayes | 1:e274a5cc021d | 86 | |
| ChrisHayes | 1:e274a5cc021d | 87 | char readDataFromBuffer(int row) |
| ChrisHayes | 1:e274a5cc021d | 88 | { |
| ChrisHayes | 1:e274a5cc021d | 89 | bufferLock.lock(); |
| ChrisHayes | 1:e274a5cc021d | 90 | float data = buffer[row][oldestIndex[row]]; |
| ChrisHayes | 1:e274a5cc021d | 91 | bufferLock.unlock(); |
| ChrisHayes | 1:e274a5cc021d | 92 | return data; |
| ChrisHayes | 1:e274a5cc021d | 93 | } |