Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Virtual attach functions #34

Merged
merged 2 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions examples/PinChangeInt/PinChangeLib/PinChangeLib.ino
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ ServoInputPin<pin2> servo2;

void setup() {
Serial.begin(115200);
servo1.attach(); // attaches the first servo input interrupt
servo2.attach(); // attaches the second servo input interrupt
ServoInput.attach(); // attach all inputs

// wait for all servo signals to be read for the first time
while (!ServoInput.available()) {
Expand Down
3 changes: 1 addition & 2 deletions examples/RC_Receiver/RC_Receiver.ino
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ ServoInputPin<ThrottleSignalPin> throttle(ThrottlePulseMin, ThrottlePulseMax);

void setup() {
Serial.begin(115200);
steering.attach(); // attaches the steering servo input interrupt
throttle.attach(); // attaches the throttle servo input interrupt
ServoInput.attach(); // attach all inputs

while (!ServoInput.available()) { // wait for all signals to be ready
Serial.println("Waiting for servo signals...");
Expand Down
18 changes: 18 additions & 0 deletions src/ServoInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@

#include "ServoInput.h"

void ServoInputManager::attach() {
ServoInputSignal* ptr = ServoInputSignal::getHead();

while (ptr != nullptr) {
ptr->attach();
ptr = ptr->getNext();
}
}

void ServoInputManager::detach() {
ServoInputSignal* ptr = ServoInputSignal::getHead();

while (ptr != nullptr) {
ptr->detach();
ptr = ptr->getNext();
}
}

bool ServoInputManager::available() {
return allAvailable();
}
Expand Down
6 changes: 6 additions & 0 deletions src/ServoInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class ServoInputSignal {
ServoInputSignal();
virtual ~ServoInputSignal();

virtual void attach() = 0;
virtual void detach() = 0;

virtual bool available() const = 0;

uint16_t getPulse();
Expand Down Expand Up @@ -276,6 +279,9 @@ template<uint8_t Pin> volatile unsigned long ServoInputPin<Pin>::pulseDuration =

class ServoInputManager {
public:
static void attach();
static void detach();

static bool available();
static bool allAvailable();
static bool anyAvailable();
Expand Down
Loading