Skip to content

Commit 00c760b

Browse files
committed
Merge branch 'feature/adding_different_attach_methods' into develop
2 parents 1e0726d + ed58683 commit 00c760b

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

src/Servo_Hardware_PWM.cpp

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@ Servo::Servo()
2929

3030
uint8_t Servo::attach(int pin)
3131
{
32+
return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH, DEFAULT_PULSE_WIDTH);
33+
}
34+
35+
uint8_t Servo::attach(int pin, int min, int max)
36+
{
37+
return this->attach(pin, min, max, DEFAULT_PULSE_WIDTH);
38+
}
39+
40+
uint8_t Servo::attach(int pin, int min, int max, int defaultPos)
41+
{
42+
this->min = min;
43+
this->max = max;
44+
this->defaultPos = defaultPos;
45+
3246
if (this->servoIndex < MAX_SERVOS) {
3347
if (pin == 2) {
3448
if(pinActive[1] == BOOL_FALSE){
@@ -50,7 +64,7 @@ uint8_t Servo::attach(int pin)
5064
//setting the output to non inverted:
5165
TCCR3A |= (1 << COM3B1);
5266

53-
OCR3B = DEFAULT_PULSE_WIDTH; //setting the pulse width
67+
OCR3B = this->defaultPos; //setting the pulse width
5468

5569
//OC3B, Port E, Bit 4; setting pin 2 as output:
5670
DDRE |= (1 << PE4); //bit 4 (pin 2) as output
@@ -78,7 +92,7 @@ uint8_t Servo::attach(int pin)
7892
//setting the output to non inverted:
7993
TCCR3A |= (1 << COM3C1);
8094

81-
OCR3C = DEFAULT_PULSE_WIDTH; //setting the pulse width
95+
OCR3C = this->defaultPos; //setting the pulse width
8296

8397
//OC3C, Port E, Bit 5; setting pin 3 as output:
8498
DDRE |= (1 << PE5); //bit 5 (pin 3) as output
@@ -106,7 +120,7 @@ uint8_t Servo::attach(int pin)
106120
//setting the output to non inverted:
107121
TCCR4A |= (1 << COM4B1);
108122

109-
OCR4B = DEFAULT_PULSE_WIDTH; //setting the pulse width
123+
OCR4B = this->defaultPos; //setting the pulse width
110124

111125
//OC4B, Port H, Bit 4; setting pin 7 as output:
112126
DDRH |= (1 << PH4); //bit 4 (pin 7) as output
@@ -134,7 +148,7 @@ uint8_t Servo::attach(int pin)
134148
//setting the output to non inverted:
135149
TCCR4A |= (1 << COM4C1);
136150

137-
OCR4C = DEFAULT_PULSE_WIDTH; //setting the pulse width
151+
OCR4C = this->defaultPos; //setting the pulse width
138152

139153
//OC4C, Port H, Bit 5; setting pin 8 as output:
140154
DDRH |= (1 << PH5); //bit 5 (pin 8) as output
@@ -162,7 +176,7 @@ uint8_t Servo::attach(int pin)
162176
//setting the output to non inverted:
163177
TCCR5A |= (1 << COM5C1);
164178

165-
OCR5C = DEFAULT_PULSE_WIDTH; //setting the pulse width
179+
OCR5C = this->defaultPos; //setting the pulse width
166180

167181
//OC5C, Port L, Bit 5; setting pin 44 as output:
168182
DDRL |= (1 << PL5); //bit 5 (pin 44) as output
@@ -190,7 +204,7 @@ uint8_t Servo::attach(int pin)
190204
//setting the output to non inverted:
191205
TCCR5A |= (1 << COM5B1);
192206

193-
OCR5B = DEFAULT_PULSE_WIDTH; //setting the pulse width
207+
OCR5B = this->defaultPos; //setting the pulse width
194208

195209
//OC5B, Port L, Bit 4; setting pin 45 as output:
196210
DDRL |= (1 << PL4); //bit 4 (pin 45) as output
@@ -370,19 +384,19 @@ void Servo::write(int value)
370384
{
371385
value = 180;
372386
}
373-
value = (((MAX_PULSE_WIDTH - MIN_PULSE_WIDTH) * value) / 180) + MIN_PULSE_WIDTH;
387+
value = (((this->max - this->min) * value) / 180) + this->min;
374388

375389
this->writeMicroseconds(value);
376390
}
377391

378392
void Servo::writeMicroseconds(int value)
379393
{
380394
if ((this->servoIndex < MAX_SERVOS)) {
381-
if (value < MIN_PULSE_WIDTH) {
382-
value = MIN_PULSE_WIDTH;
395+
if (value < this->min) {
396+
value = this->min;
383397
}
384-
else if (value > MAX_PULSE_WIDTH) {
385-
value = MAX_PULSE_WIDTH;
398+
else if (value > this->max) {
399+
value = this->max;
386400
}
387401
if (this->servoPin == 2 && pinActive[0] == BOOL_TRUE) {
388402
OCR3B = 0x0;

src/Servo_Hardware_PWM.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,19 @@ class Servo
6969
{
7070
public:
7171
Servo();
72-
uint8_t attach(int pin); //attach the given pin; returns servoIndex number or 255 if too many servos
73-
void detach(); //detach the used pin
74-
void detachAll(); //automatically detaches all used pins
75-
void write(int value); //write angle in degrees
76-
void writeMicroseconds(int value); //write pulse width in microseconds
72+
uint8_t attach(int pin); //attach the given pin; returns servoIndex number or 255 if too many servos
73+
uint8_t attach(int pin, int min, int max); //attach the given pin and set the upper and lower pulse width limits; returns servoIndex number or 255 if too many servos
74+
uint8_t attach(int pin, int min, int max, int defaultPos); //attach the given pin, set the upper and lower pulse width limits and set the pulse width when servo is attached; returns servoIndex number or 255 if too many servos
75+
void detach(); //detach the used pin
76+
void detachAll(); //automatically detaches all used pins
77+
void write(int value); //write angle in degrees
78+
void writeMicroseconds(int value); //write pulse width in microseconds
7779
private:
78-
uint8_t servoIndex; //number of attached Servos
79-
uint8_t servoPin; //pin number of the attached Servo
80+
uint8_t servoIndex; //number of attached Servos
81+
uint8_t servoPin; //pin number of the attached Servo
82+
uint16_t min; //lower pulse width limit
83+
uint16_t max; //upper pulse width limit
84+
uint16_t defaultPos; //pulse width when servo is attached
8085
};
8186

8287
#endif

0 commit comments

Comments
 (0)