Blynk library for embedded hardware. Works with Arduino, ESP8266, Raspberry Pi, Intel Edison/Galileo, LinkIt ONE, Particle Core/Photon, Energia, ARM mbed, etc. http://www.blynk.cc/

Dependents:   Blynk_RBL_BLE_Nano Blynk_MicroBit Blynk_Serial Blynk_RBL_BLE_Nano

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BlynkFifo.h Source File

BlynkFifo.h

Go to the documentation of this file.
00001 /**
00002  * @file       BlynkFifo.h
00003  * @author     Volodymyr Shymanskyy
00004  * @license    This project is released under the MIT License (MIT)
00005  * @copyright  Copyright (c) 2015 Volodymyr Shymanskyy
00006  * @date       Feb 2015
00007  * @brief      FIFO implementation
00008  *
00009  */
00010 
00011 #ifndef BlynkFifo_h
00012 #define BlynkFifo_h
00013 
00014 #include <utility/BlynkUtility.h>
00015 
00016 template <class T, unsigned N>
00017 class BlynkFifo
00018 {
00019 public:
00020     BlynkFifo()
00021     {
00022         clear();
00023     }
00024 
00025     void clear()
00026     {
00027         _r = 0;
00028         _w = 0;
00029     }
00030 
00031     ~BlynkFifo(void)
00032     {}
00033 
00034     // writing thread/context API
00035     //-------------------------------------------------------------
00036 
00037     bool writeable(void)
00038     {
00039         return free() > 0;
00040     }
00041 
00042     int free(void)
00043     {
00044         int s = _r - _w;
00045         if (s <= 0)
00046             s += N;
00047         return s - 1;
00048     }
00049 
00050     T put(const T& c)
00051     {
00052         int i = _w;
00053         int j = i;
00054         i = _inc(i);
00055         while (i == _r) // = !writeable()
00056             /* nothing / just wait */;
00057         _b[j] = c;
00058         _w = i;
00059         return c;
00060     }
00061 
00062     int put(const T* p, int n, bool blocking = false)
00063     {
00064         int c = n;
00065         while (c)
00066         {
00067             int f;
00068             while ((f = free()) == 0) // wait for space
00069             {
00070                 if (!blocking) return n - c; // no more space and not blocking
00071                 /* nothing / just wait */;
00072             }
00073             // check free space
00074             if (c < f) f = c;
00075             int w = _w;
00076             int m = N - w;
00077             // check wrap
00078             if (f > m) f = m;
00079             memcpy(&_b[w], p, f);
00080             _w = _inc(w, f);
00081             c -= f;
00082             p += f;
00083         }
00084         return n - c;
00085     }
00086 
00087     // reading thread/context API
00088     // --------------------------------------------------------
00089 
00090     bool readable(void)
00091     {
00092         return (_r != _w);
00093     }
00094 
00095     size_t size(void)
00096     {
00097         int s = _w - _r;
00098         if (s < 0)
00099             s += N;
00100         return s;
00101     }
00102 
00103     T get(void)
00104     {
00105         int r = _r;
00106         while (r == _w) // = !readable()
00107             /* nothing / just wait */;
00108         T t = _b[r];
00109         _r = _inc(r);
00110         return t;
00111     }
00112 
00113     T peek(void)
00114     {
00115         int r = _r;
00116         while (r == _w);
00117         return _b[r];
00118     }
00119 
00120     int get(T* p, int n, bool blocking = false)
00121     {
00122         int c = n;
00123         while (c)
00124         {
00125             int f;
00126             for (;;) // wait for data
00127             {
00128                 f = size();
00129                 if (f)  break;        // free space
00130                 if (!blocking) return n - c; // no space and not blocking
00131                 /* nothing / just wait */;
00132             }
00133             // check available data
00134             if (c < f) f = c;
00135             int r = _r;
00136             int m = N - r;
00137             // check wrap
00138             if (f > m) f = m;
00139             memcpy(p, &_b[r], f);
00140             _r = _inc(r, f);
00141             c -= f;
00142             p += f;
00143         }
00144         return n - c;
00145     }
00146 
00147 private:
00148     int _inc(int i, int n = 1)
00149     {
00150         return (i + n) % N;
00151     }
00152 
00153     T             _b[N];
00154     volatile int  _w;
00155     volatile int  _r;
00156 };
00157 
00158 #endif