-
Notifications
You must be signed in to change notification settings - Fork 616
Better abstraction for PWM: PwmChannel #576
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
54c2f70
Initial PwmChannel changes
shaggygi 0e60776
Porting SoftwarePwm on top of PwmChannel so that bindings that use Pw…
joperezr 233084c
Fix DCMotor, tiny fixes in Buzzer and SoftPwm
krwq 9651ef6
Adding windows IoT PwmChannel implementation
joperezr 4a6205d
fix XML comment
krwq 6bb79e8
remove Console.WriteLine from the product
krwq b155b5f
Fixing README files and add xml comments
joperezr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
191 changes: 191 additions & 0 deletions
191
src/System.Device.Gpio/System/Device/Pwm/Channels/UnixPwmChannel.Linux.cs
This file contains hidden or 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,191 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System.IO; | ||
| using System.Threading; | ||
|
|
||
| namespace System.Device.Pwm.Channels | ||
| { | ||
| /// <summary> | ||
| /// Represents a PWM channel running on Unix. | ||
| /// </summary> | ||
| internal class UnixPwmChannel : PwmChannel | ||
| { | ||
| private readonly int _chip; | ||
| private readonly int _channel; | ||
| private int _frequency; | ||
| private double _dutyCyclePercentage; | ||
| private readonly string _chipPath; | ||
| private readonly string _channelPath; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="UnixPwmChannel"/> class. | ||
| /// </summary> | ||
| /// <param name="chip">The PWM chip number.</param> | ||
| /// <param name="channel">The PWM channel number.</param> | ||
| /// <param name="frequency">The frequency in hertz.</param> | ||
| /// <param name="dutyCyclePercentage">The duty cycle percentage represented as a value between 0.0 and 1.0.</param> | ||
| public UnixPwmChannel( | ||
| int chip, | ||
| int channel, | ||
| int frequency = 400, | ||
| double dutyCyclePercentage = 0.5) | ||
| { | ||
| _chipPath = $"/sys/class/pwm/pwmchip{_chip}"; | ||
| _channelPath = $"{_chipPath}/pwm{_channel}"; | ||
| _chip = chip; | ||
| _channel = channel; | ||
| Validate(); | ||
| Open(); | ||
| SetFrequency(frequency); | ||
| DutyCyclePercentage = dutyCyclePercentage; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The frequency in hertz. | ||
| /// </summary> | ||
| public override int Frequency | ||
| { | ||
| get | ||
| { | ||
| return _frequency; | ||
| } | ||
| set | ||
| { | ||
| SetFrequency(value); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The duty cycle percentage represented as a value between 0.0 and 1.0. | ||
| /// </summary> | ||
| public override double DutyCyclePercentage | ||
| { | ||
| get | ||
| { | ||
| return _dutyCyclePercentage; | ||
| } | ||
| set | ||
| { | ||
| SetDutyCyclePercentage(value); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the frequency period in nanoseconds. | ||
| /// </summary> | ||
| /// <param name="frequency">The frequency in hertz.</param> | ||
| /// <returns>The frequency period in nanoseconds.</returns> | ||
| private static int GetPeriodInNanoSeconds(int frequency) | ||
| { | ||
| // In Linux, the period needs to be a whole number and can't have a decimal point. | ||
| return (int)((1.0 / frequency) * 1_000_000_000); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sets the frequency for the channel. | ||
| /// </summary> | ||
| /// <param name="frequency">The frequency in hertz to set.</param> | ||
| private void SetFrequency(int frequency) | ||
| { | ||
| if (frequency < 0) | ||
joperezr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(frequency), frequency, "Value must not be negative."); | ||
| } | ||
joperezr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| int periodInNanoSeconds = GetPeriodInNanoSeconds(frequency); | ||
| File.WriteAllText($"{_channelPath}/period", Convert.ToString(periodInNanoSeconds)); | ||
| _frequency = frequency; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sets the duty cycle percentage for the channel. | ||
| /// </summary> | ||
| /// <param name="dutyCyclePercentage">The duty cycle percentage to set represented as a value between 0.0 and 1.0.</param> | ||
| private void SetDutyCyclePercentage(double dutyCyclePercentage) | ||
| { | ||
| if (dutyCyclePercentage < 0 || dutyCyclePercentage > 1) | ||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(dutyCyclePercentage), dutyCyclePercentage, "Value must be between 0.0 and 1.0."); | ||
| } | ||
|
|
||
| // In Linux, the period needs to be a whole number and can't have decimal point. | ||
| int dutyCycleInNanoSeconds = (int)(GetPeriodInNanoSeconds(_frequency) * dutyCyclePercentage); | ||
| File.WriteAllText($"{_channelPath}/duty_cycle", Convert.ToString(dutyCycleInNanoSeconds)); | ||
| _dutyCyclePercentage = dutyCyclePercentage; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Verifies the specified chip and channel are available. | ||
| /// </summary> | ||
| private void Validate() | ||
| { | ||
| if (!Directory.Exists(_chipPath)) | ||
| { | ||
| throw new ArgumentException($"The chip number {_chip} is invalid or is not enabled."); | ||
| } | ||
|
|
||
| string npwmPath = $"{_chipPath}/npwm"; | ||
|
|
||
| if (int.TryParse(File.ReadAllText(npwmPath), out int numberOfSupportedChannels)) | ||
| { | ||
| if (_channel < 0 || _channel >= numberOfSupportedChannels) | ||
| { | ||
| throw new ArgumentException($"The PWM chip {_chip} does not support the channel {_channel}."); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| throw new IOException($"Unable to parse the number of supported channels at {npwmPath}."); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Stops and closes the channel. | ||
| /// </summary> | ||
| private void Close() | ||
| { | ||
| if (Directory.Exists(_channelPath)) | ||
| { | ||
| Stop(); | ||
| File.WriteAllText($"{_chipPath}/unexport", Convert.ToString(_channel)); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Opens the channel. | ||
| /// </summary> | ||
| private void Open() | ||
| { | ||
| if (!Directory.Exists(_channelPath)) | ||
| { | ||
| File.WriteAllText($"{_chipPath}/export", Convert.ToString(_channel)); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Starts writing to the channel. | ||
| /// </summary> | ||
| public override void Start() | ||
shaggygi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| string enablePath = $"{_channelPath}/enable"; | ||
| File.WriteAllText(enablePath, "1"); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Stops writing to the channel. | ||
| /// </summary> | ||
| public override void Stop() | ||
| { | ||
| string enablePath = $"{_channelPath}/enable"; | ||
| File.WriteAllText(enablePath, "0"); | ||
| } | ||
|
|
||
| protected override void Dispose(bool disposing) | ||
| { | ||
| Close(); | ||
| base.Dispose(disposing); | ||
| } | ||
| } | ||
| } | ||
125 changes: 125 additions & 0 deletions
125
src/System.Device.Gpio/System/Device/Pwm/Channels/Windows10PwmChannel.Windows.cs
This file contains hidden or 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,125 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using Windows.Devices.Enumeration; | ||
| using Windows.Security.ExchangeActiveSyncProvisioning; | ||
| using WinPwm = Windows.Devices.Pwm; | ||
|
|
||
| namespace System.Device.Pwm.Channels | ||
| { | ||
| /// <summary> | ||
| /// Represents a PWM channel running on Windows 10 IoT. | ||
| /// </summary> | ||
| internal partial class Windows10PwmChannel : PwmChannel | ||
| { | ||
| private WinPwm.PwmController _winController; | ||
| private WinPwm.PwmPin _winPin; | ||
| private int _frequency; | ||
| private double _dutyCyclePercentage; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="Windows10PwmChannel"/> class. | ||
| /// </summary> | ||
| /// <param name="chip">The PWM chip number.</param> | ||
| /// <param name="channel">The PWM channel number.</param> | ||
| /// <param name="frequency">The frequency in hertz.</param> | ||
| /// <param name="dutyCyclePercentage">The duty cycle percentage represented as a value between 0.0 and 1.0.</param> | ||
| public Windows10PwmChannel( | ||
| int chip, | ||
| int channel, | ||
| int frequency = 400, | ||
| double dutyCyclePercentage = 0.5) | ||
| { | ||
| // When running on Hummingboard we require to use the default chip. | ||
| var deviceInfo = new EasClientDeviceInformation(); | ||
krwq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| bool useDefaultChip = false; | ||
| if (deviceInfo.SystemProductName.IndexOf("Hummingboard", StringComparison.OrdinalIgnoreCase) >= 0) | ||
| { | ||
| useDefaultChip = true; | ||
| } | ||
|
|
||
| // Open the Windows PWM controller for the specified PWM chip. | ||
| string deviceSelector = useDefaultChip ? WinPwm.PwmController.GetDeviceSelector() : WinPwm.PwmController.GetDeviceSelector($"PWM{chip}"); | ||
|
|
||
| DeviceInformationCollection deviceInformationCollection = DeviceInformation.FindAllAsync(deviceSelector).WaitForCompletion(); | ||
| if (deviceInformationCollection.Count == 0) | ||
| { | ||
| throw new ArgumentException($"No PWM device exists for PWM chip at index {chip}.", nameof(chip)); | ||
| } | ||
|
|
||
| string deviceId = deviceInformationCollection[0].Id; | ||
| _winController = WinPwm.PwmController.FromIdAsync(deviceId).WaitForCompletion(); | ||
|
|
||
| _winPin = _winController.OpenPin(channel); | ||
| if (_winPin == null) | ||
| { | ||
| throw new ArgumentOutOfRangeException($"The PWM chip is unable to open a channel at index {channel}.", nameof(channel)); | ||
| } | ||
|
|
||
| Frequency = frequency; | ||
| DutyCyclePercentage = dutyCyclePercentage; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The frequency in hertz. | ||
| /// </summary> | ||
| public override int Frequency | ||
| { | ||
| get => _frequency; | ||
| set | ||
| { | ||
| if (value < 0) | ||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(value), value, "Value must not be negative."); | ||
| } | ||
| _winController.SetDesiredFrequency(value); | ||
| _frequency = value; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The duty cycle percentage represented as a value between 0.0 and 1.0. | ||
| /// </summary> | ||
| public override double DutyCyclePercentage | ||
| { | ||
| get => _dutyCyclePercentage; | ||
| set | ||
| { | ||
| if (value < 0.0 || value > 1.0) | ||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(value), value, "Value must be between 0.0 and 1.0."); | ||
| } | ||
| _winPin.SetActiveDutyCyclePercentage(value); | ||
| _dutyCyclePercentage = value; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Starts the PWM channel. | ||
| /// </summary> | ||
| public override void Start() | ||
| { | ||
| _winPin.Start(); | ||
| // This extra call is required to generate PWM output - remove when the underlying issue is fixed. See issue #109 | ||
| DutyCyclePercentage = _dutyCyclePercentage; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Stops the PWM channel. | ||
| /// </summary> | ||
| public override void Stop() | ||
| { | ||
| _winPin.Stop(); | ||
| } | ||
|
|
||
| protected override void Dispose(bool disposing) | ||
| { | ||
| Stop(); | ||
| _winPin?.Dispose(); | ||
| _winPin = null; | ||
| _winController = null; | ||
| base.Dispose(disposing); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.