Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor arduino interface to avoid event callback #1445

Merged
merged 7 commits into from
Jul 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ e6f499ad9c78eff87c57ded6ea620fccb255d691
e12cd15478ef4dd7cc2a391ae74d877f33acee97

# Ensure unix portability of uri launcher
a949f77d92437690840a24857f421c0f47161d43
a949f77d92437690840a24857f421c0f47161d43

# Move arduino package into separate repository
1804cdb10530babc3146f9c07bc7d23a877a93f5
1a60ab3cd625cb89e29531895efdd96936c2c271
39 changes: 39 additions & 0 deletions Bonsai.Arduino/AnalogInput.cs
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);
}
}
}
32 changes: 32 additions & 0 deletions Bonsai.Arduino/AnalogInputReceivedEventArgs.cs
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; }
}
}
61 changes: 61 additions & 0 deletions Bonsai.Arduino/AnalogOutput.cs
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);
}
}));
});
}
}
}
Loading