Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There is a bug in PCA9685 implementation #196

Closed
dafik opened this issue Mar 30, 2024 · 1 comment
Closed

There is a bug in PCA9685 implementation #196

dafik opened this issue Mar 30, 2024 · 1 comment

Comments

@dafik
Copy link

dafik commented Mar 30, 2024

When you call "on" with float 1

public void setValue(int channel, float value) throws RuntimeIOException {
		if (value < 0 || value > 1) {
			throw new IllegalArgumentException("PWM value must 0..1, you requested " + value);
		}
		int off = Math.round(value * RANGE);
		setPwm(channel, 0, off);
	}

off is assigned with 4096, beacause of RANGE constant

next in setPwm method, validateOnOff(on, off) is called

which causes IllegalArgumentException to be thrown

if (off < 0 || off >= RANGE) {
	throw new IllegalArgumentException(String.format("Error: off (" + off + ") must be 0.." + (RANGE - 1)));
}

I think, as pca is 12bit resolution, max value should be 4095

4095 = 1111 1111 1111
4096 = 1 0000 0000 0000

and validation should be open set (> not >=)

	private static void validateOnOff(int on, int off) {
		if (on < 0 || on > RANGE) {
			throw new IllegalArgumentException(String.format("Error: on (" + on + ") must be 0.." + (RANGE - 1)));
		}
		if (off < 0 || off > RANGE) {
			throw new IllegalArgumentException(String.format("Error: off (" + off + ") must be 0.." + (RANGE - 1)));
		}
		// Off must be after on
		if (off < on) {
			throw new IllegalArgumentException("Off value (" + off + ") must be > on value (" + on + ")");
		}
		// Total must be < 4096
		if (on + off > RANGE) {
			throw new IllegalArgumentException(String.format("Error: on (%d) + off (%d) must be < %d", on, off, RANGE));
		}
	}
@mattjlewis
Copy link
Owner

Thank you - will fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants