18
18
#include < Adafruit_PWMServoDriver.h>
19
19
#include < Wire.h>
20
20
21
- #if defined(ARDUINO_SAM_DUE)
22
- #define Wire Wire1
23
- #endif
24
-
25
-
26
21
// Set to true to print some debug messages, or false to disable them.
27
22
// #define ENABLE_DEBUG_OUTPUT
28
23
29
24
30
25
/* *************************************************************************/
31
26
/* !
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.
33
28
@param addr The 7-bit I2C address to locate this chip, default is 0x40
34
29
*/
35
30
/* *************************************************************************/
36
31
Adafruit_PWMServoDriver::Adafruit_PWMServoDriver (uint8_t addr) {
37
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
+ /* *************************************************************************/
48
+ Adafruit_PWMServoDriver::Adafruit_PWMServoDriver (TwoWire *i2c, uint8_t addr) {
49
+ _i2c = i2c;
50
+ _i2caddr = addr;
38
51
}
39
52
40
53
/* *************************************************************************/
@@ -43,7 +56,7 @@ Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(uint8_t addr) {
43
56
*/
44
57
/* *************************************************************************/
45
58
void Adafruit_PWMServoDriver::begin (void ) {
46
- Wire. begin ();
59
+ _i2c-> begin ();
47
60
reset ();
48
61
}
49
62
@@ -110,13 +123,13 @@ void Adafruit_PWMServoDriver::setPWM(uint8_t num, uint16_t on, uint16_t off) {
110
123
Serial.print (" Setting PWM " ); Serial.print (num); Serial.print (" : " ); Serial.print (on); Serial.print (" ->" ); Serial.println (off);
111
124
#endif
112
125
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 ();
120
133
}
121
134
122
135
/* *************************************************************************/
@@ -130,7 +143,7 @@ void Adafruit_PWMServoDriver::setPWM(uint8_t num, uint16_t on, uint16_t off) {
130
143
void Adafruit_PWMServoDriver::setPin (uint8_t num, uint16_t val, bool invert)
131
144
{
132
145
// Clamp value between 0 and 4095 inclusive.
133
- val = min (val, 4095 );
146
+ val = min (val, ( uint16_t ) 4095 );
134
147
if (invert) {
135
148
if (val == 0 ) {
136
149
// Special value for signal fully on.
@@ -162,17 +175,17 @@ void Adafruit_PWMServoDriver::setPin(uint8_t num, uint16_t val, bool invert)
162
175
/* ******************************************************************************************/
163
176
164
177
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 ();
168
181
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 ();
171
184
}
172
185
173
186
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 ();
178
191
}
0 commit comments