Browse Source

Apply const, safe_delay in servo.*

Scott Lahteine 6 years ago
parent
commit
e297748b22
2 changed files with 7 additions and 11 deletions
  1. 4
    8
      Marlin/src/HAL/servo.cpp
  2. 3
    3
      Marlin/src/HAL/servo.h

+ 4
- 8
Marlin/src/HAL/servo.cpp View File

54
 
54
 
55
 #include "../inc/MarlinConfig.h"
55
 #include "../inc/MarlinConfig.h"
56
 
56
 
57
-#include "HAL.h"
58
-
59
 #if HAS_SERVOS && !(IS_32BIT_TEENSY || defined(TARGET_LPC1768))
57
 #if HAS_SERVOS && !(IS_32BIT_TEENSY || defined(TARGET_LPC1768))
60
 
58
 
61
 //#include <Arduino.h>
59
 //#include <Arduino.h>
62
-
63
 #include "servo.h"
60
 #include "servo.h"
64
 #include "servo_private.h"
61
 #include "servo_private.h"
65
 
62
 
66
 ServoInfo_t servo_info[MAX_SERVOS];                  // static array of servo info structures
63
 ServoInfo_t servo_info[MAX_SERVOS];                  // static array of servo info structures
67
 uint8_t ServoCount = 0;                              // the total number of attached servos
64
 uint8_t ServoCount = 0;                              // the total number of attached servos
68
 
65
 
69
-
70
 #define SERVO_MIN() (MIN_PULSE_WIDTH - this->min * 4)  // minimum value in uS for this servo
66
 #define SERVO_MIN() (MIN_PULSE_WIDTH - this->min * 4)  // minimum value in uS for this servo
71
 #define SERVO_MAX() (MAX_PULSE_WIDTH - this->max * 4)  // maximum value in uS for this servo
67
 #define SERVO_MAX() (MAX_PULSE_WIDTH - this->max * 4)  // maximum value in uS for this servo
72
 
68
 
92
     this->servoIndex = INVALID_SERVO;  // too many servos
88
     this->servoIndex = INVALID_SERVO;  // too many servos
93
 }
89
 }
94
 
90
 
95
-int8_t Servo::attach(int pin) {
91
+int8_t Servo::attach(const int pin) {
96
   return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
92
   return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
97
 }
93
 }
98
 
94
 
99
-int8_t Servo::attach(int pin, int min, int max) {
95
+int8_t Servo::attach(const int pin, const int min, const int max) {
100
 
96
 
101
   if (this->servoIndex >= MAX_SERVOS) return -1;
97
   if (this->servoIndex >= MAX_SERVOS) return -1;
102
 
98
 
151
 
147
 
152
 bool Servo::attached() { return servo_info[this->servoIndex].Pin.isActive; }
148
 bool Servo::attached() { return servo_info[this->servoIndex].Pin.isActive; }
153
 
149
 
154
-void Servo::move(int value) {
150
+void Servo::move(const int value) {
155
   constexpr uint16_t servo_delay[] = SERVO_DELAY;
151
   constexpr uint16_t servo_delay[] = SERVO_DELAY;
156
   static_assert(COUNT(servo_delay) == NUM_SERVOS, "SERVO_DELAY must be an array NUM_SERVOS long.");
152
   static_assert(COUNT(servo_delay) == NUM_SERVOS, "SERVO_DELAY must be an array NUM_SERVOS long.");
157
   if (this->attach(0) >= 0) {
153
   if (this->attach(0) >= 0) {
158
     this->write(value);
154
     this->write(value);
159
-    delay(servo_delay[this->servoIndex]);
155
+    safe_delay(servo_delay[this->servoIndex]);
160
     #if ENABLED(DEACTIVATE_SERVOS_AFTER_MOVE)
156
     #if ENABLED(DEACTIVATE_SERVOS_AFTER_MOVE)
161
       this->detach();
157
       this->detach();
162
     #endif
158
     #endif

+ 3
- 3
Marlin/src/HAL/servo.h View File

89
   class Servo {
89
   class Servo {
90
     public:
90
     public:
91
       Servo();
91
       Servo();
92
-      int8_t attach(int pin);            // attach the given pin to the next free channel, set pinMode, return channel number (-1 on fail)
93
-      int8_t attach(int pin, int min, int max); // as above but also sets min and max values for writes.
92
+      int8_t attach(const int pin);      // attach the given pin to the next free channel, set pinMode, return channel number (-1 on fail)
93
+      int8_t attach(const int pin, const int min, const int max); // as above but also sets min and max values for writes.
94
       void detach();
94
       void detach();
95
       void write(int value);             // if value is < 200 it is treated as an angle, otherwise as pulse width in microseconds
95
       void write(int value);             // if value is < 200 it is treated as an angle, otherwise as pulse width in microseconds
96
       void writeMicroseconds(int value); // write pulse width in microseconds
96
       void writeMicroseconds(int value); // write pulse width in microseconds
97
-      void move(int value);              // attach the servo, then move to value
97
+      void move(const int value);        // attach the servo, then move to value
98
                                          // if value is < 200 it is treated as an angle, otherwise as pulse width in microseconds
98
                                          // if value is < 200 it is treated as an angle, otherwise as pulse width in microseconds
99
                                          // if DEACTIVATE_SERVOS_AFTER_MOVE wait SERVO_DELAY, then detach
99
                                          // if DEACTIVATE_SERVOS_AFTER_MOVE wait SERVO_DELAY, then detach
100
       int read();                        // returns current pulse width as an angle between 0 and 180 degrees
100
       int read();                        // returns current pulse width as an angle between 0 and 180 degrees

Loading…
Cancel
Save