Skip to content

Commit fbd235e

Browse files
authored
Merge pull request bonsai-rx#1445 from glopesdev/polling-arduino
Refactor arduino interface to avoid event callback
2 parents f12bb6c + 4f02762 commit fbd235e

25 files changed

+1588
-3
lines changed

.git-blame-ignore-revs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ e6f499ad9c78eff87c57ded6ea620fccb255d691
55
e12cd15478ef4dd7cc2a391ae74d877f33acee97
66

77
# Ensure unix portability of uri launcher
8-
a949f77d92437690840a24857f421c0f47161d43
8+
a949f77d92437690840a24857f421c0f47161d43
9+
10+
# Move arduino package into separate repository
11+
1804cdb10530babc3146f9c07bc7d23a877a93f5
12+
1a60ab3cd625cb89e29531895efdd96936c2c271

Bonsai.Arduino/AnalogInput.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.ComponentModel;
3+
4+
namespace Bonsai.Arduino
5+
{
6+
/// <summary>
7+
/// Represents an operator that generates a sequence of digitized analog readings
8+
/// from the specified Arduino input pin.
9+
/// </summary>
10+
[DefaultProperty(nameof(Pin))]
11+
[Description("Generates a sequence of digitized analog readings from the specified Arduino input pin.")]
12+
public class AnalogInput : Source<int>
13+
{
14+
/// <summary>
15+
/// Gets or sets the name of the serial port used to communicate with the Arduino.
16+
/// </summary>
17+
[TypeConverter(typeof(PortNameConverter))]
18+
[Description("The name of the serial port used to communicate with the Arduino.")]
19+
public string PortName { get; set; }
20+
21+
/// <summary>
22+
/// Gets or sets the analog input pin number from which to take readings.
23+
/// </summary>
24+
[Description("The analog input pin number from which to take readings.")]
25+
public int Pin { get; set; }
26+
27+
/// <summary>
28+
/// Generates an observable sequence of digitized analog values.
29+
/// </summary>
30+
/// <returns>
31+
/// A sequence of <see cref="int"/> values that report the digitized analog
32+
/// readings from the specified Arduino analog input pin.
33+
/// </returns>
34+
public override IObservable<int> Generate()
35+
{
36+
return ObservableArduino.AnalogInput(PortName, Pin);
37+
}
38+
}
39+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
3+
namespace Bonsai.Arduino
4+
{
5+
/// <summary>
6+
/// Provides data for the <see cref="Arduino.AnalogInputReceived"/> event.
7+
/// </summary>
8+
public class AnalogInputReceivedEventArgs : EventArgs
9+
{
10+
/// <summary>
11+
/// Initializes a new instance of the <see cref="AnalogInputReceivedEventArgs"/>
12+
/// class using the pin number and analog value received in the analog input message.
13+
/// </summary>
14+
/// <param name="pin">The pin number from which the analog value was sampled.</param>
15+
/// <param name="value">The digitized analog value.</param>
16+
public AnalogInputReceivedEventArgs(int pin, int value)
17+
{
18+
Pin = pin;
19+
Value = value;
20+
}
21+
22+
/// <summary>
23+
/// Gets the pin number from which the analog value was sampled.
24+
/// </summary>
25+
public int Pin { get; private set; }
26+
27+
/// <summary>
28+
/// Gets the digitized analog value.
29+
/// </summary>
30+
public int Value { get; private set; }
31+
}
32+
}

Bonsai.Arduino/AnalogOutput.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,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

Comments
 (0)