robot arm demo team / Mbed 2 deprecated RobotArmDemo Featured

Dependencies:   AX-12A Dynamixel mbed iothub_client EthernetInterface NTPClient ConfigFile SDFileSystem iothub_amqp_transport mbed-rtos proton-c-mbed wolfSSL

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SafeCircBuf.h Source File

SafeCircBuf.h

00001 // Copyright (c) Microsoft. All rights reserved.
00002 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
00003 
00004 #ifndef __SAFECIRCBUF_H__
00005 #define __SAFECIRCBUF_H__
00006 
00007 #include "mbed.h"
00008 #include "rtos.h"
00009 
00010 #include <CircularBuffer.h>
00011 
00012 template<typename T, uint32_t BufferSize, typename CounterType = uint32_t>
00013 class SafeCircBuf
00014 {
00015 public:
00016     SafeCircBuf() {
00017     }
00018 
00019     ~SafeCircBuf() {
00020     }
00021 
00022     /** Push the transaction to the buffer. This overwrites the buffer if it's
00023      *  full
00024      *
00025      * @param data Data to be pushed to the buffer
00026      */
00027     void push(const T& data) {
00028         _mutex.lock();
00029         _circBuf.push(data);
00030         _mutex.unlock();
00031     }
00032 
00033     /** Pop the transaction from the buffer
00034      *
00035      * @param data Data to be pushed to the buffer
00036      * @return True if the buffer is not empty and data contains a transaction, false otherwise
00037      */
00038     bool pop(T& data) {
00039         bool rc;
00040         _mutex.lock();
00041         rc = _circBuf.pop(data);
00042         _mutex.unlock();
00043         return rc;
00044     }
00045 
00046     /** Check if the buffer is empty
00047      *
00048      * @return True if the buffer is empty, false if not
00049      */
00050     bool empty() {
00051         bool rc;
00052         _mutex.lock();
00053         rc = _circBuf.empty();
00054         _mutex.unlock();
00055         return rc;
00056     }
00057 
00058     /** Check if the buffer is full
00059      *
00060      * @return True if the buffer is full, false if not
00061      */
00062     bool full() {
00063         bool rc;
00064         _mutex.lock();
00065         rc = _circBuf.full();
00066         _mutex.unlock();
00067         return rc;
00068     }
00069 
00070     /** Reset the buffer
00071      *
00072      */
00073     void reset() {
00074         _mutex.lock();
00075         _circBuf.reset();
00076         _mutex.unlock();
00077     }
00078 
00079 private:
00080     Mutex _mutex;
00081     CircularBuffer<T, BufferSize, CounterType> _circBuf;
00082 };
00083 
00084 #endif