Skip to content

Instantly share code, notes, and snippets.

@AlexisTM
Created May 29, 2018 14:05
Show Gist options
  • Save AlexisTM/db3450a25c8600923cf2456d1dc74f3f to your computer and use it in GitHub Desktop.
Save AlexisTM/db3450a25c8600923cf2456d1dc74f3f to your computer and use it in GitHub Desktop.
PCA9633 LED Python library
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
PCA9633.py
Author : Alexis PAQUES (@AlexisTM)
"""
import wiringpi2 as wpi
import time
class PCA9633(object):
def __init__(self, addr=0x62, dev="/dev/i2c-1"):
super(PCA9633, self).__init__()
self.i2c_dev = "/dev/i2c-1"
self.i2c_addr = 0x62
wpi.wiringPiSetup()
self.i2c_fd = wpi.wiringPiI2CSetupInterface(self.i2c_dev, self.i2c_addr)
# leave time to init
time.sleep(0.2)
wpi.wiringPiI2CWriteReg8(self.i2c_fd, 0x08, 0x2A)
wpi.wiringPiI2CWriteReg8(self.i2c_fd, 0x06, 0x00)
wpi.wiringPiI2CWriteReg8(self.i2c_fd, 0x07, 0x80)
def sanitize(self, data):
data = int((1-data)*255)
if data > 255:
data = 255
if data < 0:
data = 0
return data
def set_color(self, r, g, b):
wpi.wiringPiI2CWriteReg8(self.i2c_fd, 0x02, self.sanitize(self.current_color.g))
wpi.wiringPiI2CWriteReg8(self.i2c_fd, 0x03, self.sanitize(self.current_color.b))
wpi.wiringPiI2CWriteReg8(self.i2c_fd, 0x04, self.sanitize(self.current_color.r))
wpi.wiringPiI2CWriteReg8(self.i2c_fd, 0x06, 0x00)
wpi.wiringPiI2CWriteReg8(self.i2c_fd, 0x07, 0x80)
def main():
led = PCA9633()
print "Testing the LED (PCA9633 device on i2c bus)"
print "RED"
led.set_color(1,0,0)
time.sleep(1)
print "GREEN"
led.set_color(0,1,0)
time.sleep(1)
print "BLUE"
led.set_color(0,0,1)
time.sleep(1)
print "WHITE"
led.set_color(1,1,1)
time.sleep(5)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment