From a7d568efc2e12049f70225c5599f2ad1af1b693a Mon Sep 17 00:00:00 2001 From: ssnover95 Date: Sun, 8 Jul 2018 20:30:03 -0400 Subject: [PATCH] Remove scaling of user input and add comments to SetPWMFreq function in PCA9685 i2c driver Signed-off-by: ssnover95 --- drivers/i2c/pca9685_driver.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/drivers/i2c/pca9685_driver.go b/drivers/i2c/pca9685_driver.go index 7a5d5285..d8bcf389 100644 --- a/drivers/i2c/pca9685_driver.go +++ b/drivers/i2c/pca9685_driver.go @@ -104,12 +104,14 @@ func (p *PCA9685Driver) SetPWM(channel int, on uint16, off uint16) (err error) { // SetPWMFreq sets the PWM frequency in Hz func (p *PCA9685Driver) SetPWMFreq(freq float32) error { - freq *= 0.9 - + // IC oscillator frequency is 25 MHz var prescalevel float32 = 25000000 + // Find frequency of PWM waveform prescalevel /= 4096 + // Ratio between desired frequency and maximum prescalevel /= freq prescalevel -= 1 + // Round value to nearest whole prescale := byte(prescalevel + 0.5) if _, err := p.connection.Write([]byte{byte(PCA9685_MODE1)}); err != nil { @@ -121,20 +123,23 @@ func (p *PCA9685Driver) SetPWMFreq(freq float32) error { return err } + // Put oscillator in sleep mode, clear bit 7 here to avoid overwriting + // previous setting newmode := (oldmode & 0x7F) | 0x10 if _, err := p.connection.Write([]byte{byte(PCA9685_MODE1), byte(newmode)}); err != nil { return err } - + // Write prescaler value if _, err := p.connection.Write([]byte{byte(PCA9685_PRESCALE), prescale}); err != nil { return err } - + // Put back to old settings if _, err := p.connection.Write([]byte{byte(PCA9685_MODE1), byte(oldmode)}); err != nil { return err } time.Sleep(100 * time.Millisecond) + // Enable response to All Call address, enable auto-increment, clear restart if _, err := p.connection.Write([]byte{byte(PCA9685_MODE1), byte(oldmode | 0xa1)}); err != nil { return err }