SHARE
TWEET

Untitled

silver2row Feb 14th, 2020 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python3
  2.  
  3. import time
  4. import math
  5. from smbus2 import SMBus
  6.  
  7. # ============================================================================
  8. # Adafruit PCA9685 16-Channel PWM Servo Driver
  9. # ============================================================================
  10.  
  11. bus = SMBus("/dev/i2c-2")
  12.  
  13. class PWM :
  14.  
  15.     i2c = None
  16.  
  17.   # Registers/etc.
  18.     address              = 0x40
  19.     __SUBADR1            = 0x02
  20.     __SUBADR2            = 0x03
  21.     __SUBADR3            = 0x04
  22.     __MODE1              = 0x00
  23.     __PRESCALE           = 0xFE
  24.     __LED0_ON_L          = 0x06
  25.     __LED0_ON_H          = 0x07
  26.     __LED0_OFF_L         = 0x08
  27.     __LED0_OFF_H         = 0x09
  28.     __ALLLED_ON_L        = 0xFA
  29.     __ALLLED_ON_H        = 0xFB
  30.     __ALLLED_OFF_L       = 0xFC
  31.     __ALLLED_OFF_H       = 0xFD
  32.  
  33.     def __init__(self, address):
  34.         self = self
  35.         self.address = address
  36.         #if (self):
  37.         print("Reseting PCA9685")
  38.         bus.write_i2c_block_data(bus, 0x00, data)
  39.  
  40.     def setPWMFreq(self, freq):
  41.     # Sets the PWM frequency
  42.         prescaleval = 25000000.0    # 25MHz
  43.         prescaleval /= 4096.0       # 12-bit
  44.         prescaleval /= float(freq)
  45.         prescaleval -= 1.0
  46.         #if (self):
  47.         print("Setting PWM frequency to %d Hz" % freq)
  48.         print("Estimated pre-scale: %d" % prescaleval)
  49.         prescale = math.floor(prescaleval + 0.5)
  50.         #if (self):
  51.         print("Final pre-scale: %d" % prescale)
  52.  
  53.         oldmode = self.read_i2c_block_data(self.__MODE1);
  54.         newmode = (oldmode & 0x7F) | 0x10             # sleep
  55.         self.i2c.write_i2c_block_data(self.__MODE1, newmode)        # go to sleep
  56.         self.write_i2c_block_data(self.__PRESCALE, int(math.floor(prescale)))
  57.         self.write_i2c_block_data(self.__MODE1, oldmode)
  58.         time.sleep(0.005)
  59.         self.write_i2c_block_data(self.__MODE1, oldmode | 0x80)
  60.  
  61.     def setPWM(self, channel, on, off):
  62.         # Sets a single PWM channel
  63.         self.write_i2c_block_data(self.__LED0_ON_L+4 * channel, on & 0xFF)
  64.         self.write_i2c_block_data(self.__LED0_ON_H+4 * channel, on >> 8)
  65.         self.write_i2c_block_data(self.__LED0_OFF_L+4 * channel, off & 0xFF)
  66.         self.write_i2c_block_data(self.__LED0_OFF_H+4 * channel, off >> 8)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top