|
1 |
| -using System; |
2 |
| -using System.ComponentModel; |
3 |
| -using System.Reactive.Linq; |
4 |
| -using System.Threading.Tasks; |
5 |
| - |
6 |
| -namespace Bonsai.Arduino |
7 |
| -{ |
8 |
| - /// <summary> |
9 |
| - /// Represents an operator that writes the sequence of numerical values to the |
10 |
| - /// specified Arduino output pin using PWM. |
11 |
| - /// </summary> |
12 |
| - [DefaultProperty(nameof(Pin))] |
13 |
| - [Description("Writes the sequence of numerical values to the specified Arduino output pin using PWM.")] |
14 |
| - public class AnalogOutput : Sink<int> |
15 |
| - { |
16 |
| - /// <summary> |
17 |
| - /// Gets or sets the name of the serial port used to communicate with the Arduino. |
18 |
| - /// </summary> |
19 |
| - [TypeConverter(typeof(PortNameConverter))] |
20 |
| - [Description("The name of the serial port used to communicate with the Arduino.")] |
21 |
| - public string PortName { get; set; } |
22 |
| - |
23 |
| - /// <summary> |
24 |
| - /// Gets or sets the digital output (PWM) pin number on which to write values. |
25 |
| - /// </summary> |
26 |
| - [Description("The digital output (PWM) pin number on which to write values.")] |
27 |
| - public int Pin { get; set; } |
28 |
| - |
29 |
| - /// <summary> |
30 |
| - /// Writes a sequence of <see cref="int"/> values to the specified Arduino output pin using PWM. |
31 |
| - /// </summary> |
32 |
| - /// <param name="source"> |
33 |
| - /// A sequence of <see cref="int"/> values to write into the specified Arduino output pin. |
34 |
| - /// </param> |
35 |
| - /// <returns> |
36 |
| - /// A sequence of the <see cref="int"/> values which have been written into the Arduino |
37 |
| - /// output pin. |
38 |
| - /// </returns> |
39 |
| - /// <remarks> |
40 |
| - /// This operator only subscribes to the <paramref name="source"/> sequence after initializing |
41 |
| - /// the connection to the Arduino and configuring the output pin mode to PWM. |
42 |
| - /// </remarks> |
43 |
| - public override IObservable<int> Process(IObservable<int> source) |
44 |
| - { |
45 |
| - return Observable.Using( |
46 |
| - cancellationToken => ArduinoManager.ReserveConnectionAsync(PortName), |
47 |
| - (connection, cancellationToken) => |
48 |
| - { |
49 |
| - var pin = Pin; |
50 |
| - connection.Arduino.PinMode(pin, PinMode.Pwm); |
51 |
| - return Task.FromResult(source.Do(value => |
52 |
| - { |
53 |
| - lock (connection.Arduino) |
54 |
| - { |
55 |
| - connection.Arduino.AnalogWrite(pin, value); |
56 |
| - } |
57 |
| - })); |
58 |
| - }); |
59 |
| - } |
60 |
| - } |
61 |
| -} |
| 1 | +using System; |
| 2 | +using System.ComponentModel; |
| 3 | +using System.Reactive.Linq; |
| 4 | +using System.Threading.Tasks; |
| 5 | + |
| 6 | +namespace Bonsai.Arduino |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Represents an operator that writes the sequence of numerical values to the |
| 10 | + /// specified Arduino output pin using PWM. |
| 11 | + /// </summary> |
| 12 | + [DefaultProperty(nameof(Pin))] |
| 13 | + [Description("Writes the sequence of numerical values to the specified Arduino output pin using PWM.")] |
| 14 | + public class AnalogOutput : Sink<int> |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// Gets or sets the name of the serial port used to communicate with the Arduino. |
| 18 | + /// </summary> |
| 19 | + [TypeConverter(typeof(PortNameConverter))] |
| 20 | + [Description("The name of the serial port used to communicate with the Arduino.")] |
| 21 | + public string PortName { get; set; } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Gets or sets the digital output (PWM) pin number on which to write values. |
| 25 | + /// </summary> |
| 26 | + [Description("The digital output (PWM) pin number on which to write values.")] |
| 27 | + public int Pin { get; set; } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Writes a sequence of <see cref="int"/> values to the specified Arduino output pin using PWM. |
| 31 | + /// </summary> |
| 32 | + /// <param name="source"> |
| 33 | + /// A sequence of <see cref="int"/> values to write into the specified Arduino output pin. |
| 34 | + /// </param> |
| 35 | + /// <returns> |
| 36 | + /// A sequence of the <see cref="int"/> values which have been written into the Arduino |
| 37 | + /// output pin. |
| 38 | + /// </returns> |
| 39 | + /// <remarks> |
| 40 | + /// This operator only subscribes to the <paramref name="source"/> sequence after initializing |
| 41 | + /// the connection to the Arduino and configuring the output pin mode to PWM. |
| 42 | + /// </remarks> |
| 43 | + public override IObservable<int> Process(IObservable<int> source) |
| 44 | + { |
| 45 | + return Observable.Using( |
| 46 | + cancellationToken => ArduinoManager.ReserveConnectionAsync(PortName), |
| 47 | + (connection, cancellationToken) => |
| 48 | + { |
| 49 | + var pin = Pin; |
| 50 | + connection.Arduino.PinMode(pin, PinMode.Pwm); |
| 51 | + return Task.FromResult(source.Do(value => |
| 52 | + { |
| 53 | + lock (connection.Arduino) |
| 54 | + { |
| 55 | + connection.Arduino.AnalogWrite(pin, value); |
| 56 | + } |
| 57 | + })); |
| 58 | + }); |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments