Skip to content

Commit 144d6b1

Browse files
authored
Merge pull request #3 from lucidcode/update-brainflow
Update BrainFlow to 5.14
2 parents 116da46 + e9096f2 commit 144d6b1

File tree

9 files changed

+249
-23
lines changed

9 files changed

+249
-23
lines changed

BrainFlow/BrainFlow.csproj

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@
6363
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
6464
</PropertyGroup>
6565
<ItemGroup>
66-
<Reference Include="brainflow, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
67-
<HintPath>..\packages\brainflow.5.2.0\lib\net45\brainflow.dll</HintPath>
66+
<Reference Include="brainflow, Version=5.14.0.0, Culture=neutral, processorArchitecture=MSIL">
67+
<HintPath>..\packages\brainflow.5.14.0\lib\netstandard2.0\brainflow.dll</HintPath>
68+
<Private>True</Private>
6869
</Reference>
6970
<Reference Include="lucidcode.Controls, Version=1.0.0.1, Culture=neutral, processorArchitecture=MSIL">
7071
<SpecificVersion>False</SpecificVersion>
@@ -90,6 +91,8 @@
9091
<Reference Include="System.Xml" />
9192
</ItemGroup>
9293
<ItemGroup>
94+
<Compile Include="Channels\Restfulness.cs" />
95+
<Compile Include="Channels\Mindfulness.cs" />
9396
<Compile Include="UserSettings.cs" />
9497
<Compile Include="Board.cs" />
9598
<Compile Include="Channels\Channel16.cs" />
@@ -129,13 +132,6 @@
129132
<None Include="packages.config" />
130133
</ItemGroup>
131134
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
132-
<Import Project="..\packages\brainflow.5.2.0\build\brainflow.targets" Condition="Exists('..\packages\brainflow.5.2.0\build\brainflow.targets')" />
133-
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
134-
<PropertyGroup>
135-
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
136-
</PropertyGroup>
137-
<Error Condition="!Exists('..\packages\brainflow.5.2.0\build\brainflow.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\brainflow.5.2.0\build\brainflow.targets'))" />
138-
</Target>
139135
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
140136
Other similar extension points exist, see Microsoft.Common.targets.
141137
<Target Name="BeforeBuild">

BrainFlow/Channels/Mindfulness.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
3+
namespace lucidcode.LucidScribe.Plugin.BrainFlow
4+
{
5+
namespace Mindfulness
6+
{
7+
public class PluginHandler : Interface.LucidPluginBase
8+
{
9+
public override string Name { get { return "Mindfulness"; } }
10+
11+
public override bool Initialize()
12+
{
13+
try
14+
{
15+
return Device.Initialize();
16+
}
17+
catch (Exception ex)
18+
{
19+
throw (new Exception("The '" + Name + "' plugin failed to initialize: " + ex.Message));
20+
}
21+
}
22+
23+
public override double Value
24+
{
25+
get
26+
{
27+
return Device.GetMetric(brainflow.BrainFlowMetrics.MINDFULNESS);
28+
}
29+
}
30+
31+
public override void Dispose()
32+
{
33+
Device.Dispose();
34+
}
35+
}
36+
}
37+
}

BrainFlow/Channels/Restfulness.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
3+
namespace lucidcode.LucidScribe.Plugin.BrainFlow
4+
{
5+
namespace Restfulness
6+
{
7+
public class PluginHandler : Interface.LucidPluginBase
8+
{
9+
public override string Name { get { return "Restfulness"; } }
10+
11+
public override bool Initialize()
12+
{
13+
try
14+
{
15+
return Device.Initialize();
16+
}
17+
catch (Exception ex)
18+
{
19+
throw (new Exception("The '" + Name + "' plugin failed to initialize: " + ex.Message));
20+
}
21+
}
22+
23+
public override double Value
24+
{
25+
get
26+
{
27+
return Device.GetMetric(brainflow.BrainFlowMetrics.RESTFULNESS);
28+
}
29+
}
30+
31+
public override void Dispose()
32+
{
33+
Device.Dispose();
34+
}
35+
}
36+
}
37+
}

BrainFlow/PluginHandler.cs

Lines changed: 103 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Threading;
34
using System.Windows.Forms;
45
using brainflow;
@@ -12,13 +13,24 @@ public static class Device
1213
static bool initError;
1314
static bool disposing;
1415

16+
static Thread boardThread;
17+
1518
static BoardShim boardShim;
1619
static int[] eegChannels;
17-
static Thread boardThread;
20+
static int samplingRate;
1821

1922
static double[] eegValues;
2023
static double[] eegTicks;
2124
static bool[] clearValues;
25+
static bool readMetrics = false;
26+
static bool hasMetrics = false;
27+
static double[] featureVector;
28+
29+
static MLModel mindfulnessMlModel;
30+
static BrainFlowModelParams mindfulnessModelParams;
31+
32+
static MLModel restfulnessMlModel;
33+
static BrainFlowModelParams restfulnessModelParams;
2234

2335
public static EventHandler<EventArgs> Channel1Changed;
2436
public static EventHandler<EventArgs> Channel2Changed;
@@ -81,24 +93,35 @@ public static Boolean Initialize()
8193
return true;
8294
}
8395

84-
static void GetBoardData(int board_id, BrainFlowInputParams input_params)
96+
static void GetBoardData(int boardId, BrainFlowInputParams inputParams)
8597
{
86-
boardShim = new BoardShim(board_id, input_params);
98+
boardShim = new BoardShim(boardId, inputParams);
8799

88100
boardShim.prepare_session();
89101
boardShim.start_stream();
90102

91-
eegChannels = BoardShim.get_eeg_channels(board_id);
103+
samplingRate = BoardShim.get_sampling_rate(boardId);
104+
eegChannels = BoardShim.get_eeg_channels(boardId);
92105
eegValues = new double[eegChannels.Length];
93106
eegTicks = new double[eegChannels.Length];
94107
clearValues = new bool[eegChannels.Length];
95108

109+
mindfulnessModelParams = new BrainFlowModelParams((int)BrainFlowMetrics.MINDFULNESS, (int)BrainFlowClassifiers.DEFAULT_CLASSIFIER);
110+
mindfulnessMlModel = new MLModel(mindfulnessModelParams);
111+
mindfulnessMlModel.prepare();
112+
113+
restfulnessModelParams = new BrainFlowModelParams((int)BrainFlowMetrics.RESTFULNESS, (int)BrainFlowClassifiers.DEFAULT_CLASSIFIER);
114+
restfulnessMlModel = new MLModel(mindfulnessModelParams);
115+
restfulnessMlModel.prepare();
116+
117+
var bandData = new List<double[,]>();
118+
96119
do
97120
{
98-
double[,] unprocessed_data = boardShim.get_board_data(20);
121+
double[,] boardData = boardShim.get_board_data();
99122
foreach (var index in eegChannels)
100123
{
101-
double[] rows = unprocessed_data.GetRow(index);
124+
double[] rows = boardData.GetRow(index);
102125

103126
foreach (var row in rows)
104127
{
@@ -130,6 +153,31 @@ static void GetBoardData(int board_id, BrainFlowInputParams input_params)
130153
if (index == 15 && Channel15Changed != null) Channel15Changed(string.Join(",", rows), null);
131154
if (index == 16 && Channel16Changed != null) Channel16Changed(string.Join(",", rows), null);
132155
}
156+
157+
if (readMetrics)
158+
{
159+
bandData.Add(boardData);
160+
161+
if (bandData.Count > 512)
162+
{
163+
bandData.RemoveAt(0);
164+
165+
double[,] data = new double[0, 0];
166+
167+
foreach (var d in bandData)
168+
{
169+
data = mergeArrays(data, d, eegChannels.Length * 2);
170+
}
171+
172+
if (data.Length > 16384)
173+
{
174+
Tuple<double[], double[]> bands = DataFilter.get_avg_band_powers(data, eegChannels, samplingRate, true);
175+
featureVector = bands.Item1;
176+
hasMetrics = true;
177+
}
178+
179+
}
180+
}
133181
}
134182
while (!disposing);
135183
}
@@ -154,5 +202,54 @@ public static double GetEEG(int index)
154202
clearValues[index - 1] = true;
155203
return average;
156204
}
205+
206+
public static double GetMetric(BrainFlowMetrics metric)
207+
{
208+
readMetrics = true;
209+
210+
if (!initialized) return 0;
211+
if (!hasMetrics) return 0;
212+
213+
double prediction = 0;
214+
215+
if (metric == BrainFlowMetrics.MINDFULNESS)
216+
{
217+
prediction = mindfulnessMlModel.predict(featureVector)[0];
218+
}
219+
else if (metric == BrainFlowMetrics.RESTFULNESS)
220+
{
221+
prediction = restfulnessMlModel.predict(featureVector)[0];
222+
}
223+
224+
return prediction;
225+
}
226+
227+
static double[,] mergeArrays(double[,] data1, double[,] data2, int depth)
228+
{
229+
var data1Length = data1.Length / depth;
230+
var data2Length = data2.Length / depth;
231+
232+
double[,] data = new double[depth, data1Length + data2Length]; ;
233+
234+
int i = 0;
235+
for (; i < depth; i++)
236+
{
237+
for (int j = 0; j < data1Length; j++)
238+
{
239+
data[i, j] = data1[i, j];
240+
}
241+
}
242+
i = 0;
243+
for (; i < depth; i++)
244+
{
245+
for (int j = 0; j < data2Length; j++)
246+
{
247+
data[i, data1Length + j] = data2[i, j];
248+
}
249+
250+
}
251+
252+
return data;
253+
}
157254
}
158255
}

BrainFlow/Plugins/Mindfulness.lsd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<LucidScribeData>
2+
<Plugin>
3+
<Enabled>False</Enabled>
4+
<Baseline>
5+
</Baseline>
6+
<Color>-1</Color>
7+
</Plugin>
8+
</LucidScribeData>

BrainFlow/Plugins/Restfulness.lsd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<LucidScribeData>
2+
<Plugin>
3+
<Enabled>False</Enabled>
4+
<Baseline>
5+
</Baseline>
6+
<Color>-1</Color>
7+
</Plugin>
8+
</LucidScribeData>

BrainFlow/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
[assembly: AssemblyConfiguration("")]
1111
[assembly: AssemblyCompany("lucidcode")]
1212
[assembly: AssemblyProduct("Plugin.BrainFlow")]
13-
[assembly: AssemblyCopyright("Copyright © lucidcode 2022 - 2022")]
13+
[assembly: AssemblyCopyright("Copyright © lucidcode 2022 - 2024")]
1414
[assembly: AssemblyTrademark("")]
1515
[assembly: AssemblyCulture("")]
1616

@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.1.0")]
36-
[assembly: AssemblyFileVersion("1.0.1.0")]
35+
[assembly: AssemblyVersion("1.0.3.0")]
36+
[assembly: AssemblyFileVersion("1.0.3.0")]

BrainFlow/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="brainflow" version="5.2.0" targetFramework="net48" />
3+
<package id="brainflow" version="5.14.0" targetFramework="net48" />
44
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net48" />
55
</packages>

0 commit comments

Comments
 (0)