diff --git a/Marlin/servo.cpp b/Marlin/servo.cpp index 32e5d84937..6e3f398ce1 100644 --- a/Marlin/servo.cpp +++ b/Marlin/servo.cpp @@ -35,12 +35,14 @@ write() - Sets the servo angle in degrees. (invalid angle that is valid as pulse in microseconds is treated as microseconds) writeMicroseconds() - Sets the servo pulse width in microseconds + move(pin, angel) - Sequence of attach(pin), write(angel), + if DEACTIVATE_SERVOS_AFTER_MOVE is defined waits SERVO_DEACTIVATION_DELAY, than detaches. read() - Gets the last written servo pulse width as an angle between 0 and 180. readMicroseconds() - Gets the last written servo pulse width in microseconds. (was read_us() in first release) attached() - Returns true if there is a servo attached. detach() - Stops an attached servos from pulsing its i/o pin. -*/ + */ #include "Configuration.h" #ifdef NUM_SERVOS @@ -301,4 +303,17 @@ int Servo::readMicroseconds() { bool Servo::attached() { return servos[this->servoIndex].Pin.isActive; } +uint8_t Servo::move(int pin, int value) { + uint8_t ret; + ret = this->attach(pin); + if (ret) { + this->write(value); + #ifdef DEACTIVATE_SERVOS_AFTER_MOVE && (SERVO_DEACTIVATION_DELAY > 0) + delay(SERVO_DEACTIVATION_DELAY); + this->detach(); + #endif + } + return ret; +} + #endif diff --git a/Marlin/servo.h b/Marlin/servo.h index 4647561754..4c58991c73 100644 --- a/Marlin/servo.h +++ b/Marlin/servo.h @@ -40,6 +40,8 @@ readMicroseconds() - Gets the last written servo pulse width in microseconds. (was read_us() in first release) attached() - Returns true if there is a servo attached. detach() - Stops an attached servos from pulsing its i/o pin. + move(pin, angel) - Sequence of attach(pin), write(angel), + if DEACTIVATE_SERVOS_AFTER_MOVE is defined waits SERVO_DEACTIVATION_DELAY, than detaches. */ #ifndef servo_h @@ -120,6 +122,9 @@ class Servo { void detach(); void write(int value); // if value is < 200 it is treated as an angle, otherwise as pulse width in microseconds void writeMicroseconds(int value); // Write pulse width in microseconds + uint8_t move(int pin, int value); // attach the given pin to the next free channel, sets pinMode, returns channel number or 0 if failure. + // if value is < 200 it is treated as an angle, otherwise as pulse width in microseconds. + // if DEACTIVATE_SERVOS_AFTER_MOVE is defined waits SERVO_DEACTIVATION_DELAY, than detaches. int read(); // returns current pulse width as an angle between 0 and 180 degrees int readMicroseconds(); // returns current pulse width in microseconds for this servo (was read_us() in first release) bool attached(); // return true if this servo is attached, otherwise false