
trabalho
Dependencies: X_NUCLEO_IKS01A1 mbed-rtos mbed
Fork of HelloWorld_IKS01A1 by
Revision 26:71c54c8ae536, committed 2016-05-08
- Comitter:
- stwykd
- Date:
- Sun May 08 08:37:33 2016 +0000
- Parent:
- 25:55c0dc5b32b3
- Child:
- 27:0bbc1d575986
- Commit message:
- Created a buffer and finished implementing expansion board.; There's missing to send the data to the buffer just after reading on readData. You can copy and paste buffer.cpp to mailbox.cpp, or continue buffer.cpp as a mailbox
Changed in this revision
buffer.cpp | Show annotated file Show diff for this revision Revisions of this file |
expansionBoard.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/buffer.cpp Sun May 08 08:37:33 2016 +0000 @@ -0,0 +1,77 @@ +#include "mbed.h" +#include "rtos.h" +#include "string.h" +#include <stdio.h> +#include <ctype.h> + +// A First-In-First-Out buffer is used with streams of data. +// It is thread-safe and often used to buffer data across threads + +#define BUFFERSIZE 100 + +typedef struct { + uint8_t id; + float tempCelcius; + float tempFarenheit; + float humidity; + float pressure; + int accelerometer; + int gyroscope; + int magnetometer; + char* date; + } log_data; + +Semaphore *spaceAvailable; +Semaphore *samplesInBuffer; +Mutex *bufferLock; + +log_data buffer[BUFFERSIZE]; +unsigned int newestIndex = BUFFERSIZE-1; +unsigned int oldestIndex = BUFFERSIZE-1; + +class Buffer { +public: + void addDataToQueue(log_data d) { + // Check whether there's space + int32_t Nspaces = spaceAvailable->wait(); + + // If there's space, use the mutex + bufferLock->lock(); + + // Update buffer + newestIndex = (newestIndex+1) % BUFFERSIZE; + buffer[newestIndex] = d; + printf("\tAdded log_data id %d to buffer, %d space available\n", + unsigned(d.id), Nspaces-1); + + bufferLock->unlock(); + + // Signal that a sample has been added + samplesInBuffer->release(); + } + + log_data takeCharacterFromQueue(){ + // Check whether there are samples + int32_t Nsamples = samplesInBuffer->wait(); + + bufferLock->lock(); + + oldestIndex = (oldestIndex+1) % BUFFERSIZE; + log_data d = buffer[oldestIndex]; + printf("\tRemoved log_data id %d from buffer, %d bytes remaining\n", + unsigned(d.id), Nsamples-1); + + bufferLock->unlock(); + + //Signal there's space in the buffer + spaceAvailable->release(); + + return d; + } + + Buffer() { + bufferLock = new Mutex(); + spaceAvailable = new Semaphore(BUFFERSIZE); + samplesInBuffer = new Semaphore(0); + } +}; \ No newline at end of file
--- a/expansionBoard.cpp Sun May 08 07:00:11 2016 +0000 +++ b/expansionBoard.cpp Sun May 08 08:37:33 2016 +0000 @@ -1,5 +1,6 @@ #include "mbed.h" #include "x_nucleo_iks01a1.h" +#include "buffer.cpp" /* Instantiate the expansion board */ static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15);