public class PCA9685GpioProvider extends GpioProviderBase implements GpioProvider
This GPIO provider implements the PCA9685 16-channel, 12-bit PWM I2C-bus LED/Servo controller as native Pi4J GPIO pins. The PCA9685 is connected via I2C connection to the Raspberry Pi and provides 16 PWM pins that can be used for PWM output.
More information about the PCA9685 can be found here:
PCA9685.pdf
...and especially about the board here:
Adafruit 16-Channel 12-bit PWM/Servo Driver
Modifier and Type | Field and Description |
---|---|
static BigDecimal |
ANALOG_SERVO_FREQUENCY
This would result in a period duration of ~22ms which is save for all type of servo
|
protected PCA9685GpioProviderPinCache[] |
cache |
static BigDecimal |
DEFAULT_FREQUENCY |
static String |
DESCRIPTION |
static BigDecimal |
DIGITAL_SERVO_FREQUENCY
This would result in a period duration of ~11ms which is recommended when using digital servos ONLY!
|
static int |
INTERNAL_CLOCK_FREQ |
static BigDecimal |
MAX_FREQUENCY |
static BigDecimal |
MIN_FREQUENCY |
static String |
NAME |
static int |
PWM_STEPS |
DEFAULT_CACHE_SIZE, isshutdown, listeners
Constructor and Description |
---|
PCA9685GpioProvider(I2CBus bus,
int address) |
PCA9685GpioProvider(I2CBus bus,
int address,
BigDecimal targetFrequency) |
PCA9685GpioProvider(I2CBus bus,
int address,
BigDecimal targetFrequency,
BigDecimal frequencyCorrectionFactor) |
PCA9685GpioProvider(int busNumber,
int address) |
Modifier and Type | Method and Description |
---|---|
int |
calculateOffPositionForPulseDuration(int duration)
Calculates the OFF position for a certain pulse duration.
|
BigDecimal |
getFrequency() |
String |
getName() |
int |
getPeriodDurationMicros() |
protected PCA9685GpioProviderPinCache |
getPinCache(Pin pin) |
int |
getPwm(Pin pin) |
int[] |
getPwmOnOffValues(Pin pin) |
void |
reset()
Reset all outputs (set to always OFF)
|
void |
setAlwaysOff(Pin pin)
Permanently sets the output to Low (no PWM anymore).
The LEDn_OFF_H output control bit 4, when set to logic 1, causes the output to be always OFF. |
void |
setAlwaysOn(Pin pin)
Permanently sets the output to High (no PWM anymore).
The LEDn_ON_H output control bit 4, when set to logic 1, causes the output to be always ON. |
void |
setFrequency(BigDecimal frequency)
Target frequency (accuracy is around +/- 5%!)
|
void |
setFrequency(BigDecimal targetFrequency,
BigDecimal frequencyCorrectionFactor)
The built-in Oscillator runs at ~25MHz.
|
void |
setPwm(Pin pin,
int duration)
Set pulse duration in microseconds.
Make sure duration doesn't exceed period time(1'000'000/freq)! |
void |
setPwm(Pin pin,
int onPosition,
int offPosition)
The LEDn_ON and LEDn_OFF counts can vary from 0 to 4095 max.
The LEDn_ON and LEDn_OFF count registers should never be programmed with the same values. |
void |
shutdown() |
addListener, dispatchPinAnalogValueChangeEvent, dispatchPinDigitalStateChangeEvent, export, export, getMode, getPullResistance, getState, getValue, hasPin, isExported, isShutdown, removeAllListeners, removeListener, setMode, setPullResistance, setPwmRange, setState, setValue, unexport
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
addListener, export, export, getMode, getPullResistance, getState, getValue, hasPin, isExported, isShutdown, removeAllListeners, removeListener, setMode, setPullResistance, setPwmRange, setState, setValue, unexport
public static final String NAME
public static final String DESCRIPTION
public static final int INTERNAL_CLOCK_FREQ
public static final BigDecimal MIN_FREQUENCY
public static final BigDecimal MAX_FREQUENCY
public static final BigDecimal ANALOG_SERVO_FREQUENCY
public static final BigDecimal DIGITAL_SERVO_FREQUENCY
public static final BigDecimal DEFAULT_FREQUENCY
public static final int PWM_STEPS
protected PCA9685GpioProviderPinCache[] cache
public PCA9685GpioProvider(int busNumber, int address) throws I2CFactory.UnsupportedBusNumberException, IOException
public PCA9685GpioProvider(I2CBus bus, int address) throws IOException
IOException
public PCA9685GpioProvider(I2CBus bus, int address, BigDecimal targetFrequency) throws IOException
IOException
public PCA9685GpioProvider(I2CBus bus, int address, BigDecimal targetFrequency, BigDecimal frequencyCorrectionFactor) throws IOException
IOException
public void setFrequency(BigDecimal frequency)
frequency
- desired PWM frequency#setFrequency(int, BigDecimal)
public void setFrequency(BigDecimal targetFrequency, BigDecimal frequencyCorrectionFactor)
Note: correction is limited to a certain degree because the calculated prescale value has to be rounded to an integer value!
Example:
target freq: 50Hz
actual freq: 52.93Hz
correction factor: 52.93 / 50 = 1.0586
targetFrequency
- desired frequencyfrequencyCorrectionFactor
- 'actual frequency' / 'target frequency'public void setPwm(Pin pin, int duration)
setPwm
in interface GpioProvider
setPwm
in class GpioProviderBase
pin
- represents channel 0..15duration
- pulse duration in microsecondspublic void setPwm(Pin pin, int onPosition, int offPosition)
Because the loading of the LEDn_ON and LEDn_OFF registers is via the I2C-bus, and asynchronous to the internal oscillator, we want to ensure that we do not see any visual artifacts of changing the ON and OFF values. This is achieved by updating the changes at the end of the LOW cycle.
pin
- represents channel 0..15onPosition
- value between 0 and 4095offPosition
- value between 0 and 4095public void setAlwaysOn(Pin pin)
pin
- represents channel 0..15public void setAlwaysOff(Pin pin)
pin
- represents channel 0..15public BigDecimal getFrequency()
public int getPeriodDurationMicros()
public int calculateOffPositionForPulseDuration(int duration)
duration
- pulse duration in microsecondspublic int[] getPwmOnOffValues(Pin pin)
pin
- represents channel 0..15public void reset()
protected PCA9685GpioProviderPinCache getPinCache(Pin pin)
getPinCache
in class GpioProviderBase
public int getPwm(Pin pin)
getPwm
in interface GpioProvider
getPwm
in class GpioProviderBase
public String getName()
getName
in interface GpioProvider
getName
in class GpioProviderBase
public void shutdown()
shutdown
in interface GpioProvider
shutdown
in class GpioProviderBase
Copyright © 2012–2016 Pi4J. All rights reserved.