Skip to content

Commit 9b33900

Browse files
authored
Merge pull request #45 from hoffmannjan/master
doxygen + readme
2 parents 0c909db + 12c4926 commit 9b33900

File tree

4 files changed

+139
-145
lines changed

4 files changed

+139
-145
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# osx
2+
.DS_Store
3+
4+
# doxygen
5+
Doxyfile*
6+
doxygen_sqlite3.db
7+
html
8+
*.tmp

Adafruit_PWMServoDriver.cpp

Lines changed: 92 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,182 +1,170 @@
1-
/***************************************************
2-
This is a library for our Adafruit 16-channel PWM & Servo driver
3-
4-
Pick one up today in the adafruit shop!
5-
------> http://www.adafruit.com/products/815
6-
7-
These displays use I2C to communicate, 2 pins are required to
8-
interface.
9-
10-
Adafruit invests time and resources providing this open source code,
11-
please support Adafruit and open-source hardware by purchasing
12-
products from Adafruit!
13-
14-
Written by Limor Fried/Ladyada for Adafruit Industries.
15-
BSD license, all text above must be included in any redistribution
16-
****************************************************/
1+
/*!
2+
* @file Adafruit_PWMServoDriver.cpp
3+
*
4+
* @mainpage Adafruit 16-channel PWM & Servo driver
5+
*
6+
* @section intro_sec Introduction
7+
*
8+
* This is a library for the 16-channel PWM & Servo driver.
9+
*
10+
* Designed specifically to work with the Adafruit PWM & Servo driver.
11+
*
12+
* Pick one up today in the adafruit shop!
13+
* ------> https://www.adafruit.com/product/815
14+
*
15+
* These displays use I2C to communicate, 2 pins are required to interface.
16+
*
17+
* Adafruit invests time and resources providing this open source code,
18+
* please support Adafruit andopen-source hardware by purchasing products
19+
* from Adafruit!
20+
*
21+
* @section author Author
22+
*
23+
* Limor Fried/Ladyada (Adafruit Industries).
24+
*
25+
* @section license License
26+
*
27+
* BSD license, all text above must be included in any redistribution
28+
*/
1729

1830
#include "Adafruit_PWMServoDriver.h"
1931
#include <Wire.h>
2032

21-
// Set to true to print some debug messages, or false to disable them.
22-
//#define ENABLE_DEBUG_OUTPUT
23-
24-
25-
/**************************************************************************/
26-
/*!
27-
@brief Instantiates a new PCA9685 PWM driver chip with the I2C address on the Wire interface. On Due we use Wire1 since its the interface on the 'default' I2C pins.
28-
@param addr The 7-bit I2C address to locate this chip, default is 0x40
29-
*/
30-
/**************************************************************************/
31-
Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(uint8_t addr) {
32-
_i2caddr = addr;
33-
34-
#if defined(ARDUINO_SAM_DUE)
35-
_i2c = &Wire1;
36-
#else
37-
_i2c = &Wire;
38-
#endif
39-
}
40-
41-
/**************************************************************************/
42-
/*!
43-
@brief Instantiates a new PCA9685 PWM driver chip with the I2C address on a TwoWire interface
44-
@param i2c A pointer to a 'Wire' compatible object that we'll use to communicate with
45-
@param addr The 7-bit I2C address to locate this chip, default is 0x40
46-
*/
47-
/**************************************************************************/
33+
/*!
34+
* @brief Instantiates a new PCA9685 PWM driver chip with the I2C address on a
35+
* TwoWire interface
36+
* @param i2c A pointer to a 'Wire' compatible object that we'll use to
37+
* communicate with
38+
* @param addr The 7-bit I2C address to locate this chip, default is 0x40
39+
*/
4840
Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(TwoWire *i2c, uint8_t addr) {
4941
_i2c = i2c;
5042
_i2caddr = addr;
5143
}
5244

53-
/**************************************************************************/
54-
/*!
55-
@brief Setups the I2C interface and hardware
56-
*/
57-
/**************************************************************************/
45+
/*!
46+
* @brief Setups the I2C interface and hardware
47+
*/
5848
void Adafruit_PWMServoDriver::begin(void) {
5949
_i2c->begin();
6050
reset();
6151
// set a default frequency
6252
setPWMFreq(1000);
6353
}
6454

65-
66-
/**************************************************************************/
67-
/*!
68-
@brief Sends a reset command to the PCA9685 chip over I2C
69-
*/
70-
/**************************************************************************/
55+
/*!
56+
* @brief Sends a reset command to the PCA9685 chip over I2C
57+
*/
7158
void Adafruit_PWMServoDriver::reset(void) {
7259
write8(PCA9685_MODE1, 0x80);
7360
delay(10);
7461
}
7562

76-
/**************************************************************************/
77-
/*!
78-
@brief Sets the PWM frequency for the entire chip, up to ~1.6 KHz
79-
@param freq Floating point frequency that we will attempt to match
80-
*/
81-
/**************************************************************************/
63+
/*!
64+
* @brief Sets the PWM frequency for the entire chip, up to ~1.6 KHz
65+
* @param freq Floating point frequency that we will attempt to match
66+
*/
8267
void Adafruit_PWMServoDriver::setPWMFreq(float freq) {
8368
#ifdef ENABLE_DEBUG_OUTPUT
8469
Serial.print("Attempting to set freq ");
8570
Serial.println(freq);
8671
#endif
8772

88-
freq *= 0.9; // Correct for overshoot in the frequency setting (see issue #11).
73+
freq *=
74+
0.9; // Correct for overshoot in the frequency setting (see issue #11).
8975
float prescaleval = 25000000;
9076
prescaleval /= 4096;
9177
prescaleval /= freq;
9278
prescaleval -= 1;
9379

9480
#ifdef ENABLE_DEBUG_OUTPUT
95-
Serial.print("Estimated pre-scale: "); Serial.println(prescaleval);
81+
Serial.print("Estimated pre-scale: ");
82+
Serial.println(prescaleval);
9683
#endif
9784

9885
uint8_t prescale = floor(prescaleval + 0.5);
9986
#ifdef ENABLE_DEBUG_OUTPUT
100-
Serial.print("Final pre-scale: "); Serial.println(prescale);
87+
Serial.print("Final pre-scale: ");
88+
Serial.println(prescale);
10189
#endif
102-
90+
10391
uint8_t oldmode = read8(PCA9685_MODE1);
104-
uint8_t newmode = (oldmode&0x7F) | 0x10; // sleep
105-
write8(PCA9685_MODE1, newmode); // go to sleep
106-
write8(PCA9685_PRESCALE, prescale); // set the prescaler
92+
uint8_t newmode = (oldmode & 0x7F) | 0x10; // sleep
93+
write8(PCA9685_MODE1, newmode); // go to sleep
94+
write8(PCA9685_PRESCALE, prescale); // set the prescaler
10795
write8(PCA9685_MODE1, oldmode);
10896
delay(5);
109-
write8(PCA9685_MODE1, oldmode | 0xa0); // This sets the MODE1 register to turn on auto increment.
97+
write8(PCA9685_MODE1,
98+
oldmode |
99+
0xa0); // This sets the MODE1 register to turn on auto increment.
110100

111101
#ifdef ENABLE_DEBUG_OUTPUT
112-
Serial.print("Mode now 0x"); Serial.println(read8(PCA9685_MODE1), HEX);
102+
Serial.print("Mode now 0x");
103+
Serial.println(read8(PCA9685_MODE1), HEX);
113104
#endif
114105
}
115106

116-
/**************************************************************************/
117-
/*!
118-
@brief Sets the PWM output of one of the PCA9685 pins
119-
@param num One of the PWM output pins, from 0 to 15
120-
@param on At what point in the 4096-part cycle to turn the PWM output ON
121-
@param off At what point in the 4096-part cycle to turn the PWM output OFF
122-
*/
123-
/**************************************************************************/
107+
/*!
108+
* @brief Sets the PWM output of one of the PCA9685 pins
109+
* @param num One of the PWM output pins, from 0 to 15
110+
* @param on At what point in the 4096-part cycle to turn the PWM output ON
111+
* @param off At what point in the 4096-part cycle to turn the PWM output OFF
112+
*/
124113
void Adafruit_PWMServoDriver::setPWM(uint8_t num, uint16_t on, uint16_t off) {
125114
#ifdef ENABLE_DEBUG_OUTPUT
126-
Serial.print("Setting PWM "); Serial.print(num); Serial.print(": "); Serial.print(on); Serial.print("->"); Serial.println(off);
115+
Serial.print("Setting PWM ");
116+
Serial.print(num);
117+
Serial.print(": ");
118+
Serial.print(on);
119+
Serial.print("->");
120+
Serial.println(off);
127121
#endif
128122

129123
_i2c->beginTransmission(_i2caddr);
130-
_i2c->write(LED0_ON_L+4*num);
124+
_i2c->write(LED0_ON_L + 4 * num);
131125
_i2c->write(on);
132-
_i2c->write(on>>8);
126+
_i2c->write(on >> 8);
133127
_i2c->write(off);
134-
_i2c->write(off>>8);
128+
_i2c->write(off >> 8);
135129
_i2c->endTransmission();
136130
}
137131

138-
/**************************************************************************/
139-
/*!
140-
@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 and 4095 as completely on. Optional invert parameter supports inverting the pulse for sinking to ground.
141-
@param num One of the PWM output pins, from 0 to 15
142-
@param val The number of ticks out of 4096 to be active, should be a value from 0 to 4095 inclusive.
143-
@param invert If true, inverts the output, defaults to 'false'
144-
*/
145-
/**************************************************************************/
146-
void Adafruit_PWMServoDriver::setPin(uint8_t num, uint16_t val, bool invert)
147-
{
132+
/*!
133+
* @brief Helper to set pin PWM output. Sets pin without having to deal with
134+
* on/off tick placement and properly handles a zero value as completely off and
135+
* 4095 as completely on. Optional invert parameter supports inverting the
136+
* pulse for sinking to ground.
137+
* @param num One of the PWM output pins, from 0 to 15
138+
* @param val The number of ticks out of 4096 to be active, should be a value
139+
* from 0 to 4095 inclusive.
140+
* @param invert If true, inverts the output, defaults to 'false'
141+
*/
142+
void Adafruit_PWMServoDriver::setPin(uint8_t num, uint16_t val, bool invert) {
148143
// Clamp value between 0 and 4095 inclusive.
149144
val = min(val, (uint16_t)4095);
150145
if (invert) {
151146
if (val == 0) {
152147
// Special value for signal fully on.
153148
setPWM(num, 4096, 0);
154-
}
155-
else if (val == 4095) {
149+
} else if (val == 4095) {
156150
// Special value for signal fully off.
157151
setPWM(num, 0, 4096);
152+
} else {
153+
setPWM(num, 0, 4095 - val);
158154
}
159-
else {
160-
setPWM(num, 0, 4095-val);
161-
}
162-
}
163-
else {
155+
} else {
164156
if (val == 4095) {
165157
// Special value for signal fully on.
166158
setPWM(num, 4096, 0);
167-
}
168-
else if (val == 0) {
159+
} else if (val == 0) {
169160
// Special value for signal fully off.
170161
setPWM(num, 0, 4096);
171-
}
172-
else {
162+
} else {
173163
setPWM(num, 0, val);
174164
}
175165
}
176166
}
177167

178-
/*******************************************************************************************/
179-
180168
uint8_t Adafruit_PWMServoDriver::read8(uint8_t addr) {
181169
_i2c->beginTransmission(_i2caddr);
182170
_i2c->write(addr);

Adafruit_PWMServoDriver.h

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,54 @@
1-
/***************************************************
2-
This is a library for our Adafruit 16-channel PWM & Servo driver
3-
4-
Pick one up today in the adafruit shop!
5-
------> http://www.adafruit.com/products/815
6-
7-
These displays use I2C to communicate, 2 pins are required to
8-
interface. For Arduino UNOs, thats SCL -> Analog 5, SDA -> Analog 4
9-
10-
Adafruit invests time and resources providing this open source code,
11-
please support Adafruit and open-source hardware by purchasing
12-
products from Adafruit!
13-
14-
Written by Limor Fried/Ladyada for Adafruit Industries.
15-
BSD license, all text above must be included in any redistribution
16-
****************************************************/
1+
/*!
2+
* @file Adafruit_PWMServoDriver.h
3+
*
4+
* This is a library for our Adafruit 16-channel PWM & Servo driver.
5+
*
6+
* Designed specifically to work with the Adafruit 16-channel PWM & Servo driver.
7+
*
8+
* Pick one up today in the adafruit shop!
9+
* ------> https://www.adafruit.com/product/815
10+
*
11+
* These driver use I2C to communicate, 2 pins are required to interface.
12+
* For Arduino UNOs, thats SCL -> Analog 5, SDA -> Analog 4.
13+
*
14+
* Adafruit invests time and resources providing this open source code,
15+
* please support Adafruit andopen-source hardware by purchasing products
16+
* from Adafruit!
17+
*
18+
* Limor Fried/Ladyada (Adafruit Industries).
19+
*
20+
* BSD license, all text above must be included in any redistribution
21+
*/
1722

1823
#ifndef _ADAFRUIT_PWMServoDriver_H
1924
#define _ADAFRUIT_PWMServoDriver_H
2025

21-
#if ARDUINO >= 100
22-
#include <Arduino.h>
23-
#else
24-
#include <WProgram.h>
25-
#endif
26+
#include <Arduino.h>
2627
#include <Wire.h>
2728

28-
#define PCA9685_SUBADR1 0x2
29-
#define PCA9685_SUBADR2 0x3
30-
#define PCA9685_SUBADR3 0x4
29+
#define PCA9685_SUBADR1 0x2 /**< i2c bus address 1 */
30+
#define PCA9685_SUBADR2 0x3 /**< i2c bus address 2 */
31+
#define PCA9685_SUBADR3 0x4 /**< i2c bus address 3 */
3132

32-
#define PCA9685_MODE1 0x0
33-
#define PCA9685_PRESCALE 0xFE
33+
#define PCA9685_MODE1 0x0 /**< Mode Register 1 */
34+
#define PCA9685_PRESCALE 0xFE /**< Prescaler for PWM output frequency */
3435

35-
#define LED0_ON_L 0x6
36-
#define LED0_ON_H 0x7
37-
#define LED0_OFF_L 0x8
38-
#define LED0_OFF_H 0x9
36+
#define LED0_ON_L 0x6 /**< LED0 output and brightness control byte 0 */
37+
#define LED0_ON_H 0x7 /**< LED0 output and brightness control byte 1 */
38+
#define LED0_OFF_L 0x8 /**< LED0 output and brightness control byte 2 */
39+
#define LED0_OFF_H 0x9 /**< LED0 output and brightness control byte 3 */
3940

40-
#define ALLLED_ON_L 0xFA
41-
#define ALLLED_ON_H 0xFB
42-
#define ALLLED_OFF_L 0xFC
43-
#define ALLLED_OFF_H 0xFD
41+
#define ALLLED_ON_L 0xFA /**< load all the LEDn_ON registers, byte 0 */
42+
#define ALLLED_ON_H 0xFB /**< load all the LEDn_ON registers, byte 1 */
43+
#define ALLLED_OFF_L 0xFC /**< load all the LEDn_OFF registers, byte 0 */
44+
#define ALLLED_OFF_H 0xFD /**< load all the LEDn_OFF registers, byte 1 */
4445

45-
/**************************************************************************/
4646
/*!
47-
@brief Class that stores state and functions for interacting with PCA9685 PWM chip
48-
*/
49-
/**************************************************************************/
47+
* @brief Class that stores state and functions for interacting with PCA9685 PWM chip
48+
*/
5049
class Adafruit_PWMServoDriver {
5150
public:
52-
Adafruit_PWMServoDriver(uint8_t addr = 0x40);
53-
Adafruit_PWMServoDriver(TwoWire *I2C, uint8_t addr = 0x40);
51+
Adafruit_PWMServoDriver(TwoWire *I2C = &Wire, uint8_t addr = 0x40);
5452
void begin(void);
5553
void reset(void);
5654
void setPWMFreq(float freq);

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This is a library for our Adafruit 16-channel PWM & Servo driver, shield or FeatherWing
44

5-
<img src="https://cdn-shop.adafruit.com/970x728/815-04.jpg" height="300"/>
5+
<a href="https://www.adafruit.com/products/815"><img src="https://cdn-shop.adafruit.com/970x728/815-04.jpg" height="300"/></a>
66

77
Pick one up today in the adafruit shop!
88
* https://www.adafruit.com/products/815

0 commit comments

Comments
 (0)