我最近买了一台脑电耳机(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);
}
}
}
}
}上述代码首先尝试连接到EEG耳机。一旦连接完毕,每次从耳机接收数据时,都会调用OnDataReceived()。此方法将相关的流耳机数据值(alpha waves)打印到控制台。
现在我想在笛卡儿图上实时显示这些α波值,我发现了LiveCharts,它看起来像一个整洁的图形库。这个WinForms示例与我所要达到的目标是一致的。
阿尔法波值应与X轴上的时间相对应,绘制在Y轴上。但是,与其像示例中那样每500 is更新一次图表,我希望它只在从耳机接收数据时更新(换句话说,当AlphaValue变量在ReadEEG类中由OnDataReceived()更新时)。
我想知道如何让我的WinForm与ReadEEG类交互,以这种方式更新其笛卡尔图。我是个新手,所以任何帮助都会受到极大的感谢。
我真的希望我已经说得很清楚,并尽量使解释尽量简单。如果你有任何问题,不要犹豫。提前感谢您的帮助!
发布于 2019-04-08 15:30:30
有一种方法你可以做到。我向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;
}
}
}
}https://stackoverflow.com/questions/55567258
复制相似问题