Skip to content

Commit

Permalink
Merge pull request #1113 from WildernessLabs/bug/pca-9685
Browse files Browse the repository at this point in the history
bug fix for PCA9685 100% on
  • Loading branch information
adrianstevens authored Jan 11, 2025
2 parents a48e654 + db085a5 commit 2812789
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,32 @@ internal PwmPort(
/// </summary>
public void Start()
{
if (DutyCycle >= 1.0)
{
// Special case for always ON - set bit 4 of ON_H register
if (Inverted)
{
controller.SetPwm(portNumber, 0, 4096); // This signals always-off
}
else
{
controller.SetPwm(portNumber, 4096, 0); // 4096 signals always-on
}
}
else if (DutyCycle <= 0)
{
// Special case for always OFF
if (Inverted)
{
controller.SetPwm(portNumber, 4096, 0); // This signals always-off
}
else
{
controller.SetPwm(portNumber, 0, 4096); // This signals always-off
}
}


// DEV NOTE: according to the data sheetdiagrams (starting on page 17)
// You tell it at what "tick" to turn on (from the start) and what tick to turn off
// Since it's a repeated tick, we can just always turn on a 0 (start of the cycle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,24 @@ private void SetPwm(byte pin, int on, int off)
throw new ArgumentException("Value has to be between 0 and 15", nameof(pin));
}

if (on is < 0 or > 4096)
// Special case for fully on
if (off == 4096)
{
throw new ArgumentException("Value has to be between 0 and 4096", nameof(on));
on = 4096; // Set the special "fully on" bit
off = 0;
}

if (off is < 0 or > 4096)
else
{
throw new ArgumentException("Value has to be between 0 and 4096", nameof(off));
// Normal validation for PWM operation
if (on is < 0 or > 4096)
{
throw new ArgumentException("Value has to be between 0 and 4096", nameof(on));
}

if (off is < 0 or > 4096)
{
throw new ArgumentException("Value has to be between 0 and 4096", nameof(off));
}
}

Write((byte)(Registers.Led0OnL + (4 * pin)), (byte)(on & 0xFF), (byte)(on >> 8), (byte)(off & 0xFF), (byte)(off >> 8));
Expand Down Expand Up @@ -208,6 +218,8 @@ public IPwmPort CreatePwmPort(IPin pin, Frequency frequency, float dutyCycle = 0
{
var portNumber = (byte)pin.Key;

Resolver.Log.Trace($"CreatePwmPort on port {portNumber} at {frequency.Hertz:N0}Hz and DC {dutyCycle:N2}", "Pca9685");

if (portNumber is < 0 or > 15)
{
throw new ArgumentException("Value must be between 0 and 15", nameof(portNumber));
Expand Down

0 comments on commit 2812789

Please # to comment.