Skip to content

Commit ddbc2da

Browse files
committed
Merge branch 'photodude-patch-1'
2 parents 95ee436 + 16705a5 commit ddbc2da

File tree

5 files changed

+161
-123
lines changed

5 files changed

+161
-123
lines changed

.travis.yml

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
language: c
22
sudo: false
3-
4-
# Blacklist
5-
branches:
6-
except:
7-
- gh-pages
3+
cache:
4+
directories:
5+
- ~/arduino_ide
6+
- ~/.arduino15/packages/
7+
git:
8+
depth: false
9+
quiet: true
810

911
env:
1012
global:
1113
- PRETTYNAME="Adafruit PCA9685 PWM Servo Driver Arduino Library"
1214

15+
addons:
16+
apt:
17+
sources:
18+
- llvm-toolchain-trusty-5.0
19+
- key_url: 'http://apt.llvm.org/llvm-snapshot.gpg.key'
20+
packages:
21+
- clang-format-5.0
22+
1323
before_install:
1424
- source <(curl -SLs https://raw.githubusercontent.com/adafruit/travis-ci-arduino/master/install.sh)
25+
- curl -SLs https://raw.githubusercontent.com/adafruit/travis-ci-arduino/master/run-clang-format.py > run-clang-format.py
1526

1627
script:
28+
- python run-clang-format.py -r .
1729
- build_main_platforms
1830

1931
# Generate and deploy documentation

Adafruit_PWMServoDriver.cpp

Lines changed: 82 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,22 @@
3030
#include "Adafruit_PWMServoDriver.h"
3131
#include <Wire.h>
3232

33+
//#define ENABLE_DEBUG_OUTPUT
34+
3335
/*!
3436
* @brief Instantiates a new PCA9685 PWM driver chip with the I2C address on a
3537
* TwoWire interface
3638
*/
37-
Adafruit_PWMServoDriver::Adafruit_PWMServoDriver():
38-
_i2caddr(PCA9685_I2C_ADDRESS), _i2c(&Wire) {
39-
}
39+
Adafruit_PWMServoDriver::Adafruit_PWMServoDriver()
40+
: _i2caddr(PCA9685_I2C_ADDRESS), _i2c(&Wire) {}
4041

4142
/*!
4243
* @brief Instantiates a new PCA9685 PWM driver chip with the I2C address on a
4344
* TwoWire interface
4445
* @param addr The 7-bit I2C address to locate this chip, default is 0x40
4546
*/
46-
Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(const uint8_t addr):
47-
_i2caddr(addr), _i2c(&Wire) {
48-
}
47+
Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(const uint8_t addr)
48+
: _i2caddr(addr), _i2c(&Wire) {}
4949

5050
/*!
5151
* @brief Instantiates a new PCA9685 PWM driver chip with the I2C address on a
@@ -54,9 +54,9 @@ Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(const uint8_t addr):
5454
* @param i2c A reference to a 'TwoWire' object that we'll use to communicate
5555
* with
5656
*/
57-
Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(const uint8_t addr, TwoWire& i2c) :
58-
_i2caddr(addr), _i2c(&i2c) {
59-
}
57+
Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(const uint8_t addr,
58+
TwoWire &i2c)
59+
: _i2caddr(addr), _i2c(&i2c) {}
6060

6161
/*!
6262
* @brief Setups the I2C interface and hardware
@@ -72,6 +72,8 @@ void Adafruit_PWMServoDriver::begin(uint8_t prescale) {
7272
// set a default frequency
7373
setPWMFreq(1000);
7474
}
75+
// set the default internal frequency
76+
setOscillatorFrequency(FREQUENCY_OSCILLATOR);
7577
}
7678

7779
/*!
@@ -137,21 +139,17 @@ void Adafruit_PWMServoDriver::setPWMFreq(float freq) {
137139
Serial.println(freq);
138140
#endif
139141
// Range output modulation frequency is dependant on oscillator
140-
if (freq < 1) freq = 1;
141-
if (freq > 3500) freq = 3500; // Datasheet limit is 3052=50MHz/(4*4096)
142-
/*
143-
freq *= 0.9; // Correct for overshoot in the frequency setting (see issue #11)
144-
float prescaleval = FREQUENCY_OSCILLATOR;
145-
*/
146-
uint32_t prescaleval = FREQUENCY_CALIBRATED;
147-
prescaleval /= freq; // required output modulation frequency
148-
// rounding to nearest number is equal to adding 0,5 and floor to nearest number
149-
prescaleval += 2048;
150-
prescaleval /= 4096;
151-
prescaleval -= 1;
152-
if (prescaleval < PCA9685_PRESCALE_MIN) prescaleval = PCA9685_PRESCALE_MIN;
153-
if (prescaleval > PCA9685_PRESCALE_MAX) prescaleval = PCA9685_PRESCALE_MAX;
154-
uint8_t prescale = (uint8_t) prescaleval;
142+
if (freq < 1)
143+
freq = 1;
144+
if (freq > 3500)
145+
freq = 3500; // Datasheet limit is 3052=50MHz/(4*4096)
146+
147+
float prescaleval = ((_oscillator_freq / (freq * 4096.0)) + 0.5) - 1;
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;
155153

156154
#ifdef ENABLE_DEBUG_OUTPUT
157155
Serial.print("Final pre-scale: ");
@@ -160,12 +158,12 @@ void Adafruit_PWMServoDriver::setPWMFreq(float freq) {
160158

161159
uint8_t oldmode = read8(PCA9685_MODE1);
162160
uint8_t newmode = (oldmode & ~MODE1_RESTART) | MODE1_SLEEP; // sleep
163-
write8(PCA9685_MODE1, newmode); // go to sleep
164-
write8(PCA9685_PRESCALE, prescale); // set the prescaler
161+
write8(PCA9685_MODE1, newmode); // go to sleep
162+
write8(PCA9685_PRESCALE, prescale); // set the prescaler
165163
write8(PCA9685_MODE1, oldmode);
166164
delay(5);
167165
// This sets the MODE1 register to turn on auto increment.
168-
write8(PCA9685_MODE1, oldmode | MODE1_RESTART | MODE1_AI);
166+
write8(PCA9685_MODE1, oldmode | MODE1_RESTART | MODE1_AI);
169167

170168
#ifdef ENABLE_DEBUG_OUTPUT
171169
Serial.print("Mode now 0x");
@@ -174,22 +172,21 @@ void Adafruit_PWMServoDriver::setPWMFreq(float freq) {
174172
}
175173

176174
/*!
177-
* @brief Sets the output mode of the PCA9685 to either
178-
* open drain or push pull / totempole.
175+
* @brief Sets the output mode of the PCA9685 to either
176+
* open drain or push pull / totempole.
179177
* Warning: LEDs with integrated zener diodes should
180-
* only be driven in open drain mode.
181-
* @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.
182180
*/
183-
void Adafruit_PWMServoDriver::setOutputMode(bool totempole) {
184-
uint8_t oldmode = read8(PCA9685_MODE2);
181+
void Adafruit_PWMServoDriver::setOutputMode(bool totempole) {
182+
uint8_t oldmode = read8(PCA9685_MODE2);
185183
uint8_t newmode;
186184
if (totempole) {
187185
newmode = oldmode | MODE2_OUTDRV;
188-
}
189-
else {
186+
} else {
190187
newmode = oldmode & ~MODE2_OUTDRV;
191188
}
192-
write8(PCA9685_MODE2, newmode);
189+
write8(PCA9685_MODE2, newmode);
193190
#ifdef ENABLE_DEBUG_OUTPUT
194191
Serial.print("Setting output mode: ");
195192
Serial.print(totempole ? "totempole" : "open drain");
@@ -202,8 +199,7 @@ void Adafruit_PWMServoDriver::setOutputMode(bool totempole) {
202199
* @brief Reads set Prescale from PCA9685
203200
* @return prescale value
204201
*/
205-
uint8_t Adafruit_PWMServoDriver::readPrescale(void)
206-
{
202+
uint8_t Adafruit_PWMServoDriver::readPrescale(void) {
207203
return read8(PCA9685_PRESCALE);
208204
}
209205

@@ -279,55 +275,74 @@ void Adafruit_PWMServoDriver::setPin(uint8_t num, uint16_t val, bool invert) {
279275
}
280276

281277
/*!
282-
* @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
283280
* @param num One of the PWM output pins, from 0 to 15
284281
* @param Microseconds The number of Microseconds to turn the PWM output ON
285282
*/
286-
void Adafruit_PWMServoDriver::writeMicroseconds(uint8_t num, uint16_t Microseconds) {
287-
#ifdef ENABLE_DEBUG_OUTPUT
283+
void Adafruit_PWMServoDriver::writeMicroseconds(uint8_t num,
284+
uint16_t Microseconds) {
285+
#ifdef ENABLE_DEBUG_OUTPUT
288286
Serial.print("Setting PWM Via Microseconds on output");
289287
Serial.print(num);
290288
Serial.print(": ");
291289
Serial.print(Microseconds);
292290
Serial.println("->");
293-
#endif
291+
#endif
294292

295293
double pulse = Microseconds;
296294
double pulselength;
297-
pulselength = 1000000; // 1,000,000 us per second
295+
pulselength = 1000000; // 1,000,000 us per second
296+
297+
// Read prescale
298+
uint16_t prescale = Adafruit_PWMServoDriver::readPrescale();
299+
300+
#ifdef ENABLE_DEBUG_OUTPUT
301+
Serial.print(prescale);
302+
Serial.println(" PCA9685 chip prescale");
303+
#endif
298304

299-
// Read prescale and convert to frequency
300-
double prescale = Adafruit_PWMServoDriver::readPrescale();
305+
// Calculate the pulse for PWM based on Equation 1 from the datasheet section
306+
// 7.3.5
301307
prescale += 1;
302-
uint32_t freq = 25000000; // Chip frequency is 25MHz
303-
freq /= prescale;
304-
freq /= 4096; // 12 bits of resolution
305-
306-
#ifdef ENABLE_DEBUG_OUTPUT
307-
Serial.print(freq); Serial.println(" Calculated PCA9685 chip PWM Frequency");
308-
#endif
309-
310-
pulselength /= freq; // us per period from PCA9685 chip PWM Frequency using prescale reverse frequency calc
311-
312-
#ifdef ENABLE_DEBUG_OUTPUT
313-
Serial.print(pulselength); Serial.println(" us per period");
314-
#endif
315-
316-
pulselength /= 4096; // 12 bits of resolution
317-
318-
#ifdef ENABLE_DEBUG_OUTPUT
319-
Serial.print(pulselength); Serial.println(" us per bit");
320-
#endif
308+
pulselength *= prescale;
309+
pulselength /= _oscillator_freq;
310+
311+
#ifdef ENABLE_DEBUG_OUTPUT
312+
Serial.print(pulselength);
313+
Serial.println(" us per bit");
314+
#endif
321315

322316
pulse /= pulselength;
323317

324-
#ifdef ENABLE_DEBUG_OUTPUT
325-
Serial.print(pulse);Serial.println(" pulse for PWM");
326-
#endif
318+
#ifdef ENABLE_DEBUG_OUTPUT
319+
Serial.print(pulse);
320+
Serial.println(" pulse for PWM");
321+
#endif
327322

328323
Adafruit_PWMServoDriver::setPWM(num, 0, pulse);
329324
}
330325

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+
*/
332+
uint32_t Adafruit_PWMServoDriver::getOscillatorFrequency(void) {
333+
return _oscillator_freq;
334+
}
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+
*/
341+
void Adafruit_PWMServoDriver::setOscillatorFrequency(uint32_t freq) {
342+
_oscillator_freq = freq;
343+
}
344+
345+
/******************* Low level I2C interface */
331346
uint8_t Adafruit_PWMServoDriver::read8(uint8_t addr) {
332347
_i2c->beginTransmission(_i2caddr);
333348
_i2c->write(addr);

0 commit comments

Comments
 (0)