# Circular Buffer - FIFO

**URL:** <https://community.particle.io/t/circular-buffer-fifo/42226>\
**Category:** General\
**Created:** [June 3, 2018, 5:59pm UTC](https://community.particle.io/t/circular-buffer-fifo/42226 "2018-06-03T17:59:01Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![RWB](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rwb/32/22599_2.png) [@RWB](https://community.particle.io/u/RWB)\
**Post date:** [June 3, 2018, 5:59pm UTC](https://community.particle.io/t/circular-buffer-fifo/42226/1 "2018-06-03T17:59:01Z")

</div>

@rlogiacco I came across your Circular Buffer Arduino library while searching Google to learn about FIFO buffers.

I remember seeing you on this forum so figured I would reachout and ask if any changes need to be made for this to work on the Photon or the Electron without any problems.

> **[rlogiacco/CircularBuffer](https://github.com/rlogiacco/CircularBuffer)**
>
> Arduino circular buffer library. Contribute to rlogiacco/CircularBuffer development by creating an account on GitHub.

I’m looking at using this for graphing out a running stream of data like seen below:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/1/e/1ed359249939164711da62cbdaabc07867c4ebd2.jpg)

---

<div class="post-metadata">

**Author:** ![rlogiacco](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rlogiacco/32/20725_2.png) [@rlogiacco](https://community.particle.io/u/rlogiacco)\
**Post date:** [June 4, 2018, 8:42am UTC](https://community.particle.io/t/circular-buffer-fifo/42226/2 "2018-06-04T08:42:30Z")

</div>

It should work out of the box, for what I recall there is no dependency on anything specific to Arduino in the library.  
I believe I’ve also used it on a Photon (actually a Spark Core), but I’m not 100% sure about that…  
Give it a try and let me know: we can work out the hiccups if you find any 😉

---

<div class="post-metadata">

**Author:** ![RWB](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rwb/32/22599_2.png) [@RWB](https://community.particle.io/u/RWB)\
**Post date:** [June 5, 2018, 7:07am UTC](https://community.particle.io/t/circular-buffer-fifo/42226/3 "2018-06-05T07:07:20Z")

</div>

I got it working!

But not without one issue 🙂

So as is it would not compile for me and threw an error about not being able to find the CircularBuffer.tpp file which is called in the CircularBuffer.h file.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/8/b/8bd495a65ab1c9f1a6b37f08acc902763565b0d4.png)

So after reading about .tpp files to understand better what they do, I commented out the #include “CircularBuffer.tpp” as shown below:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/b/a/ba8b5165a5ef369bb5cfaaded442409b1ab42c84.png)

And then I just copied and pasted the contents of the CircularBuffer.tpp file to the bottom of the code in the CircularBuffer.h file and it compiled and is working as described now.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/9/c/9ca9879fd156036975063200fa7c4b0eb22a6130.png)

I’ve learned a lot about how Circular Buffers work by playing with this code and graphing out the Buffer over time.

The library made it easy to understand and work with 🙂

---

<div class="post-metadata">

**Author:** ![RWB](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rwb/32/22599_2.png) [@RWB](https://community.particle.io/u/RWB)\
**Post date:** [June 8, 2018, 2:28am UTC](https://community.particle.io/t/circular-buffer-fifo/42226/4 "2018-06-08T02:28:04Z")

</div>

On the Photon I had to add this line to keep the array from being filled with random Spikes in data readings which I’m guessing were caused by the Particle OS interrupts that were operating in the background because your Interrupt trick below prevented the random spikes and dips from happening.

Not sure what compiler optimizations get turned off though. Any ideas if this applies to the Particle Platform?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/5/8/5829e076509e00b3d2e1e546cfe2cfcfc6572ad2.png)

So I have to say your library allowed me to get going quickly with Circular Buffers without needing to write the code from scratch.

Everything is working the way I wanted it to so I’m happy to see some of my ideas realized with graphing data over time on a color LCD screen.

---

<div class="post-metadata">

**Author:** ![rlogiacco](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rlogiacco/32/20725_2.png) [@rlogiacco](https://community.particle.io/u/rlogiacco)\
**Post date:** [June 8, 2018, 6:38pm UTC](https://community.particle.io/t/circular-buffer-fifo/42226/5 "2018-06-08T18:38:14Z")

</div>

Good to know the trick does work as intended!  
I guess you are populating or accessing the buffer within an interrupt routine, right?

I’m not a C expert, but my understanding is, when the count variable is defined as volatile, the compiler is forced to read it’s value, without trying to use the latest buffered value, as it might change out of the normal process flow.  
Normally, if the compiler doesn’t see any change in the process flow, it just uses the latest known value, but when a change occurs in an interrupt, the compiler has no way to guess that.  
The downside of declaring a variable volatile is you are forcing read operations to access the RAM rather than just use the register value, a little price in your case. Obviously you want to use it only where needed, that’s why is not on by default…

Thanks for your feedback: I will add the Photon to the supported devices. If you don’t mind I would appreciate a star on the Github project 😉

---

<div class="post-metadata">

**Author:** ![rlogiacco](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rlogiacco/32/20725_2.png) [@rlogiacco](https://community.particle.io/u/rlogiacco)\
**Post date:** [June 8, 2018, 6:41pm UTC](https://community.particle.io/t/circular-buffer-fifo/42226/6 "2018-06-08T18:41:25Z")

</div>

Humm… what if you rename the `.tpp` to `.cpp`? Does it work that way? It might be something related to how the compiler is invoked by the Particle build platform… Anybody willing to advice on that?

BTW, I’m really happy the library is easy to understand 😊

---

<div class="post-metadata">

**Author:** ![RWB](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rwb/32/22599_2.png) [@RWB](https://community.particle.io/u/RWB)\
**Post date:** [June 8, 2018, 6:49pm UTC](https://community.particle.io/t/circular-buffer-fifo/42226/7 "2018-06-08T18:49:58Z")

</div>

> [@rlogiacco](#):
>
> Good to know the trick does work as intended!

I think it is.

I just saw one blip indicating the value was out of normal realistic bounds but this happened while I was moving something so it could have been a 255 reading from a bad connection. Other than this one blip I have not seeing any others after adding the Interrupt code the library provides.

I'll be testing this for months I'll know if it's stable long-term or not.

> [@rlogiacco](#):
>
> The downside of declaring a variable volatile is you are forcing read operations to access the RAM rather than just use the register value, a little price in your case. Obviously you want to use it only where needed, that’s why is not on by default…

So does things get worse if I want to have many circular buffers using the Volatile method? I do want to add more buffers.

> I guess you are populating or accessing the buffer within an interrupt routine, right?

I'm using two Particle Software Timer, so I assume it's working via interrupts.

```cpp
Timer FillPowerBufferTimer(60000, FillPowerBuffer); //Take a Power Reading every min.

```

> [@rlogiacco](#):
>
> Thanks for your feedback: I will add the Photon to the supported devices. If you don’t mind I would appreciate a star on the Github project 😉

I have no idea how to star Github projects 🙂 I do not have an account there.

> [@rlogiacco](#):
>
> Humm… what if you rename the .tpp to .cpp? Does it work that way?

Tried that and it will not compile.

> [@rlogiacco](#):
>
> BTW, I’m really happy the library is easy to understand 😊

It allowed me to get going quickly, so good job.

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [June 8, 2018, 7:02pm UTC](https://community.particle.io/t/circular-buffer-fifo/42226/8 "2018-06-08T19:02:48Z")

</div>

> [@RWB](#):
>
> I’m using two Particle Software Timer, so I assume it’s working via interrupts.

@RWB, Software Tiers are not interrupts. They are (daisy chained) timers implemented in a single FreeRTOS thread. Because of this, much like an ISR, you will need to be careful when you access queue data in your application. You may need to use SINGLE\_THREAD\_BLOCK() when reading from your circular buffer. Using volatile as was done doesn't hurt either as you found.

One thing to remember with Software Timers is that a long running timer callback can potentially prevent other timers from firing since all timers are daisy chained and on a single thread. So make sure your data acquisition is faster than the fastest timer interval.

---

<div class="post-metadata">

**Author:** ![RWB](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rwb/32/22599_2.png) [@RWB](https://community.particle.io/u/RWB)\
**Post date:** [June 8, 2018, 7:08pm UTC](https://community.particle.io/t/circular-buffer-fifo/42226/9 "2018-06-08T19:08:16Z")

</div>

Thanks for that.

Is the SparkIntervalTimer app able to do timers spaced to fire every min? I tried but was not able to get it to work like the software timer. Maybe I was doing something wrong or maybe the library only works with shorter timing frames.

Since declaring the buffers as Volatile things seem to be stable.

I’m calling the timers to take readings every min. I should just bundle all the read task into the same function that the timer calls.

Graphing data out over a 3.5-hour timeframe is looking good on this 240x240 color LCD using that graphing library.

---

<div class="post-metadata">

**Author:** ![rlogiacco](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rlogiacco/32/20725_2.png) [@rlogiacco](https://community.particle.io/u/rlogiacco)\
**Post date:** [June 8, 2018, 7:31pm UTC](https://community.particle.io/t/circular-buffer-fifo/42226/10 "2018-06-08T19:31:23Z")

</div>

> [@RWB](#):
>
> I have no idea how to star Github projects 🙂 I do not have an account there.

Than don't worry 😉

> [@RWB](#):
>
> Tried that and it will not compile.

Too bad... We might want to verify with Particle if there's any trick to have templated code compile in any other manner compatible to the Arduino world....

> [@RWB](#):
>
> So does things get worse if I want to have many circular buffers using the Volatile method? I do want to add more buffers.

No, that shouldn't be a big issue, it's probably using 4 or 6 clock cycles instead of 1, but that's a tiny tiny fraction of time: I just didn't want to have it always on.

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [June 8, 2018, 7:44pm UTC](https://community.particle.io/t/circular-buffer-fifo/42226/11 "2018-06-08T19:44:42Z")

</div>

> [@RWB](#):
>
> Is the SparkIntervalTimer app able to do timers spaced to fire every min? I tried but was not able to get it to work like the software timer. Maybe I was doing something wrong or maybe the library only works with shorter timing frames.

The SparkIntervalTimer library CAN give you one minute interrupts but your application is better served with Software Timers IMO.

---

<div class="post-metadata">

**Author:** ![RWB](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rwb/32/22599_2.png) [@RWB](https://community.particle.io/u/RWB)\
**Post date:** [June 8, 2018, 7:56pm UTC](https://community.particle.io/t/circular-buffer-fifo/42226/12 "2018-06-08T19:56:41Z")

</div>

That makes me feel like I’m on the right track then.

---

<div class="post-metadata">

**Author:** ![RWB](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rwb/32/22599_2.png) [@RWB](https://community.particle.io/u/RWB)\
**Post date:** [June 8, 2018, 9:04pm UTC](https://community.particle.io/t/circular-buffer-fifo/42226/13 "2018-06-08T21:04:37Z")

</div>

Saw another spike, so I put the read from Buffer for loop() into a SINGLE\_THREAD\_BLOCK() to see if that is indeed the cause of the random out of range readings.

Learning something new every day.
