Question

There is an Arduino library called 'ByteBuffer' (found here), which is a circular buffer implementation. I modified it slightly (calling it 'ByteBufferPro'), by chopping off support for all non-byte data-types, and adding few convenience methods. I intend to use this in my Interrupt-Service-Routine, that is already doing some heavy-lifting. To relieve some bit of workload on the ISR, I intend to let the ISR push bytes of information on the circular-buffer (since capturing/storing them is time-critical), but actual processing of that information is not as time-critical, which I do in the main loop().

Following the logic that any variable that can be updated in ISR should have the 'volatile' qualifier in declaration, I did declare the buffer as volatile, but now I am seeing these compilation errors:-

ByteBufferProExample.cpp: In function 'void setup()':
ByteBufferProExample:12: error: passing 'volatile ByteBufferPro' as 'this' argument of 'void ByteBufferPro::init(unsigned int)' discards qualifiers
ByteBufferProExample.cpp: In function 'void loop()':
ByteBufferProExample:24: error: passing 'volatile ByteBufferPro' as 'this' argument of 'void ByteBufferPro::clear()' discards qualifiers

Wondering as to what is the reason, and how I might fix this ? Taking the cribbage being about 'this' pointer, I have a feeling that if I turn the implementation of ByteBuffer from C++ to a C one (OO to procedural), that should be an easy way to fix it, although I really like the OO semantics. Would do so, if there is no other way.

Was it helpful?

Solution

All of your functions should have volatile qualifier, just like const qualifier needed for constant objects. Here is an example:

class A
{
public:
    A(unsigned int a)
    {
    }

    void init() volatile
    {
        cout << "A::init()" << endl;
    }
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top