pca9685

package
v0.0.0-...-0087ba1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 25, 2023 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	I2CSWRSTADR      = 0x0
	PCA9685SWRSTBYTE = 0b0000_0110 // 0x06
)

Software reset addresses for generic i2c implementation and PCA specific SWRST. See section 7.6

View Source
const (
	SUBADR1 uint8 = 0x2
	SUBADR2 uint8 = 0x3
	SUBADR3 uint8 = 0x4

	MODE1      uint8 = 0x0
	MODE2      uint8 = 0x1
	ALLCALLADR uint8 = 0x05

	SWRESET uint8 = 0b0000_0011

	// Start of LED registries. corresponds to LED0_ON_L register.
	// Use LED function to get registries of a specific PWM channel register.
	LEDSTART = 0x06
)

Registries with nomenclature as seen in manual

View Source
const (
	RESET  byte = 0b1000_0000
	EXTCLK byte = 0b0100_0000
	AI     byte = 0b0010_0000
	SLEEP  byte = 0b0001_0000
)

MODE1

View Source
const (
	OUTDRV = 1 << 2
	INVRT  = 1 << 4
)

MODE2

View Source
const (
	// ALLLED is channel that selects registries to control all leds. Use with function LED
	ALLLED = 0xfa
	// PRESCALE Prescaling byte
	PRESCALE byte = 0xFE
)

Variables

View Source
var (
	ErrInvalidMode1 = errors.New("pca9685: data read from MODE1 not valid")
	ErrBadPeriod    = errors.New("pca9685: period must be in range 1..25ms")
)

Functions

func LED

func LED(ch uint8) (ONL, ONH, OFFL, OFFH uint8)

LED channels from 0-15. Returns 4 registries associated with the channel PWM signal. Channel 250 (0xFA) gives ALL_LED registers. The L suffix represents the 8 LSB of 12 bit timing, the H suffix represents the 4 MSB of the timing. This way you have 0-4095 timing options. ON or OFF will be a number between these two or will be simply ON or OFF (fifth bit of H registry).

The SetPWM implementation in this library does not do phase-shifting so OFF will always happen at time stamp 0. ON will solely decide duty cycle.

Types

type Dev

type Dev struct {
	// contains filtered or unexported fields
}

Dev is a handle to the PCA9685 device given an address (usually 0x47) and an i2c bus.

func New

func New(bus drivers.I2C, addr uint8) Dev

New creates a new instance of a PCA9685 device. It performs no IO on the i2c bus.

func (Dev) Configure

func (d Dev) Configure(cfg PWMConfig) error

Configure enables autoincrement, sets all PWM signals to logic low (Ground) and finally sets the Period.

func (Dev) IsConnected

func (d Dev) IsConnected() error

IsConnected returns error if read fails or if driver suspects device is not connected.

func (Dev) Set

func (d Dev) Set(channel uint8, on uint32)

Set sets the `on` value of a PWM channel in the range [0..15]. Max value `on` can take is 4095. Example:

d.Set(1, d.Top()/4)

sets the dutycycle of second (LED1) channel to 25%.

func (Dev) SetAI

func (d Dev) SetAI(ai bool) error

SetAI enables or disables autoincrement feature on device. Useful for writing to many consecutive registers in one shot.

func (Dev) SetAll

func (d Dev) SetAll(on uint32)

SetAll sets all PWM signals to a ON value. Equivalent of calling

Dev.Set(pca9685.ALLLED, value)

func (Dev) SetDrive

func (d Dev) SetDrive(outdrv bool) error

SetDrive configures PWM output connection in MODE2 register.

false: The 16 LEDn outputs are configured with an open-drain structure.
true: The 16 LEDn outputs are configured with a totem pole structure.

func (Dev) SetInverting

func (d Dev) SetInverting(_ uint8, inverting bool) error

SetInverting inverts ALL PCA9685 PWMs. The channel argument merely implements PWM interface.

Without inverting, a 25% duty cycle would mean the output is high for 25% of the time and low for the rest. Inverting flips the output as if a NOT gate was placed at the output, meaning that the output would be 25% low and 75% high with a duty cycle of 25%.

func (Dev) SetPeriod

func (d Dev) SetPeriod(period uint64) error

SetPeriod updates the period of this PWM integrated circuit in nanoseconds. To set a particular frequency, use the following formula:

period = 1e9 / frequency

In the equation above frequency is in Hertz.

If you use a period of 0, a period that works well for LEDs will be picked.

PCA9685 accepts frequencies inbetween [40..1000 Hz], or expressed as a period [1..25ms].

func (Dev) SetPhased

func (d Dev) SetPhased(channel uint8, on, off uint32)

SetPhased sets PWM on and off mark. The ON time, which is programmable, will be the time the LED output will be asserted and the OFF time, which is also programmable, will be the time when the LED output will be negated. In this way, the phase shift becomes completely programmable. The resolution for the phase shift is 1⁄4096 of the target frequency.

func (Dev) Sleep

func (d Dev) Sleep(sleepEnabled bool) error

Sleep sets/unsets SLEEP bit in MODE1.

if sleepEnabled
  Stops PWM. Allows writing to PRE_SCALE register.
else
  wakes PCA9685. Resumes PWM.

func (Dev) Top

func (d Dev) Top() uint32

Top returns max value PWM can take.

type DevBuffered

type DevBuffered struct {
	Dev
	// contains filtered or unexported fields
}

DevBuffered provides a way of performing one-shot writes on all PWM signals. This is useful when working with systems which require as little as possible I/O overhead.

func NewBuffered

func NewBuffered(bus drivers.I2C, addr uint8) *DevBuffered

New creates a new instance of a PCA9685 device. It performs no IO on the i2c bus.

func (*DevBuffered) PrepPhasedSet

func (b *DevBuffered) PrepPhasedSet(channel uint8, on, off uint32)

PrepPhasedSet prepares a phased PWM value to be written to the channel's register on Update() call.

func (*DevBuffered) PrepSet

func (b *DevBuffered) PrepSet(channel uint8, on uint32)

PrepSet prepares a value to be written to the channel's PWM register on Update() call.

func (*DevBuffered) Update

func (b *DevBuffered) Update() error

Update writes the prepared values to the PWM device registers in one shot.

type PWMConfig

type PWMConfig struct {
	Period uint64
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL