-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathBufferManager.cs
More file actions
157 lines (133 loc) · 9.68 KB
/
Copy pathBufferManager.cs
File metadata and controls
157 lines (133 loc) · 9.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using UnityEngine;
using System.Collections;
using System.Threading;
using System.Linq;
using UnityEngine.UI;
using FieldTrip.Buffer;
/*
* This script manages the connection to the Buffer
* and processes events.
*
*/
public class BufferManager : MonoBehaviour
{
public Text StatusLabel;
BufferClientClock client = new BufferClientClock();
public string hostname = "localhost";
public int port = 1972;
float counter = 0;
Header hdr;
int nEvents;
public static string currentBCIAction;
public static int highestPredictionindex;
string predictions;
double[] predictionArray;
// Use this for initialization
void Start()
{
//BufferEvent testevent = new BufferEvent("classifier.prediction",new double[] { 0.20, 0.80 },100);
DontDestroyOnLoad(gameObject);
highestPredictionindex = 10;
//double[] output = (double[])testevent.getValue().array;
// for (int i = 0; i<output.Length; i++)
// {
// print(output[i]);
//}
// double maxValue = output.Max();
// int maxIndex = output.ToList().IndexOf(maxValue);
// print(maxValue);
// print(maxIndex);
}
// Update is called once per frame
// Slow the update rate by a counter to prevent overflow.
void Update()
{
counter += Time.deltaTime;
if (counter > 0.2)
{
counter = 0;
if (client.isConnected())
{
processBufferEvents();
}
}
}
// Find events from the buffer and process them according to their type.
public void processBufferEvents (){
int timeout = 5000;
print(hdr.nEvents);
try
{
print("Waiting for events...");
SamplesEventsCount sec = client.waitForEvents(nEvents -1, timeout);
if (sec.nEvents > nEvents)
{
BufferEvent[] evs = client.getEvents(nEvents, sec.nEvents - 1);
nEvents = sec.nEvents;
print("Got " + evs.Length + " events");
//grab the newest event (This means it only take 1 event every update)
BufferEvent evt = evs[evs.Length-1];
string evttype = evt.getType().toString();
// Handle Exit event
if (evttype.Equals("exit"))
{
client.disconnect();
}
// Handle keyboard events
else if (evttype.Equals("keyboard"))
{
predictions = evt.getValue().toString();
currentBCIAction = predictions;
print(predictions);
}
// handle predictions events
else if (evttype.Equals("classifier.prediction"))
{
// Get highest prediction
predictionArray = (double[])evt.getValue().array;
double maxValue = predictionArray.Max();
int maxIndex = predictionArray.ToList().IndexOf(maxValue);
highestPredictionindex = maxIndex;
// set variable to index of highest prediction or some other boundary.
//highestPredictionindex = 1;
}
}
}
catch
{
print("catch Error");
}
}
// Connects to the Buffer
public void connectBuffer()
{
hdr = null;
try
{
print("Connecting to " + hostname + ":" + port + "...");
client.connect(hostname, port);
print("done");
print("Getting Header...");
if (client.isConnected())
{
hdr = client.getHeader();
}
print("done.");
StatusLabel.text = "Connected";
StatusLabel.color = new Color(0, 255, 0);
}
catch
{ //(IOException e)
hdr = null;
}
if (hdr == null)
{
print("Invalid Header... waiting");
}
else
{
print("Succes!");
}
nEvents = hdr.nEvents;
}
}