Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import time
- import math
- from smbus2 import SMBus
- # ============================================================================
- # Adafruit PCA9685 16-Channel PWM Servo Driver
- # ============================================================================
- bus = SMBus("/dev/i2c-2")
- class PWM :
- i2c = None
- # Registers/etc.
- address = 0x40
- __SUBADR1 = 0x02
- __SUBADR2 = 0x03
- __SUBADR3 = 0x04
- __MODE1 = 0x00
- __PRESCALE = 0xFE
- __LED0_ON_L = 0x06
- __LED0_ON_H = 0x07
- __LED0_OFF_L = 0x08
- __LED0_OFF_H = 0x09
- __ALLLED_ON_L = 0xFA
- __ALLLED_ON_H = 0xFB
- __ALLLED_OFF_L = 0xFC
- __ALLLED_OFF_H = 0xFD
- def __init__(self, address):
- self = self
- self.address = address
- #if (self):
- print("Reseting PCA9685")
- bus.write_i2c_block_data(bus, 0x00, data)
- def setPWMFreq(self, freq):
- # Sets the PWM frequency
- prescaleval = 25000000.0 # 25MHz
- prescaleval /= 4096.0 # 12-bit
- prescaleval /= float(freq)
- prescaleval -= 1.0
- #if (self):
- print("Setting PWM frequency to %d Hz" % freq)
- print("Estimated pre-scale: %d" % prescaleval)
- prescale = math.floor(prescaleval + 0.5)
- #if (self):
- print("Final pre-scale: %d" % prescale)
- oldmode = self.read_i2c_block_data(self.__MODE1);
- newmode = (oldmode & 0x7F) | 0x10 # sleep
- self.i2c.write_i2c_block_data(self.__MODE1, newmode) # go to sleep
- self.write_i2c_block_data(self.__PRESCALE, int(math.floor(prescale)))
- self.write_i2c_block_data(self.__MODE1, oldmode)
- time.sleep(0.005)
- self.write_i2c_block_data(self.__MODE1, oldmode | 0x80)
- def setPWM(self, channel, on, off):
- # Sets a single PWM channel
- self.write_i2c_block_data(self.__LED0_ON_L+4 * channel, on & 0xFF)
- self.write_i2c_block_data(self.__LED0_ON_H+4 * channel, on >> 8)
- self.write_i2c_block_data(self.__LED0_OFF_L+4 * channel, off & 0xFF)
- self.write_i2c_block_data(self.__LED0_OFF_H+4 * channel, off >> 8)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement