Skip to content

Commit 3c6a0e0

Browse files
authored
Adding static constructor from pin (#1)
1 parent fda1fc7 commit 3c6a0e0

File tree

3 files changed

+85
-16
lines changed

3 files changed

+85
-16
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,48 @@
1313
| System.Device.Pwm | [![Build Status](https://dev.azure.com/nanoframework/System.Device.Pwm/_apis/build/status/nanoframework.System.Device.Pwm?branchName=develop)](https://dev.azure.com/nanoframework/System.Device.Pwm/_build/latest?definitionId=77&branchName=main) | [![NuGet](https://img.shields.io/nuget/v/nanoFramework.System.Device.Pwm.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.System.Device.Pwm/) |
1414
| System.Device.Pwm (preview) | [![Build Status](https://dev.azure.com/nanoframework/System.Device.Pwm/_apis/build/status/nanoframework.System.Device.Pwm?branchName=develop)](https://dev.azure.com/nanoframework/System.Device.Pwm/_build/latest?definitionId=77&branchName=develop) | [![NuGet](https://img.shields.io/nuget/vpre/nanoFramework.System.Device.Pwm.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.System.Device.Pwm/) |
1515

16+
## Usage
17+
18+
You can create a PWM channel from a pin number, this is the recommended way. Keep in mind, you will have to allocate the pin in the case of ESP32 and make sure your pin is PWM enabled for STM32 devices.
19+
20+
```csharp
21+
// Case of ESP32, you need to set the pin function, in this example PWM3 for pin 18:
22+
Configuration.SetPinFunction(18, DeviceFunction.PWM3);
23+
PwmChannel pwmPin = PwmChannel.CreateFromPin(18, 40000);
24+
// You can check then if it has created a valid one:
25+
if (pwmPin != null)
26+
{
27+
// You do have a valid one
28+
}
29+
```
30+
### Duty cycle
31+
32+
You can adjust the duty cycle by using the property:
33+
34+
```csharp
35+
pwmPin.DutyCycle = 0.42;
36+
```
37+
38+
The duty cycle goes from 0.0 to 1.0.
39+
40+
### Frequency
41+
42+
It is recommended to setup the frequency when creating the PWM Channel. You can technically change it at any time but keep in mind some platform may not behave properly when adjusting this element.
43+
44+
### Advance PwmChannel creation
45+
46+
You can as well, if you know the chip/timer Id and the channel use the create function:
47+
48+
```csharp
49+
PwmChannel pwmPin = new(1, 2, 40000, 0.5);
50+
```
51+
52+
This is only recommended for advance users.
53+
54+
### Other considerations
55+
56+
PWM precision may vary from platform to platform. It is highly recommended to check what precision can be achieved, either with the frequency, either with the duty cycle.
57+
1658
## Feedback and documentation
1759

1860
For documentation, providing feedback, issues and finding out how to contribute please refer to the [Home repo](https://github.com/nanoframework/Home).

System.Device.Pwm/PwmChannel.cs

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ public sealed class PwmChannel : IDisposable
1717
[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
1818
private uint _dutyCycle;
1919

20-
#pragma warning disable 0414
20+
#pragma warning disable 0414
2121
[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
2222
private PwmPulsePolarity _polarity;
23-
#pragma warning restore 0414
23+
#pragma warning restore 0414
2424

2525
[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
2626
private int _pinNumber;
@@ -81,10 +81,10 @@ public void Stop()
8181
}
8282

8383
/// <summary>
84-
/// Creates a new instance of the <see cref="PwmChannel"/> running on the current platform. (Windows 10 IoT or Unix/Raspbian)
84+
/// Creates a new instance of the <see cref="PwmChannel"/> running on the current platform.
8585
/// </summary>
86-
/// <param name="chip">The PWM chip number.</param>
87-
/// <param name="channel">The PWM channel number.</param>
86+
/// <param name="chip">The PWM chip number, formally known as TIM channel.</param>
87+
/// <param name="channel">The PWM pin number or channel number depending on your platform.</param>
8888
/// <param name="frequency">The frequency in hertz.</param>
8989
/// <param name="dutyCyclePercentage">The duty cycle percentage represented as a value between 0.0 and 1.0.</param>
9090
/// <returns>A PWM channel</returns>
@@ -99,15 +99,44 @@ public static PwmChannel Create(
9999
}
100100

101101
/// <summary>
102-
/// Creates a new instance of the <see cref="PwmChannel"/> running on the current platform. (Windows 10 IoT or Unix/Raspbian)
102+
/// Creates a PwmChannel from a pin number.
103103
/// </summary>
104-
/// <param name="chip">The PWM chip number.</param>
105-
/// <param name="channel">The PWM channel number.</param>
104+
/// <param name="pin">The pin number.</param>
105+
/// <param name="frequency">The frequency in hertz.</param>
106+
/// <param name="dutyCyclePercentage">The duty cycle percentage represented as a value between 0.0 and 1.0.</param>
107+
/// <returns>A PWM channel</returns>
108+
public static PwmChannel CreateFromPin(int pin, int frequency = 400, double dutyCyclePercentage = 0.5)
109+
{
110+
int channel = -1;
111+
int chip;
112+
// We do have a channels from 0 to 8
113+
for (chip = 0; chip < 8; chip++)
114+
{
115+
channel = GetChannel(pin, chip);
116+
if (channel != -1)
117+
{
118+
break;
119+
}
120+
}
121+
122+
if (channel == -1)
123+
{
124+
return null;
125+
}
126+
127+
return new PwmChannel(chip, channel, frequency, dutyCyclePercentage);
128+
}
129+
130+
/// <summary>
131+
/// Creates a new instance of the <see cref="PwmChannel"/> running on the current platform.
132+
/// </summary>
133+
/// <param name="chip">The PWM chip number, formally known as TIM channel.</param>
134+
/// <param name="channel">The PWM pin number or channel number depending on your platform.</param>
106135
/// <param name="frequency">The frequency in hertz.</param>
107136
/// <param name="dutyCyclePercentage">The duty cycle percentage represented as a value between 0.0 and 1.0.</param>
108137
/// <returns>A PWM channel</returns>
109138
public PwmChannel(
110-
int chip,
139+
int chip,
111140
int channel,
112141
int frequency = 400,
113142
double dutyCyclePercentage = 0.5)
@@ -147,13 +176,6 @@ private void Dispose(bool disposing)
147176
}
148177
}
149178

150-
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
151-
// ~PwmChannel()
152-
// {
153-
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
154-
// Dispose(disposing: false);
155-
// }
156-
157179
/// <inheritdoc cref="IDisposable.Dispose"/>
158180
public void Dispose()
159181
{
@@ -183,6 +205,10 @@ public void Dispose()
183205

184206
[MethodImpl(MethodImplOptions.InternalCall)]
185207
private extern void DisposeNative();
208+
209+
[MethodImpl(MethodImplOptions.InternalCall)]
210+
private static extern int GetChannel(int pin, int timerId);
211+
186212
#endregion
187213
}
188214
}

System.Device.Pwm/System.Device.Pwm.nfproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<Reference Include="mscorlib, Version=1.10.5.4, Culture=neutral, PublicKeyToken=c07d481e9758c731">
5353
<HintPath>..\packages\nanoFramework.CoreLibrary.1.10.5\lib\mscorlib.dll</HintPath>
5454
<Private>True</Private>
55+
<SpecificVersion>True</SpecificVersion>
5556
</Reference>
5657
</ItemGroup>
5758
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />

0 commit comments

Comments
 (0)