Skip to content

Commit dd088c4

Browse files
committed
clang
1 parent 71164df commit dd088c4

File tree

2 files changed

+113
-85
lines changed

2 files changed

+113
-85
lines changed

Adafruit_PWMServoDriver.cpp

Lines changed: 61 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,16 @@
3636
* @brief Instantiates a new PCA9685 PWM driver chip with the I2C address on a
3737
* TwoWire interface
3838
*/
39-
Adafruit_PWMServoDriver::Adafruit_PWMServoDriver():
40-
_i2caddr(PCA9685_I2C_ADDRESS), _i2c(&Wire) {
41-
}
39+
Adafruit_PWMServoDriver::Adafruit_PWMServoDriver()
40+
: _i2caddr(PCA9685_I2C_ADDRESS), _i2c(&Wire) {}
4241

4342
/*!
4443
* @brief Instantiates a new PCA9685 PWM driver chip with the I2C address on a
4544
* TwoWire interface
4645
* @param addr The 7-bit I2C address to locate this chip, default is 0x40
4746
*/
48-
Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(const uint8_t addr):
49-
_i2caddr(addr), _i2c(&Wire) {
50-
}
47+
Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(const uint8_t addr)
48+
: _i2caddr(addr), _i2c(&Wire) {}
5149

5250
/*!
5351
* @brief Instantiates a new PCA9685 PWM driver chip with the I2C address on a
@@ -56,9 +54,9 @@ Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(const uint8_t addr):
5654
* @param i2c A reference to a 'TwoWire' object that we'll use to communicate
5755
* with
5856
*/
59-
Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(const uint8_t addr, TwoWire& i2c) :
60-
_i2caddr(addr), _i2c(&i2c) {
61-
}
57+
Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(const uint8_t addr,
58+
TwoWire &i2c)
59+
: _i2caddr(addr), _i2c(&i2c) {}
6260

6361
/*!
6462
* @brief Setups the I2C interface and hardware
@@ -141,14 +139,17 @@ void Adafruit_PWMServoDriver::setPWMFreq(float freq) {
141139
Serial.println(freq);
142140
#endif
143141
// Range output modulation frequency is dependant on oscillator
144-
if (freq < 1) freq = 1;
145-
if (freq > 3500) freq = 3500; // Datasheet limit is 3052=50MHz/(4*4096)
146-
142+
if (freq < 1)
143+
freq = 1;
144+
if (freq > 3500)
145+
freq = 3500; // Datasheet limit is 3052=50MHz/(4*4096)
147146

148147
float prescaleval = ((_oscillator_freq / (freq * 4096.0)) + 0.5) - 1;
149-
if (prescaleval < PCA9685_PRESCALE_MIN) prescaleval = PCA9685_PRESCALE_MIN;
150-
if (prescaleval > PCA9685_PRESCALE_MAX) prescaleval = PCA9685_PRESCALE_MAX;
151-
uint8_t prescale = (uint8_t) prescaleval;
148+
if (prescaleval < PCA9685_PRESCALE_MIN)
149+
prescaleval = PCA9685_PRESCALE_MIN;
150+
if (prescaleval > PCA9685_PRESCALE_MAX)
151+
prescaleval = PCA9685_PRESCALE_MAX;
152+
uint8_t prescale = (uint8_t)prescaleval;
152153

153154
#ifdef ENABLE_DEBUG_OUTPUT
154155
Serial.print("Final pre-scale: ");
@@ -157,12 +158,12 @@ void Adafruit_PWMServoDriver::setPWMFreq(float freq) {
157158

158159
uint8_t oldmode = read8(PCA9685_MODE1);
159160
uint8_t newmode = (oldmode & ~MODE1_RESTART) | MODE1_SLEEP; // sleep
160-
write8(PCA9685_MODE1, newmode); // go to sleep
161-
write8(PCA9685_PRESCALE, prescale); // set the prescaler
161+
write8(PCA9685_MODE1, newmode); // go to sleep
162+
write8(PCA9685_PRESCALE, prescale); // set the prescaler
162163
write8(PCA9685_MODE1, oldmode);
163164
delay(5);
164165
// This sets the MODE1 register to turn on auto increment.
165-
write8(PCA9685_MODE1, oldmode | MODE1_RESTART | MODE1_AI);
166+
write8(PCA9685_MODE1, oldmode | MODE1_RESTART | MODE1_AI);
166167

167168
#ifdef ENABLE_DEBUG_OUTPUT
168169
Serial.print("Mode now 0x");
@@ -171,22 +172,21 @@ void Adafruit_PWMServoDriver::setPWMFreq(float freq) {
171172
}
172173

173174
/*!
174-
* @brief Sets the output mode of the PCA9685 to either
175-
* open drain or push pull / totempole.
175+
* @brief Sets the output mode of the PCA9685 to either
176+
* open drain or push pull / totempole.
176177
* Warning: LEDs with integrated zener diodes should
177-
* only be driven in open drain mode.
178-
* @param totempole Totempole if true, open drain if false.
178+
* only be driven in open drain mode.
179+
* @param totempole Totempole if true, open drain if false.
179180
*/
180-
void Adafruit_PWMServoDriver::setOutputMode(bool totempole) {
181-
uint8_t oldmode = read8(PCA9685_MODE2);
181+
void Adafruit_PWMServoDriver::setOutputMode(bool totempole) {
182+
uint8_t oldmode = read8(PCA9685_MODE2);
182183
uint8_t newmode;
183184
if (totempole) {
184185
newmode = oldmode | MODE2_OUTDRV;
185-
}
186-
else {
186+
} else {
187187
newmode = oldmode & ~MODE2_OUTDRV;
188188
}
189-
write8(PCA9685_MODE2, newmode);
189+
write8(PCA9685_MODE2, newmode);
190190
#ifdef ENABLE_DEBUG_OUTPUT
191191
Serial.print("Setting output mode: ");
192192
Serial.print(totempole ? "totempole" : "open drain");
@@ -199,8 +199,7 @@ void Adafruit_PWMServoDriver::setOutputMode(bool totempole) {
199199
* @brief Reads set Prescale from PCA9685
200200
* @return prescale value
201201
*/
202-
uint8_t Adafruit_PWMServoDriver::readPrescale(void)
203-
{
202+
uint8_t Adafruit_PWMServoDriver::readPrescale(void) {
204203
return read8(PCA9685_PRESCALE);
205204
}
206205

@@ -276,52 +275,69 @@ void Adafruit_PWMServoDriver::setPin(uint8_t num, uint16_t val, bool invert) {
276275
}
277276

278277
/*!
279-
* @brief Sets the PWM output of one of the PCA9685 pins based on the input microseconds, output is not precise
278+
* @brief Sets the PWM output of one of the PCA9685 pins based on the input
279+
* microseconds, output is not precise
280280
* @param num One of the PWM output pins, from 0 to 15
281281
* @param Microseconds The number of Microseconds to turn the PWM output ON
282282
*/
283-
void Adafruit_PWMServoDriver::writeMicroseconds(uint8_t num, uint16_t Microseconds) {
284-
#ifdef ENABLE_DEBUG_OUTPUT
283+
void Adafruit_PWMServoDriver::writeMicroseconds(uint8_t num,
284+
uint16_t Microseconds) {
285+
#ifdef ENABLE_DEBUG_OUTPUT
285286
Serial.print("Setting PWM Via Microseconds on output");
286287
Serial.print(num);
287288
Serial.print(": ");
288289
Serial.print(Microseconds);
289290
Serial.println("->");
290-
#endif
291+
#endif
291292

292293
double pulse = Microseconds;
293294
double pulselength;
294-
pulselength = 1000000; // 1,000,000 us per second
295+
pulselength = 1000000; // 1,000,000 us per second
295296

296297
// Read prescale
297298
uint16_t prescale = Adafruit_PWMServoDriver::readPrescale();
298299

299-
#ifdef ENABLE_DEBUG_OUTPUT
300-
Serial.print(prescale); Serial.println(" PCA9685 chip prescale");
301-
#endif
300+
#ifdef ENABLE_DEBUG_OUTPUT
301+
Serial.print(prescale);
302+
Serial.println(" PCA9685 chip prescale");
303+
#endif
302304

303-
// Calculate the pulse for PWM based on Equation 1 from the datasheet section 7.3.5
305+
// Calculate the pulse for PWM based on Equation 1 from the datasheet section
306+
// 7.3.5
304307
prescale += 1;
305-
pulselength *= prescale;
308+
pulselength *= prescale;
306309
pulselength /= _oscillator_freq;
307310

308-
#ifdef ENABLE_DEBUG_OUTPUT
309-
Serial.print(pulselength); Serial.println(" us per bit");
310-
#endif
311+
#ifdef ENABLE_DEBUG_OUTPUT
312+
Serial.print(pulselength);
313+
Serial.println(" us per bit");
314+
#endif
311315

312316
pulse /= pulselength;
313317

314-
#ifdef ENABLE_DEBUG_OUTPUT
315-
Serial.print(pulse);Serial.println(" pulse for PWM");
316-
#endif
318+
#ifdef ENABLE_DEBUG_OUTPUT
319+
Serial.print(pulse);
320+
Serial.println(" pulse for PWM");
321+
#endif
317322

318323
Adafruit_PWMServoDriver::setPWM(num, 0, pulse);
319324
}
320325

326+
/*!
327+
* @brief Getter for the internally tracked oscillator used for freq
328+
* calculations
329+
* @returns The frequency the PCA9685 thinks it is running at (it cannot
330+
* introspect)
331+
*/
321332
uint32_t Adafruit_PWMServoDriver::getOscillatorFrequency(void) {
322333
return _oscillator_freq;
323334
}
324335

336+
/*!
337+
* @brief Setter for the internally tracked oscillator used for freq
338+
* calculations
339+
* @param freq The frequency the PCA9685 should use for frequency calculations
340+
*/
325341
void Adafruit_PWMServoDriver::setOscillatorFrequency(uint32_t freq) {
326342
_oscillator_freq = freq;
327343
}

Adafruit_PWMServoDriver.h

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
*
44
* This is a library for our Adafruit 16-channel PWM & Servo driver.
55
*
6-
* Designed specifically to work with the Adafruit 16-channel PWM & Servo driver.
6+
* Designed specifically to work with the Adafruit 16-channel PWM & Servo
7+
* driver.
78
*
89
* Pick one up today in the adafruit shop!
910
* ------> https://www.adafruit.com/product/815
@@ -26,54 +27,65 @@
2627
#include <Wire.h>
2728

2829
// REGISTER ADDRESSES
29-
#define PCA9685_MODE1 0x00 /**< Mode Register 1 */
30-
#define PCA9685_MODE2 0x01 /**< Mode Register 2 */
31-
#define PCA9685_SUBADR1 0x02 /**< I2C-bus subaddress 1 */
32-
#define PCA9685_SUBADR2 0x03 /**< I2C-bus subaddress 2 */
33-
#define PCA9685_SUBADR3 0x04 /**< I2C-bus subaddress 3 */
34-
#define PCA9685_ALLCALLADR 0x05 /**< LED All Call I2C-bus address */
35-
#define PCA9685_LED0_ON_L 0x06 /**< LED0 output and brightness control byte 0 */
36-
#define PCA9685_LED0_ON_H 0x07 /**< LED0 output and brightness control byte 1 */
37-
#define PCA9685_LED0_OFF_L 0x08 /**< LED0 output and brightness control byte 2 */
38-
#define PCA9685_LED0_OFF_H 0x09 /**< LED0 output and brightness control byte 3 */
30+
#define PCA9685_MODE1 0x00 /**< Mode Register 1 */
31+
#define PCA9685_MODE2 0x01 /**< Mode Register 2 */
32+
#define PCA9685_SUBADR1 0x02 /**< I2C-bus subaddress 1 */
33+
#define PCA9685_SUBADR2 0x03 /**< I2C-bus subaddress 2 */
34+
#define PCA9685_SUBADR3 0x04 /**< I2C-bus subaddress 3 */
35+
#define PCA9685_ALLCALLADR 0x05 /**< LED All Call I2C-bus address */
36+
#define PCA9685_LED0_ON_L 0x06 /**< LED0 output and brightness control byte 0 \
37+
*/
38+
#define PCA9685_LED0_ON_H 0x07 /**< LED0 output and brightness control byte 1 \
39+
*/
40+
#define PCA9685_LED0_OFF_L \
41+
0x08 /**< LED0 output and brightness control byte 2 */
42+
#define PCA9685_LED0_OFF_H \
43+
0x09 /**< LED0 output and brightness control byte 3 */
3944
// etc all 16: LED15_OFF_H 0x45
40-
#define PCA9685_ALLLED_ON_L 0xFA /**< load all the LEDn_ON registers, byte 0 */
41-
#define PCA9685_ALLLED_ON_H 0xFB /**< load all the LEDn_ON registers, byte 1 */
42-
#define PCA9685_ALLLED_OFF_L 0xFC /**< load all the LEDn_OFF registers, byte 0 */
43-
#define PCA9685_ALLLED_OFF_H 0xFD /**< load all the LEDn_OFF registers, byte 1 */
44-
#define PCA9685_PRESCALE 0xFE /**< Prescaler for PWM output frequency */
45-
#define PCA9685_TESTMODE 0xFF /**< defines the test mode to be entered */
45+
#define PCA9685_ALLLED_ON_L 0xFA /**< load all the LEDn_ON registers, byte 0 \
46+
*/
47+
#define PCA9685_ALLLED_ON_H 0xFB /**< load all the LEDn_ON registers, byte 1 \
48+
*/
49+
#define PCA9685_ALLLED_OFF_L \
50+
0xFC /**< load all the LEDn_OFF registers, byte 0 */
51+
#define PCA9685_ALLLED_OFF_H \
52+
0xFD /**< load all the LEDn_OFF registers, byte 1 */
53+
#define PCA9685_PRESCALE 0xFE /**< Prescaler for PWM output frequency */
54+
#define PCA9685_TESTMODE 0xFF /**< defines the test mode to be entered */
4655

4756
// MODE1 bits
48-
#define MODE1_ALLCAL 0x01 /**< respond to LED All Call I2C-bus address */
49-
#define MODE1_SUB3 0x02 /**< respond to I2C-bus subaddress 3 */
50-
#define MODE1_SUB2 0x04 /**< respond to I2C-bus subaddress 2 */
51-
#define MODE1_SUB1 0x08 /**< respond to I2C-bus subaddress 1 */
52-
#define MODE1_SLEEP 0x10 /**< Low power mode. Oscillator off */
53-
#define MODE1_AI 0x20 /**< Auto-Increment enabled */
54-
#define MODE1_EXTCLK 0x40 /**< Use EXTCLK pin clock */
55-
#define MODE1_RESTART 0x80 /**< Restart enabled */
57+
#define MODE1_ALLCAL 0x01 /**< respond to LED All Call I2C-bus address */
58+
#define MODE1_SUB3 0x02 /**< respond to I2C-bus subaddress 3 */
59+
#define MODE1_SUB2 0x04 /**< respond to I2C-bus subaddress 2 */
60+
#define MODE1_SUB1 0x08 /**< respond to I2C-bus subaddress 1 */
61+
#define MODE1_SLEEP 0x10 /**< Low power mode. Oscillator off */
62+
#define MODE1_AI 0x20 /**< Auto-Increment enabled */
63+
#define MODE1_EXTCLK 0x40 /**< Use EXTCLK pin clock */
64+
#define MODE1_RESTART 0x80 /**< Restart enabled */
5665
// MODE2 bits
57-
#define MODE2_OUTNE_0 0x01 /**< Active LOW output enable input */
58-
#define MODE2_OUTNE_1 0x02 /**< Active LOW output enable input - high impedience */
59-
#define MODE2_OUTDRV 0x04 /**< totem pole structure vs open-drain */
60-
#define MODE2_OCH 0x08 /**< Outputs change on ACK vs STOP */
61-
#define MODE2_INVRT 0x10 /**< Output logic state inverted */
66+
#define MODE2_OUTNE_0 0x01 /**< Active LOW output enable input */
67+
#define MODE2_OUTNE_1 \
68+
0x02 /**< Active LOW output enable input - high impedience */
69+
#define MODE2_OUTDRV 0x04 /**< totem pole structure vs open-drain */
70+
#define MODE2_OCH 0x08 /**< Outputs change on ACK vs STOP */
71+
#define MODE2_INVRT 0x10 /**< Output logic state inverted */
6272

63-
#define PCA9685_I2C_ADDRESS 0x40 /**< Default PCA9685 I2C Slave Address */
64-
#define FREQUENCY_OSCILLATOR 25000000 /**< Oscillator frequency in datasheet */
73+
#define PCA9685_I2C_ADDRESS 0x40 /**< Default PCA9685 I2C Slave Address */
74+
#define FREQUENCY_OSCILLATOR 25000000 /**< Oscillator frequency in datasheet \
75+
*/
6576

66-
#define PCA9685_PRESCALE_MIN 3 /**< minimum prescale value */
77+
#define PCA9685_PRESCALE_MIN 3 /**< minimum prescale value */
6778
#define PCA9685_PRESCALE_MAX 255 /**< maximum prescale value */
6879

69-
/*!
70-
* @brief Class that stores state and functions for interacting with PCA9685 PWM chip
80+
/*!
81+
* @brief Class that stores state and functions for interacting with PCA9685
82+
* PWM chip
7183
*/
7284
class Adafruit_PWMServoDriver {
73-
public:
85+
public:
7486
Adafruit_PWMServoDriver();
7587
Adafruit_PWMServoDriver(const uint8_t addr);
76-
Adafruit_PWMServoDriver(const uint8_t addr, TwoWire& i2c);
88+
Adafruit_PWMServoDriver(const uint8_t addr, TwoWire &i2c);
7789
void begin(uint8_t prescale = 0);
7890
void reset();
7991
void sleep();
@@ -83,16 +95,16 @@ class Adafruit_PWMServoDriver {
8395
void setOutputMode(bool totempole);
8496
uint8_t getPWM(uint8_t num);
8597
void setPWM(uint8_t num, uint16_t on, uint16_t off);
86-
void setPin(uint8_t num, uint16_t val, bool invert=false);
98+
void setPin(uint8_t num, uint16_t val, bool invert = false);
8799
uint8_t readPrescale(void);
88100
void writeMicroseconds(uint8_t num, uint16_t Microseconds);
89101

90102
void setOscillatorFrequency(uint32_t freq);
91103
uint32_t getOscillatorFrequency(void);
92104

93-
private:
105+
private:
94106
uint8_t _i2caddr;
95-
TwoWire* _i2c;
107+
TwoWire *_i2c;
96108

97109
uint32_t _oscillator_freq;
98110
uint8_t read8(uint8_t addr);

0 commit comments

Comments
 (0)