1
0
mirror of https://github.com/kidoman/embd synced 2024-12-22 12:50:19 +01:00

refactoring

This commit is contained in:
Karan Misra 2014-01-07 07:32:26 +05:30
parent 0135623f1c
commit 0e32dc099e
2 changed files with 54 additions and 76 deletions

View File

@ -20,54 +20,32 @@ const (
pwm0OnLowReg = 0x6
)
// A PCA9685 interface implements access to the controller.
type PCA9685 interface {
// SetPwm sets the ON and OFF time registers for pwm signal shaping.
// n: channel 0-15
// onTime/offTime: 0-4095
SetPwm(n int, onTime int, offTime int) error
// Wake allows the controller to exit sleep mode and resume with PWM generation.
Wake() error
// Sleep puts the controller in sleep mode. Does not change the pwm control registers.
Sleep() error
// Close stops the controller and resets mode and pwm controller registers.
Close() error
// SetDebug is used to enable logging (debug mode).
SetDebug(status bool)
}
type pca9685 struct {
bus i2c.Bus
addr byte
freq int
// PCA9685 represents a PCA9685 PWM generator.
type PCA9685 struct {
Bus i2c.Bus
Addr byte
Freq int
initialized bool
mu sync.RWMutex
debug bool
Debug bool
}
// The pca9685 controller supports pwm frequencies from 40Hz to 1000Hz
// New creates a new PCA9685 interface.
func New(bus i2c.Bus, addr byte, freq int) PCA9685 {
return &pca9685{bus: bus, addr: addr, freq: freq}
func New(bus i2c.Bus, addr byte, freq int) *PCA9685 {
return &PCA9685{
Bus: bus,
Addr: addr,
Freq: freq,
}
}
// SetDebug is used to enable logging (debug mode).
func (d *pca9685) SetDebug(status bool) {
d.debug = status
func (d *PCA9685) mode1Reg() (byte, error) {
return d.Bus.ReadByteFromReg(d.Addr, mode1RegAddr)
}
func (d *pca9685) mode1Reg() (byte, error) {
return d.bus.ReadByteFromReg(d.addr, mode1RegAddr)
}
func (d *pca9685) setup() (err error) {
func (d *PCA9685) setup() (err error) {
d.mu.RLock()
if d.initialized {
d.mu.RUnlock()
@ -78,8 +56,8 @@ func (d *pca9685) setup() (err error) {
d.mu.Lock()
defer d.mu.Unlock()
preScaleValue := byte(math.Floor(float64(clockFreq/(pwmControlPoints*d.freq))+float64(0.5)) - 1)
if d.debug {
preScaleValue := byte(math.Floor(float64(clockFreq/(pwmControlPoints*d.Freq))+float64(0.5)) - 1)
if d.Debug {
log.Printf("pca9685: calculated Prescale value = %#02x", preScaleValue)
}
@ -87,7 +65,7 @@ func (d *pca9685) setup() (err error) {
if err != nil {
return
}
if d.debug {
if d.Debug {
log.Printf("pca9685: read MODE1 Reg [regAddr: %#02x] Value: [%v]", mode1RegAddr, mode1Reg)
}
@ -95,10 +73,10 @@ func (d *pca9685) setup() (err error) {
return
}
if err = d.bus.WriteByteToReg(d.addr, preScaleRegAddr, byte(preScaleValue)); err != nil {
if err = d.Bus.WriteByteToReg(d.Addr, preScaleRegAddr, byte(preScaleValue)); err != nil {
return
}
if d.debug {
if d.Debug {
log.Printf("pca9685: prescale value [%#02x] written to PRE_SCALE Reg [regAddr: %#02x]", preScaleValue, preScaleRegAddr)
}
@ -107,16 +85,16 @@ func (d *pca9685) setup() (err error) {
}
newmode := ((mode1Reg | 0x01) & 0xDF)
if err = d.bus.WriteByteToReg(d.addr, mode1RegAddr, newmode); err != nil {
if err = d.Bus.WriteByteToReg(d.Addr, mode1RegAddr, newmode); err != nil {
return
}
if d.debug {
if d.Debug {
log.Printf("pca9685: new mode [%#02x] [disabling register auto increment] written to MODE1 Reg [regAddr: %#02x]", newmode, mode1RegAddr)
}
d.initialized = true
if d.debug {
log.Printf("pca9685: driver initialized with pwm freq: %v", d.freq)
if d.Debug {
log.Printf("pca9685: driver initialized with pwm freq: %v", d.Freq)
}
return
@ -125,7 +103,7 @@ func (d *pca9685) setup() (err error) {
// SetPwm sets the ON and OFF time registers for pwm signal shaping.
// channel: 0-15
// onTime/offTime: 0-4095
func (d *pca9685) SetPwm(n, onTime, offTime int) (err error) {
func (d *PCA9685) SetPwm(n, onTime, offTime int) (err error) {
if err = d.setup(); err != nil {
return
}
@ -137,34 +115,34 @@ func (d *pca9685) SetPwm(n, onTime, offTime int) (err error) {
offTimeLow := byte(offTime & 0xFF)
offTimeHigh := byte(offTime >> 8)
if err = d.bus.WriteByteToReg(d.addr, onTimeLowReg, onTimeLow); err != nil {
if err = d.Bus.WriteByteToReg(d.Addr, onTimeLowReg, onTimeLow); err != nil {
return
}
if d.debug {
if d.Debug {
log.Printf("pca9685: writing On-Time Low [%#02x] to CHAN%v_ON_L reg [RegAddr: %#02x]", onTimeLow, n, onTimeLowReg)
}
onTimeHighReg := onTimeLowReg + 1
if err = d.bus.WriteByteToReg(d.addr, onTimeHighReg, onTimeHighReg); err != nil {
if err = d.Bus.WriteByteToReg(d.Addr, onTimeHighReg, onTimeHighReg); err != nil {
return
}
if d.debug {
if d.Debug {
log.Printf("pca9685: writing On-Time High [%#02x] to CHAN%v_ON_H reg [RegAddr: %#02x]", onTimeHigh, n, onTimeHighReg)
}
offTimeLowReg := onTimeHighReg + 1
if err = d.bus.WriteByteToReg(d.addr, offTimeLowReg, offTimeLow); err != nil {
if err = d.Bus.WriteByteToReg(d.Addr, offTimeLowReg, offTimeLow); err != nil {
return
}
if d.debug {
if d.Debug {
log.Printf("pca9685: writing Off-Time Low [%#02x] to CHAN%v_OFF_L reg [RegAddr: %#02x]", offTimeLow, n, offTimeLowReg)
}
offTimeHighReg := offTimeLowReg + 1
if err = d.bus.WriteByteToReg(d.addr, offTimeHighReg, offTimeHigh); err != nil {
if err = d.Bus.WriteByteToReg(d.Addr, offTimeHighReg, offTimeHigh); err != nil {
return
}
if d.debug {
if d.Debug {
log.Printf("pca9685: writing Off-Time High [%#02x] to CHAN%v_OFF_H reg [RegAddr: %#02x]", offTimeHigh, n, offTimeHighReg)
}
@ -172,7 +150,7 @@ func (d *pca9685) SetPwm(n, onTime, offTime int) (err error) {
}
// Close stops the controller and resets mode and pwm controller registers.
func (d *pca9685) Close() (err error) {
func (d *PCA9685) Close() (err error) {
if err = d.setup(); err != nil {
return
}
@ -181,37 +159,37 @@ func (d *pca9685) Close() (err error) {
return
}
if d.debug {
if d.Debug {
log.Println("pca9685: reset request received")
}
if err = d.bus.WriteByteToReg(d.addr, mode1RegAddr, 0x00); err != nil {
if err = d.Bus.WriteByteToReg(d.Addr, mode1RegAddr, 0x00); err != nil {
return
}
if d.debug {
if d.Debug {
log.Printf("pca9685: cleaning up all PWM control registers")
}
for regAddr := 0x0; regAddr <= 0x45; regAddr++ {
if err = d.bus.WriteByteToReg(d.addr, byte(regAddr), 0x00); err != nil {
if err = d.Bus.WriteByteToReg(d.Addr, byte(regAddr), 0x00); err != nil {
return
}
}
if d.debug {
if d.Debug {
log.Printf("pca9685: done Cleaning up all PWM control registers")
}
if d.debug {
if d.Debug {
log.Println("pca9685: controller reset")
}
return
}
func (d *pca9685) sleep() (err error) {
if d.debug {
func (d *PCA9685) sleep() (err error) {
if d.Debug {
log.Println("pca9685: sleep request received")
}
@ -220,14 +198,14 @@ func (d *pca9685) sleep() (err error) {
return
}
sleepmode := (mode1Reg & 0x7F) | 0x10
if err = d.bus.WriteByteToReg(d.addr, mode1RegAddr, sleepmode); err != nil {
if err = d.Bus.WriteByteToReg(d.Addr, mode1RegAddr, sleepmode); err != nil {
return
}
if d.debug {
if d.Debug {
log.Printf("pca9685: sleep mode [%#02x] written to MODE1 Reg [regAddr: %#02x]", sleepmode, mode1RegAddr)
}
if d.debug {
if d.Debug {
log.Println("pca9685: controller set to Sleep mode")
}
@ -235,7 +213,7 @@ func (d *pca9685) sleep() (err error) {
}
// Sleep puts the controller in sleep mode. Does not change the pwm control registers.
func (d *pca9685) Sleep() (err error) {
func (d *PCA9685) Sleep() (err error) {
if err = d.setup(); err != nil {
return
}
@ -243,8 +221,8 @@ func (d *pca9685) Sleep() (err error) {
return d.sleep()
}
func (d *pca9685) wake() (err error) {
if d.debug {
func (d *PCA9685) wake() (err error) {
if d.Debug {
log.Println("pca9685: wake request received")
}
@ -254,10 +232,10 @@ func (d *pca9685) wake() (err error) {
}
wakeMode := mode1Reg & 0xEF
if (mode1Reg & 0x80) == 0x80 {
if err = d.bus.WriteByteToReg(d.addr, mode1RegAddr, wakeMode); err != nil {
if err = d.Bus.WriteByteToReg(d.Addr, mode1RegAddr, wakeMode); err != nil {
return
}
if d.debug {
if d.Debug {
log.Printf("pca9685: wake mode [%#02x] written to MODE1 Reg [regAddr: %#02x]", wakeMode, mode1RegAddr)
}
@ -265,10 +243,10 @@ func (d *pca9685) wake() (err error) {
}
restartOpCode := wakeMode | 0x80
if err = d.bus.WriteByteToReg(d.addr, mode1RegAddr, restartOpCode); err != nil {
if err = d.Bus.WriteByteToReg(d.Addr, mode1RegAddr, restartOpCode); err != nil {
return
}
if d.debug {
if d.Debug {
log.Printf("pca9685: restart mode [%#02x] written to MODE1 Reg [regAddr: %#02x]", restartOpCode, mode1RegAddr)
}
@ -276,7 +254,7 @@ func (d *pca9685) wake() (err error) {
}
// Wake allows the controller to exit sleep mode and resume with PWM generation.
func (d *pca9685) Wake() (err error) {
func (d *PCA9685) Wake() (err error) {
if err = d.setup(); err != nil {
return
}

View File

@ -17,7 +17,7 @@ func main() {
}
pca9685 := pca9685.New(bus, 0x41, 1000)
pca9685.SetDebug(true)
pca9685.Debug = true
defer pca9685.Close()
if err := pca9685.SetPwm(15, 0, 1000); err != nil {