Skip to content
This repository was archived by the owner on Jun 5, 2019. It is now read-only.

Commit 0adaed4

Browse files
author
ColinA-MSFT
committed
Merge pull request #166 from ColinA-MSFT/WindowsDevicesPwm
Add support for Windows.Devices.Pwm.
2 parents 3e46196 + a1a7e0b commit 0adaed4

File tree

7 files changed

+361
-3
lines changed

7 files changed

+361
-3
lines changed

CLR/Libraries/Windows_Devices/windows_devices_native.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,37 @@ static const CLR_RT_MethodHandler method_lookup[] =
112112
NULL,
113113
NULL,
114114
NULL,
115+
NULL,
116+
NULL,
117+
NULL,
118+
NULL,
119+
NULL,
120+
NULL,
121+
NULL,
122+
NULL,
123+
NULL,
124+
NULL,
125+
NULL,
126+
NULL,
127+
NULL,
128+
NULL,
129+
NULL,
130+
NULL,
131+
NULL,
132+
NULL,
133+
NULL,
134+
NULL,
135+
NULL,
136+
NULL,
137+
NULL,
138+
NULL,
139+
NULL,
140+
NULL,
141+
NULL,
142+
NULL,
143+
NULL,
144+
NULL,
145+
NULL,
115146
Library_windows_devices_native_Windows_Devices_Spi_SpiBusInfo::_ctor___VOID__I4,
116147
NULL,
117148
NULL,
@@ -154,7 +185,7 @@ static const CLR_RT_MethodHandler method_lookup[] =
154185
const CLR_RT_NativeAssemblyData g_CLR_AssemblyNative_Windows_Devices =
155186
{
156187
"Windows.Devices",
157-
0xDD62098A,
188+
0x240D5E7D,
158189
method_lookup
159190
};
160191

CLR/Libraries/Windows_Devices/windows_devices_native.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#define _WINDOWS_DEVICES_NATIVE_H_
1212

1313
#include <TinyCLR_Interop.h>
14-
1514
struct Library_windows_devices_native_Windows_Devices_Adc_AdcChannel
1615
{
1716
static const int FIELD__m_channelNumber = 1;
@@ -143,6 +142,30 @@ struct Library_windows_devices_native_Windows_Devices_I2c_I2cTransferResult
143142

144143
};
145144

145+
struct Library_windows_devices_native_Windows_Devices_Pwm_PwmController
146+
{
147+
static const int FIELD__m_provider = 1;
148+
149+
150+
//--//
151+
152+
};
153+
154+
struct Library_windows_devices_native_Windows_Devices_Pwm_PwmPin
155+
{
156+
static const int FIELD__m_pinNumber = 1;
157+
static const int FIELD__m_controller = 2;
158+
static const int FIELD__m_provider = 3;
159+
static const int FIELD__m_started = 4;
160+
static const int FIELD__m_disposed = 5;
161+
static const int FIELD__m_dutyCycle = 6;
162+
static const int FIELD__m_polarity = 7;
163+
164+
165+
//--//
166+
167+
};
168+
146169
struct Library_windows_devices_native_Windows_Devices_Spi_SpiBusInfo
147170
{
148171
static const int FIELD__MinClockFrequency_ = 1;

Framework/Core/Windows/Devices/Devices.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
<MMP_STUB_SKIP>false</MMP_STUB_SKIP>
1818
<MMP_STUB_GenerateSkeletonFile>$(BUILD_TREE_STUBS)\windows_devices_native</MMP_STUB_GenerateSkeletonFile>
1919
<MMP_STUB_GenerateSkeletonProject>Windows_Devices</MMP_STUB_GenerateSkeletonProject>
20-
<MMP_STUB_LegacySkeletonInterop>TRUE</MMP_STUB_LegacySkeletonInterop>
2120
</PropertyGroup>
2221
<ItemGroup>
2322
<MMP_STUB_Load Include="$(BUILD_TREE_PE)\Windows.Devices.pe">
@@ -38,6 +37,10 @@
3837
<Compile Include="I2c\I2cConnectionSettings.cs" />
3938
<Compile Include="I2c\I2cDevice.cs" />
4039
<Compile Include="I2c\I2cEnums.cs" />
40+
<Compile Include="Pwm\PwmController.cs" />
41+
<Compile Include="Pwm\PwmEnums.cs" />
42+
<Compile Include="Pwm\PwmPin.cs" />
43+
<Compile Include="Pwm\PwmProvider.cs" />
4144
<Compile Include="Spi\SpiBusInfo.cs" />
4245
<Compile Include="Spi\SpiConnectionSettings.cs" />
4346
<Compile Include="Spi\SpiDevice.cs" />
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System;
2+
using Windows.Devices.Pwm.Provider;
3+
4+
namespace Windows.Devices.Pwm
5+
{
6+
public sealed class PwmController
7+
{
8+
private IPwmControllerProvider m_provider;
9+
10+
internal PwmController(IPwmControllerProvider provider)
11+
{
12+
m_provider = provider;
13+
}
14+
15+
public int PinCount
16+
{
17+
get
18+
{
19+
return m_provider.PinCount;
20+
}
21+
}
22+
23+
public double MinFrequency
24+
{
25+
get
26+
{
27+
return m_provider.MinFrequency;
28+
}
29+
}
30+
31+
public double MaxFrequency
32+
{
33+
get
34+
{
35+
return m_provider.MaxFrequency;
36+
}
37+
}
38+
39+
public double ActualFrequency
40+
{
41+
get
42+
{
43+
return m_provider.ActualFrequency;
44+
}
45+
}
46+
47+
public static PwmController[] GetControllers(IPwmProvider provider)
48+
{
49+
// FUTURE: This should return "Task<IReadOnlyList<PwmController>>"
50+
51+
var providers = provider.GetControllers();
52+
var controllers = new PwmController[providers.Length];
53+
54+
for (int i = 0; i < providers.Length; ++i)
55+
{
56+
controllers[i] = new PwmController(providers[i]);
57+
}
58+
59+
return controllers;
60+
}
61+
62+
public double SetDesiredFrequency(double desiredFrequency)
63+
{
64+
if ((desiredFrequency < m_provider.MinFrequency) || (desiredFrequency > m_provider.MaxFrequency))
65+
{
66+
throw new ArgumentOutOfRangeException();
67+
}
68+
69+
return m_provider.SetDesiredFrequency(desiredFrequency);
70+
}
71+
72+
public PwmPin OpenPin(int pinNumber)
73+
{
74+
if ((pinNumber < 0) || (pinNumber >= m_provider.PinCount))
75+
{
76+
throw new ArgumentOutOfRangeException();
77+
}
78+
79+
return new PwmPin(this, m_provider, pinNumber);
80+
}
81+
}
82+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Windows.Devices.Pwm
2+
{
3+
public enum PwmPulsePolarity
4+
{
5+
ActiveHigh = 0,
6+
ActiveLow,
7+
}
8+
}
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
using System;
2+
using Windows.Devices.Pwm.Provider;
3+
4+
namespace Windows.Devices.Pwm
5+
{
6+
public sealed class PwmPin : IDisposable
7+
{
8+
private readonly int m_pinNumber;
9+
private PwmController m_controller;
10+
private IPwmControllerProvider m_provider;
11+
private bool m_started = false;
12+
private bool m_disposed = false;
13+
private double m_dutyCycle = 0;
14+
private PwmPulsePolarity m_polarity = PwmPulsePolarity.ActiveHigh;
15+
16+
internal PwmPin(PwmController controller, IPwmControllerProvider provider, int pinNumber)
17+
{
18+
m_controller = controller;
19+
m_provider = provider;
20+
m_pinNumber = pinNumber;
21+
22+
m_provider.AcquirePin(pinNumber);
23+
}
24+
25+
~PwmPin()
26+
{
27+
Dispose(false);
28+
}
29+
30+
public PwmController Controller
31+
{
32+
get
33+
{
34+
if (m_disposed)
35+
{
36+
throw new ObjectDisposedException();
37+
}
38+
39+
return m_controller;
40+
}
41+
}
42+
43+
public PwmPulsePolarity Polarity
44+
{
45+
get
46+
{
47+
if (m_disposed)
48+
{
49+
throw new ObjectDisposedException();
50+
}
51+
52+
return m_polarity;
53+
}
54+
55+
set
56+
{
57+
if (m_disposed)
58+
{
59+
throw new ObjectDisposedException();
60+
}
61+
62+
switch (value)
63+
{
64+
case PwmPulsePolarity.ActiveHigh:
65+
case PwmPulsePolarity.ActiveLow:
66+
break;
67+
68+
default:
69+
throw new ArgumentException();
70+
}
71+
72+
if (m_started)
73+
{
74+
m_provider.SetPulseParameters(m_pinNumber, m_dutyCycle, value == PwmPulsePolarity.ActiveLow);
75+
m_polarity = value;
76+
}
77+
}
78+
}
79+
80+
public bool IsStarted
81+
{
82+
get
83+
{
84+
if (m_disposed)
85+
{
86+
throw new ObjectDisposedException();
87+
}
88+
89+
return m_started;
90+
}
91+
}
92+
93+
public void Start()
94+
{
95+
if (m_disposed)
96+
{
97+
throw new ObjectDisposedException();
98+
}
99+
100+
if (!m_started)
101+
{
102+
m_provider.EnablePin(m_pinNumber);
103+
m_provider.SetPulseParameters(m_pinNumber, m_dutyCycle, m_polarity == PwmPulsePolarity.ActiveLow);
104+
m_started = true;
105+
}
106+
}
107+
108+
public void Stop()
109+
{
110+
if (m_disposed)
111+
{
112+
throw new ObjectDisposedException();
113+
}
114+
115+
if (m_started)
116+
{
117+
m_provider.DisablePin(m_pinNumber);
118+
m_started = false;
119+
}
120+
}
121+
122+
public double GetActiveDutyCyclePercentage()
123+
{
124+
if (m_disposed)
125+
{
126+
throw new ObjectDisposedException();
127+
}
128+
129+
return m_dutyCycle;
130+
}
131+
132+
public void SetActiveDutyCyclePercentage(double dutyCyclePercentage)
133+
{
134+
if (m_disposed)
135+
{
136+
throw new ObjectDisposedException();
137+
}
138+
139+
if ((dutyCyclePercentage < 0) || (dutyCyclePercentage > 1))
140+
{
141+
throw new ArgumentOutOfRangeException();
142+
}
143+
144+
if (m_started)
145+
{
146+
m_provider.SetPulseParameters(m_pinNumber, dutyCyclePercentage, m_polarity == PwmPulsePolarity.ActiveLow);
147+
m_dutyCycle = dutyCyclePercentage;
148+
}
149+
}
150+
151+
public void Dispose()
152+
{
153+
if (!m_disposed)
154+
{
155+
Dispose(true);
156+
GC.SuppressFinalize(this);
157+
m_disposed = true;
158+
}
159+
}
160+
161+
private void Dispose(bool disposing)
162+
{
163+
if (disposing)
164+
{
165+
m_provider.ReleasePin(m_pinNumber);
166+
m_controller = null;
167+
m_provider = null;
168+
}
169+
}
170+
}
171+
}

0 commit comments

Comments
 (0)