Skip to main content
Works with extern "C"
Source Link
Edgar Bonet
  • 44.3k
  • 4
  • 41
  • 79

Just throwing an idea. No time to test nowJust throwing an idea. No time to test now.

Edit: I tested the solution below, and it works as expected, provided both the __wrap_ and __real_digitalWrite() are declared extern "C".

If you can convince the IDE to add extra options to the compile command line, you could:

  1. name __wrap_digitalWrite() your implementation of digitalWrite()
  2. inside it, call __real_digitalWrite() when you want to get the implementation from Arduino core
  3. add -Wl,--wrap=digitalWrite to the compile command of the final link step

Example wrapper:

extern "C" {
    void __wrap_digitalWrite(uint8_t pin, uint8_t value)
{;
    void __real_digitalWrite(pinuint8_t, valueuint8_t);
}

// Calls to digitalWrite() are ifdiverted to this.
void __wrap_digitalWrite(uint8_t pin, ==uint8_t 8value)  
{
    __real_digitalWrite(pin, value);  // Serial.print("Pincall ");
implementation from Arduino core
    if Serial.print(pin == 8); {
        Serial.print("F("Pin 8 changed to "));
        Serial.println(value);
    }
}

Trying to put this inside a class won't help.

C.f. the man page of gnu ld.

Just throwing an idea. No time to test now.

If you can convince the IDE to add extra options to the compile command line, you could:

  1. name __wrap_digitalWrite() your implementation of digitalWrite()
  2. inside it, call __real_digitalWrite() when you want to get the implementation from Arduino core
  3. add -Wl,--wrap=digitalWrite to the compile command of the final link step

Example wrapper:

void __wrap_digitalWrite(uint8_t pin, uint8_t value)
{
    __real_digitalWrite(pin, value);
    if (pin == 8) {
        Serial.print("Pin ");
        Serial.print(pin);
        Serial.print(" changed to ");
        Serial.println(value);
    }
}

Trying to put this inside a class won't help.

C.f. the man page of gnu ld.

Just throwing an idea. No time to test now.

Edit: I tested the solution below, and it works as expected, provided both the __wrap_ and __real_digitalWrite() are declared extern "C".

If you can convince the IDE to add extra options to the compile command line, you could:

  1. name __wrap_digitalWrite() your implementation of digitalWrite()
  2. inside it, call __real_digitalWrite() when you want to get the implementation from Arduino core
  3. add -Wl,--wrap=digitalWrite to the compile command of the final link step

Example wrapper:

extern "C" {
    void __wrap_digitalWrite(uint8_t, uint8_t);
    void __real_digitalWrite(uint8_t, uint8_t);
}

// Calls to digitalWrite() are diverted to this.
void __wrap_digitalWrite(uint8_t pin, uint8_t value) 
{
    __real_digitalWrite(pin, value);  // call implementation from Arduino core
    if (pin == 8) {
        Serial.print(F("Pin 8 changed to "));
        Serial.println(value);
    }
}

Trying to put this inside a class won't help.

C.f. the man page of gnu ld.

Source Link
Edgar Bonet
  • 44.3k
  • 4
  • 41
  • 79

Just throwing an idea. No time to test now.

If you can convince the IDE to add extra options to the compile command line, you could:

  1. name __wrap_digitalWrite() your implementation of digitalWrite()
  2. inside it, call __real_digitalWrite() when you want to get the implementation from Arduino core
  3. add -Wl,--wrap=digitalWrite to the compile command of the final link step

Example wrapper:

void __wrap_digitalWrite(uint8_t pin, uint8_t value)
{
    __real_digitalWrite(pin, value);
    if (pin == 8) {
        Serial.print("Pin ");
        Serial.print(pin);
        Serial.print(" changed to ");
        Serial.println(value);
    }
}

Trying to put this inside a class won't help.

C.f. the man page of gnu ld.