Skip to content

Commit 2e87933

Browse files
Support angle and microseconds for nrf52, renesasm stn32f4
1 parent 1f25386 commit 2e87933

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

src/nrf52/Servo.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,13 @@ void Servo::detach()
7474

7575
void Servo::write(int value)
7676
{
77-
if (value < 0)
78-
value = 0;
79-
else if (value > 180)
80-
value = 180;
81-
value = map(value, 0, 180, MIN_PULSE, MAX_PULSE);
82-
77+
if(value < MIN_PULSE_WIDTH) {
78+
if (value < 0)
79+
value = 0;
80+
else if (value > 180)
81+
value = 180;
82+
value = map(value, 0, 180, MIN_PULSE, MAX_PULSE);
83+
}
8384
writeMicroseconds(value);
8485
}
8586

src/renesas/Servo.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,15 @@ void Servo::detach()
232232
}
233233
}
234234

235-
void Servo::write(int angle)
235+
void Servo::write(int value)
236236
{
237237
if (servoIndex != SERVO_INVALID_INDEX) {
238238
ra_servo_t *servo = &ra_servos[servoIndex];
239-
angle = constrain(angle, 0, 180);
240-
writeMicroseconds(map(angle, 0, 180, servo->period_min, servo->period_max));
239+
if(value < MIN_PULSE_WIDTH) {
240+
value = constrain(value, 0, 180);
241+
value = map(value, 0, 180, servo->period_min, servo->period_max);
242+
}
243+
this->writeMicroseconds(value);
241244
}
242245
}
243246

src/stm32f4/Servo.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,12 @@ bool Servo::detach() {
148148
return true;
149149
}
150150

151-
void Servo::write(int degrees) {
152-
degrees = constrain(degrees, this->minAngle, this->maxAngle);
153-
this->writeMicroseconds(ANGLE_TO_US(degrees));
151+
void Servo::write(int value) {
152+
if(value < MIN_PULSE_WIDTH) {
153+
value = constrain(value, this->minAngle, this->maxAngle);
154+
value = ANGLE_TO_US(value);
155+
}
156+
this->writeMicroseconds(value);
154157
}
155158

156159
int Servo::read() const {

0 commit comments

Comments
 (0)