Sampling ldr temp and pressure data into 120 sample FIFO buffer.
Fork of Task690-mbed-os-FZ429ZI by
buffer.cpp
- Committer:
- osmith2
- Date:
- 2017-12-23
- Revision:
- 6:c2299e3de428
- Parent:
- 5:cc1b9f5c27a0
File content as of revision 6:c2299e3de428:
#include "buffer.hpp" //#include "sample_hardware.hpp" //Thread sychronisation primatives Semaphore spaceAvailable(BUFFERSIZE); //set to 120 in buffer.hpp Semaphore samplesInBuffer(0); //init 0 Mutex bufferLock; //binary semaphore //Output buffer float buffer[BUFFERSIZE]; // can be used to display buffersize to PuTTY unsigned int newestIndex = BUFFERSIZE-1; //First time it is incremented, it will be 0 unsigned int oldestIndex = BUFFERSIZE-1; //Producer void addToBuffer(float c) // char c used for char buffer? { //Is there space? int32_t Nspaces = spaceAvailable.wait(); //Ok, there is space - take the lock bufferLock.lock(); //redLED = 1; //Update buffer newestIndex = (newestIndex+1) % BUFFERSIZE; buffer[newestIndex] = c; printf("Added data: %6.4f to buffer, %d spaces available\n", c, Nspaces-1); //Release lock bufferLock.unlock(); //redLED = 0; //Signal that a sample has been added samplesInBuffer.release(); } //Consumer float takeFromBuffer() { //Are thre any samples in the buffer int32_t Nsamples = samplesInBuffer.wait(); printf("newestIndex = %i\n", newestIndex); printf("oldestIndex = %i\n", oldestIndex); if (newestIndex == oldestIndex){ //Ok, there are samples - take the lock bufferLock.lock(); //yellowLED = 1; //Update buffer - remove oldest oldestIndex = (oldestIndex+1) % BUFFERSIZE; float cc = buffer[oldestIndex]; printf("Taking old data (%f) from buffer, %d bytes remaining\n", cc, Nsamples-1); //Release lock bufferLock.unlock(); //yellowLED = 0; //Signal there is space in the buffer spaceAvailable.release(); //return a copy of the result return cc; } }