Move private to protected members allowing inheritance

Issue #66 resolved
Iván Raskovsky created an issue

I’m implementing my own esp32 interrupt based readings (non blocking and with no timer/polling) and I need to be able to call methods such as ping_trigger. Changing the private members to protected allows me to subclass NewPing into NewPingInterrupt without needing to change this library.

I wanted to create a pull request but I don’t have the needed permissions. The change I need is here: https://bitbucket.org/rasca7/arduino-new-ping/commits/e024eefbaff44fec103ab63d0fb6f6baa9b82a2e

For reference this is the (work in progress) code:

class NewPingInterrupt : public NewPing {
    public:

    using NewPing::NewPing;
    unsigned long ping_cm(void (*callback)());
    void echo();
    void stop();
};
unsigned long NewPingInterrupt::ping_cm(void (*callback)()) {
    if (!ping_trigger()) return NO_ECHO; // Trigger a ping, if it returns false, return NO_ECHO to the calling function.

    // Interrupt
    attachInterrupt(digitalPinToInterrupt(_echoPin), callback, FALLING);
    // TODO: launch a timer to stop the reading
}

void NewPingInterrupt::echo() {
    unsigned long echoTime = (micros() - (_max_time - _maxEchoTime) - PING_OVERHEAD); // Calculate ping time, include overhead.
    unsigned long distanceCm = echoTime / US_ROUNDTRIP_CM;

    detachInterrupt(digitalPinToInterrupt(_echoPin));
}

void NewPingInterrupt::stop() {
    detachInterrupt(digitalPinToInterrupt(_echoPin));
}

Comments (4)

  1. Tim Eckel repo owner

    Not sure what I’d need to do to with the settings to allow pull requests from a fork. I didn’t set anything to block it and it’s not setup to be private. So I’m not sure why it doesn’t allow it.

    Anyway, those routes are designed to be used only from the library not in your code, which is why they’re set as private.

    Also, I’m not using interrupts on purpose as it greatly reduces the flexibility of the library. This was tried initially, but it simply wasn’t needed. Polling is more flexible on the AVR microcontroller. This library wasn’t intended for the esp32 platform.

  2. Log in to comment