From c8d79256dba2517aaf2545017c87885c3964df72 Mon Sep 17 00:00:00 2001 From: David Madison Date: Thu, 25 Jan 2024 11:03:05 -0500 Subject: [PATCH] Do not auto-attach interrupt on construction This was causing issues on certain platforms where the platform's interrupt attachment system was not set up or available when the object was constructed in global scope (see #28). Instead, the user must attach() the object themselves before they can read signals. The library examples have been updated accordingly. --- examples/BasicAngle/BasicAngle.ino | 1 + examples/Calibration/Calibration.ino | 1 + examples/Loopback/Loopback.ino | 1 + examples/PinChangeInt/PinChange/PinChange.ino | 2 ++ examples/PinChangeInt/PinChangeLib/PinChangeLib.ino | 2 ++ examples/RC_Receiver/RC_Receiver.ino | 2 ++ examples/Remapping/Remapping.ino | 1 + src/ServoInput.h | 1 - 8 files changed, 10 insertions(+), 1 deletion(-) diff --git a/examples/BasicAngle/BasicAngle.ino b/examples/BasicAngle/BasicAngle.ino index 2eba445..77065ee 100644 --- a/examples/BasicAngle/BasicAngle.ino +++ b/examples/BasicAngle/BasicAngle.ino @@ -38,6 +38,7 @@ ServoInputPin<2> servo; void setup() { Serial.begin(115200); + servo.attach(); // attaches the servo input interrupt while (servo.available() == false) { Serial.println("Waiting for servo signal..."); diff --git a/examples/Calibration/Calibration.ino b/examples/Calibration/Calibration.ino index 5774684..4ee2b10 100644 --- a/examples/Calibration/Calibration.ino +++ b/examples/Calibration/Calibration.ino @@ -40,6 +40,7 @@ ServoInputPin<2> servo; void setup() { Serial.begin(115200); + servo.attach(); // attaches the servo input interrupt int center = servo.getRangeCenter(); // get center value of range servo.setRange(center, center); // set min/max values to center diff --git a/examples/Loopback/Loopback.ino b/examples/Loopback/Loopback.ino index c4bc407..c8a5c11 100644 --- a/examples/Loopback/Loopback.ino +++ b/examples/Loopback/Loopback.ino @@ -49,6 +49,7 @@ int waitTime = 10; // milliseconds (ms) void setup() { Serial.begin(115200); + inputServo.attach(); // attaches the servo input interrupt outputServo.attach(OutputPin); // attaches the servo on pin 9 to the servo object while (inputServo.available() == false) { diff --git a/examples/PinChangeInt/PinChange/PinChange.ino b/examples/PinChangeInt/PinChange/PinChange.ino index cd4bf89..e97483c 100644 --- a/examples/PinChangeInt/PinChange/PinChange.ino +++ b/examples/PinChangeInt/PinChange/PinChange.ino @@ -60,6 +60,8 @@ ISR(PCINT0_vect) { // pin change ISR handler for Arduino Uno pins D8 - D13 void setup() { Serial.begin(115200); + // note that we do *not* need to call servo.attach(), because we are + // using our own custom interrupt service routine (ISR) setInterrupt(); // set pin change interrupt (see above) while (servo.available() == false) { diff --git a/examples/PinChangeInt/PinChangeLib/PinChangeLib.ino b/examples/PinChangeInt/PinChangeLib/PinChangeLib.ino index 799ffb1..fe1c281 100644 --- a/examples/PinChangeInt/PinChangeLib/PinChangeLib.ino +++ b/examples/PinChangeInt/PinChangeLib/PinChangeLib.ino @@ -53,6 +53,8 @@ ServoInputPin servo2; void setup() { Serial.begin(115200); + servo1.attach(); // attaches the first servo input interrupt + servo2.attach(); // attaches the second servo input interrupt // wait for all servo signals to be read for the first time while (!ServoInput.available()) { diff --git a/examples/RC_Receiver/RC_Receiver.ino b/examples/RC_Receiver/RC_Receiver.ino index fb8cc95..f74202b 100644 --- a/examples/RC_Receiver/RC_Receiver.ino +++ b/examples/RC_Receiver/RC_Receiver.ino @@ -53,6 +53,8 @@ ServoInputPin throttle(ThrottlePulseMin, ThrottlePulseMax); void setup() { Serial.begin(115200); + steering.attach(); // attaches the steering servo input interrupt + throttle.attach(); // attaches the throttle servo input interrupt while (!ServoInput.available()) { // wait for all signals to be ready Serial.println("Waiting for servo signals..."); diff --git a/examples/Remapping/Remapping.ino b/examples/Remapping/Remapping.ino index 5a9f68e..9cfc0e2 100644 --- a/examples/Remapping/Remapping.ino +++ b/examples/Remapping/Remapping.ino @@ -38,6 +38,7 @@ ServoInputPin<2> servo; void setup() { Serial.begin(115200); + servo.attach(); // attaches the servo input interrupt while (servo.available() == false) { Serial.println("Waiting for servo signal..."); diff --git a/src/ServoInput.h b/src/ServoInput.h index d1cb448..5d15304 100644 --- a/src/ServoInput.h +++ b/src/ServoInput.h @@ -96,7 +96,6 @@ class ServoInputPin : public ServoInputSignal { ServoInputPin::PortRegister = SERVOINPUT_PIN_TO_BASEREG(Pin); #endif pinMode(Pin, INPUT_PULLUP); - attach(); } ServoInputPin(uint16_t pMin, uint16_t pMax) : ServoInputPin() {