-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1445 from glopesdev/polling-arduino
Refactor arduino interface to avoid event callback
- Loading branch information
Showing
25 changed files
with
1,588 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.ComponentModel; | ||
|
||
namespace Bonsai.Arduino | ||
{ | ||
/// <summary> | ||
/// Represents an operator that generates a sequence of digitized analog readings | ||
/// from the specified Arduino input pin. | ||
/// </summary> | ||
[DefaultProperty(nameof(Pin))] | ||
[Description("Generates a sequence of digitized analog readings from the specified Arduino input pin.")] | ||
public class AnalogInput : Source<int> | ||
{ | ||
/// <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 analog input pin number from which to take readings. | ||
/// </summary> | ||
[Description("The analog input pin number from which to take readings.")] | ||
public int Pin { get; set; } | ||
|
||
/// <summary> | ||
/// Generates an observable sequence of digitized analog values. | ||
/// </summary> | ||
/// <returns> | ||
/// A sequence of <see cref="int"/> values that report the digitized analog | ||
/// readings from the specified Arduino analog input pin. | ||
/// </returns> | ||
public override IObservable<int> Generate() | ||
{ | ||
return ObservableArduino.AnalogInput(PortName, Pin); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
|
||
namespace Bonsai.Arduino | ||
{ | ||
/// <summary> | ||
/// Provides data for the <see cref="Arduino.AnalogInputReceived"/> event. | ||
/// </summary> | ||
public class AnalogInputReceivedEventArgs : EventArgs | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AnalogInputReceivedEventArgs"/> | ||
/// class using the pin number and analog value received in the analog input message. | ||
/// </summary> | ||
/// <param name="pin">The pin number from which the analog value was sampled.</param> | ||
/// <param name="value">The digitized analog value.</param> | ||
public AnalogInputReceivedEventArgs(int pin, int value) | ||
{ | ||
Pin = pin; | ||
Value = value; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the pin number from which the analog value was sampled. | ||
/// </summary> | ||
public int Pin { get; private set; } | ||
|
||
/// <summary> | ||
/// Gets the digitized analog value. | ||
/// </summary> | ||
public int Value { get; private set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
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 numerical values to the | ||
/// specified Arduino output pin using PWM. | ||
/// </summary> | ||
[DefaultProperty(nameof(Pin))] | ||
[Description("Writes the sequence of numerical values to the specified Arduino output pin using PWM.")] | ||
public class AnalogOutput : Sink<int> | ||
{ | ||
/// <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 (PWM) pin number on which to write values. | ||
/// </summary> | ||
[Description("The digital output (PWM) pin number on which to write values.")] | ||
public int Pin { get; set; } | ||
|
||
/// <summary> | ||
/// Writes a sequence of <see cref="int"/> values to the specified Arduino output pin using PWM. | ||
/// </summary> | ||
/// <param name="source"> | ||
/// A sequence of <see cref="int"/> values to write into the specified Arduino output pin. | ||
/// </param> | ||
/// <returns> | ||
/// A sequence of the <see cref="int"/> 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 output pin mode to PWM. | ||
/// </remarks> | ||
public override IObservable<int> Process(IObservable<int> source) | ||
{ | ||
return Observable.Using( | ||
cancellationToken => ArduinoManager.ReserveConnectionAsync(PortName), | ||
(connection, cancellationToken) => | ||
{ | ||
var pin = Pin; | ||
connection.Arduino.PinMode(pin, PinMode.Pwm); | ||
return Task.FromResult(source.Do(value => | ||
{ | ||
lock (connection.Arduino) | ||
{ | ||
connection.Arduino.AnalogWrite(pin, value); | ||
} | ||
})); | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.