Skip to content

Commit bab7b25

Browse files
committed
Don't start and stop timer every write()
1 parent b20bc83 commit bab7b25

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/mbed/Servo.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class ServoImpl {
77
mbed::DigitalOut *pin;
88
mbed::Timeout timeout; // calls a callback once when a timeout expires
99
mbed::Ticker ticker; // calls a callback repeatedly with a timeout
10-
uint32_t duration;
1110

1211
public:
1312
ServoImpl(PinName _pin) {
@@ -34,9 +33,7 @@ class ServoImpl {
3433
*pin = !*pin;
3534
}
3635

37-
uint32_t read() {
38-
return duration;
39-
}
36+
int32_t duration = -1;
4037
};
4138

4239
static ServoImpl* servos[MAX_SERVOS]; // static array of servo structures
@@ -107,7 +104,10 @@ void Servo::writeMicroseconds(int value)
107104
value = SERVO_MAX();
108105

109106
value = value - TRIM_DURATION;
110-
servos[this->servoIndex]->start(value);
107+
if (servos[this->servoIndex]->duration == -1) {
108+
servos[this->servoIndex]->start(value);
109+
}
110+
servos[this->servoIndex]->duration = value;
111111
}
112112
}
113113

@@ -121,7 +121,7 @@ int Servo::readMicroseconds()
121121
if (!servos[this->servoIndex]) {
122122
return 0;
123123
}
124-
return servos[this->servoIndex]->read();
124+
return servos[this->servoIndex]->duration;
125125
}
126126

127127
bool Servo::attached()

0 commit comments

Comments
 (0)