Skip to content

Commit 5d3468f

Browse files
committed
doxy'd
1 parent 8583b97 commit 5d3468f

File tree

3 files changed

+97
-47
lines changed

3 files changed

+97
-47
lines changed

Adafruit_PWMServoDriver.cpp

Lines changed: 84 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
------> http://www.adafruit.com/products/815
66
77
These displays use I2C to communicate, 2 pins are required to
8-
interface. For Arduino UNOs, thats SCL -> Analog 5, SDA -> Analog 4
8+
interface.
99
1010
Adafruit invests time and resources providing this open source code,
1111
please support Adafruit and open-source hardware by purchasing
@@ -17,45 +17,72 @@
1717

1818
#include <Adafruit_PWMServoDriver.h>
1919
#include <Wire.h>
20+
2021
#if defined(ARDUINO_SAM_DUE)
21-
#define WIRE Wire1
22-
#else
23-
#define WIRE Wire
22+
#define Wire Wire1
2423
#endif
2524

2625

2726
// Set to true to print some debug messages, or false to disable them.
28-
#define ENABLE_DEBUG_OUTPUT false
27+
//#define ENABLE_DEBUG_OUTPUT
28+
2929

30+
/**************************************************************************/
31+
/*!
32+
@brief Instantiates a new PCA9685 PWM driver chip with the I2C address
33+
@param addr The 7-bit I2C address to locate this chip, default is 0x40
34+
*/
35+
/**************************************************************************/
3036
Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(uint8_t addr) {
3137
_i2caddr = addr;
3238
}
3339

40+
/**************************************************************************/
41+
/*!
42+
@brief Setups the I2C interface and hardware
43+
*/
44+
/**************************************************************************/
3445
void Adafruit_PWMServoDriver::begin(void) {
35-
WIRE.begin();
36-
reset();
46+
Wire.begin();
47+
reset();
3748
}
3849

3950

51+
/**************************************************************************/
52+
/*!
53+
@brief Sends a reset command to the PCA9685 chip over I2C
54+
*/
55+
/**************************************************************************/
4056
void Adafruit_PWMServoDriver::reset(void) {
41-
write8(PCA9685_MODE1, 0x0);
57+
write8(PCA9685_MODE1, 0x0);
4258
}
4359

60+
/**************************************************************************/
61+
/*!
62+
@brief Sets the PWM frequency for the entire chip, up to ~1.6 KHz
63+
@param freq Floating point frequency that we will attempt to match
64+
*/
65+
/**************************************************************************/
4466
void Adafruit_PWMServoDriver::setPWMFreq(float freq) {
45-
//Serial.print("Attempting to set freq ");
46-
//Serial.println(freq);
67+
#ifdef ENABLE_DEBUG_OUTPUT
68+
Serial.print("Attempting to set freq ");
69+
Serial.println(freq);
70+
#endif
71+
4772
freq *= 0.9; // Correct for overshoot in the frequency setting (see issue #11).
4873
float prescaleval = 25000000;
4974
prescaleval /= 4096;
5075
prescaleval /= freq;
5176
prescaleval -= 1;
52-
if (ENABLE_DEBUG_OUTPUT) {
53-
//Serial.print("Estimated pre-scale: "); Serial.println(prescaleval);
54-
}
77+
78+
#ifdef ENABLE_DEBUG_OUTPUT
79+
Serial.print("Estimated pre-scale: "); Serial.println(prescaleval);
80+
#endif
81+
5582
uint8_t prescale = floor(prescaleval + 0.5);
56-
if (ENABLE_DEBUG_OUTPUT) {
57-
//Serial.print("Final pre-scale: "); Serial.println(prescale);
58-
}
83+
#ifdef ENABLE_DEBUG_OUTPUT
84+
Serial.print("Final pre-scale: "); Serial.println(prescale);
85+
#endif
5986

6087
uint8_t oldmode = read8(PCA9685_MODE1);
6188
uint8_t newmode = (oldmode&0x7F) | 0x10; // sleep
@@ -65,24 +92,41 @@ void Adafruit_PWMServoDriver::setPWMFreq(float freq) {
6592
delay(5);
6693
write8(PCA9685_MODE1, oldmode | 0xa1); // This sets the MODE1 register to turn on auto increment.
6794
// This is why the beginTransmission below was not working.
68-
// Serial.print("Mode now 0x"); Serial.println(read8(PCA9685_MODE1), HEX);
95+
#ifdef ENABLE_DEBUG_OUTPUT
96+
Serial.print("Mode now 0x"); Serial.println(read8(PCA9685_MODE1), HEX);
97+
#endif
6998
}
7099

100+
/**************************************************************************/
101+
/*!
102+
@brief Sets the PWM output of one of the PCA9685 pins
103+
@param num One of the PWM output pins, from 0 to 15
104+
@param on At what point in the 4096-part cycle to turn the PWM output ON
105+
@param off At what point in the 4096-part cycle to turn the PWM output OFF
106+
*/
107+
/**************************************************************************/
71108
void Adafruit_PWMServoDriver::setPWM(uint8_t num, uint16_t on, uint16_t off) {
72-
//Serial.print("Setting PWM "); Serial.print(num); Serial.print(": "); Serial.print(on); Serial.print("->"); Serial.println(off);
73-
74-
WIRE.beginTransmission(_i2caddr);
75-
WIRE.write(LED0_ON_L+4*num);
76-
WIRE.write(on);
77-
WIRE.write(on>>8);
78-
WIRE.write(off);
79-
WIRE.write(off>>8);
80-
WIRE.endTransmission();
109+
#ifdef ENABLE_DEBUG_OUTPUT
110+
Serial.print("Setting PWM "); Serial.print(num); Serial.print(": "); Serial.print(on); Serial.print("->"); Serial.println(off);
111+
#endif
112+
113+
Wire.beginTransmission(_i2caddr);
114+
Wire.write(LED0_ON_L+4*num);
115+
Wire.write(on);
116+
Wire.write(on>>8);
117+
Wire.write(off);
118+
Wire.write(off>>8);
119+
Wire.endTransmission();
81120
}
82121

83-
// Sets pin without having to deal with on/off tick placement and properly handles
84-
// a zero value as completely off. Optional invert parameter supports inverting
85-
// the pulse for sinking to ground. Val should be a value from 0 to 4095 inclusive.
122+
/**************************************************************************/
123+
/*!
124+
@brief Helper to set pin PWM output. Sets pin without having to deal with on/off tick placement and properly handles a zero value as completely off. Optional invert parameter supports inverting the pulse for sinking to ground.
125+
@param num One of the PWM output pins, from 0 to 15
126+
@param val The number of ticks out of 4096 to be active, should be a value from 0 to 4095 inclusive.
127+
@param invert If true, inverts the output, defaults to 'false'
128+
*/
129+
/**************************************************************************/
86130
void Adafruit_PWMServoDriver::setPin(uint8_t num, uint16_t val, bool invert)
87131
{
88132
// Clamp value between 0 and 4095 inclusive.
@@ -115,18 +159,20 @@ void Adafruit_PWMServoDriver::setPin(uint8_t num, uint16_t val, bool invert)
115159
}
116160
}
117161

162+
/*******************************************************************************************/
163+
118164
uint8_t Adafruit_PWMServoDriver::read8(uint8_t addr) {
119-
WIRE.beginTransmission(_i2caddr);
120-
WIRE.write(addr);
121-
WIRE.endTransmission();
165+
Wire.beginTransmission(_i2caddr);
166+
Wire.write(addr);
167+
Wire.endTransmission();
122168

123-
WIRE.requestFrom((uint8_t)_i2caddr, (uint8_t)1);
124-
return WIRE.read();
169+
Wire.requestFrom((uint8_t)_i2caddr, (uint8_t)1);
170+
return Wire.read();
125171
}
126172

127173
void Adafruit_PWMServoDriver::write8(uint8_t addr, uint8_t d) {
128-
WIRE.beginTransmission(_i2caddr);
129-
WIRE.write(addr);
130-
WIRE.write(d);
131-
WIRE.endTransmission();
174+
Wire.beginTransmission(_i2caddr);
175+
Wire.write(addr);
176+
Wire.write(d);
177+
Wire.endTransmission();
132178
}

Adafruit_PWMServoDriver.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@
4242
#define ALLLED_OFF_L 0xFC
4343
#define ALLLED_OFF_H 0xFD
4444

45-
45+
/**************************************************************************/
46+
/*!
47+
@brief Class that stores state and functions for interacting with PCA9685 PWM chip
48+
*/
49+
/**************************************************************************/
4650
class Adafruit_PWMServoDriver {
4751
public:
4852
Adafruit_PWMServoDriver(uint8_t addr = 0x40);

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
This is a library for our Adafruit 16-channel PWM & Servo driver
1+
# Adafruit PCA9685 PWM Servo Driver Library [![Build Status](https://travis-ci.org/adafruit/Adafruit_PWMServoDriver.svg?branch=master)](https://travis-ci.org/adafruit/Adafruit_PWMServoDriver)
22

3-
Pick one up today in the adafruit shop!
4-
------> http://www.adafruit.com/products/815
3+
This is a library for our Adafruit 16-channel PWM & Servo driver, shield or FeatherWing
4+
5+
Pick one up today in the adafruit shop!
6+
* https://www.adafruit.com/products/815
7+
* https://www.adafruit.com/product/1411
8+
* https://www.adafruit.com/product/2928
59

610
These displays use I2C to communicate, 2 pins are required to
7-
interface. For Arduinos, thats SCL -> Analog 5, SDA -> Analog 4
11+
interface.
812

913
Adafruit invests time and resources providing this open source code,
1014
please support Adafruit and open-source hardware by purchasing
@@ -14,10 +18,6 @@ Written by Limor Fried/Ladyada for Adafruit Industries.
1418
BSD license, check license.txt for more information
1519
All text above must be included in any redistribution
1620

17-
To download. click the DOWNLOADS button in the top right corner, rename the uncompressed folder Adafruit_PWMServoDriver. Check that the Adafruit_PWMServoDriver folder contains Adafruit_PWMServoDriver.cpp and Adafruit_PWMServoDriver.h
18-
19-
Place the Adafruit_PWMServoDriver library folder your <arduinosketchfolder>/libraries/ folder. You may need to create the libraries subfolder if its your first library. Restart the IDE.
20-
2121
<!-- START COMPATIBILITY TABLE -->
2222

2323
## Compatibility

0 commit comments

Comments
 (0)