36
36
* @brief Instantiates a new PCA9685 PWM driver chip with the I2C address on a
37
37
* TwoWire interface
38
38
*/
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) {}
42
41
43
42
/* !
44
43
* @brief Instantiates a new PCA9685 PWM driver chip with the I2C address on a
45
44
* TwoWire interface
46
45
* @param addr The 7-bit I2C address to locate this chip, default is 0x40
47
46
*/
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) {}
51
49
52
50
/* !
53
51
* @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):
56
54
* @param i2c A reference to a 'TwoWire' object that we'll use to communicate
57
55
* with
58
56
*/
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) { }
62
60
63
61
/* !
64
62
* @brief Setups the I2C interface and hardware
@@ -141,14 +139,17 @@ void Adafruit_PWMServoDriver::setPWMFreq(float freq) {
141
139
Serial.println (freq);
142
140
#endif
143
141
// 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)
147
146
148
147
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;
152
153
153
154
#ifdef ENABLE_DEBUG_OUTPUT
154
155
Serial.print (" Final pre-scale: " );
@@ -157,12 +158,12 @@ void Adafruit_PWMServoDriver::setPWMFreq(float freq) {
157
158
158
159
uint8_t oldmode = read8 (PCA9685_MODE1);
159
160
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
162
163
write8 (PCA9685_MODE1, oldmode);
163
164
delay (5 );
164
165
// 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);
166
167
167
168
#ifdef ENABLE_DEBUG_OUTPUT
168
169
Serial.print (" Mode now 0x" );
@@ -171,22 +172,21 @@ void Adafruit_PWMServoDriver::setPWMFreq(float freq) {
171
172
}
172
173
173
174
/* !
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.
176
177
* 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.
179
180
*/
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);
182
183
uint8_t newmode;
183
184
if (totempole) {
184
185
newmode = oldmode | MODE2_OUTDRV;
185
- }
186
- else {
186
+ } else {
187
187
newmode = oldmode & ~MODE2_OUTDRV;
188
188
}
189
- write8 (PCA9685_MODE2, newmode);
189
+ write8 (PCA9685_MODE2, newmode);
190
190
#ifdef ENABLE_DEBUG_OUTPUT
191
191
Serial.print (" Setting output mode: " );
192
192
Serial.print (totempole ? " totempole" : " open drain" );
@@ -199,8 +199,7 @@ void Adafruit_PWMServoDriver::setOutputMode(bool totempole) {
199
199
* @brief Reads set Prescale from PCA9685
200
200
* @return prescale value
201
201
*/
202
- uint8_t Adafruit_PWMServoDriver::readPrescale (void )
203
- {
202
+ uint8_t Adafruit_PWMServoDriver::readPrescale (void ) {
204
203
return read8 (PCA9685_PRESCALE);
205
204
}
206
205
@@ -276,52 +275,69 @@ void Adafruit_PWMServoDriver::setPin(uint8_t num, uint16_t val, bool invert) {
276
275
}
277
276
278
277
/* !
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
280
280
* @param num One of the PWM output pins, from 0 to 15
281
281
* @param Microseconds The number of Microseconds to turn the PWM output ON
282
282
*/
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
285
286
Serial.print (" Setting PWM Via Microseconds on output" );
286
287
Serial.print (num);
287
288
Serial.print (" : " );
288
289
Serial.print (Microseconds);
289
290
Serial.println (" ->" );
290
- #endif
291
+ #endif
291
292
292
293
double pulse = Microseconds;
293
294
double pulselength;
294
- pulselength = 1000000 ; // 1,000,000 us per second
295
+ pulselength = 1000000 ; // 1,000,000 us per second
295
296
296
297
// Read prescale
297
298
uint16_t prescale = Adafruit_PWMServoDriver::readPrescale ();
298
299
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
302
304
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
304
307
prescale += 1 ;
305
- pulselength *= prescale;
308
+ pulselength *= prescale;
306
309
pulselength /= _oscillator_freq;
307
310
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
311
315
312
316
pulse /= pulselength;
313
317
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
317
322
318
323
Adafruit_PWMServoDriver::setPWM (num, 0 , pulse);
319
324
}
320
325
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
+ */
321
332
uint32_t Adafruit_PWMServoDriver::getOscillatorFrequency (void ) {
322
333
return _oscillator_freq;
323
334
}
324
335
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
+ */
325
341
void Adafruit_PWMServoDriver::setOscillatorFrequency (uint32_t freq) {
326
342
_oscillator_freq = freq;
327
343
}
0 commit comments