refined pulse handling

This commit is contained in:
2017-12-12 23:32:09 +01:00
parent d8612c11c4
commit 71400d7128
2 changed files with 22 additions and 22 deletions

View File

@@ -32,8 +32,8 @@ type config struct {
Pca9685 struct { Pca9685 struct {
Device string Device string
Address int Address int
MinPulse int MinPulse uint16
MaxPulse int MaxPulse uint16
} }
} }

View File

@@ -23,15 +23,15 @@ const (
ALL_LED_OFF_H byte = 0xFD ALL_LED_OFF_H byte = 0xFD
OUTDRV byte = 0x04 OUTDRV byte = 0x04
SLEEP byte = 0x10 SLEEP byte = 0x10
BYTE byte = 0xFF WORD byte = 0xFF
) )
type PCA9685 struct { type PCA9685 struct {
i2cBus *i2c.Device i2cBus *i2c.Device
name string name string
initiated bool initiated bool
minPulse int minPulse uint16
maxPulse int maxPulse uint16
log *logging.Logger log *logging.Logger
frequency float32 frequency float32
} }
@@ -42,7 +42,7 @@ type Pwm struct {
lastValue float32 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)) log.Info(fmt.Sprintf("Creating a new PCA9685 device. Alias: %v", name))
return &PCA9685{ return &PCA9685{
@@ -89,7 +89,7 @@ func (p *PCA9685) Init() {
return return
} }
mode1 &= BYTE mode1 &= WORD
mode1 = mode1 & ^SLEEP mode1 = mode1 & ^SLEEP
p.i2cBus.WriteReg(MODE1, []byte{mode1 & 0xFF}) p.i2cBus.WriteReg(MODE1, []byte{mode1 & 0xFF})
@@ -155,48 +155,48 @@ func (p *PCA9685) setPwmFreq(freqHz float32) {
p.log.Error("Can't read!") p.log.Error("Can't read!")
} }
oldMode &= BYTE oldMode &= WORD
newMode := (oldMode & 0x7F) | 0x10 newMode := (oldMode & 0x7F) | 0x10
p.i2cBus.WriteReg(MODE1, []byte{newMode & BYTE}) p.i2cBus.WriteReg(MODE1, []byte{newMode & WORD})
p.i2cBus.WriteReg(PRESCALE, []byte{byte(prescale) & BYTE}) p.i2cBus.WriteReg(PRESCALE, []byte{byte(prescale) & WORD})
p.i2cBus.WriteReg(MODE1, []byte{oldMode & BYTE}) p.i2cBus.WriteReg(MODE1, []byte{oldMode & WORD})
time.Sleep(5 * time.Millisecond) 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) onB := byte(on)
offB := byte(off) offB := byte(off)
p.i2cBus.WriteReg(ALL_LED_ON_L, []byte{onB & BYTE}) p.i2cBus.WriteReg(ALL_LED_ON_L, []byte{onB & WORD})
p.i2cBus.WriteReg(ALL_LED_ON_H, []byte{onB & BYTE}) p.i2cBus.WriteReg(ALL_LED_ON_H, []byte{onB & WORD})
p.i2cBus.WriteReg(ALL_LED_OFF_L, []byte{offB & BYTE}) p.i2cBus.WriteReg(ALL_LED_OFF_L, []byte{offB & WORD})
p.i2cBus.WriteReg(ALL_LED_OFF_H, []byte{offB & BYTE}) 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) onB := byte(on)
offB := byte(off) 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_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}) p.i2cBus.WriteReg(LED0_OFF_H+byte(4)*byte(pwm), []byte{offB >> 8})
} }
func (pwm *Pwm) setPercentage(percentage float32) error { 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)) 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.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 return nil
} }