Skip to content

Commit e7ce458

Browse files
committedJun 21, 2014
Extended fix in 4d92bf1 to work if more than one PCA9685 is connected. In the old version any subsequent board initialization reset all previously initialized boards.
1 parent 0780adb commit e7ce458

File tree

2 files changed

+42
-15
lines changed

2 files changed

+42
-15
lines changed
 

‎Adafruit_I2C/Adafruit_I2C.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@ def getPiRevision():
2525
def getPiI2CBusNumber():
2626
# Gets the I2C bus number /dev/i2c#
2727
return 1 if Adafruit_I2C.getPiRevision() > 1 else 0
28-
28+
2929
def __init__(self, address, busnum=-1, debug=False):
3030
self.address = address
3131
# By default, the correct I2C bus is auto-detected using /proc/cpuinfo
3232
# Alternatively, you can hard-code the bus version below:
3333
# self.bus = smbus.SMBus(0); # Force I2C0 (early 256MB Pi's)
3434
# self.bus = smbus.SMBus(1); # Force I2C1 (512MB Pi's)
35-
self.bus = smbus.SMBus(
36-
busnum if busnum >= 0 else Adafruit_I2C.getPiI2CBusNumber())
35+
self.bus = smbus.SMBus(busnum if busnum >= 0 else Adafruit_I2C.getPiI2CBusNumber())
3736
self.debug = debug
3837

3938
def reverseByteOrder(self, data):
@@ -69,6 +68,15 @@ def write16(self, reg, value):
6968
except IOError, err:
7069
return self.errMsg()
7170

71+
def writeRaw8(self, value):
72+
"Writes an 8-bit value on the bus"
73+
try:
74+
self.bus.write_byte(self.address, value)
75+
if self.debug:
76+
print "I2C: Wrote 0x%02X" % value
77+
except IOError, err:
78+
return self.errMsg()
79+
7280
def writeList(self, reg, list):
7381
"Writes an array of bytes using I2C format"
7482
try:

‎Adafruit_PWM_Servo_Driver/Adafruit_PWM_Servo_Driver.py

+31-12
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,48 @@
99
# ============================================================================
1010

1111
class PWM :
12-
i2c = None
13-
1412
# Registers/etc.
13+
__MODE1 = 0x00
14+
__MODE2 = 0x01
1515
__SUBADR1 = 0x02
1616
__SUBADR2 = 0x03
1717
__SUBADR3 = 0x04
18-
__MODE1 = 0x00
1918
__PRESCALE = 0xFE
2019
__LED0_ON_L = 0x06
2120
__LED0_ON_H = 0x07
2221
__LED0_OFF_L = 0x08
2322
__LED0_OFF_H = 0x09
24-
__ALLLED_ON_L = 0xFA
25-
__ALLLED_ON_H = 0xFB
26-
__ALLLED_OFF_L = 0xFC
27-
__ALLLED_OFF_H = 0xFD
23+
__ALL_LED_ON_L = 0xFA
24+
__ALL_LED_ON_H = 0xFB
25+
__ALL_LED_OFF_L = 0xFC
26+
__ALL_LED_OFF_H = 0xFD
27+
28+
# Bits
29+
__RESTART = 0x80
2830
__SLEEP = 0x10
31+
__ALLCALL = 0x01
32+
__INVRT = 0x10
33+
__OUTDRV = 0x04
34+
35+
general_call_i2c = Adafruit_I2C(0x00)
36+
37+
@classmethod
38+
def softwareReset(cls):
39+
"Sends a software reset (SWRST) command to all the servo drivers on the bus"
40+
cls.general_call_i2c.writeRaw8(0x06) # SWRST
2941

3042
def __init__(self, address=0x40, debug=False):
3143
self.i2c = Adafruit_I2C(address)
3244
self.i2c.debug = debug
3345
self.address = address
3446
self.debug = debug
3547
if (self.debug):
36-
print "Reseting PCA9685"
37-
self.i2c.bus.write_byte(0x00, 0x06)
48+
print "Reseting PCA9685 MODE1 (without SLEEP) and MODE2"
49+
self.setAllPWM(0, 0)
50+
self.i2c.write8(self.__MODE2, self.__OUTDRV)
51+
self.i2c.write8(self.__MODE1, self.__ALLCALL)
52+
time.sleep(0.005) # wait for oscillator
53+
3854
mode1 = self.i2c.readU8(self.__MODE1)
3955
mode1 = mode1 & ~self.__SLEEP # wake up (reset sleep)
4056
self.i2c.write8(self.__MODE1, mode1)
@@ -68,6 +84,9 @@ def setPWM(self, channel, on, off):
6884
self.i2c.write8(self.__LED0_OFF_L+4*channel, off & 0xFF)
6985
self.i2c.write8(self.__LED0_OFF_H+4*channel, off >> 8)
7086

71-
72-
73-
87+
def setAllPWM(self, on, off):
88+
"Sets a all PWM channels"
89+
self.i2c.write8(self.__ALL_LED_ON_L, on & 0xFF)
90+
self.i2c.write8(self.__ALL_LED_ON_H, on >> 8)
91+
self.i2c.write8(self.__ALL_LED_OFF_L, off & 0xFF)
92+
self.i2c.write8(self.__ALL_LED_OFF_H, off >> 8)

0 commit comments

Comments
 (0)
Please sign in to comment.