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: X_NUCLEO_IKS01A1 mbed-rtos mbed
Fork of HelloWorld_IKS01A1 by
buffer.cpp
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 #include "string.h" 00004 #include <stdio.h> 00005 #include <ctype.h> 00006 00007 // A First-In-First-Out buffer is used with streams of data. 00008 // It is thread-safe and often used to buffer data across threads 00009 00010 #define BUFFERSIZE 100 00011 /* 00012 typedef struct { 00013 uint8_t id; 00014 float tempCelsius; 00015 float tempFarenheit; 00016 float humidity; 00017 float pressure; 00018 int32_t accelerometer[3]; 00019 int32_t gyroscope[3]; 00020 int32_t magnetometer[3]; 00021 char* date; 00022 } log_data; 00023 00024 Semaphore *spaceAvailable; 00025 Semaphore *samplesInBuffer; 00026 Mutex *bufferLock; 00027 00028 log_data buffer[BUFFERSIZE]; 00029 unsigned int newestIndex = BUFFERSIZE-1; 00030 unsigned int oldestIndex = BUFFERSIZE-1; 00031 00032 class Buffer { 00033 public: 00034 void addDataToQueue(log_data d) { 00035 // Check whether there's space 00036 int32_t Nspaces = spaceAvailable->wait(); 00037 00038 // If there's space, use the mutex 00039 bufferLock->lock(); 00040 00041 // Update buffer 00042 newestIndex = (newestIndex+1) % BUFFERSIZE; 00043 buffer[newestIndex] = d; 00044 printf("\tAdded log_data id %d to buffer, %d space available\n", 00045 unsigned(d.id), Nspaces-1); 00046 00047 bufferLock->unlock(); 00048 00049 // Signal that a sample has been added 00050 samplesInBuffer->release(); 00051 } 00052 00053 log_data takeCharacterFromQueue(){ 00054 // Check whether there are samples 00055 int32_t Nsamples = samplesInBuffer->wait(); 00056 00057 bufferLock->lock(); 00058 00059 oldestIndex = (oldestIndex+1) % BUFFERSIZE; 00060 log_data d = buffer[oldestIndex]; 00061 printf("\tRemoved log_data id %d from buffer, %d bytes remaining\n", 00062 unsigned(d.id), Nsamples-1); 00063 00064 bufferLock->unlock(); 00065 00066 //Signal there's space in the buffer 00067 spaceAvailable->release(); 00068 00069 return d; 00070 } 00071 00072 Buffer() { 00073 bufferLock = new Mutex(); 00074 spaceAvailable = new Semaphore(BUFFERSIZE); 00075 samplesInBuffer = new Semaphore(0); 00076 } 00077 }; 00078 */
Generated on Sun Jul 17 2022 00:26:54 by
1.7.2
