Skip to content

Commit afbc80f

Browse files
authored
Merge pull request #34 from dmadison/virtual-attach
Virtual attach functions
2 parents dd5fa82 + 9e3408d commit afbc80f

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

examples/PinChangeInt/PinChangeLib/PinChangeLib.ino

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ ServoInputPin<pin2> servo2;
5353

5454
void setup() {
5555
Serial.begin(115200);
56-
servo1.attach(); // attaches the first servo input interrupt
57-
servo2.attach(); // attaches the second servo input interrupt
56+
ServoInput.attach(); // attach all inputs
5857

5958
// wait for all servo signals to be read for the first time
6059
while (!ServoInput.available()) {

examples/RC_Receiver/RC_Receiver.ino

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ ServoInputPin<ThrottleSignalPin> throttle(ThrottlePulseMin, ThrottlePulseMax);
5353

5454
void setup() {
5555
Serial.begin(115200);
56-
steering.attach(); // attaches the steering servo input interrupt
57-
throttle.attach(); // attaches the throttle servo input interrupt
56+
ServoInput.attach(); // attach all inputs
5857

5958
while (!ServoInput.available()) { // wait for all signals to be ready
6059
Serial.println("Waiting for servo signals...");

src/ServoInput.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@
2222

2323
#include "ServoInput.h"
2424

25+
void ServoInputManager::attach() {
26+
ServoInputSignal* ptr = ServoInputSignal::getHead();
27+
28+
while (ptr != nullptr) {
29+
ptr->attach();
30+
ptr = ptr->getNext();
31+
}
32+
}
33+
34+
void ServoInputManager::detach() {
35+
ServoInputSignal* ptr = ServoInputSignal::getHead();
36+
37+
while (ptr != nullptr) {
38+
ptr->detach();
39+
ptr = ptr->getNext();
40+
}
41+
}
42+
2543
bool ServoInputManager::available() {
2644
return allAvailable();
2745
}

src/ServoInput.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class ServoInputSignal {
3636
ServoInputSignal();
3737
virtual ~ServoInputSignal();
3838

39+
virtual void attach() = 0;
40+
virtual void detach() = 0;
41+
3942
virtual bool available() const = 0;
4043

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

277280
class ServoInputManager {
278281
public:
282+
static void attach();
283+
static void detach();
284+
279285
static bool available();
280286
static bool allAvailable();
281287
static bool anyAvailable();

0 commit comments

Comments
 (0)