RingBuffer Component with Put/Get/Clear Events

Sometimes I have a good idea how to extend one of my Processor Expert components with an extra feature, but then I step back because why implementing more than I need at the moment? Until another user of the component simply asks for the same thing, and here we go: if one or more can take advantage of a feature, that’s definitely a strong argument to add it :-). This happened with the RingBuffer Processor Expert component I’m using in many projects. And a reader of this blog asked to add some extra event methods: when an item is added or removed to the buffer.

RingBuffer used in USB Component with Extra Events

RingBuffer used in USB Component with Extra Events

Beside of the OnItemPut() and OnItemGet() events, I added an extra OnClear() event which gets called when the ring buffer Clear() method gets called. The events are disabled  by default not to add any overhead, and they can be enabled individually.

Using the CDE (Component Development Environment) of Processor Expert makes it very easy to such additional events: define the interface for the event, and then add the event code to the driver. Below shows it for the Put() method:

byte %'ModuleName'%.%Put(%'ModuleName'_ElementType elem)
  byte res = ERR_OK;

  if (%'ModuleName'%.inSize==%'ModuleName'%.BUF_SIZE) {
    res = ERR_TXFULL;
  } else {
    %'ModuleName'%.buffer[%'ModuleName'%.inIdx] = elem;
    if (%'ModuleName'%.inIdx==%'ModuleName'%.BUF_SIZE) {
      %'ModuleName'%.inIdx = 0;
%if defined(OnBufferFull)
  if (res==ERR_TXFULL) {
    %OnBufferFull(); /* call user event */
%if defined(OnItemPut)
  %OnItemPut(elem); /* call optional user event */
  return res;

To check the details of the change, see the commit on GitHub.

So with using the new RingBuffer events in my USB stack, I can get application notifications for every byte received or sent, which is very useful.

The updated component will be available with the next *.PEupd release.

Happy Eventing 🙂


8 thoughts on “RingBuffer Component with Put/Get/Clear Events

  1. Is it possible to increase the size of Ring buffer ? I am using CodeWarrior version 10.3 and there the limit on ring buffer size is 256bytes.


  2. Hi Erick,
    Thanks for keeping us informed with new features.
    I was using an old version version of this ring buffer component, just updated and seems the new one doesn’t have the “Clear:” Method. is it correct?


    • Hi Marco,
      No, that would not be correct. The ‘RingBuffer’ component has a Clear() method. Are you sure that you use the ‘RingBuffer’ component? I ask because in the past I had a RingBufferUInt8 which is obsolete new.
      Can you check again?


      • Hi Erick,
        This happened in a Home copy of the project where I need to do some code walk through, Compilation failed due the lack of the “Clear” method. After updating the components from Sourceforge, compilation failed again, but If I add a new Ringbuffer then I get the Clear method along with the events mentioned in this post, what I need is erase and replace with the new.
        One thing I notice is that this new ring buffer component generates a Critical Section. I have worked with the component that lacks this referenced component for months. is there any advice on this? so far I haven’t notice any critical section problem as just move the ring buffer to another buffer where I I do any data handling. My application requires 8 UARTs and I am multiplexing the serial port 2:1 with each one having its own working buffer, where I do the data processing.
        Thanks again for valuable work, it is very useful and practical. It has saved me tons of hours of work!


What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.