Library for 16 channel PWM servo driver PCA9685. v2

Dependents:   totoV1-2

Fork of Adafruit-PWM-Servo-Driver by Shundo Kishi

Files at this revision

API Documentation at this revision

Comitter:
fixair
Date:
Tue Nov 14 18:06:44 2017 +0000
Parent:
2:88bdd5c4e77b
Commit message:
test spd15

Changed in this revision

Adafruit_PWMServoDriver.cpp Show annotated file Show diff for this revision Revisions of this file
Adafruit_PWMServoDriver.h Show annotated file Show diff for this revision Revisions of this file
diff -r 88bdd5c4e77b -r 07497eda12c2 Adafruit_PWMServoDriver.cpp
--- a/Adafruit_PWMServoDriver.cpp	Wed Sep 04 03:58:53 2013 +0000
+++ b/Adafruit_PWMServoDriver.cpp	Tue Nov 14 18:06:44 2017 +0000
@@ -23,15 +23,20 @@
  
 #include "Adafruit_PWMServoDriver.h"
 
-Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(PinName sda, PinName scl, int addr) : i2c(sda, scl), _i2caddr(addr) {}
+//Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(PinName sda, PinName scl, int addr) : i2c(sda, scl), _i2caddr(addr) {}
+
+Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(PinName sda, PinName scl, int addr) {
+    i2c = new I2C(sda, scl);
+    _i2caddr = addr;
+}
 
 void Adafruit_PWMServoDriver::i2c_probe(void) 
 {
-    printf("Searching for I2C devices...\n");
+    printf("Probing I2C devices...\n");
     
     int count = 0;
-    for (int address=4; address<256; address+=2) {
-        if (!i2c.write(address, NULL, 0)) { // 0 returned is ok
+    for (int address=1; address<256; address+=1) {
+        if (!i2c->write(address, NULL, 0)) { // 0 returned is ok
             printf(" - I2C device found at address 0x%02X\r\n", address);
             count++;
         }
@@ -44,7 +49,7 @@
 }
 
 void Adafruit_PWMServoDriver::setI2Cfreq(int freq) {
-    i2c.frequency(freq);
+    i2c->frequency(freq);
 }
 
 void Adafruit_PWMServoDriver::reset(void) {
@@ -83,7 +88,7 @@
     cmd[2] = on >> 8;
     cmd[3] = off;
     cmd[4] = off >> 8;
-    i2c.write(_i2caddr, cmd, 5);
+    i2c->write(_i2caddr, cmd, 5);
     
     /*write8(LED0_ON_L+4*num, on);
     write8(LED0_ON_H+4*num, on >> 8);
@@ -99,9 +104,9 @@
 }
 
 uint8_t Adafruit_PWMServoDriver::read8(char addr) {
-    i2c.write(_i2caddr, &addr, 1);
+    i2c->write(_i2caddr, &addr, 1);
     char rtn;
-    i2c.read(_i2caddr, &rtn, 1);
+    i2c->read(_i2caddr, &rtn, 1);
     return rtn;
 }
 
@@ -109,5 +114,5 @@
     char cmd[2];
     cmd[0] = addr;
     cmd[1] = d;
-    i2c.write(_i2caddr, cmd, 2);
+    i2c->write(_i2caddr, cmd, 2);
 }
\ No newline at end of file
diff -r 88bdd5c4e77b -r 07497eda12c2 Adafruit_PWMServoDriver.h
--- a/Adafruit_PWMServoDriver.h	Wed Sep 04 03:58:53 2013 +0000
+++ b/Adafruit_PWMServoDriver.h	Tue Nov 14 18:06:44 2017 +0000
@@ -47,7 +47,8 @@
 
 class Adafruit_PWMServoDriver {
  public:
-  Adafruit_PWMServoDriver(PinName sda, PinName scl, int addr = 0x80); //0b 1_000000_(R/W) <- default slave adress
+  //Adafruit_PWMServoDriver(PinName sda, PinName scl, int addr = 0x41); //0b 1_000000_(R/W) <- default slave adress
+  Adafruit_PWMServoDriver(PinName sda, PinName scl, int addr);
   void i2c_probe(void);
   void begin(void);
   void setI2Cfreq(int freq);
@@ -58,8 +59,8 @@
   void setDuty(uint8_t num, uint16_t duty);
 
  private:
+  I2C *i2c;
   int _i2caddr;
-  I2C i2c;
 
   uint8_t read8(char addr);
   void write8(char addr, char d);