C# Class Emlid.WindowsIot.Hardware.Components.NxpPca9685.NxpPca9685Device

PCA9685 PWM LED driver (hardware device), connected via I2C.
The PCA9685 is a 16 channel, 12-bit PWM LED controller with I2C bus connection.

This class enables direct control of the device intended for internal use. If exposed to other users, it should wrapped by a thread safe class which coordinates access and deals with object lifetime (dispose). See http://www.nxp.com/documents/data_sheet/PCA9685.pdf for more information.

Inheritance: DisposableObject
Mostra file Open project: emlid/Navio-SDK-Windows-IoT

Private Properties

Property Type Description
NxpPca9685Device System
OnChannelChanged void

Public Methods

Method Description
CalculateFrequency ( byte preScale, int clockSpeed ) : float

Calculates the effective frequency from a NxpPca9685Register.PreScale value and clock speed.

CalculatePreScale ( float frequency, int clockSpeed ) : byte

Calculates the NxpPca9685Register.PreScale value from a desired frequency and clock speed.

Due to scaling only certain frequencies are possible. To get the resulting frequency from the desired frequency it is necessary to re-calcualte the effective frequency back from the pre-scale, i.e. call CalculateFrequency.

Clear ( ) : void

Clears all channels cleanly, then updates all Channels.

To "cleanly" clear the channels, it is necessary to first ensure they are not disabled, set them to zero, then disable them. Otherwise the ON value and the low OFF value remain because writes are ignored when the OFF channel bit 12 is already set.

GetChannelAddress ( int index ) : byte

Gets the register address of a channel by index.

ReadAll ( ) : void

Reads all values from the device and updates properties.

ReadChannel ( int index ) : NxpPca9685Channel

Reads a whole channel value (on and off), and updates it in Channels.

ReadChannelOff ( int index ) : int

Reads the PWM "off" (falling) value of a channel, and updates it in Channels.

ReadChannelOn ( int index ) : int

Reads the PWM "on" (rising) value of a channel, and updates it in Channels.

ReadFrequency ( ) : float

Reads the pre-scale register and calculates the Frequency (and related properties) based on ClockSpeed.

ReadMode1 ( ) : NxpPca9685Mode1Bits

Reads the current value of the NxpPca9685Register.Mode1 register.

ReadMode2 ( ) : NxpPca9685Mode2Bits

Reads the current value of the NxpPca9685Register.Mode2 register.

Restart ( ) : void

Restarts the device with default options, then updates all properties.

Restart ( NxpPca9685Mode1Bits options ) : void

Restarts the device with additional options specified, then updates all properties.

Sleep ( ) : bool

Enters sleep mode.

Sets the NxpPca9685Register.Mode1 register NxpPca9685Mode1Bits.Sleep bit then waits for ModeSwitchDelay to allow the oscillator to stop.

Wake ( ) : bool

Leaves sleep mode.

Clears the NxpPca9685Register.Mode1 register NxpPca9685Mode1Bits.Sleep bit then waits for ModeSwitchDelay to allow the oscillator to start.

WriteChannel ( int index, NxpPca9685ChannelValue value ) : NxpPca9685Channel

Writes the "on" and "off" values of a channel together, and updates it in Channels.

WriteChannelLength ( int index, int length, int delay ) : NxpPca9685Channel

Calculates the "on" and "off" values of a channel from length (and optional delay), then writes them together, and updates it in Channels.

WriteChannelMilliseconds ( int index, float length, float delay ) : NxpPca9685Channel

Calculates the "on" and "off" values of a channel from milliseconds (and optional delay), then writes them together, and updates it in Channels.

WriteChannelOff ( int index, int value ) : void

Writes the PWM "off" (falling) value of a channel.

WriteChannelOn ( int index, int value ) : void

Writes the PWM "on" (rising) value of a channel.

WriteFrequency ( float frequency ) : float

Calculates the pre-scale value from the frequency (according to ClockSpeed) then writes that register, then calls ReadFrequency to update properties. Note the actual frequency may differ to the requested frequency due to clock scale (rounding).

The pre-scale can only be set during sleep mode. This method enters Sleep if necessary, then only if the device was awake before, calls Wake afterwards. It's important not to start output unexpectedly to avoid damage, i.e. if the device was sleeping before, the frequency is changed without starting the oscillator.

Protected Methods

Method Description
Dispose ( bool disposing ) : void

DisposableObject.Dispose(bool).

ReadAllChannels ( ) : void

Reads all channels and updates Channels.

Private Methods

Method Description
NxpPca9685Device ( Windows.Devices.I2c.I2cDevice device, int clockSpeed ) : System
OnChannelChanged ( object sender, EventArgs arguments ) : void

Writes channel values to the device when the Channels member changes.

Method Details

CalculateFrequency() public static method

Calculates the effective frequency from a NxpPca9685Register.PreScale value and clock speed.
public static CalculateFrequency ( byte preScale, int clockSpeed ) : float
preScale byte Pre-scale value from which to calculate frequency.
clockSpeed int Clock speed with with to calcualte the frequency.
return float

CalculatePreScale() public static method

Calculates the NxpPca9685Register.PreScale value from a desired frequency and clock speed.
Due to scaling only certain frequencies are possible. To get the resulting frequency from the desired frequency it is necessary to re-calcualte the effective frequency back from the pre-scale, i.e. call CalculateFrequency.
/// Thrown when an invalid frequency is used which causes the result to overflow a byte value. ///
public static CalculatePreScale ( float frequency, int clockSpeed ) : byte
frequency float /// Desired frequency. /// Must be between and to get a valid result. ///
clockSpeed int
return byte

Clear() public method

Clears all channels cleanly, then updates all Channels.
To "cleanly" clear the channels, it is necessary to first ensure they are not disabled, set them to zero, then disable them. Otherwise the ON value and the low OFF value remain because writes are ignored when the OFF channel bit 12 is already set.
public Clear ( ) : void
return void

Dispose() protected method

DisposableObject.Dispose(bool).
protected Dispose ( bool disposing ) : void
disposing bool
return void

GetChannelAddress() public static method

Gets the register address of a channel by index.
public static GetChannelAddress ( int index ) : byte
index int Zero based channel number (0-15) or 16 for the "all call" channel.
return byte

ReadAll() public method

Reads all values from the device and updates properties.
public ReadAll ( ) : void
return void

ReadAllChannels() protected method

Reads all channels and updates Channels.
protected ReadAllChannels ( ) : void
return void

ReadChannel() public method

Reads a whole channel value (on and off), and updates it in Channels.
public ReadChannel ( int index ) : NxpPca9685Channel
index int Zero based channel number (0-15).
return NxpPca9685Channel

ReadChannelOff() public method

Reads the PWM "off" (falling) value of a channel, and updates it in Channels.
public ReadChannelOff ( int index ) : int
index int Zero based channel number (0-15).
return int

ReadChannelOn() public method

Reads the PWM "on" (rising) value of a channel, and updates it in Channels.
public ReadChannelOn ( int index ) : int
index int Zero based channel number (0-15).
return int

ReadFrequency() public method

Reads the pre-scale register and calculates the Frequency (and related properties) based on ClockSpeed.
public ReadFrequency ( ) : float
return float

ReadMode1() public method

Reads the current value of the NxpPca9685Register.Mode1 register.
public ReadMode1 ( ) : NxpPca9685Mode1Bits
return NxpPca9685Mode1Bits

ReadMode2() public method

Reads the current value of the NxpPca9685Register.Mode2 register.
public ReadMode2 ( ) : NxpPca9685Mode2Bits
return NxpPca9685Mode2Bits

Restart() public method

Restarts the device with default options, then updates all properties.
public Restart ( ) : void
return void

Restart() public method

Restarts the device with additional options specified, then updates all properties.
public Restart ( NxpPca9685Mode1Bits options ) : void
options NxpPca9685Mode1Bits /// Optional mode 1 parameters to add to the final restart sequence. A logical OR is applied to this value and /// the standard , and /// bits. ///
return void

Sleep() public method

Enters sleep mode.
Sets the NxpPca9685Register.Mode1 register NxpPca9685Mode1Bits.Sleep bit then waits for ModeSwitchDelay to allow the oscillator to stop.
public Sleep ( ) : bool
return bool

Wake() public method

Leaves sleep mode.
Clears the NxpPca9685Register.Mode1 register NxpPca9685Mode1Bits.Sleep bit then waits for ModeSwitchDelay to allow the oscillator to start.
public Wake ( ) : bool
return bool

WriteChannel() public method

Writes the "on" and "off" values of a channel together, and updates it in Channels.
public WriteChannel ( int index, NxpPca9685ChannelValue value ) : NxpPca9685Channel
index int Zero based channel number (0-15) or 16 for the "all call" channel.
value NxpPca9685ChannelValue to write.
return NxpPca9685Channel

WriteChannelLength() public method

Calculates the "on" and "off" values of a channel from length (and optional delay), then writes them together, and updates it in Channels.
public WriteChannelLength ( int index, int length, int delay ) : NxpPca9685Channel
index int Zero based channel number (0-15) or 16 for the "all call" channel.
length int Pulse length in clock ticks.
delay int Optional delay in clock ticks.
return NxpPca9685Channel

WriteChannelMilliseconds() public method

Calculates the "on" and "off" values of a channel from milliseconds (and optional delay), then writes them together, and updates it in Channels.
public WriteChannelMilliseconds ( int index, float length, float delay ) : NxpPca9685Channel
index int Zero based channel number (0-15) or 16 for the "all call" channel.
length float /// Pulse length in milliseconds. Cannot be greater than one clock interval (1000 / frequency). ///
delay float Optional delay in milliseconds. Cannot be greater than one clock interval (1000 / frequency).
return NxpPca9685Channel

WriteChannelOff() public method

Writes the PWM "off" (falling) value of a channel.
Thrown when the is greater then .
public WriteChannelOff ( int index, int value ) : void
index int Zero based channel number (0-15) or 16 for the "all call" channel.
value int 12-bit channel value in the range 0-.
return void

WriteChannelOn() public method

Writes the PWM "on" (rising) value of a channel.
Thrown when the is greater than .
public WriteChannelOn ( int index, int value ) : void
index int Zero based channel number (0-15) or 16 for the "all call" channel.
value int 12-bit channel value in the range 0-.
return void

WriteFrequency() public method

Calculates the pre-scale value from the frequency (according to ClockSpeed) then writes that register, then calls ReadFrequency to update properties. Note the actual frequency may differ to the requested frequency due to clock scale (rounding).
The pre-scale can only be set during sleep mode. This method enters Sleep if necessary, then only if the device was awake before, calls Wake afterwards. It's important not to start output unexpectedly to avoid damage, i.e. if the device was sleeping before, the frequency is changed without starting the oscillator.
/// Thrown when is less than or greater than /// . ///
public WriteFrequency ( float frequency ) : float
frequency float Frequency to convert in Hz.
return float