reformated pca class
This commit is contained in:
13
pca9685.go
13
pca9685.go
@@ -68,11 +68,8 @@ func (p *PCA9685) NewPwm(pin int) *Pwm {
|
|||||||
|
|
||||||
func (p *PCA9685) Init() {
|
func (p *PCA9685) Init() {
|
||||||
if p.initiated {
|
if p.initiated {
|
||||||
|
|
||||||
p.log.Warning(fmt.Sprintf("Device \"%v\" already initiated!", p.name))
|
p.log.Warning(fmt.Sprintf("Device \"%v\" already initiated!", p.name))
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
p.log.Info(fmt.Sprintf("Initiating \"%v\" PCA9685 device", p.name))
|
p.log.Info(fmt.Sprintf("Initiating \"%v\" PCA9685 device", p.name))
|
||||||
|
|
||||||
p.setAllPwm(0, 0)
|
p.setAllPwm(0, 0)
|
||||||
@@ -84,7 +81,6 @@ func (p *PCA9685) Init() {
|
|||||||
err := p.i2cBus.ReadReg(MODE1, []byte{mode1})
|
err := p.i2cBus.ReadReg(MODE1, []byte{mode1})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
||||||
p.log.Error("Can't read!")
|
p.log.Error("Can't read!")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -104,14 +100,11 @@ func (p *PCA9685) Init() {
|
|||||||
|
|
||||||
func (p *PCA9685) SwichOn(pwm []int) error {
|
func (p *PCA9685) SwichOn(pwm []int) error {
|
||||||
if !p.initiated {
|
if !p.initiated {
|
||||||
|
|
||||||
return errors.New(fmt.Sprintf("Device \"%v\"is not initiated!", p.name))
|
return errors.New(fmt.Sprintf("Device \"%v\"is not initiated!", p.name))
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < len(pwm); i++ {
|
for i := 0; i < len(pwm); i++ {
|
||||||
|
|
||||||
p.log.Info(fmt.Sprintf("Swiching on pwm #%v", pwm[i]))
|
p.log.Info(fmt.Sprintf("Swiching on pwm #%v", pwm[i]))
|
||||||
|
|
||||||
p.setPwm(pwm[i], p.minPulse, p.maxPulse)
|
p.setPwm(pwm[i], p.minPulse, p.maxPulse)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,14 +113,11 @@ func (p *PCA9685) SwichOn(pwm []int) error {
|
|||||||
|
|
||||||
func (p *PCA9685) SwichOff(pwm []int) error {
|
func (p *PCA9685) SwichOff(pwm []int) error {
|
||||||
if !p.initiated {
|
if !p.initiated {
|
||||||
|
|
||||||
return errors.New(fmt.Sprintf("Device \"%v\"is not initiated!", p.name))
|
return errors.New(fmt.Sprintf("Device \"%v\"is not initiated!", p.name))
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < len(pwm); i++ {
|
for i := 0; i < len(pwm); i++ {
|
||||||
|
|
||||||
p.log.Info(fmt.Sprintf("Swiching off pwm #%v", pwm[i]))
|
p.log.Info(fmt.Sprintf("Swiching off pwm #%v", pwm[i]))
|
||||||
|
|
||||||
p.setPwm(pwm[i], 0, p.minPulse)
|
p.setPwm(pwm[i], 0, p.minPulse)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,7 +141,6 @@ func (p *PCA9685) setPwmFreq(freqHz float32) {
|
|||||||
err := p.i2cBus.ReadReg(MODE1, []byte{oldMode})
|
err := p.i2cBus.ReadReg(MODE1, []byte{oldMode})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
||||||
p.log.Error("Can't read!")
|
p.log.Error("Can't read!")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -190,12 +179,10 @@ func (p *PCA9685) setPwm(pwm int, on uint16, off uint16) {
|
|||||||
|
|
||||||
func (pwm *Pwm) setPercentage(percentage float32) error {
|
func (pwm *Pwm) setPercentage(percentage float32) error {
|
||||||
if percentage < 0.0 || percentage > 100.0 {
|
if percentage < 0.0 || percentage > 100.0 {
|
||||||
|
|
||||||
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, uint16(percentage*float32(pwm.pca.maxPulse)))
|
pwm.pca.setPwm(pwm.pin, 0, uint16(percentage*float32(pwm.pca.maxPulse)))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
Reference in New Issue
Block a user