我最近购买了EEG耳机(NeuroSky MindWave Mobile)。它只是戴在头上的一种设备,用于捕获脑电波数据。该设备通过蓝牙实时流式传输这些数据,然后可以通过软件程序读取/分析这些数据。
NeuroSky 提供了一组易于使用的 API,我用它来编写一个基本类来读取流式耳机数据。它的缩写版本如下:
using System;
using NeuroSky.ThinkGear;
namespace MindWave_Reader
{
class ReadEEG
{
public double AlphaValue { get; set; }
private Connector connector;
public ReadEEG()
{
// Initialize a new Connector and add event handlers
connector = new Connector();
connector.DeviceConnected += new EventHandler(OnDeviceConnected);
// Scan for headset on COM7 port
connector.ConnectScan("COM7");
}
// Called when a device is connected
public void OnDeviceConnected(object sender, EventArgs e) {
Connector.DeviceEventArgs de = (Connector.DeviceEventArgs)e;
Console.WriteLine("Device found on: " + de.Device.PortName);
de.Device.DataReceived += new EventHandler(OnDataReceived);
}
// Called when data is received from a device
public void OnDataReceived(object sender, EventArgs e) {
Device.DataEventArgs de = (Device.DataEventArgs)e;
DataRow[] tempDataRowArray = de.DataRowArray;
TGParser tgParser = new TGParser();
tgParser.Read(de.DataRowArray);
/* Loops through the newly parsed data of the connected headset */
for (int i = 0; i < tgParser.ParsedData.Length; i++) {
if(tgParser.ParsedData[i].ContainsKey("EegPowerAlpha")) {
AlphaValue = tgParser.ParsedData[i]["EegPowerAlpha"];
Console.WriteLine("Alpha: " + AlphaValue);
}
}
}
}
}
上面的代码首先尝试连接到脑电图耳机。连接后,每次从头戴式耳机接收数据时,都会调用OnDataReceived()
。此方法将相关的流式耳机数据值(alpha 波)打印到控制台。
我现在想在笛卡尔图上实时显示这些阿尔法波值,我发现了 LiveCharts,它似乎是一个整洁的图形库。这个 WinForms 示例与我试图实现的目标一致。
α 波值应绘制在 Y 轴上,与 X 轴上的时间相对。但是,我不希望像示例中那样每 500 毫秒更新一次图表,而是希望它仅在从耳机接收数据时更新(换句话说,当AlphaValue
变量由 ReadEEG 类中的OnDataReceived()
更新时)。
我想知道如何让我的WinForm与ReadEEG类交互,以这种方式更新其笛卡尔图。我有点新手,所以任何帮助将不胜感激。
我真的希望我已经把自己说清楚了,并试图使解释尽可能简单。如果您有任何问题,请随时提出。提前感谢您的所有帮助!
这里有一种方法可以做到这一点。 我在ReadEEG
类中添加了一个新事件。 每当有新AlphaValue
时,都会引发此新事件。 在主窗体中,它订阅此事件并将新值添加到ChartValues
集合中,这是 LiveChart 上显示的内容。
using System;
using System.Data;
using NeuroSky.ThinkGear;
namespace MindWave_Reader
{
class ReadEEG
{
public double AlphaValue { get; set; }
private Connector connector;
public ReadEEG()
{
// Initialize a new Connector and add event handlers
connector = new Connector();
connector.DeviceConnected += new EventHandler(OnDeviceConnected);
// Scan for headset on COM7 port
connector.ConnectScan("COM7");
}
// Called when a device is connected
public void OnDeviceConnected(object sender, EventArgs e)
{
Connector.DeviceEventArgs de = (Connector.DeviceEventArgs)e;
Console.WriteLine("Device found on: " + de.Device.PortName);
de.Device.DataReceived += new EventHandler(OnDataReceived);
}
// Called when data is received from a device
public void OnDataReceived(object sender, EventArgs e)
{
Device.DataEventArgs de = (Device.DataEventArgs)e;
DataRow[] tempDataRowArray = de.DataRowArray;
TGParser tgParser = new TGParser();
tgParser.Read(de.DataRowArray);
/* Loops through the newly parsed data of the connected headset */
for (int i = 0; i < tgParser.ParsedData.Length; i++)
{
if (tgParser.ParsedData[i].ContainsKey("EegPowerAlpha"))
{
AlphaValue = tgParser.ParsedData[i]["EegPowerAlpha"];
Console.WriteLine("Alpha: " + AlphaValue);
// Raise the AlphaReceived event with the new reading.
OnAlphaReceived(new AlphaReceivedEventArgs() { Alpha = AlphaValue });
}
}
}
/// <summary>
/// The arguments for the <see cref="AlphaReceived"/> event.
/// </summary>
public class AlphaReceivedEventArgs : EventArgs
{
/// <summary>
/// The alpha value that was just received.
/// </summary>
public double Alpha { get; set; }
}
/// <summary>
/// Raises the <see cref="AlphaReceived"/> event if there is a subscriber.
/// </summary>
/// <param name="e">Contains the new alpha value.</param>
protected virtual void OnAlphaReceived(AlphaReceivedEventArgs e)
{
AlphaReceived?.Invoke(this, e);
}
/// <summary>
/// Event that gets raised whenever a new AlphaValue is received from the
/// device.
/// </summary>
public event EventHandler AlphaReceived;
}
}
Form1
我与设计师一起添加了一个LiveCharts.WinForms.CartesianChart
。 背后的代码如下:
using LiveCharts;
using LiveCharts.Configurations;
using LiveCharts.Wpf;
using System;
using System.Windows.Forms;
using static MindWave_Reader.ReadEEG;
namespace MindWave_Reader
{
public partial class Form1 : Form
{
/// <summary>
/// Simple class to hold an alpha value and the time it was received. Used
/// for charting.
/// </summary>
public class EEGPowerAlphaValue
{
public DateTime Time { get; }
public double AlphaValue { get; }
public EEGPowerAlphaValue(DateTime time, double alpha)
{
Time = time;
AlphaValue = alpha;
}
}
private ReadEEG _readEEG;
/// <summary>
/// Contains the alpha values we're showing on the chart.
/// </summary>
public ChartValues<EEGPowerAlphaValue> ChartValues { get; set; }
public Form1()
{
InitializeComponent();
// Create the mapper.
var mapper = Mappers.Xy<EEGPowerAlphaValue>()
.X(model => model.Time.Ticks) // use Time.Ticks as X
.Y(model => model.AlphaValue); // use the AlphaValue property as Y
// Lets save the mapper globally.
Charting.For<EEGPowerAlphaValue>(mapper);
// The ChartValues property will store our values array.
ChartValues = new ChartValues<EEGPowerAlphaValue>();
cartesianChart1.Series = new SeriesCollection
{
new LineSeries
{
Values = ChartValues,
PointGeometrySize = 18,
StrokeThickness = 4
}
};
cartesianChart1.AxisX.Add(new Axis
{
DisableAnimations = true,
LabelFormatter = value => new DateTime((long)value).ToString("mm:ss"),
Separator = new Separator
{
Step = TimeSpan.FromSeconds(1).Ticks
}
});
SetAxisLimits(DateTime.Now);
}
private void StartButton_Click(object sender, EventArgs e)
{
_readEEG = new ReadEEG();
_readEEG.AlphaReceived += _readEEG_AlphaReceived;
}
/// <summary>
/// Called when a new alpha value is received from the device. Updates the
/// chart with the new value.
/// </summary>
/// <param name="sender">The <see cref="ReadEEG"/> object that raised this
/// event.</param>
/// <param name="e">The <see cref="AlphaReceivedEventArgs"/> that contains
/// the new alpha value.</param>
private void _readEEG_AlphaReceived(object sender, EventArgs e)
{
AlphaReceivedEventArgs alphaReceived = (AlphaReceivedEventArgs)e;
// Add the new alpha reading to our ChartValues.
ChartValues.Add(
new EEGPowerAlphaValue(
DateTime.Now,
alphaReceived.Alpha));
// Update the chart limits.
SetAxisLimits(DateTime.Now);
// Lets only use the last 30 values. You may want to adjust this.
if (ChartValues.Count > 30)
{
ChartValues.RemoveAt(0);
}
}
private void SetAxisLimits(DateTime now)
{
if (cartesianChart1.InvokeRequired)
{
cartesianChart1.Invoke(new Action(() => SetAxisLimits(now)));
}
else
{
// Lets force the axis to be 100ms ahead. You may want to adjust this.
cartesianChart1.AxisX[0].MaxValue =
now.Ticks + TimeSpan.FromSeconds(1).Ticks;
// We only care about the last 8 seconds. You may want to adjust this.
cartesianChart1.AxisX[0].MinValue =
now.Ticks - TimeSpan.FromSeconds(8).Ticks;
}
}
}
}