Skip to content

Commit 019b1c2

Browse files
committed
servo clean up
1 parent 947d8bb commit 019b1c2

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

cores/nRF5/HardwarePWM.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ void HardwarePWM::stop(void)
224224
_pwm->ENABLE = 0;
225225
}
226226

227-
bool HardwarePWM::writeChannel(uint8_t ch, uint16_t value, bool inverted )
227+
bool HardwarePWM::writeChannel(uint8_t ch, uint16_t value, bool inverted)
228228
{
229229
VERIFY( ch < MAX_CHANNELS );
230230

libraries/Servo/src/Servo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ class Servo
111111
bool attached(); // return true if this servo is attached, otherwise false
112112
private:
113113
uint8_t servoIndex; // index into the channel data for this servo
114-
int16_t min; // minimum pulse in µs
115-
int16_t max; // maximum pulse in µs
114+
int16_t min; // minimum pulse in µs
115+
int16_t max; // maximum pulse in µs
116116

117117
HardwarePWM* pwm;
118118
};

libraries/Servo/src/nrf52/Servo.cpp

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,53 +47,58 @@ uint8_t Servo::attach(int pin, int min, int max)
4747
return INVALID_SERVO;
4848
}
4949
bool succeeded = false;
50-
pinMode(pin, OUTPUT); // set servo pin to output
51-
servos[this->servoIndex].Pin.nbr = pin;
5250

53-
if (min < MIN_PULSE_WIDTH) {
54-
min = MIN_PULSE_WIDTH;
55-
}
56-
if (max > MAX_PULSE_WIDTH) {
57-
max = MAX_PULSE_WIDTH;
58-
}
51+
if (min < MIN_PULSE_WIDTH) min = MIN_PULSE_WIDTH;
52+
if (max > MAX_PULSE_WIDTH) max = MAX_PULSE_WIDTH;
5953

6054
//fix min if conversion to pulse cycle value is too low
6155
if ( (min/DUTY_CYCLE_RESOLUTION) * DUTY_CYCLE_RESOLUTION < min) {
6256
min += DUTY_CYCLE_RESOLUTION;
6357
}
6458

65-
this->min = min;
66-
this->max = max;
67-
68-
servos[this->servoIndex].Pin.isActive = true;
59+
this->min = min;
60+
this->max = max;
6961

7062
// Adafruit, add pin to 1 of available Hw PWM
7163
// first, use existing HWPWM modules (already owned by Servo)
72-
for(int i=0; i<HWPWM_MODULE_NUM; i++) {
73-
if (HwPWMx[i]->isOwner(_servoToken) &&
74-
HwPWMx[i]->addPin(pin)) {
64+
for ( int i = 0; i < HWPWM_MODULE_NUM; i++ )
65+
{
66+
if ( HwPWMx[i]->isOwner(_servoToken) && HwPWMx[i]->addPin(pin) )
67+
{
7568
this->pwm = HwPWMx[i];
7669
succeeded = true;
7770
break;
7871
}
7972
}
73+
8074
// if could not add to existing owned PWM modules, try to add to a new PWM module
81-
if (!succeeded) {
82-
for(int i=0; i<HWPWM_MODULE_NUM; i++) {
83-
if (HwPWMx[i]->takeOwnership(_servoToken) &&
84-
HwPWMx[i]->addPin(pin)) {
75+
if ( !succeeded )
76+
{
77+
for ( int i = 0; i < HWPWM_MODULE_NUM; i++ )
78+
{
79+
if ( HwPWMx[i]->takeOwnership(_servoToken) && HwPWMx[i]->addPin(pin) )
80+
{
8581
this->pwm = HwPWMx[i];
8682
succeeded = true;
8783
break;
8884
}
8985
}
9086
}
9187

92-
if (succeeded) { // do not use this->pwm unless success above!
88+
if ( succeeded )
89+
{
90+
pinMode(pin, OUTPUT);
91+
servos[this->servoIndex].Pin.nbr = pin;
92+
servos[this->servoIndex].Pin.isActive = true;
93+
9394
this->pwm->setMaxValue(MAXVALUE);
9495
this->pwm->setClockDiv(CLOCKDIV);
96+
97+
return this->servoIndex;
98+
}else
99+
{
100+
return INVALID_SERVO;
95101
}
96-
return succeeded ? this->servoIndex : INVALID_SERVO; // return INVALID_SERVO on failure (zero is a valid servo index)
97102
}
98103

99104
void Servo::detach()
@@ -104,6 +109,7 @@ void Servo::detach()
104109

105110
uint8_t const pin = servos[this->servoIndex].Pin.nbr;
106111
servos[this->servoIndex].Pin.isActive = false;
112+
107113
// remove pin from HW PWM
108114
HardwarePWM * pwm = this->pwm;
109115
this->pwm = nullptr;

0 commit comments

Comments
 (0)