-
Notifications
You must be signed in to change notification settings - Fork 0
/
DigitalOutput.cs
64 lines (61 loc) · 2.59 KB
/
DigitalOutput.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
using System;
using System.ComponentModel;
using System.Reactive.Linq;
using System.Threading.Tasks;
namespace Bonsai.Arduino
{
/// <summary>
/// Represents an operator that writes the sequence of digital state transitions
/// to the specified Arduino output pin.
/// </summary>
[DefaultProperty(nameof(Pin))]
[Description("Writes the sequence of digital state transitions to the specified Arduino output pin.")]
public class DigitalOutput : Sink<bool>
{
/// <summary>
/// Gets or sets the name of the serial port used to communicate with the Arduino.
/// </summary>
[TypeConverter(typeof(PortNameConverter))]
[Description("The name of the serial port used to communicate with the Arduino.")]
public string PortName { get; set; }
/// <summary>
/// Gets or sets the digital output pin number on which to write the state values.
/// </summary>
[Description("The digital output pin number on which to write the state values.")]
public int Pin { get; set; }
/// <summary>
/// Writes a sequence of binary states to the specified Arduino digital output pin.
/// </summary>
/// <param name="source">
/// A sequence of <see cref="bool"/> values used to update the state of the specified
/// Arduino output pin. If a value in the sequence is <see langword="true"/>, the pin
/// will be set to HIGH; otherwise, the pin will be set to LOW.
/// </param>
/// <returns>
/// A sequence of the <see cref="bool"/> values which have been written into the Arduino
/// output pin.
/// </returns>
/// <remarks>
/// This operator only subscribes to the <paramref name="source"/> sequence after
/// initializing the connection to the Arduino and configuring the digital pin mode
/// to OUTPUT.
/// </remarks>
public override IObservable<bool> Process(IObservable<bool> source)
{
return Observable.Using(
cancellationToken => ArduinoManager.ReserveConnectionAsync(PortName),
(connection, cancellationToken) =>
{
var pin = Pin;
connection.Arduino.PinMode(pin, PinMode.Output);
return Task.FromResult(source.Do(value =>
{
lock (connection.Arduino)
{
connection.Arduino.DigitalWrite(pin, value);
}
}));
});
}
}
}