How to use a private function from .h archive

Hi everyone! I recently started to use a library (GitHub - Seeed-Studio/Seeed_Arduino_CAN: Seeed Arduino CAN-BUS library - MCP2518FD&MCP2515&MCP2551) and wanted to use one of their private function which I saw in the .h archive:

byte mcp2515_readRegister(const byte address);              // read mcp2515's register

The thing is I don't know how to call it in my program since it is not public... I tried to edit the archive and change it from private and put it on public but doesn't work. What do I have to do to be able to use this function?

Show your attempt. Any error messages too.

Looking at how that function is used in the .cpp file, maybe you should add another function to call that one to do what you want.

The project is about 2 arduinos trying to communicate using a CAN-Bus protocol. Here is the send code where I use the function and also the error message I get:
CODE:

// demo: CAN-BUS Shield, send data
#include <mcp_can.h>
#include <SPI.h>

/*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
  #define SERIAL SerialUSB
#else
  #define SERIAL Serial
#endif

// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;
const int ledHIGH    = 1;
const int ledLOW     = 0;

MCP_CAN CAN(SPI_CS_PIN);                                    // Set CS pin

void setup()
{
    SERIAL.begin(115200);

    while (CAN_OK != CAN.begin(CAN_500KBPS))              // init can bus : baudrate = 500k
    {
        SERIAL.println("CAN BUS Shield init fail");
        SERIAL.println(" Init CAN BUS Shield again");
        delay(100);
    }
    SERIAL.println("CAN BUS Shield init ok!");
}

unsigned char stmp[8] = {ledHIGH, 1, 2, 3, ledLOW, 5, 6, 7};

void loop()
{   SERIAL.println("In loop");
    // send data:  id = 0x70, standard frame, data len = 8, stmp: data buf
    CAN.sendMsgBuf(0x70, 0, 8, stmp);
    delay(1000);                       // send data once per second
    CAN.mcp2515_readRegister(28);
}

ERROR MSG:
exit status 1
'byte MCP_CAN::mcp2515_readRegister(byte)' is private within this context

Or maybe just change the library so the function you want is not private?

...R

The thing is that I have already developed an entire code using this library and I learned with this one... Is there no option to use private functions??

Mephirius:
The thing is that I have already developed an entire code using this library and I learned with this one... Is there no option to use private functions??

No, that's what private means. You've already been given 2 viable options:

  • Change the function in question to 'public'.

  • Add a 'public' function to the library that calls the 'private' one.

You could also specify a 'friend' function in the library.

I see... then can someone explain to me briefly how to do any of the given options? or maybe give me a link to learn how to either change a private function to public or how to call a private function by adding a public.

Thanks.

If you look at the library code you will see the PRIVATE and PUBLIC keywords. Either move the function you want from the PRIVATE to the PUBLIC area or make a copy of it in the PUBLIC area with a slightly different name. Or create your own function in the PUBLIC area that makes a call to the private function.

...R

This is what I actually tried but I can't edit this file... when I copy the function from the PRIVATE into the PUBLIC part and try to save the file it appears an error message. I don't know if I am doing something wrong, any idea?

Mephirius:
it appears an error message.

It's rather difficult to help when you have not told us what the error message says. Post the error message.

...R

My bad, this is the error that appears once I try to save the file where I declared a new Public function:

Access to C:\Program Files (x86)\Arduino\libraries\CAN_BUS_Shield-master\mcp_can.h was denied.

That sounds like you need administrator or super-user privileges to edit the file. What PC operating system are you using?

...R

Im using windows 8

Custom libraries don't belong in that location. How did you install the library there?

If you copy the entire library to Documents\Arduino\libraries with your other custom libraries then it will give a warning about multiple copies but it should use that one first.

If you must edit a library or core file in Program Files then when you first open your editor (Notepad+ or your preference) you can right-click the program icon and choose "run as administrator".

I installed the library by downloading it then copied and pasted in Program Files\Arduino\libraries. Is that the wrong way?

I also tried to run it as administrator but apparently I can only run as administrator applications but not files like WordPad. Any ideas?

Yes. That is the wrong place to install a library. Use the library manager for most mainstream libraries.