Skip to content

Commit 57f5589

Browse files
committed
Merge pull request adafruit#83 from sebastianludwig/master
Fixed PWM driver reset
2 parents 5d38766 + e7ce458 commit 57f5589

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed

Adafruit_I2C/Adafruit_I2C.py

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

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

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

Adafruit_PWM_Servo_Driver/Adafruit_PWM_Servo_Driver.py

+37-12
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,52 @@
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
30+
__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
2841

2942
def __init__(self, address=0x40, debug=False):
3043
self.i2c = Adafruit_I2C(address)
44+
self.i2c.debug = debug
3145
self.address = address
3246
self.debug = debug
3347
if (self.debug):
34-
print "Reseting PCA9685"
35-
self.i2c.write8(self.__MODE1, 0x00)
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+
54+
mode1 = self.i2c.readU8(self.__MODE1)
55+
mode1 = mode1 & ~self.__SLEEP # wake up (reset sleep)
56+
self.i2c.write8(self.__MODE1, mode1)
57+
time.sleep(0.005) # wait for oscillator
3658

3759
def setPWMFreq(self, freq):
3860
"Sets the PWM frequency"
@@ -62,6 +84,9 @@ def setPWM(self, channel, on, off):
6284
self.i2c.write8(self.__LED0_OFF_L+4*channel, off & 0xFF)
6385
self.i2c.write8(self.__LED0_OFF_H+4*channel, off >> 8)
6486

65-
66-
67-
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)