-
Notifications
You must be signed in to change notification settings - Fork 598
/
Copy pathPwmChannelManager.cs
88 lines (76 loc) · 2.35 KB
/
PwmChannelManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Device;
using System.Device.Pwm;
using System.Globalization;
namespace Iot.Device.Board
{
internal class PwmChannelManager : PwmChannel, IDeviceManager
{
private readonly Board _board;
private readonly int _pin;
private PwmChannel _pwm;
public PwmChannelManager(Board board, int pin, int chip, int channel, int frequency, double dutyCyclePercentage, Func<int, int, int, double, PwmChannel> createOperation)
{
_board = board;
_pin = pin;
try
{
_board.ReservePin(pin, PinUsage.Pwm, this);
_pwm = createOperation(chip, channel, frequency, dutyCyclePercentage);
}
catch (Exception)
{
_board.ReleasePin(pin, PinUsage.Pwm, this);
throw;
}
}
public override int Frequency
{
get => _pwm.Frequency;
set => _pwm.Frequency = value;
}
public override double DutyCycle
{
get => _pwm.DutyCycle;
set => _pwm.DutyCycle = value;
}
public override void Start()
{
_pwm.Start();
}
public override void Stop()
{
_pwm.Stop();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (_pwm != null)
{
_pwm.Dispose();
_board.ReleasePin(_pin, PinUsage.Pwm, this);
}
_pwm = null!;
}
base.Dispose(disposing);
}
public IReadOnlyCollection<int> GetActiveManagedPins()
{
return new List<int>()
{
_pin
};
}
public override ComponentInformation QueryComponentInformation()
{
var self = new ComponentInformation(this, "PWM Channel manager");
self.Properties["ManagedPins"] = _pin.ToString(CultureInfo.InvariantCulture);
self.AddSubComponent(_pwm.QueryComponentInformation());
return self;
}
}
}