Skip to content

Commit 0cf5751

Browse files
ABOSTMfpistm
authored andcommitted
servo: optional default servo angle on attach
Allow user to set servo default angle on attach. Signed-off-by: Alexandre Bourdiol <alexandre.bourdiol@st.com>
1 parent 4aa0163 commit 0cf5751

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

libraries/Servo/src/Servo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ typedef struct {
102102
class Servo {
103103
public:
104104
Servo();
105-
uint8_t attach(int pin); // attach the given pin to the next free channel, sets pinMode, returns channel number or 0 if failure
106-
uint8_t attach(int pin, int min, int max); // as above but also sets min and max values for writes.
105+
uint8_t attach(int pin, int value = DEFAULT_PULSE_WIDTH); // attach the given pin to the next free channel, sets pinMode, set angle value, returns channel number or 0 if failure
106+
uint8_t attach(int pin, int min, int max, int value = DEFAULT_PULSE_WIDTH); // as above but also sets min and max values for writes.
107107
void detach();
108108
void write(int value); // if value is < 200 its treated as an angle, otherwise as pulse width in microseconds
109109
void writeMicroseconds(int value); // Write pulse width in microseconds

libraries/Servo/src/stm32/Servo.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,17 @@ Servo::Servo()
111111
}
112112
}
113113

114-
uint8_t Servo::attach(int pin)
114+
uint8_t Servo::attach(int pin, int value)
115115
{
116-
return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
116+
return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH, value);
117117
}
118118

119-
uint8_t Servo::attach(int pin, int min, int max)
119+
uint8_t Servo::attach(int pin, int min, int max, int value)
120120
{
121121
if (this->servoIndex < MAX_SERVOS) {
122122
pinMode(pin, OUTPUT); // set servo pin to output
123123
servos[this->servoIndex].Pin.nbr = pin;
124-
servos[this->servoIndex].ticks = DEFAULT_PULSE_WIDTH;
124+
write(value);
125125
// todo min/max check: abs(min - MIN_PULSE_WIDTH) /4 < 128
126126
this->min = (MIN_PULSE_WIDTH - min) / 4; //resolution of min/max is 4 uS
127127
this->max = (MAX_PULSE_WIDTH - max) / 4;

0 commit comments

Comments
 (0)