From 71400d7128b59577729a20c4e998fe1d3f83d342 Mon Sep 17 00:00:00 2001 From: Giovanni Harting <539@idlegandalf.com> Date: Tue, 12 Dec 2017 23:32:09 +0100 Subject: [PATCH] refined pulse handling --- main.go | 4 ++-- pca9685.go | 40 ++++++++++++++++++++-------------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/main.go b/main.go index 0c0b5af..aae7acc 100644 --- a/main.go +++ b/main.go @@ -32,8 +32,8 @@ type config struct { Pca9685 struct { Device string Address int - MinPulse int - MaxPulse int + MinPulse uint16 + MaxPulse uint16 } } diff --git a/pca9685.go b/pca9685.go index fe7a6c2..c07b5af 100644 --- a/pca9685.go +++ b/pca9685.go @@ -23,15 +23,15 @@ const ( ALL_LED_OFF_H byte = 0xFD OUTDRV byte = 0x04 SLEEP byte = 0x10 - BYTE byte = 0xFF + WORD byte = 0xFF ) type PCA9685 struct { i2cBus *i2c.Device name string initiated bool - minPulse int - maxPulse int + minPulse uint16 + maxPulse uint16 log *logging.Logger frequency float32 } @@ -42,7 +42,7 @@ type Pwm struct { lastValue float32 } -func createPCA9685(i2cDevice *i2c.Device, name string, minPulse int, maxPulse int, log *logging.Logger) *PCA9685 { +func createPCA9685(i2cDevice *i2c.Device, name string, minPulse uint16, maxPulse uint16, log *logging.Logger) *PCA9685 { log.Info(fmt.Sprintf("Creating a new PCA9685 device. Alias: %v", name)) return &PCA9685{ @@ -89,7 +89,7 @@ func (p *PCA9685) Init() { return } - mode1 &= BYTE + mode1 &= WORD mode1 = mode1 & ^SLEEP p.i2cBus.WriteReg(MODE1, []byte{mode1 & 0xFF}) @@ -155,48 +155,48 @@ func (p *PCA9685) setPwmFreq(freqHz float32) { p.log.Error("Can't read!") } - oldMode &= BYTE + oldMode &= WORD newMode := (oldMode & 0x7F) | 0x10 - p.i2cBus.WriteReg(MODE1, []byte{newMode & BYTE}) - p.i2cBus.WriteReg(PRESCALE, []byte{byte(prescale) & BYTE}) - p.i2cBus.WriteReg(MODE1, []byte{oldMode & BYTE}) + p.i2cBus.WriteReg(MODE1, []byte{newMode & WORD}) + p.i2cBus.WriteReg(PRESCALE, []byte{byte(prescale) & WORD}) + p.i2cBus.WriteReg(MODE1, []byte{oldMode & WORD}) time.Sleep(5 * time.Millisecond) - p.i2cBus.WriteReg(MODE1, []byte{oldMode&BYTE | 0x80}) + p.i2cBus.WriteReg(MODE1, []byte{oldMode&WORD | 0x80}) } -func (p *PCA9685) setAllPwm(on int, off int) { +func (p *PCA9685) setAllPwm(on uint16, off uint16) { onB := byte(on) offB := byte(off) - p.i2cBus.WriteReg(ALL_LED_ON_L, []byte{onB & BYTE}) - p.i2cBus.WriteReg(ALL_LED_ON_H, []byte{onB & BYTE}) - p.i2cBus.WriteReg(ALL_LED_OFF_L, []byte{offB & BYTE}) - p.i2cBus.WriteReg(ALL_LED_OFF_H, []byte{offB & BYTE}) + p.i2cBus.WriteReg(ALL_LED_ON_L, []byte{onB & WORD}) + p.i2cBus.WriteReg(ALL_LED_ON_H, []byte{onB & WORD}) + p.i2cBus.WriteReg(ALL_LED_OFF_L, []byte{offB & WORD}) + p.i2cBus.WriteReg(ALL_LED_OFF_H, []byte{offB & WORD}) } -func (p *PCA9685) setPwm(pwm int, on int, off int) { +func (p *PCA9685) setPwm(pwm int, on uint16, off uint16) { onB := byte(on) offB := byte(off) - p.i2cBus.WriteReg(LED0_ON_L+byte(4)*byte(pwm), []byte{onB & BYTE}) + p.i2cBus.WriteReg(LED0_ON_L+byte(4)*byte(pwm), []byte{onB & WORD}) p.i2cBus.WriteReg(LED0_ON_H+byte(4)*byte(pwm), []byte{onB >> 8}) - p.i2cBus.WriteReg(LED0_OFF_L+byte(4)*byte(pwm), []byte{offB & BYTE}) + p.i2cBus.WriteReg(LED0_OFF_L+byte(4)*byte(pwm), []byte{offB & WORD}) p.i2cBus.WriteReg(LED0_OFF_H+byte(4)*byte(pwm), []byte{offB >> 8}) } func (pwm *Pwm) setPercentage(percentage float32) error { - if percentage < 0.0 || percentage > 100.0 || pwm.pca.maxPulse > 4095 { + if percentage < 0.0 || percentage > 100.0 || pwm.pca.maxPulse > 65536 { return errors.New(fmt.Sprintf("Percentage must be between 0.0 and 100.0. Got %v.", percentage)) } pwm.pca.log.Info(fmt.Sprintf("Setting pwm #%v to %v%% at \"%v\" device.", pwm.pin, percentage, pwm.pca.name)) - pwm.pca.setPwm(pwm.pin, 0, int(percentage*float32(pwm.pca.maxPulse))) + pwm.pca.setPwm(pwm.pin, 0, uint16(percentage*float32(pwm.pca.maxPulse))) return nil }