提交 24535610 编写于 作者: G Greg Ingram 提交者: Jose Perez Rodriguez

Fixed by setting usePrecisionTimer in constructor (#618)

* Fixed by setting usePrecisionTimer in constructor

* Updates from feedback

* Cleaned up project file for Linux thread bug

* Added ending line

* Added feedback code for Linux thread bug

* Added RuntimeIdentifier back in project file

* Removed unused RuntimeIdentifier from project files

* Remove the thread helper for now
上级 15ede2e7
......@@ -62,4 +62,4 @@ namespace System.Interop
public fixed int reserved[8];
}
}
}
\ No newline at end of file
}
......@@ -4,7 +4,6 @@
<TargetFramework>netcoreapp2.1</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<EnableDefaultItems>false</EnableDefaultItems>
<RuntimeIdentifier>linux-arm</RuntimeIdentifier>
</PropertyGroup>
<ItemGroup>
......
......@@ -3,7 +3,6 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<RuntimeIdentifier>linux-arm</RuntimeIdentifier>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
......
......@@ -13,10 +13,10 @@ var softwarePwmChannel = new SoftwarePwmChannel(17, 200, 0);
By default SoftwarePwmChannel is using a low priority clock to emulate the PWM. It is ok if you have leds and other non time sensitive elements attached.
**Important:** if you have clock sensitive elements attached to this software PWM, you need to use a high precision timer. In order to make it happen, you need to select it at creation time by passing ```true``` to the precisionTimer parameter in the constructor:
**Important:** if you have clock sensitive elements attached to this software PWM, you need to use a high precision timer. In order to make it happen, you need to select it at creation time by passing ```true``` to the usePrecisionTimer parameter in the constructor:
```csharp
var softwarePwmChannelWithPrecisionTimer = new SoftwarePwmChannel(17, frequency: 50, dutyCyclePercentage = 0.5, precisionTimer: true);
var softwarePwmChannelWithPrecisionTimer = new SoftwarePwmChannel(17, frequency: 50, dutyCyclePercentage = 0.5, usePrecisionTimer: true);
```
### Starting the PWM
......
......@@ -2,8 +2,8 @@
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<EnableDefaultItems>false</EnableDefaultItems>
<!--Disabling default items so samples source won't get build by the main library-->
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<EnableDefaultItems>false</EnableDefaultItems>
</PropertyGroup>
<ItemGroup>
......
......@@ -20,8 +20,9 @@ namespace System.Device.Pwm.Drivers
// Use to determine the length of the pulse
// 100 % = full output. 0%= nothing as output
private double _percentage;
// Use to determine if we are using a high precision timer or not
private bool _precisionPWM = false;
// Determines if a high precision timer should be used.
private bool _usePrecisionTimer = false;
private bool _isRunning;
private bool _isStopped = true;
......@@ -74,9 +75,9 @@ namespace System.Device.Pwm.Drivers
/// <param name="pinNumber">The GPIO pin number to be used</param>
/// <param name="frequency">The frequency in hertz. Defaults to 400</param>
/// <param name="dutyCyclePercentage">The duty cycle percentage represented as a value between 0.0 and 1.0</param>
/// <param name="precisionTimer"><see langword="true"/> to use a precision timer. <see langword="false"/> otherwise</param>
/// <param name="usePrecisionTimer">Determines if a high precision timer should be used.</param>
/// <param name="controller">The <see cref="GpioController"/> to which <paramref name="pinNumber"/> belongs to. Null defaults to board GpioController</param>
public SoftwarePwmChannel(int pinNumber, int frequency = 400, double dutyCyclePercentage = 0.5, bool precisionTimer = false, GpioController controller = null)
public SoftwarePwmChannel(int pinNumber, int frequency = 400, double dutyCyclePercentage = 0.5, bool usePrecisionTimer = false, GpioController controller = null)
{
_controller = controller ?? new GpioController();
if (_controller == null)
......@@ -86,6 +87,7 @@ namespace System.Device.Pwm.Drivers
}
_servoPin = pinNumber;
_controller.OpenPin(_servoPin, PinMode.Output);
_usePrecisionTimer = usePrecisionTimer;
_isRunning = false;
_runningThread = new Thread(RunSoftPWM);
_runningThread.Start();
......@@ -103,7 +105,7 @@ namespace System.Device.Pwm.Drivers
private void RunSoftPWM()
{
if (_precisionPWM)
if (_usePrecisionTimer)
{
Thread.CurrentThread.Priority = ThreadPriority.Highest;
}
......@@ -120,7 +122,7 @@ namespace System.Device.Pwm.Drivers
}
// Use the wait helper method to wait for the length of the pulse
if (_precisionPWM)
if (_usePrecisionTimer)
{
Wait(_currentPulseWidth);
}
......@@ -132,7 +134,7 @@ namespace System.Device.Pwm.Drivers
// The pulse if over and so set the pin to low and then wait until it's time for the next pulse
_controller.Write(_servoPin, PinValue.Low);
if (_precisionPWM)
if (_usePrecisionTimer)
{
Wait(_pulseFrequency - _currentPulseWidth);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册