Skip to content

Commit a5068e0

Browse files
committed
add optional Wire, clean up and modernize examples
1 parent bd6961d commit a5068e0

File tree

4 files changed

+56
-50
lines changed

4 files changed

+56
-50
lines changed

Adafruit_PWMServoDriver.cpp

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,36 @@
1818
#include <Adafruit_PWMServoDriver.h>
1919
#include <Wire.h>
2020

21-
#if defined(ARDUINO_SAM_DUE)
22-
#define Wire Wire1
23-
#endif
24-
25-
2621
// Set to true to print some debug messages, or false to disable them.
2722
//#define ENABLE_DEBUG_OUTPUT
2823

2924

3025
/**************************************************************************/
3126
/*!
32-
@brief Instantiates a new PCA9685 PWM driver chip with the I2C address
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.
3328
@param addr The 7-bit I2C address to locate this chip, default is 0x40
3429
*/
3530
/**************************************************************************/
3631
Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(uint8_t addr) {
3732
_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+
/**************************************************************************/
48+
Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(TwoWire *i2c, uint8_t addr) {
49+
_i2c = i2c;
50+
_i2caddr = addr;
3851
}
3952

4053
/**************************************************************************/
@@ -43,7 +56,7 @@ Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(uint8_t addr) {
4356
*/
4457
/**************************************************************************/
4558
void Adafruit_PWMServoDriver::begin(void) {
46-
Wire.begin();
59+
_i2c->begin();
4760
reset();
4861
}
4962

@@ -110,13 +123,13 @@ void Adafruit_PWMServoDriver::setPWM(uint8_t num, uint16_t on, uint16_t off) {
110123
Serial.print("Setting PWM "); Serial.print(num); Serial.print(": "); Serial.print(on); Serial.print("->"); Serial.println(off);
111124
#endif
112125

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();
126+
_i2c->beginTransmission(_i2caddr);
127+
_i2c->write(LED0_ON_L+4*num);
128+
_i2c->write(on);
129+
_i2c->write(on>>8);
130+
_i2c->write(off);
131+
_i2c->write(off>>8);
132+
_i2c->endTransmission();
120133
}
121134

122135
/**************************************************************************/
@@ -130,7 +143,7 @@ void Adafruit_PWMServoDriver::setPWM(uint8_t num, uint16_t on, uint16_t off) {
130143
void Adafruit_PWMServoDriver::setPin(uint8_t num, uint16_t val, bool invert)
131144
{
132145
// Clamp value between 0 and 4095 inclusive.
133-
val = min(val, 4095);
146+
val = min(val, (uint16_t)4095);
134147
if (invert) {
135148
if (val == 0) {
136149
// Special value for signal fully on.
@@ -162,17 +175,17 @@ void Adafruit_PWMServoDriver::setPin(uint8_t num, uint16_t val, bool invert)
162175
/*******************************************************************************************/
163176

164177
uint8_t Adafruit_PWMServoDriver::read8(uint8_t addr) {
165-
Wire.beginTransmission(_i2caddr);
166-
Wire.write(addr);
167-
Wire.endTransmission();
178+
_i2c->beginTransmission(_i2caddr);
179+
_i2c->write(addr);
180+
_i2c->endTransmission();
168181

169-
Wire.requestFrom((uint8_t)_i2caddr, (uint8_t)1);
170-
return Wire.read();
182+
_i2c->requestFrom((uint8_t)_i2caddr, (uint8_t)1);
183+
return _i2c->read();
171184
}
172185

173186
void Adafruit_PWMServoDriver::write8(uint8_t addr, uint8_t d) {
174-
Wire.beginTransmission(_i2caddr);
175-
Wire.write(addr);
176-
Wire.write(d);
177-
Wire.endTransmission();
187+
_i2c->beginTransmission(_i2caddr);
188+
_i2c->write(addr);
189+
_i2c->write(d);
190+
_i2c->endTransmission();
178191
}

Adafruit_PWMServoDriver.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#else
2424
#include "WProgram.h"
2525
#endif
26-
26+
#include "Wire.h"
2727

2828
#define PCA9685_SUBADR1 0x2
2929
#define PCA9685_SUBADR2 0x3
@@ -50,6 +50,7 @@
5050
class Adafruit_PWMServoDriver {
5151
public:
5252
Adafruit_PWMServoDriver(uint8_t addr = 0x40);
53+
Adafruit_PWMServoDriver(TwoWire *I2C, uint8_t addr = 0x40);
5354
void begin(void);
5455
void reset(void);
5556
void setPWMFreq(float freq);
@@ -58,6 +59,8 @@ class Adafruit_PWMServoDriver {
5859

5960
private:
6061
uint8_t _i2caddr;
62+
63+
TwoWire *_i2c;
6164

6265
uint8_t read8(uint8_t addr);
6366
void write8(uint8_t addr, uint8_t d);

examples/pwmtest/pwmtest.ino

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
Pick one up today in the adafruit shop!
66
------> http://www.adafruit.com/products/815
77
8-
These displays use I2C to communicate, 2 pins are required to
9-
interface. For Arduino UNOs, thats SCL -> Analog 5, SDA -> Analog 4
8+
These drivers use I2C to communicate, 2 pins are required to
9+
interface.
1010
1111
Adafruit invests time and resources providing this open source code,
1212
please support Adafruit and open-source hardware by purchasing
@@ -23,17 +23,10 @@
2323
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
2424
// you can also call it with a different address you want
2525
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);
26-
27-
#if defined(ARDUINO_ARCH_SAMD)
28-
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
29-
#define Serial SerialUSB
30-
#endif
26+
// you can also call it with a different address and I2C interface
27+
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(&Wire, 0x40);
3128

3229
void setup() {
33-
#ifdef ESP8266
34-
Wire.pins(2, 14); // ESP8266 can use any two pins, such as SDA to #2 and SCL to #14
35-
#endif
36-
3730
Serial.begin(9600);
3831
Serial.println("16 channel PWM test!");
3932

@@ -43,22 +36,17 @@ void setup() {
4336
// if you want to really speed stuff up, you can go into 'fast 400khz I2C' mode
4437
// some i2c devices dont like this so much so if you're sharing the bus, watch
4538
// out for this!
46-
#ifdef TWBR
47-
// save I2C bitrate
48-
uint8_t twbrbackup = TWBR;
49-
// must be changed after calling Wire.begin() (inside pwm.begin())
50-
TWBR = 12; // upgrade to 400KHz!
51-
#endif
39+
Wire.setClock(400000);
5240
}
5341

5442
void loop() {
5543
// Drive each PWM in a 'wave'
5644
for (uint16_t i=0; i<4096; i += 8) {
57-
#ifdef ESP8266
58-
yield();
59-
#endif
6045
for (uint8_t pwmnum=0; pwmnum < 16; pwmnum++) {
6146
pwm.setPWM(pwmnum, 0, (i + (4096/16)*pwmnum) % 4096 );
6247
}
48+
#ifdef ESP8266
49+
yield(); // take a breather, required for ESP8266
50+
#endif
6351
}
64-
}
52+
}

examples/servo/servo.ino

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
55
Pick one up today in the adafruit shop!
66
------> http://www.adafruit.com/products/815
7-
8-
These displays use I2C to communicate, 2 pins are required to
9-
interface. For Arduino UNOs, thats SCL -> Analog 5, SDA -> Analog 4
7+
8+
These drivers use I2C to communicate, 2 pins are required to
9+
interface.
1010
1111
Adafruit invests time and resources providing this open source code,
1212
please support Adafruit and open-source hardware by purchasing
@@ -23,6 +23,8 @@
2323
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
2424
// you can also call it with a different address you want
2525
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);
26+
// you can also call it with a different address and I2C interface
27+
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(&Wire, 0x40);
2628

2729
// Depending on your servo make, the pulse width min and max may vary, you
2830
// want these to be as small/large as possible without hitting the hard stop
@@ -42,7 +44,7 @@ void setup() {
4244

4345
pwm.setPWMFreq(60); // Analog servos run at ~60 Hz updates
4446

45-
yield();
47+
delay(10);
4648
}
4749

4850
// you can use this function if you'd like to set the pulse length in seconds

0 commit comments

Comments
 (0)