@@ -13,12 +13,12 @@ For information about the library, license and author, see Servo_Hardware_PWM.h
1313static uint8_t servoCount = 0 ;
1414static uint8_t pinActive[MAX_SERVOS] = {BOOL_FALSE};
1515
16- Servo::Servo ()
17- {
16+ Servo::Servo () {
1817 this ->servoIndex = 0 ;
1918 this ->servoPin = 0 ;
2019
21- if (servoCount < MAX_SERVOS) {
20+ if (servoCount < MAX_SERVOS)
21+ {
2222 this ->servoIndex = servoCount++; // assign a servoIndex to this instance
2323 }
2424 else
@@ -27,25 +27,25 @@ Servo::Servo()
2727 }
2828}
2929
30- uint8_t Servo::attach (int pin)
31- {
30+ uint8_t Servo::attach (int pin) {
3231 return this ->attach (pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH, DEFAULT_PULSE_WIDTH);
3332}
3433
35- uint8_t Servo::attach (int pin, int min, int max)
36- {
34+ uint8_t Servo::attach (int pin, int min, int max) {
3735 return this ->attach (pin, min, max, DEFAULT_PULSE_WIDTH);
3836}
3937
40- uint8_t Servo::attach (int pin, int min, int max, int defaultPos)
41- {
38+ uint8_t Servo::attach (int pin, int min, int max, int defaultPos) {
4239 this ->min = min;
4340 this ->max = max;
4441 this ->defaultPos = defaultPos;
4542
46- if (this ->servoIndex < MAX_SERVOS) {
47- if (pin == 2 ) {
48- if (pinActive[1 ] == BOOL_FALSE){
43+ if (this ->servoIndex < MAX_SERVOS)
44+ {
45+ if (pin == 2 )
46+ {
47+ if (pinActive[1 ] == BOOL_FALSE)
48+ {
4949 // resetting the control register A and B:
5050 TCCR3A = 0x0 ;
5151 TCCR3B = 0x0 ;
@@ -68,12 +68,14 @@ uint8_t Servo::attach(int pin, int min, int max, int defaultPos)
6868
6969 // OC3B, Port E, Bit 4; setting pin 2 as output:
7070 DDRE |= (1 << PE4); // bit 4 (pin 2) as output
71-
71+
7272 pinActive[0 ] = BOOL_TRUE;
7373 this ->servoPin = 2 ;
7474 }
75- else if (pin == 3 ) {
76- if (pinActive[0 ] == BOOL_FALSE){
75+ else if (pin == 3 )
76+ {
77+ if (pinActive[0 ] == BOOL_FALSE)
78+ {
7779 // resetting the control register A and B:
7880 TCCR3A = 0x0 ;
7981 TCCR3B = 0x0 ;
@@ -96,12 +98,14 @@ uint8_t Servo::attach(int pin, int min, int max, int defaultPos)
9698
9799 // OC3C, Port E, Bit 5; setting pin 3 as output:
98100 DDRE |= (1 << PE5); // bit 5 (pin 3) as output
99-
101+
100102 pinActive[1 ] = BOOL_TRUE;
101103 this ->servoPin = 3 ;
102104 }
103- else if (pin == 7 ) {
104- if (pinActive[3 ] == BOOL_FALSE){
105+ else if (pin == 7 )
106+ {
107+ if (pinActive[3 ] == BOOL_FALSE)
108+ {
105109 // resetting the control register A and B:
106110 TCCR4A = 0x0 ;
107111 TCCR4B = 0x0 ;
@@ -124,12 +128,14 @@ uint8_t Servo::attach(int pin, int min, int max, int defaultPos)
124128
125129 // OC4B, Port H, Bit 4; setting pin 7 as output:
126130 DDRH |= (1 << PH4); // bit 4 (pin 7) as output
127-
131+
128132 pinActive[2 ] = BOOL_TRUE;
129133 this ->servoPin = 7 ;
130134 }
131- else if (pin == 8 ) {
132- if (pinActive[2 ] == BOOL_FALSE){
135+ else if (pin == 8 )
136+ {
137+ if (pinActive[2 ] == BOOL_FALSE)
138+ {
133139 // resetting the control register A and B:
134140 TCCR4A = 0x0 ;
135141 TCCR4B = 0x0 ;
@@ -152,17 +158,19 @@ uint8_t Servo::attach(int pin, int min, int max, int defaultPos)
152158
153159 // OC4C, Port H, Bit 5; setting pin 8 as output:
154160 DDRH |= (1 << PH5); // bit 5 (pin 8) as output
155-
161+
156162 pinActive[3 ] = BOOL_TRUE;
157163 this ->servoPin = 8 ;
158164 }
159- else if (pin == 44 ) {
160- if (pinActive[5 ] == BOOL_FALSE){
165+ else if (pin == 44 )
166+ {
167+ if (pinActive[5 ] == BOOL_FALSE)
168+ {
161169 // resetting the control register A and B:
162170 TCCR5A = 0x0 ;
163171 TCCR5B = 0x0 ;
164172 }
165-
173+
166174 // setting the prescaler to 8 (2MHz):
167175 TCCR5B |= (1 << CS51);
168176
@@ -180,12 +188,14 @@ uint8_t Servo::attach(int pin, int min, int max, int defaultPos)
180188
181189 // OC5C, Port L, Bit 5; setting pin 44 as output:
182190 DDRL |= (1 << PL5); // bit 5 (pin 44) as output
183-
191+
184192 pinActive[4 ] = BOOL_TRUE;
185193 this ->servoPin = 44 ;
186194 }
187- else if (pin == 45 ) {
188- if (pinActive[4 ] == BOOL_FALSE){
195+ else if (pin == 45 )
196+ {
197+ if (pinActive[4 ] == BOOL_FALSE)
198+ {
189199 // resetting the control register A and B:
190200 TCCR5A = 0x0 ;
191201 TCCR5B = 0x0 ;
@@ -208,19 +218,20 @@ uint8_t Servo::attach(int pin, int min, int max, int defaultPos)
208218
209219 // OC5B, Port L, Bit 4; setting pin 45 as output:
210220 DDRL |= (1 << PL4); // bit 4 (pin 45) as output
211-
221+
212222 pinActive[5 ] = BOOL_TRUE;
213223 this ->servoPin = 45 ;
214224 }
215225 }
216-
226+
217227 return this ->servoIndex ;
218228}
219229
220- void Servo::detach ()
221- {
222- if (servoPin == 2 && pinActive[0 ] == BOOL_TRUE) {
223- if (pinActive[1 ] == BOOL_FALSE){
230+ void Servo::detach () {
231+ if (servoPin == 2 && pinActive[0 ] == BOOL_TRUE)
232+ {
233+ if (pinActive[1 ] == BOOL_FALSE)
234+ {
224235 // resetting the control register A and B:
225236 TCCR3A = 0x0 ;
226237 TCCR3B = 0x0 ;
@@ -229,11 +240,13 @@ void Servo::detach()
229240 }
230241 OCR3B = 0x0 ; // resetting the pulse width
231242 DDRE ^= (1 << PE4); // bit 4 (pin 2) stop output
232- pinActive[0 ] = BOOL_FALSE;
233- this ->servoPin = 0 ;
243+ pinActive[0 ] = BOOL_FALSE;
244+ this ->servoPin = 0 ;
234245 }
235- else if (servoPin == 3 && pinActive[1 ] == BOOL_TRUE) {
236- if (pinActive[0 ] == BOOL_FALSE){
246+ else if (servoPin == 3 && pinActive[1 ] == BOOL_TRUE)
247+ {
248+ if (pinActive[0 ] == BOOL_FALSE)
249+ {
237250 // resetting the control register A and B:
238251 TCCR3A = 0x0 ;
239252 TCCR3B = 0x0 ;
@@ -245,8 +258,10 @@ void Servo::detach()
245258 pinActive[1 ] = BOOL_FALSE;
246259 this ->servoPin = 0 ;
247260 }
248- else if (servoPin == 7 && pinActive[2 ] == BOOL_TRUE) {
249- if (pinActive[3 ] == BOOL_FALSE){
261+ else if (servoPin == 7 && pinActive[2 ] == BOOL_TRUE)
262+ {
263+ if (pinActive[3 ] == BOOL_FALSE)
264+ {
250265 // resetting the control register A and B:
251266 TCCR4A = 0x0 ;
252267 TCCR4B = 0x0 ;
@@ -257,9 +272,11 @@ void Servo::detach()
257272 DDRH ^= (1 << PH4); // bit 4 (pin 7) stop output
258273 pinActive[2 ] = BOOL_FALSE;
259274 this ->servoPin = 0 ;
260- }
261- else if (servoPin == 8 && pinActive[3 ] == BOOL_TRUE) {
262- if (pinActive[2 ] == BOOL_FALSE){
275+ }
276+ else if (servoPin == 8 && pinActive[3 ] == BOOL_TRUE)
277+ {
278+ if (pinActive[2 ] == BOOL_FALSE)
279+ {
263280 // resetting the control register A and B:
264281 TCCR4A = 0x0 ;
265282 TCCR4B = 0x0 ;
@@ -271,8 +288,10 @@ void Servo::detach()
271288 pinActive[3 ] = BOOL_FALSE;
272289 this ->servoPin = 0 ;
273290 }
274- else if (servoPin == 44 && pinActive[4 ] == BOOL_TRUE) {
275- if (pinActive[5 ] == BOOL_FALSE){
291+ else if (servoPin == 44 && pinActive[4 ] == BOOL_TRUE)
292+ {
293+ if (pinActive[5 ] == BOOL_FALSE)
294+ {
276295 // resetting the control register A and B:
277296 TCCR5A = 0x0 ;
278297 TCCR5B = 0x0 ;
@@ -284,8 +303,10 @@ void Servo::detach()
284303 pinActive[4 ] = BOOL_FALSE;
285304 this ->servoPin = 0 ;
286305 }
287- else if (servoPin == 45 && pinActive[5 ] == BOOL_TRUE) {
288- if (pinActive[4 ] == BOOL_FALSE){
306+ else if (servoPin == 45 && pinActive[5 ] == BOOL_TRUE)
307+ {
308+ if (pinActive[4 ] == BOOL_FALSE)
309+ {
289310 // resetting the control register A and B:
290311 TCCR5A = 0x0 ;
291312 TCCR5B = 0x0 ;
@@ -374,8 +395,7 @@ void Servo::detachAll() {
374395 }
375396}
376397
377- void Servo::write (int value)
378- {
398+ void Servo::write (int value) {
379399 float tempValue;
380400
381401 if (value <= 0 )
@@ -397,38 +417,46 @@ void Servo::write(int value)
397417 this ->writeMicroseconds (value);
398418}
399419
400- void Servo::writeMicroseconds (int value)
401- {
402- if ((this ->servoIndex < MAX_SERVOS)) {
403- if (value < this ->min ) {
420+ void Servo::writeMicroseconds (int value) {
421+ if ((this ->servoIndex < MAX_SERVOS))
422+ {
423+ if (value < this ->min )
424+ {
404425 value = this ->min ;
405426 }
406- else if (value > this ->max ) {
427+ else if (value > this ->max )
428+ {
407429 value = this ->max ;
408430 }
409431 this ->pulseWidth = value;
410432
411- if (this ->servoPin == 2 && pinActive[0 ] == BOOL_TRUE) {
433+ if (this ->servoPin == 2 && pinActive[0 ] == BOOL_TRUE)
434+ {
412435 OCR3B = 0x0 ;
413436 OCR3B = this ->pulseWidth * 2 ;
414437 }
415- else if (this ->servoPin == 3 && pinActive[1 ] == BOOL_TRUE) {
438+ else if (this ->servoPin == 3 && pinActive[1 ] == BOOL_TRUE)
439+ {
416440 OCR3C = 0x0 ;
417441 OCR3C = this ->pulseWidth * 2 ;
418442 }
419- else if (this ->servoPin == 7 && pinActive[2 ] == BOOL_TRUE) {
443+ else if (this ->servoPin == 7 && pinActive[2 ] == BOOL_TRUE)
444+ {
420445 OCR4B = 0x0 ;
421446 OCR4B = this ->pulseWidth * 2 ;
422447 }
423- else if (this ->servoPin == 8 && pinActive[3 ] == BOOL_TRUE) {
448+ else if (this ->servoPin == 8 && pinActive[3 ] == BOOL_TRUE)
449+ {
424450 OCR4C = 0x0 ;
425451 OCR4C = this ->pulseWidth * 2 ;
426452 }
427- else if (this ->servoPin == 44 && pinActive[4 ] == BOOL_TRUE) {
453+ else if (this ->servoPin == 44 && pinActive[4 ] == BOOL_TRUE)
454+ {
428455 OCR5C = 0x0 ;
429456 OCR5C = this ->pulseWidth * 2 ;
430457 }
431- else if (this ->servoPin == 45 && pinActive[5 ] == BOOL_TRUE) {
458+ else if (this ->servoPin == 45 && pinActive[5 ] == BOOL_TRUE)
459+ {
432460 OCR5B = 0x0 ;
433461 OCR5B = this ->pulseWidth * 2 ;
434462 }
0 commit comments