Skip to content
This repository was archived by the owner on Sep 30, 2019. It is now read-only.

Commit 91d402d

Browse files
Utilizing PCA9685 register address auto increment feature to reduce bus traffic by 50% when setting a new PWM value.
1 parent bf50dd3 commit 91d402d

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

Adafruit_PWM_Servo_Driver/Adafruit_PWM_Servo_Driver.py

+11-15
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ class PWM :
2626
__ALL_LED_OFF_H = 0xFD
2727

2828
# Bits
29-
__RESTART = 0x80
30-
__SLEEP = 0x10
31-
__ALLCALL = 0x01
32-
__INVRT = 0x10
33-
__OUTDRV = 0x04
29+
__RESTART = 1 << 7
30+
__AI = 1 << 5
31+
__SLEEP = 1 << 4
32+
__ALLCALL = 1 << 0
33+
34+
__INVRT = 1 << 4
35+
__OUTDRV = 1 << 2
3436

3537
general_call_i2c = Adafruit_I2C(0x00)
3638

@@ -45,10 +47,10 @@ def __init__(self, address=0x40, debug=False):
4547
self.address = address
4648
self.debug = debug
4749
if (self.debug):
48-
print "Reseting PCA9685 MODE1 (without SLEEP) and MODE2"
50+
print "Reseting PCA9685 MODE1 (without SLEEP, but with AI) and MODE2"
4951
self.setAllPWM(0, 0)
5052
self.i2c.write8(self.__MODE2, self.__OUTDRV)
51-
self.i2c.write8(self.__MODE1, self.__ALLCALL)
53+
self.i2c.write8(self.__MODE1, self.__ALLCALL | self.__AI)
5254
time.sleep(0.005) # wait for oscillator
5355

5456
mode1 = self.i2c.readU8(self.__MODE1)
@@ -79,14 +81,8 @@ def setPWMFreq(self, freq):
7981

8082
def setPWM(self, channel, on, off):
8183
"Sets a single PWM channel"
82-
self.i2c.write8(self.__LED0_ON_L+4*channel, on & 0xFF)
83-
self.i2c.write8(self.__LED0_ON_H+4*channel, on >> 8)
84-
self.i2c.write8(self.__LED0_OFF_L+4*channel, off & 0xFF)
85-
self.i2c.write8(self.__LED0_OFF_H+4*channel, off >> 8)
84+
self.i2c.writeList(self.__LED0_ON_L+4*channel, [on & 0xFF, on >> 8, off & 0xFF, off >> 8])
8685

8786
def setAllPWM(self, on, off):
8887
"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)
88+
self.i2c.writeList(self.__ALL_LED_ON_L, [on & 0xFF, on >> 8, off & 0xFF, off >> 8])

0 commit comments

Comments
 (0)