From 9b9b62b2188672469894e171eb0996013be44bb0 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <github@thinkyhead.com>
Date: Sun, 27 May 2018 23:45:01 -0500
Subject: [PATCH] delay(SERVO_DELAY) => safe_delay(servo_delay[servo_index])

---
 .travis.yml                                         |  1 +
 Marlin/src/HAL/HAL_AVR/servo_AVR.cpp                |  4 ++--
 Marlin/src/HAL/HAL_LPC1768/LPC1768_Servo.cpp        |  6 +++---
 Marlin/src/HAL/HAL_STM32F1/HAL_Servo_Stm32f1.cpp    |  4 +++-
 Marlin/src/HAL/HAL_STM32F7/HAL_Servo_STM32F7.cpp    |  4 +++-
 Marlin/src/HAL/HAL_TEENSY35_36/HAL_Servo_Teensy.cpp | 11 +++++++++--
 Marlin/src/HAL/servo.cpp                            |  5 ++---
 7 files changed, 23 insertions(+), 12 deletions(-)

diff --git a/.travis.yml b/.travis.yml
index 7e1c72d483..21fe932022 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -392,6 +392,7 @@ script:
   #
   - restore_configs
   - opt_enable NUM_SERVOS Z_PROBE_SERVO_NR Z_SERVO_ANGLES DEACTIVATE_SERVOS_AFTER_MOVE
+  - opt_set NUM_SERVOS 1
   - opt_enable AUTO_BED_LEVELING_3POINT DEBUG_LEVELING_FEATURE EEPROM_SETTINGS
   - build_marlin_pio ${TRAVIS_BUILD_DIR} ${TEST_PLATFORM}
   #
diff --git a/Marlin/src/HAL/HAL_AVR/servo_AVR.cpp b/Marlin/src/HAL/HAL_AVR/servo_AVR.cpp
index 0da95d040e..c02dd4c203 100644
--- a/Marlin/src/HAL/HAL_AVR/servo_AVR.cpp
+++ b/Marlin/src/HAL/HAL_AVR/servo_AVR.cpp
@@ -42,8 +42,8 @@
  *
  * write()               - Set the servo angle in degrees. (Invalid angles —over MIN_PULSE_WIDTH— are treated as µs.)
  * writeMicroseconds()   - Set the servo pulse width in microseconds.
- * move(pin, angle)      - Sequence of attach(pin), write(angle), delay(SERVO_DELAY).
- *                         With DEACTIVATE_SERVOS_AFTER_MOVE it detaches after SERVO_DELAY.
+ * move(pin, angle)      - Sequence of attach(pin), write(angle), safe_delay(servo_delay[servoIndex]).
+ *                         With DEACTIVATE_SERVOS_AFTER_MOVE it detaches after servo_delay[servoIndex].
  * read()                - Get the last-written servo pulse width as an angle between 0 and 180.
  * readMicroseconds()    - Get the last-written servo pulse width in microseconds.
  * attached()            - Return true if a servo is attached.
diff --git a/Marlin/src/HAL/HAL_LPC1768/LPC1768_Servo.cpp b/Marlin/src/HAL/HAL_LPC1768/LPC1768_Servo.cpp
index 317433191c..283cd52547 100644
--- a/Marlin/src/HAL/HAL_LPC1768/LPC1768_Servo.cpp
+++ b/Marlin/src/HAL/HAL_LPC1768/LPC1768_Servo.cpp
@@ -42,8 +42,8 @@
  *
  * write()               - Set the servo angle in degrees. (Invalid angles —over MIN_PULSE_WIDTH— are treated as µs.)
  * writeMicroseconds()   - Set the servo pulse width in microseconds.
- * move(pin, angle)      - Sequence of attach(pin), write(angle), delay(SERVO_DELAY).
- *                         With DEACTIVATE_SERVOS_AFTER_MOVE it detaches after SERVO_DELAY.
+ * move(pin, angle)      - Sequence of attach(pin), write(angle), safe_delay(servo_delay[servoIndex]).
+ *                         With DEACTIVATE_SERVOS_AFTER_MOVE it detaches after servo_delay[servoIndex].
  * read()                - Get the last-written servo pulse width as an angle between 0 and 180.
  * readMicroseconds()    - Get the last-written servo pulse width in microseconds.
  * attached()            - Return true if a servo is attached.
@@ -148,7 +148,7 @@
     static_assert(COUNT(servo_delay) == NUM_SERVOS, "SERVO_DELAY must be an array NUM_SERVOS long.");
     if (this->attach(0) >= 0) {    // notice the pin number is zero here
       this->write(value);
-      delay(servo_delay[this->servoIndex]);
+      safe_delay(servo_delay[this->servoIndex]);
       #if ENABLED(DEACTIVATE_SERVOS_AFTER_MOVE)
         this->detach();
         LPC1768_PWM_detach_pin(servo_info[this->servoIndex].Pin.nbr);  // shut down the PWM signal
diff --git a/Marlin/src/HAL/HAL_STM32F1/HAL_Servo_Stm32f1.cpp b/Marlin/src/HAL/HAL_STM32F1/HAL_Servo_Stm32f1.cpp
index 2dea5131b8..fe293d50d4 100644
--- a/Marlin/src/HAL/HAL_STM32F1/HAL_Servo_Stm32f1.cpp
+++ b/Marlin/src/HAL/HAL_STM32F1/HAL_Servo_Stm32f1.cpp
@@ -39,9 +39,11 @@ int8_t libServo::attach(const int pin, const int min, const int max) {
 }
 
 void libServo::move(const int value) {
+  constexpr uint16_t servo_delay[] = SERVO_DELAY;
+  static_assert(COUNT(servo_delay) == NUM_SERVOS, "SERVO_DELAY must be an array NUM_SERVOS long.");
   if (this->attach(0) >= 0) {
     this->write(value);
-    delay(SERVO_DELAY);
+    safe_delay(servo_delay[this->servoIndex]);
     #if ENABLED(DEACTIVATE_SERVOS_AFTER_MOVE)
       this->detach();
     #endif
diff --git a/Marlin/src/HAL/HAL_STM32F7/HAL_Servo_STM32F7.cpp b/Marlin/src/HAL/HAL_STM32F7/HAL_Servo_STM32F7.cpp
index 87ebde5be9..43d95e9059 100644
--- a/Marlin/src/HAL/HAL_STM32F7/HAL_Servo_STM32F7.cpp
+++ b/Marlin/src/HAL/HAL_STM32F7/HAL_Servo_STM32F7.cpp
@@ -39,9 +39,11 @@ int8_t libServo::attach(const int pin, const int min, const int max) {
 }
 
 void libServo::move(const int value) {
+  constexpr uint16_t servo_delay[] = SERVO_DELAY;
+  static_assert(COUNT(servo_delay) == NUM_SERVOS, "SERVO_DELAY must be an array NUM_SERVOS long.");
   if (this->attach(0) >= 0) {
     this->write(value);
-    delay(SERVO_DELAY);
+    safe_delay(servo_delay[this->servoIndex]);
     #if ENABLED(DEACTIVATE_SERVOS_AFTER_MOVE)
       this->detach();
     #endif
diff --git a/Marlin/src/HAL/HAL_TEENSY35_36/HAL_Servo_Teensy.cpp b/Marlin/src/HAL/HAL_TEENSY35_36/HAL_Servo_Teensy.cpp
index f8905fc1e3..6a5e40127c 100644
--- a/Marlin/src/HAL/HAL_TEENSY35_36/HAL_Servo_Teensy.cpp
+++ b/Marlin/src/HAL/HAL_TEENSY35_36/HAL_Servo_Teensy.cpp
@@ -1,8 +1,11 @@
 #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
 
-#include "HAL_Servo_Teensy.h"
 #include "../../inc/MarlinConfig.h"
 
+#if HAS_SERVOS
+
+#include "HAL_Servo_Teensy.h"
+
 int8_t libServo::attach(const int pin) {
   if (this->servoIndex >= MAX_SERVOS) return -1;
   return Servo::attach(pin);
@@ -13,13 +16,17 @@ int8_t libServo::attach(const int pin, const int min, const int max) {
 }
 
 void libServo::move(const int value) {
+  constexpr uint16_t servo_delay[] = SERVO_DELAY;
+  static_assert(COUNT(servo_delay) == NUM_SERVOS, "SERVO_DELAY must be an array NUM_SERVOS long.");
   if (this->attach(0) >= 0) {
     this->write(value);
-    delay(SERVO_DELAY);
+    safe_delay(servo_delay[this->servoIndex]);
     #if ENABLED(DEACTIVATE_SERVOS_AFTER_MOVE)
       this->detach();
     #endif
   }
 }
 
+#endif // HAS_SERVOS
+
 #endif // __MK64FX512__ || __MK66FX1M0__
diff --git a/Marlin/src/HAL/servo.cpp b/Marlin/src/HAL/servo.cpp
index 4ff2779153..1c3c003d82 100644
--- a/Marlin/src/HAL/servo.cpp
+++ b/Marlin/src/HAL/servo.cpp
@@ -42,8 +42,8 @@
  *
  * write()               - Set the servo angle in degrees. (Invalid angles —over MIN_PULSE_WIDTH— are treated as µs.)
  * writeMicroseconds()   - Set the servo pulse width in microseconds.
- * move(pin, angle)      - Sequence of attach(pin), write(angle), delay(SERVO_DELAY).
- *                         With DEACTIVATE_SERVOS_AFTER_MOVE it detaches after SERVO_DELAY.
+ * move(pin, angle)      - Sequence of attach(pin), write(angle), safe_delay(servo_delay[servoIndex]).
+ *                         With DEACTIVATE_SERVOS_AFTER_MOVE it detaches after servo_delay[servoIndex].
  * read()                - Get the last-written servo pulse width as an angle between 0 and 180.
  * readMicroseconds()    - Get the last-written servo pulse width in microseconds.
  * attached()            - Return true if a servo is attached.
@@ -160,4 +160,3 @@ void Servo::move(const int value) {
 }
 
 #endif // HAS_SERVOS
-
-- 
GitLab