Skip to content

Commit

Permalink
Merge pull request #1 from adafruit/master
Browse files Browse the repository at this point in the history
Local merge
  • Loading branch information
Bolukan authored Nov 3, 2019
2 parents bb7c39a + ddbc2da commit 83d42b9
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 120 deletions.
22 changes: 17 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
language: c
sudo: false

# Blacklist
branches:
except:
- gh-pages
cache:
directories:
- ~/arduino_ide
- ~/.arduino15/packages/
git:
depth: false
quiet: true

env:
global:
- PRETTYNAME="Adafruit PCA9685 PWM Servo Driver Arduino Library"

addons:
apt:
sources:
- llvm-toolchain-trusty-5.0
- key_url: 'http://apt.llvm.org/llvm-snapshot.gpg.key'
packages:
- clang-format-5.0

before_install:
- source <(curl -SLs https://raw.githubusercontent.com/adafruit/travis-ci-arduino/master/install.sh)
- curl -SLs https://raw.githubusercontent.com/adafruit/travis-ci-arduino/master/run-clang-format.py > run-clang-format.py

script:
- python run-clang-format.py -r .
- build_main_platforms

# Generate and deploy documentation
Expand Down
143 changes: 79 additions & 64 deletions Adafruit_PWMServoDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@
#include "Adafruit_PWMServoDriver.h"
#include <Wire.h>

//#define ENABLE_DEBUG_OUTPUT

/*!
* @brief Instantiates a new PCA9685 PWM driver chip with the I2C address on a
* TwoWire interface
*/
Adafruit_PWMServoDriver::Adafruit_PWMServoDriver():
_i2caddr(PCA9685_I2C_ADDRESS), _i2c(&Wire) {
}
Adafruit_PWMServoDriver::Adafruit_PWMServoDriver()
: _i2caddr(PCA9685_I2C_ADDRESS), _i2c(&Wire) {}

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

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

/*!
* @brief Setups the I2C interface and hardware
Expand All @@ -72,6 +72,8 @@ void Adafruit_PWMServoDriver::begin(uint8_t prescale) {
// set a default frequency
setPWMFreq(1000);
}
// set the default internal frequency
setOscillatorFrequency(FREQUENCY_OSCILLATOR);
}

/*!
Expand Down Expand Up @@ -146,21 +148,17 @@ void Adafruit_PWMServoDriver::setPWMFreq(float freq) {
Serial.println(freq);
#endif
// Range output modulation frequency is dependant on oscillator
if (freq < 1) freq = 1;
if (freq > 3500) freq = 3500; // Datasheet limit is 3052=50MHz/(4*4096)
/*
freq *= 0.9; // Correct for overshoot in the frequency setting (see issue #11)
float prescaleval = FREQUENCY_OSCILLATOR;
*/
uint32_t prescaleval = FREQUENCY_CALIBRATED;
prescaleval /= freq; // required output modulation frequency
// rounding to nearest number is equal to adding 0,5 and floor to nearest number
prescaleval += 2048;
prescaleval /= 4096;
prescaleval -= 1;
if (prescaleval < PCA9685_PRESCALE_MIN) prescaleval = PCA9685_PRESCALE_MIN;
if (prescaleval > PCA9685_PRESCALE_MAX) prescaleval = PCA9685_PRESCALE_MAX;
uint8_t prescale = (uint8_t) prescaleval;
if (freq < 1)
freq = 1;
if (freq > 3500)
freq = 3500; // Datasheet limit is 3052=50MHz/(4*4096)

float prescaleval = ((_oscillator_freq / (freq * 4096.0)) + 0.5) - 1;
if (prescaleval < PCA9685_PRESCALE_MIN)
prescaleval = PCA9685_PRESCALE_MIN;
if (prescaleval > PCA9685_PRESCALE_MAX)
prescaleval = PCA9685_PRESCALE_MAX;
uint8_t prescale = (uint8_t)prescaleval;

#ifdef ENABLE_DEBUG_OUTPUT
Serial.print("Final pre-scale: ");
Expand All @@ -183,22 +181,21 @@ void Adafruit_PWMServoDriver::setPWMFreq(float freq) {
}

/*!
* @brief Sets the output mode of the PCA9685 to either
* open drain or push pull / totempole.
* @brief Sets the output mode of the PCA9685 to either
* open drain or push pull / totempole.
* Warning: LEDs with integrated zener diodes should
* only be driven in open drain mode.
* @param totempole Totempole if true, open drain if false.
* only be driven in open drain mode.
* @param totempole Totempole if true, open drain if false.
*/
void Adafruit_PWMServoDriver::setOutputMode(bool totempole) {
uint8_t oldmode = read8(PCA9685_MODE2);
void Adafruit_PWMServoDriver::setOutputMode(bool totempole) {
uint8_t oldmode = read8(PCA9685_MODE2);
uint8_t newmode;
if (totempole) {
newmode = oldmode | MODE2_OUTDRV;
}
else {
} else {
newmode = oldmode & ~MODE2_OUTDRV;
}
write8(PCA9685_MODE2, newmode);
write8(PCA9685_MODE2, newmode);
#ifdef ENABLE_DEBUG_OUTPUT
Serial.print("Setting output mode: ");
Serial.print(totempole ? "totempole" : "open drain");
Expand All @@ -211,8 +208,7 @@ void Adafruit_PWMServoDriver::setOutputMode(bool totempole) {
* @brief Reads set Prescale from PCA9685
* @return prescale value
*/
uint8_t Adafruit_PWMServoDriver::readPrescale(void)
{
uint8_t Adafruit_PWMServoDriver::readPrescale(void) {
return read8(PCA9685_PRESCALE);
}

Expand Down Expand Up @@ -288,55 +284,74 @@ void Adafruit_PWMServoDriver::setPin(uint8_t num, uint16_t val, bool invert) {
}

/*!
* @brief Sets the PWM output of one of the PCA9685 pins based on the input microseconds, output is not precise
* @brief Sets the PWM output of one of the PCA9685 pins based on the input
* microseconds, output is not precise
* @param num One of the PWM output pins, from 0 to 15
* @param Microseconds The number of Microseconds to turn the PWM output ON
*/
void Adafruit_PWMServoDriver::writeMicroseconds(uint8_t num, uint16_t Microseconds) {
#ifdef ENABLE_DEBUG_OUTPUT
void Adafruit_PWMServoDriver::writeMicroseconds(uint8_t num,
uint16_t Microseconds) {
#ifdef ENABLE_DEBUG_OUTPUT
Serial.print("Setting PWM Via Microseconds on output");
Serial.print(num);
Serial.print(": ");
Serial.print(Microseconds);
Serial.println("->");
#endif
#endif

double pulse = Microseconds;
double pulselength;
pulselength = 1000000; // 1,000,000 us per second
pulselength = 1000000; // 1,000,000 us per second

// Read prescale
uint16_t prescale = Adafruit_PWMServoDriver::readPrescale();

#ifdef ENABLE_DEBUG_OUTPUT
Serial.print(prescale);
Serial.println(" PCA9685 chip prescale");
#endif

// Read prescale and convert to frequency
double prescale = Adafruit_PWMServoDriver::readPrescale();
// Calculate the pulse for PWM based on Equation 1 from the datasheet section
// 7.3.5
prescale += 1;
uint32_t freq = 25000000; // Chip frequency is 25MHz
freq /= prescale;
freq /= 4096; // 12 bits of resolution

#ifdef ENABLE_DEBUG_OUTPUT
Serial.print(freq); Serial.println(" Calculated PCA9685 chip PWM Frequency");
#endif

pulselength /= freq; // us per period from PCA9685 chip PWM Frequency using prescale reverse frequency calc

#ifdef ENABLE_DEBUG_OUTPUT
Serial.print(pulselength); Serial.println(" us per period");
#endif

pulselength /= 4096; // 12 bits of resolution

#ifdef ENABLE_DEBUG_OUTPUT
Serial.print(pulselength); Serial.println(" us per bit");
#endif
pulselength *= prescale;
pulselength /= _oscillator_freq;

#ifdef ENABLE_DEBUG_OUTPUT
Serial.print(pulselength);
Serial.println(" us per bit");
#endif

pulse /= pulselength;

#ifdef ENABLE_DEBUG_OUTPUT
Serial.print(pulse);Serial.println(" pulse for PWM");
#endif
#ifdef ENABLE_DEBUG_OUTPUT
Serial.print(pulse);
Serial.println(" pulse for PWM");
#endif

Adafruit_PWMServoDriver::setPWM(num, 0, pulse);
}

/*!
* @brief Getter for the internally tracked oscillator used for freq
* calculations
* @returns The frequency the PCA9685 thinks it is running at (it cannot
* introspect)
*/
uint32_t Adafruit_PWMServoDriver::getOscillatorFrequency(void) {
return _oscillator_freq;
}

/*!
* @brief Setter for the internally tracked oscillator used for freq
* calculations
* @param freq The frequency the PCA9685 should use for frequency calculations
*/
void Adafruit_PWMServoDriver::setOscillatorFrequency(uint32_t freq) {
_oscillator_freq = freq;
}

/******************* Low level I2C interface */
uint8_t Adafruit_PWMServoDriver::read8(uint8_t addr) {
_i2c->beginTransmission(_i2caddr);
_i2c->write(addr);
Expand Down
90 changes: 47 additions & 43 deletions Adafruit_PWMServoDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
*
* This is a library for our Adafruit 16-channel PWM & Servo driver.
*
* Designed specifically to work with the Adafruit 16-channel PWM & Servo driver.
* Designed specifically to work with the Adafruit 16-channel PWM & Servo
* driver.
*
* Pick one up today in the adafruit shop!
* ------> https://www.adafruit.com/product/815
Expand All @@ -26,57 +27,56 @@
#include <Wire.h>

// REGISTER ADDRESSES
#define PCA9685_MODE1 0x00 /**< Mode Register 1 */
#define PCA9685_MODE2 0x01 /**< Mode Register 2 */
#define PCA9685_SUBADR1 0x02 /**< I2C-bus subaddress 1 */
#define PCA9685_SUBADR2 0x03 /**< I2C-bus subaddress 2 */
#define PCA9685_SUBADR3 0x04 /**< I2C-bus subaddress 3 */
#define PCA9685_ALLCALLADR 0x05 /**< LED All Call I2C-bus address */
#define PCA9685_LED0_ON_L 0x06 /**< LED0 output and brightness control byte 0 */
#define PCA9685_LED0_ON_H 0x07 /**< LED0 output and brightness control byte 1 */
#define PCA9685_LED0_OFF_L 0x08 /**< LED0 output and brightness control byte 2 */
#define PCA9685_LED0_OFF_H 0x09 /**< LED0 output and brightness control byte 3 */
#define PCA9685_MODE1 0x00 /**< Mode Register 1 */
#define PCA9685_MODE2 0x01 /**< Mode Register 2 */
#define PCA9685_SUBADR1 0x02 /**< I2C-bus subaddress 1 */
#define PCA9685_SUBADR2 0x03 /**< I2C-bus subaddress 2 */
#define PCA9685_SUBADR3 0x04 /**< I2C-bus subaddress 3 */
#define PCA9685_ALLCALLADR 0x05 /**< LED All Call I2C-bus address */
#define PCA9685_LED0_ON_L 0x06 /**< LED0 on tick, low byte*/
#define PCA9685_LED0_ON_H 0x07 /**< LED0 on tick, high byte*/
#define PCA9685_LED0_OFF_L 0x08 /**< LED0 off tick, low byte */
#define PCA9685_LED0_OFF_H 0x09 /**< LED0 off tick, high byte */
// etc all 16: LED15_OFF_H 0x45
#define PCA9685_ALLLED_ON_L 0xFA /**< load all the LEDn_ON registers, byte 0 */
#define PCA9685_ALLLED_ON_H 0xFB /**< load all the LEDn_ON registers, byte 1 */
#define PCA9685_ALLLED_OFF_L 0xFC /**< load all the LEDn_OFF registers, byte 0 */
#define PCA9685_ALLLED_OFF_H 0xFD /**< load all the LEDn_OFF registers, byte 1 */
#define PCA9685_PRESCALE 0xFE /**< Prescaler for PWM output frequency */
#define PCA9685_TESTMODE 0xFF /**< defines the test mode to be entered */
#define PCA9685_ALLLED_ON_L 0xFA /**< load all the LEDn_ON registers, low */
#define PCA9685_ALLLED_ON_H 0xFB /**< load all the LEDn_ON registers, high */
#define PCA9685_ALLLED_OFF_L 0xFC /**< load all the LEDn_OFF registers, low */
#define PCA9685_ALLLED_OFF_H 0xFD /**< load all the LEDn_OFF registers,high */
#define PCA9685_PRESCALE 0xFE /**< Prescaler for PWM output frequency */
#define PCA9685_TESTMODE 0xFF /**< defines the test mode to be entered */

// MODE1 bits
#define MODE1_ALLCAL 0x01 /**< respond to LED All Call I2C-bus address */
#define MODE1_SUB3 0x02 /**< respond to I2C-bus subaddress 3 */
#define MODE1_SUB2 0x04 /**< respond to I2C-bus subaddress 2 */
#define MODE1_SUB1 0x08 /**< respond to I2C-bus subaddress 1 */
#define MODE1_SLEEP 0x10 /**< Low power mode. Oscillator off */
#define MODE1_AI 0x20 /**< Auto-Increment enabled */
#define MODE1_EXTCLK 0x40 /**< Use EXTCLK pin clock */
#define MODE1_RESTART 0x80 /**< Restart enabled */
#define MODE1_ALLCAL 0x01 /**< respond to LED All Call I2C-bus address */
#define MODE1_SUB3 0x02 /**< respond to I2C-bus subaddress 3 */
#define MODE1_SUB2 0x04 /**< respond to I2C-bus subaddress 2 */
#define MODE1_SUB1 0x08 /**< respond to I2C-bus subaddress 1 */
#define MODE1_SLEEP 0x10 /**< Low power mode. Oscillator off */
#define MODE1_AI 0x20 /**< Auto-Increment enabled */
#define MODE1_EXTCLK 0x40 /**< Use EXTCLK pin clock */
#define MODE1_RESTART 0x80 /**< Restart enabled */
// MODE2 bits
#define MODE2_OUTNE_0 0x01 /**< Active LOW output enable input */
#define MODE2_OUTNE_1 0x02 /**< Active LOW output enable input - high impedience */
#define MODE2_OUTDRV 0x04 /**< totem pole structure vs open-drain */
#define MODE2_OCH 0x08 /**< Outputs change on ACK vs STOP */
#define MODE2_INVRT 0x10 /**< Output logic state inverted */
#define MODE2_OUTNE_0 0x01 /**< Active LOW output enable input */
#define MODE2_OUTNE_1 \
0x02 /**< Active LOW output enable input - high impedience */
#define MODE2_OUTDRV 0x04 /**< totem pole structure vs open-drain */
#define MODE2_OCH 0x08 /**< Outputs change on ACK vs STOP */
#define MODE2_INVRT 0x10 /**< Output logic state inverted */

#define PCA9685_I2C_ADDRESS 0x40 /**< Default PCA9685 I2C Slave Address */
#define PCA9685_I2C_ADDRESS 0x40 /**< Default PCA9685 I2C Slave Address */
#define FREQUENCY_OSCILLATOR 25000000 /**< Int. osc. frequency in datasheet */

#define FREQUENCY_OSCILLATOR 25000000 /**< Oscillator frequency cf satasheet */
#define FREQUENCY_CALIBRATED 26075000 /**< Oscillator frequency measured at 104.3% */
#define FREQUENCY_LEGACY 27777778 /**< Oscillator frequency using freq /= 0,9 */

#define PCA9685_PRESCALE_MIN 3 /**< minimum prescale value */
#define PCA9685_PRESCALE_MIN 3 /**< minimum prescale value */
#define PCA9685_PRESCALE_MAX 255 /**< maximum prescale value */

/*!
* @brief Class that stores state and functions for interacting with PCA9685 PWM chip
/*!
* @brief Class that stores state and functions for interacting with PCA9685
* PWM chip
*/
class Adafruit_PWMServoDriver {
public:
public:
Adafruit_PWMServoDriver();
Adafruit_PWMServoDriver(const uint8_t addr);
Adafruit_PWMServoDriver(const uint8_t addr, TwoWire& i2c);
Adafruit_PWMServoDriver(const uint8_t addr, TwoWire &i2c);
void begin(uint8_t prescale = 0);
void reset();
void sleep();
Expand All @@ -87,14 +87,18 @@ class Adafruit_PWMServoDriver {
void setOutputMode(bool totempole);
uint8_t getPWM(uint8_t num);
void setPWM(uint8_t num, uint16_t on, uint16_t off);
void setPin(uint8_t num, uint16_t val, bool invert=false);
void setPin(uint8_t num, uint16_t val, bool invert = false);
uint8_t readPrescale(void);
void writeMicroseconds(uint8_t num, uint16_t Microseconds);

private:
void setOscillatorFrequency(uint32_t freq);
uint32_t getOscillatorFrequency(void);

private:
uint8_t _i2caddr;
TwoWire* _i2c;
TwoWire *_i2c;

uint32_t _oscillator_freq;
uint8_t read8(uint8_t addr);
void write8(uint8_t addr, uint8_t d);
};
Expand Down
Loading

0 comments on commit 83d42b9

Please sign in to comment.