temp

Committer:
BenRJG
Date:
Thu Dec 06 15:38:09 2018 +0000
Revision:
0:2a4af0cb6e8d
Imported Code from Kiel; Added button functionality; Added set DateTime Functionality

Who changed what in which revision?

UserRevisionLine numberNew contents of line
BenRJG 0:2a4af0cb6e8d 1 #include "mbed.h"
BenRJG 0:2a4af0cb6e8d 2 #include "sample_buffer.hpp"
BenRJG 0:2a4af0cb6e8d 3
BenRJG 0:2a4af0cb6e8d 4 //Thread sychronisation primatives
BenRJG 0:2a4af0cb6e8d 5 Semaphore spaceAvailable(BUFFERSIZE);
BenRJG 0:2a4af0cb6e8d 6 Semaphore samplesInBuffer(0);
BenRJG 0:2a4af0cb6e8d 7 Mutex bufferLock;
BenRJG 0:2a4af0cb6e8d 8
BenRJG 0:2a4af0cb6e8d 9 //Output buffer
BenRJG 0:2a4af0cb6e8d 10 char buffer[BUFFERSIZE];
BenRJG 0:2a4af0cb6e8d 11 unsigned int newestIndex = BUFFERSIZE-1; //First time it is incremented, it will be 0
BenRJG 0:2a4af0cb6e8d 12 unsigned int oldestIndex = BUFFERSIZE-1;
BenRJG 0:2a4af0cb6e8d 13
BenRJG 0:2a4af0cb6e8d 14 //Producer
BenRJG 0:2a4af0cb6e8d 15 void addCharacterToQueue(const char c)
BenRJG 0:2a4af0cb6e8d 16 {
BenRJG 0:2a4af0cb6e8d 17 //Is there space?
BenRJG 0:2a4af0cb6e8d 18 int32_t Nspaces = spaceAvailable.wait();
BenRJG 0:2a4af0cb6e8d 19
BenRJG 0:2a4af0cb6e8d 20 //Ok, there is space - take the lock
BenRJG 0:2a4af0cb6e8d 21 bufferLock.lock();
BenRJG 0:2a4af0cb6e8d 22
BenRJG 0:2a4af0cb6e8d 23 //Update buffer
BenRJG 0:2a4af0cb6e8d 24 newestIndex = (newestIndex+1) % BUFFERSIZE;
BenRJG 0:2a4af0cb6e8d 25 buffer[newestIndex] = c;
BenRJG 0:2a4af0cb6e8d 26 //printf("\tAdded ASCII Character: %2Xh (%c) to buffer, %d spaces available\n", c, c, Nspaces-1);
BenRJG 0:2a4af0cb6e8d 27
BenRJG 0:2a4af0cb6e8d 28 //Release lock
BenRJG 0:2a4af0cb6e8d 29 bufferLock.unlock();
BenRJG 0:2a4af0cb6e8d 30
BenRJG 0:2a4af0cb6e8d 31 //Signal that a sample has been added
BenRJG 0:2a4af0cb6e8d 32 samplesInBuffer.release();
BenRJG 0:2a4af0cb6e8d 33 }
BenRJG 0:2a4af0cb6e8d 34
BenRJG 0:2a4af0cb6e8d 35 //Consumer
BenRJG 0:2a4af0cb6e8d 36 char takeCharacterFromQueue()
BenRJG 0:2a4af0cb6e8d 37 {
BenRJG 0:2a4af0cb6e8d 38 //Are thre any samples in the buffer
BenRJG 0:2a4af0cb6e8d 39 int32_t Nsamples = samplesInBuffer.wait();
BenRJG 0:2a4af0cb6e8d 40
BenRJG 0:2a4af0cb6e8d 41 //Ok, there are samples - take the lock
BenRJG 0:2a4af0cb6e8d 42 bufferLock.lock();
BenRJG 0:2a4af0cb6e8d 43
BenRJG 0:2a4af0cb6e8d 44 //Update buffer - remove oldest
BenRJG 0:2a4af0cb6e8d 45 oldestIndex = (oldestIndex+1) % BUFFERSIZE;
BenRJG 0:2a4af0cb6e8d 46 char cc = buffer[oldestIndex];
BenRJG 0:2a4af0cb6e8d 47 //printf("\t\tTaking ASCII Character: %2Xh (%c) from buffer, %d bytes remaining\n", cc, cc, Nsamples-1);
BenRJG 0:2a4af0cb6e8d 48
BenRJG 0:2a4af0cb6e8d 49 //Release lock
BenRJG 0:2a4af0cb6e8d 50 bufferLock.unlock();
BenRJG 0:2a4af0cb6e8d 51
BenRJG 0:2a4af0cb6e8d 52 //Signal there is space in the buffer
BenRJG 0:2a4af0cb6e8d 53 spaceAvailable.release();
BenRJG 0:2a4af0cb6e8d 54
BenRJG 0:2a4af0cb6e8d 55 //return a copy of the result
BenRJG 0:2a4af0cb6e8d 56 return cc;
BenRJG 0:2a4af0cb6e8d 57 }
BenRJG 0:2a4af0cb6e8d 58