|
9 | 9 | # ============================================================================
|
10 | 10 |
|
11 | 11 | class PWM :
|
12 |
| - i2c = None |
13 |
| - |
14 | 12 | # Registers/etc.
|
| 13 | + __MODE1 = 0x00 |
| 14 | + __MODE2 = 0x01 |
15 | 15 | __SUBADR1 = 0x02
|
16 | 16 | __SUBADR2 = 0x03
|
17 | 17 | __SUBADR3 = 0x04
|
18 |
| - __MODE1 = 0x00 |
19 | 18 | __PRESCALE = 0xFE
|
20 | 19 | __LED0_ON_L = 0x06
|
21 | 20 | __LED0_ON_H = 0x07
|
22 | 21 | __LED0_OFF_L = 0x08
|
23 | 22 | __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 |
28 | 41 |
|
29 | 42 | def __init__(self, address=0x40, debug=False):
|
30 | 43 | self.i2c = Adafruit_I2C(address)
|
| 44 | + self.i2c.debug = debug |
31 | 45 | self.address = address
|
32 | 46 | self.debug = debug
|
33 | 47 | 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 |
36 | 58 |
|
37 | 59 | def setPWMFreq(self, freq):
|
38 | 60 | "Sets the PWM frequency"
|
@@ -62,6 +84,9 @@ def setPWM(self, channel, on, off):
|
62 | 84 | self.i2c.write8(self.__LED0_OFF_L+4*channel, off & 0xFF)
|
63 | 85 | self.i2c.write8(self.__LED0_OFF_H+4*channel, off >> 8)
|
64 | 86 |
|
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