Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from smbus2 import SMBus
- from time import sleep
- # relevant registers
- MODE1 = 0x00
- MODE2 = 0x01
- LED = 0x06
- ALL_LED = 0xFA
- PRE_SCALE = 0xFE
- def pwm_reg( output ):
- if output == 'all':
- return ALL_LED
- else:
- assert output in range(0, 16)
- return LED + 4 * output
- class Pca9685:
- def __init__(self, bus, addr):
- self.addr = 0b10000000 | addr
- self.bus = bus
- self.write_reg(MODE1, 1 << 5) # initialize MODE1 register
- sleep(500e-6) # wait 500us to allow oscillator to power up
- def read_reg(self, reg):
- assert reg in range(0, 256)
- return self.bus.read_byte_data(self.addr, reg)
- def write_reg(self, reg, value):
- assert reg in range(0, 256)
- return self.bus.write_byte_data(self.addr, reg, value)
- def read_regs(self, reg, count):
- assert reg in range(0, 256)
- assert count in range(1, 257-reg)
- return self.bus.read_i2c_block_data(self.addr, reg, count)
- def write_regs(self, reg, values):
- assert reg in range(0, 256)
- return self.bus.write_i2c_block_data(self.addr, reg, values)
- def get_pwm(self, output):
- [on_l, on_h, off_l, off_h] = self.read_regs(pwm_reg(output), 4)
- on = on_l | on_h << 8
- off = off_l | off_h << 8
- return (off, on)
- def set_pwm(self, output, off, on=0):
- assert on in range(0, 4097)
- assert off in range(0, 4097)
- assert on <= off
- on_l = on & 0xff
- on_h = on >> 8
- off_l = off & 0xff
- off_h = off >> 8
- self.write_regs(pwm_reg(output), [on_l, on_h, off_l, off_h])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement