-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathShimmerJavaClass.java
More file actions
284 lines (234 loc) · 10.8 KB
/
Copy pathShimmerJavaClass.java
File metadata and controls
284 lines (234 loc) · 10.8 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package com.shimmerresearch.tools.matlab;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.shimmerresearch.bluetooth.ShimmerBluetooth.BT_STATE;
import com.shimmerresearch.driver.BasicProcessWithCallBack;
import com.shimmerresearch.driver.CallbackObject;
import com.shimmerresearch.driver.ObjectCluster;
import com.shimmerresearch.driver.ShimmerMsg;
import com.shimmerresearch.driver.Configuration.COMMUNICATION_TYPE;
import com.shimmerresearch.driver.FormatCluster;
import com.shimmerresearch.driverUtilities.ChannelDetails;
import com.shimmerresearch.exceptions.ShimmerException;
import com.shimmerresearch.pcDriver.ShimmerPC;
import com.shimmerresearch.tools.bluetooth.BasicShimmerBluetoothManagerPc;
import com.shimmerresearch.algorithms.*;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
public class ShimmerJavaClass {
private String comPort;
private SensorDataReceived sdr = new SensorDataReceived();
private String[] channelNames;
private final ConcurrentLinkedQueue<String> connectedDevices = new ConcurrentLinkedQueue<>();
private final ConcurrentHashMap<String, Queue<Object[]>> dataQueues = new ConcurrentHashMap<>();
private List<float[]> collectedData = new ArrayList<>();
private PropertyChangeSupport pcs = new PropertyChangeSupport(this);
public void addPropertyChangeListener(PropertyChangeListener listener) {
pcs.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
pcs.removePropertyChangeListener(listener);
}
private boolean mDebug = true;
// private int rowIndex = 0;
Object[] currentData = null;
public static BasicShimmerBluetoothManagerPc mBluetoothManager;
public ShimmerJavaClass() {
mBluetoothManager = new BasicShimmerBluetoothManagerPc(false);
sdr.setWaitForData(mBluetoothManager.callBackObject);
mBluetoothManager.addCallBack(sdr);
if (mDebug) {
System.out.println("Callback Registered");
System.out.flush();
}
}
public void setDebugMode(boolean debug) {
mDebug = debug;
}
public static void main(String[] args) {
ShimmerJavaClass example = new ShimmerJavaClass();
example.createAndShowGUI();
}
public void createAndShowGUI() {
JFrame frame = new JFrame("Shimmer Device Controller");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
JPanel panel = new JPanel();
JTextField inputComPort = new JTextField("COM3");
JButton connectButton = new JButton("Connect");
JButton disconnectButton = new JButton("Disconnect");
JButton startButton = new JButton("Start Streaming");
JButton stopButton = new JButton("Stop Streaming");
connectButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
comPort = inputComPort.getText();
mBluetoothManager.connectShimmerThroughCommPort(comPort);
}
});
disconnectButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
mBluetoothManager.disconnectShimmer(comPort);
}
});
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
mBluetoothManager.getShimmerDeviceBtConnected(comPort).startStreaming();
} catch (ShimmerException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while(true) {
if (hasDataToRead(comPort)) {
readData();
}
}
}
});
stopButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
mBluetoothManager.getShimmerDeviceBtConnected(comPort).stopStreaming();
} catch (ShimmerException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
panel.add(inputComPort);
panel.add(connectButton);
panel.add(disconnectButton);
panel.add(startButton);
panel.add(stopButton);
frame.add(panel);
frame.setVisible(true);
}
public String[] retrieveSensorChannels() {
LinkedHashMap<String, ChannelDetails> mapOfAllChannels = mBluetoothManager.getShimmerDeviceBtConnected(comPort).getMapOfAllChannelsForStoringToDB(COMMUNICATION_TYPE.BLUETOOTH, null, false, false);
List<ChannelDetails> listOfChannelDetails = new ArrayList<ChannelDetails>(mapOfAllChannels.values());
List<String> channelNamesList = new ArrayList<String>();
for (ChannelDetails channel : listOfChannelDetails) {
channelNamesList.add(channel.mObjectClusterName);
}
return channelNamesList.toArray(new String[0]);
}
public boolean hasDataToRead(String comPort) {
Queue<Object[]> queue = dataQueues.get(comPort);
return queue != null && !queue.isEmpty();
}
private void sendData(String comPort, Object[] data) {
dataQueues.computeIfAbsent(comPort, k -> new LinkedList<>()).add(data);
}
public void readData() {
for (String comPort : dataQueues.keySet()) {
Object[] data = receiveData(comPort);
if (data != null && mDebug) {
float[][] matrix = (float[][]) data[0];
String[] signalNames = (String[]) data[1];
String[] formats = (String[]) data[2];
String[] units = (String[]) data[3];
System.out.println("==== Data from device: " + comPort + " ====");
for (int i = 0; i < matrix.length; i++) {
System.out.print("Row " + i + ": ");
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " " + units[j] + " [" + signalNames[j] + "] | ");
}
System.out.println();
}
}
}
}
public Object[] receiveData(String comPort) {
Queue<Object[]> queue = dataQueues.get(comPort);
if (queue == null || queue.isEmpty()) return null;
List<float[]> deviceData = new ArrayList<>();
String[] signalNameArray = null, signalFormatArray = null, signalUnitArray = null;
while (!queue.isEmpty()) {
Object[] data = queue.poll();
if (data == null) continue; // skip this iteration safely
float[] values = (float[]) data[1];
if (values == null) continue;
signalNameArray = (String[]) data[2];
signalFormatArray = (String[]) data[3];
signalUnitArray = (String[]) data[4];
deviceData.add(values);
}
float[][] dataMatrix = deviceData.toArray(new float[0][0]);
return new Object[] { dataMatrix, signalNameArray, signalFormatArray, signalUnitArray };
}
public class SensorDataReceived extends BasicProcessWithCallBack {
@Override
protected void processMsgFromCallback(ShimmerMsg shimmerMSG) {
int ind = shimmerMSG.mIdentifier;
Object object = shimmerMSG.mB;
if (ind == ShimmerPC.MSG_IDENTIFIER_STATE_CHANGE) {
CallbackObject callbackObject = (CallbackObject) object;
if (callbackObject.mState == BT_STATE.CONNECTING) {
System.out.println("Device State Change: " + callbackObject.mState);
System.out.flush();
} else if (callbackObject.mState == BT_STATE.CONNECTED) {
System.out.println("Device State Change: " + callbackObject.mState);
System.out.flush();
if (comPort == null) {
comPort = callbackObject.mComPort;
}
channelNames = retrieveSensorChannels();
if (mDebug) {
for(String channel : channelNames) {
System.out.println(channel);
}
}
System.out.println("Device State Change: CONNECTED");
pcs.firePropertyChange("ConnectedState", null, new MatlabConnectionEvent("CONNECTED", callbackObject.mComPort));
} else if (callbackObject.mState == BT_STATE.DISCONNECTED || callbackObject.mState == BT_STATE.CONNECTION_LOST) {
System.out.println("Device State Change: " + callbackObject.mState);
System.out.flush();
System.out.println("Device State Change: DISCONNECTED");
pcs.firePropertyChange("ConnectedState", null, new MatlabConnectionEvent("DISCONNECTED", callbackObject.mComPort));
}
} else if (ind == ShimmerPC.MSG_IDENTIFIER_NOTIFICATION_MESSAGE) {
} else if (ind == ShimmerPC.MSG_IDENTIFIER_DATA_PACKET) {
ObjectCluster objc = (ObjectCluster) shimmerMSG.mB;
float[] newData = new float[channelNames.length];
String[] signalNameArray = new String[channelNames.length];
String[] signalFormatArray = new String[channelNames.length];
String[] signalUnitArray = new String[channelNames.length];
for (int i = 0; i < channelNames.length; i++) {
FormatCluster formatCluster = objc.getFormatCluster(channelNames[i], "CAL");
if (formatCluster != null) {
newData[i] = (float) formatCluster.mData;
signalNameArray[i] = channelNames[i];
signalFormatArray[i] = formatCluster.mFormat;
signalUnitArray[i] = formatCluster.mUnits;
}
}
String comPort = objc.getComPort();
Object[] outputData = { comPort, newData, signalNameArray, signalFormatArray, signalUnitArray };
sendData(comPort, outputData);
}
}
}
public class MatlabConnectionEvent {
public String state;
public String comPort;
public MatlabConnectionEvent(String state, String comPort) {
this.state = state;
this.comPort = comPort;
}
}
}