Created
July 26, 2018 08:43
-
-
Save asus4/1f6d1311622994e34b366e06bbc093d4 to your computer and use it in GitHub Desktop.
Control PCA9622 I2C LED Driver from Raspberry PI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| """ PCA9622 """ | |
| from __future__ import print_function | |
| import time | |
| import smbus | |
| class PCA9622(object): | |
| """ PCA9622 LED Driver i2c wrapper | |
| Data sheet | |
| https://www.nxp.com/docs/en/data-sheet/PCA9622.pdf | |
| Available breadboard-kit here | |
| https://www.switch-science.com/catalog/2388/ | |
| """ | |
| # Address | |
| MODE1 = 0x00 | |
| LEDOUT0 = 0x14 | |
| PWM0 = 0x02 | |
| # Auto-Increment Settings | |
| AI_DISABLE = 0b00000000 # No increment | |
| AI_ALL = 0b10000000 # All increment | |
| AI_BRIGHTNESS = 0b10100000 # Increment for brightness registers only | |
| AI_GLOBAL = 0b11000000 # Increment for global control registers only | |
| AI_BRI_GLO = 0b11100000 # Increment for brightness and global | |
| def __init__(self, address=0x70): | |
| """ Initialize PCA9622 Controller """ | |
| self.i2c = smbus.SMBus(1) | |
| self.addr = address | |
| # Initialize MODE1, MODE2 | |
| self.write_block( | |
| PCA9622.MODE1 | PCA9622.AI_ALL, | |
| [0b10000001, 0b00000101] | |
| ) | |
| # LED out setting | |
| b = 0b10101010 # Affect by indivisual brightness control | |
| self.write_block( | |
| PCA9622.LEDOUT0 | PCA9622.AI_ALL, | |
| [b, b, b, b] | |
| ) | |
| def write(self, address, data): | |
| self.i2c.write_byte_data(self.addr, address | PCA9622.AI_DISABLE, data) | |
| def write_block(self, address, data): | |
| self.i2c.write_i2c_block_data(self.addr, address, data) | |
| def set_pwm(self, index, pwm): | |
| """ Set all LED gain """ | |
| self.write(PCA9622.PWM0 + index, pwm) | |
| def set_pwms(self, pwms): | |
| """ Set gain with list """ | |
| self.write_block( | |
| PCA9622.PWM0 | PCA9622.AI_BRIGHTNESS, | |
| pwms | |
| ) | |
| def sleep(self, isSleep): | |
| """ Set sleep """ | |
| if isSleep: | |
| data = [0 for i in range(16)] | |
| self.set_pwms(data) | |
| self.write(PCA9622.MODE1, 0b10010001) | |
| else: | |
| self.write(PCA9622.MODE1, 0b10000001) | |
| if __name__ == '__main__': | |
| import math | |
| led = PCA9622() | |
| # simple test | |
| for i in range(16): | |
| led.set_pwm(i, 0) | |
| time.sleep(0.5) | |
| for i in range(16): | |
| led.set_pwm(i, 0xff) | |
| time.sleep(0.5) | |
| for i in range(16): | |
| led.set_pwm(i, 0) | |
| # block test | |
| data = [0 for i in range(16)] | |
| for t in range(500): | |
| _t = t * math.pi * 0.02 | |
| for i in range(16): | |
| theta = math.pi * (i / 16.0) * 2 | |
| data[i] = int((math.sin(theta + _t) + 1.0) * 0.5 * 255) | |
| led.set_pwms(data) | |
| time.sleep(0.02) | |
| # sleep | |
| led.sleep(True) | |
| time.sleep(2) | |
| led.sleep(False) | |
| time.sleep(2) | |
| # all off | |
| for i in range(16): | |
| led.set_pwm(i, 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment