Advertisement
zmatt

pca9685.py

Feb 14th, 2020
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.04 KB | None | 0 0
  1. from time import sleep
  2.  
  3. # relevant registers
  4. MODE1       = 0x00
  5. MODE2       = 0x01
  6. LED         = 0x06
  7. ALL_LED     = 0xFA
  8. PRE_SCALE   = 0xFE
  9.  
  10. class Pca9685:
  11.     def __init__( self, bus, addr ):
  12.         self.addr = 0b1000000 | addr
  13.         self.bus = bus
  14.         self.write_reg( MODE1, 1 << 5 )  # initialize MODE1 register
  15.         sleep( 500e-6 )  # wait 500us to allow oscillator to power up
  16.  
  17.     def read_reg( self, reg )
  18.         return self.read_regs( reg, 1 )[0]
  19.  
  20.     def write_reg( self, reg, value ):
  21.         return self.write_regs( reg, [ value ] )
  22.  
  23.     def read_regs( self, reg, count ):
  24.         assert reg in range( 0, 256 )
  25.         assert count in range( 1, 257-reg )
  26.         return self.bus.read_i2c_block_data( self.addr, reg, count )
  27.  
  28.     def write_regs( self, reg, values ):
  29.         assert reg in range( 0, 256 )
  30.         return self.bus.write_i2c_block_data( self.addr, reg, values )
  31.  
  32.     def get_pwm( self, output ):
  33.         assert output in range( 0, 16 )
  34.         reg = LED + 4 * output
  35.  
  36.         [ on_l, on_h, off_l, off_h ] = self.read_regs( reg, 4 )
  37.         on = on_l | on_h << 8
  38.         off = off_l | off_h << 8
  39.  
  40.         phase = on
  41.         duty = ( off - on ) & 0xfff
  42.         if off & 0x1000:
  43.             duty = 0
  44.         elif on & 0x1000:
  45.             duty = 4096
  46.  
  47.         return ( duty, phase )
  48.  
  49.     def set_pwm( self, output, duty, phase=0 ):
  50.         assert duty in range( 0, 4097 )
  51.         assert phase in range( 0, 4096 )
  52.  
  53.         if output == 'all':
  54.             reg = ALL_LED
  55.         else:
  56.             assert output in range( 0, 16 )
  57.             reg = LED + 4 * output
  58.  
  59.         on = phase
  60.         off = ( duty + phase ) & 0xfff
  61.         if duty == 0:
  62.             off |= 0x1000
  63.         elif duty == 4096:
  64.             on |= 0x1000
  65.  
  66.         on_l = on & 0xff
  67.         on_h = on >> 8
  68.         off_l = off & 0xff
  69.         off_h = off >> 8
  70.         self.write_regs( reg, [ on_l, on_h, off_l, off_h ] )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement