forked from viamrobotics/rdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RSDK-8494] Validate PWM-setting requests (viamrobotics#4309)
The Jira ticket was specifically for the pinctrl module, but I think it should be generalized to all boards. So, here we go. Thanks to John for the suggestion of calling "slightly over 1.0" a non-error value! If you want a different threshold for "slightly over," I'm happy to change it. The rpi4 doesn't validate that the frequency is positive because apparently setting it to 0 already sets it to the default of 800 Hz, and it's a uint so can't be negative. I haven't tested on actual hardware, but the new unit tests pass. I'm willing to test on hardware if you think it's important.
- Loading branch information
1 parent
69e97dc
commit 0a078d5
Showing
5 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package board_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"go.viam.com/test" | ||
|
||
"go.viam.com/rdk/components/board" | ||
) | ||
|
||
func TestValidatePWMDutyCycle(t *testing.T) { | ||
// Normal values are unchanged | ||
val, err := board.ValidatePWMDutyCycle(0.5) | ||
test.That(t, err, test.ShouldBeNil) | ||
test.That(t, val, test.ShouldEqual, 0.5) | ||
|
||
// No negative values | ||
_, err = board.ValidatePWMDutyCycle(-1.0) | ||
test.That(t, err, test.ShouldNotBeNil) | ||
|
||
// Values slightly over 100% get rounded down | ||
val, err = board.ValidatePWMDutyCycle(1.005) | ||
test.That(t, err, test.ShouldBeNil) | ||
test.That(t, val, test.ShouldEqual, 1.0) | ||
|
||
// No values well over 100% | ||
_, err = board.ValidatePWMDutyCycle(2.0) | ||
test.That(t, err, test.ShouldNotBeNil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters