Search code examples
c++cansi-c

C reference to library object


Im working with my arduino to improve my C knowlege. I made several attemps, but I can't get it to work:-( I hope someone can help me.

I have different objects from a library.

Adafruit_NeoPixel hours = Adafruit_NeoPixel(NUMPIXELS, .....);
Adafruit_NeoPixel minutes = Adafruit_NeoPixel(NUMPIXELS, ....-);
Adafruit_NeoPixel seconds = Adafruit_NeoPixel(NUMPIXELS, .....);

Now I want to call a function several times with a pointer to the declared library object (hours, minute & seconds => E.G POINTERTOLIB ?)

void showTime(int iShowTime, **POINTERTOLIB** ) 
{
    int ones = iShowTime % 24; 

    //set LEDs according to acutal Time
    for (int i=0; i<8; i++) 
    {
        ((ones >> i) & 1) ? **POINTERTOLIB** .setPixelColor(i, **POINTERTOLIB** .Color(0,150,0)) :          **POINTERTOLIB** .setPixelColor(i, hours.Color(0,0,0));
        **POINTERTOLIB** .setBrightness(40);
    }
    **POINTERTOLIB** .show(); // This sends the updated pixel color to the hardware.
}

Thanks for your help


Solution

  • Since you want to call the function showTime several times using different objects (hours, minutes, seconds) of class Adafruit_NeoPixel then you can use the following signature:

    void showTime(int iShowTime, Adafruit_NeoPixel *pObj);
    

    Therefore, now showTime expects a pointer to any valid object of class Adafruit_NeoPixel.

    Inside showTime you can call the member_functions of class Adafruit_NeoPixel as follows:

    pObj->setPixelColor() 
    pObj->Color(). 
    

    You can call showTime as follows:

    showTime(3, &hours); or showTime(3, &minutes);
    showTime(3, &hours); or showTime(3, &hours);
    showTime(3, &hours); or showTime(3, &seconds);