-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathDCMotorWithStartStop.cs
89 lines (82 loc) · 2.27 KB
/
DCMotorWithStartStop.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
89
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Iot.Device.DCMotor
{
/// <summary>
/// Direct current (DC) motor with Start/Stop
/// </summary>
public class DCMotorWithStartStop : DCMotor
{
private DCMotor _inner;
private bool _stopped = false;
private double _speed;
/// <summary>
/// Constructs instance with added Start() and Stop() as additional protection
/// </summary>
/// <param name="innerMotor">Crate DCMotor instance</param>
public DCMotorWithStartStop(DCMotor innerMotor)
: base(null, true)
{
_inner = innerMotor;
_speed = innerMotor.Speed;
}
/// <summary>
/// Releases the resources used by the <see cref="DCMotor"/> instance.
/// </summary>
public override void Dispose()
{
_inner.Dispose();
base.Dispose();
}
/// <summary>
/// Enable motor operation.
/// </summary>
public void Start()
{
_stopped = false;
_inner.Speed = _speed;
}
/// <summary>
/// Disable motor operation.
/// </summary>
public void Stop()
{
_stopped = true;
_inner.Speed = 0.0;
}
/// <summary>
/// Gets or sets the speed of the motor. Range is -1..1 or 0..1 for 1-pin connection.
/// 1 means maximum speed, 0 means no movement and -1 means movement in opposite direction.
/// </summary>
public override double Speed
{
get => _speed;
set
{
_speed = value;
if (!_stopped)
{
_inner.Speed = _speed;
}
}
}
/// <summary>
/// Get or Set motor status.
/// </summary>
public bool Enabled
{
get => !_stopped;
set
{
if (value)
{
Start();
}
else
{
Stop();
}
}
}
}
}