Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
{
throw new ArgumentOutOfRangeException(nameof(frequency), frequency, "Value must not be negative.");
}

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()
{
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);
}
}
}
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();
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);
}
}
}
Loading