|
1 | | -import org.apache.commons.lang3.tuple.Pair; |
2 | | - |
3 | | -abstract class Board implements DataSource { |
4 | | - |
5 | | - private FixedStack<double[]> accumulatedData = new FixedStack<double[]>(); |
6 | | - private double[][] dataThisFrame; |
7 | | - protected PacketLossTracker packetLossTracker; |
8 | | - |
9 | | - // accessible by all boards, can be returned as valid empty data |
10 | | - protected double[][] emptyData; |
11 | | - |
12 | | - @Override |
13 | | - public boolean initialize() { |
14 | | - boolean res = initializeInternal(); |
15 | | - |
16 | | - double[] fillData = new double[getTotalChannelCount()]; |
17 | | - accumulatedData.setSize(getCurrentBoardBufferSize()); |
18 | | - accumulatedData.fill(fillData); |
19 | | - |
20 | | - emptyData = new double[getTotalChannelCount()][0]; |
21 | | - |
22 | | - packetLossTracker = setupPacketLossTracker(); |
23 | | - |
24 | | - return res; |
25 | | - } |
26 | | - |
27 | | - @Override |
28 | | - public void uninitialize() { |
29 | | - uninitializeInternal(); |
30 | | - } |
31 | | - |
32 | | - @Override |
33 | | - public void startStreaming() { |
34 | | - packetLossTracker.onStreamStart(); |
35 | | - } |
36 | | - |
37 | | - @Override |
38 | | - public void stopStreaming() { |
39 | | - |
40 | | - // empty |
41 | | - } |
42 | | - |
43 | | - @Override |
44 | | - public void update() { |
45 | | - updateInternal(); |
46 | | - |
47 | | - dataThisFrame = getNewDataInternal(); |
48 | | - |
49 | | - for (int i = 0; i < dataThisFrame[0].length; i++) { |
50 | | - double[] newEntry = new double[getTotalChannelCount()]; |
51 | | - for (int j = 0; j < getTotalChannelCount(); j++) { |
52 | | - newEntry[j] = dataThisFrame[j][i]; |
53 | | - } |
54 | | - |
55 | | - accumulatedData.push(newEntry); |
56 | | - } |
57 | | - |
58 | | - if( packetLossTracker != null) { |
59 | | - //TODO: make all API including getNewDataInternal() return List<double[]> |
60 | | - // and we can just pass dataThisFrame here. |
61 | | - packetLossTracker.addSamples(getData(dataThisFrame[0].length)); |
62 | | - } |
63 | | - } |
64 | | - |
65 | | - @Override |
66 | | - public int getNumEXGChannels() { |
67 | | - return getEXGChannels().length; |
68 | | - } |
69 | | - |
70 | | - // returns all the data this board has received in this frame |
71 | | - @Override |
72 | | - public double[][] getFrameData() { |
73 | | - return dataThisFrame; |
74 | | - } |
75 | | - |
76 | | - @Override |
77 | | - public List<double[]> getData(int maxSamples) { |
78 | | - int endIndex = accumulatedData.size(); |
79 | | - int startIndex = max(0, endIndex - maxSamples); |
80 | | - |
81 | | - return accumulatedData.subList(startIndex, endIndex); |
82 | | - } |
83 | | - |
84 | | - public String[] getChannelNames() { |
85 | | - String[] names = new String[getTotalChannelCount()]; |
86 | | - Arrays.fill(names, "Other"); |
87 | | - |
88 | | - names[getTimestampChannel()] = "Timestamp"; |
89 | | - names[getSampleIndexChannel()] = "Sample Index"; |
90 | | - |
91 | | - int[] exgChannels = getEXGChannels(); |
92 | | - for (int i=0; i<exgChannels.length; i++) { |
93 | | - names[exgChannels[i]] = "EXG Channel " + i; |
94 | | - } |
95 | | - |
96 | | - addChannelNamesInternal(names); |
97 | | - return names; |
98 | | - } |
99 | | - |
100 | | - public PacketLossTracker getPacketLossTracker() { |
101 | | - return packetLossTracker; |
102 | | - } |
103 | | - |
104 | | - public abstract boolean isConnected(); |
105 | | - |
106 | | - public abstract boolean isStreaming(); |
107 | | - |
108 | | - public abstract Pair <Boolean, String> sendCommand(String command); |
109 | | - |
110 | | - public abstract void insertMarker(int value); |
111 | | - |
112 | | - public abstract void insertMarker(double value); |
113 | | - |
114 | | - // *************************************** |
115 | | - // protected methods implemented by board |
116 | | - |
117 | | - // implemented by each board class and used internally here to accumulate the FixedStack |
118 | | - // and provide with public interfaces getFrameData() and getData(int) |
119 | | - protected abstract double[][] getNewDataInternal(); |
120 | | - |
121 | | - protected abstract boolean initializeInternal(); |
122 | | - |
123 | | - protected abstract void uninitializeInternal(); |
124 | | - |
125 | | - protected abstract void updateInternal(); |
126 | | - |
127 | | - protected abstract void addChannelNamesInternal(String[] channelNames); |
128 | | - |
129 | | - protected abstract PacketLossTracker setupPacketLossTracker(); |
130 | | -}; |
| 1 | +import org.apache.commons.lang3.tuple.Pair; |
| 2 | + |
| 3 | +abstract class Board implements DataSource { |
| 4 | + |
| 5 | + private FixedStack<double[]> accumulatedData = new FixedStack<double[]>(); |
| 6 | + private double[][] dataThisFrame; |
| 7 | + protected PacketLossTracker packetLossTracker; |
| 8 | + |
| 9 | + // accessible by all boards, can be returned as valid empty data |
| 10 | + protected double[][] emptyData; |
| 11 | + |
| 12 | + @Override |
| 13 | + public boolean initialize() { |
| 14 | + boolean res = initializeInternal(); |
| 15 | + |
| 16 | + double[] fillData = new double[getTotalChannelCount()]; |
| 17 | + accumulatedData.setSize(getCurrentBoardBufferSize()); |
| 18 | + accumulatedData.fill(fillData); |
| 19 | + |
| 20 | + emptyData = new double[getTotalChannelCount()][0]; |
| 21 | + |
| 22 | + packetLossTracker = setupPacketLossTracker(); |
| 23 | + |
| 24 | + return res; |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public void uninitialize() { |
| 29 | + uninitializeInternal(); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public void startStreaming() { |
| 34 | + packetLossTracker.onStreamStart(); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void stopStreaming() { |
| 39 | + |
| 40 | + // empty |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void update() { |
| 45 | + updateInternal(); |
| 46 | + |
| 47 | + dataThisFrame = getNewDataInternal(); |
| 48 | + |
| 49 | + for (int i = 0; i < dataThisFrame[0].length; i++) { |
| 50 | + double[] newEntry = new double[getTotalChannelCount()]; |
| 51 | + for (int j = 0; j < getTotalChannelCount(); j++) { |
| 52 | + newEntry[j] = dataThisFrame[j][i]; |
| 53 | + } |
| 54 | + |
| 55 | + accumulatedData.push(newEntry); |
| 56 | + } |
| 57 | + |
| 58 | + if( packetLossTracker != null) { |
| 59 | + //TODO: make all API including getNewDataInternal() return List<double[]> |
| 60 | + // and we can just pass dataThisFrame here. |
| 61 | + packetLossTracker.addSamples(getData(dataThisFrame[0].length)); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public int getNumEXGChannels() { |
| 67 | + return getEXGChannels().length; |
| 68 | + } |
| 69 | + |
| 70 | + // returns all the data this board has received in this frame |
| 71 | + @Override |
| 72 | + public double[][] getFrameData() { |
| 73 | + return dataThisFrame; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public List<double[]> getData(int maxSamples) { |
| 78 | + int endIndex = accumulatedData.size(); |
| 79 | + int startIndex = max(0, endIndex - maxSamples); |
| 80 | + |
| 81 | + return accumulatedData.subList(startIndex, endIndex); |
| 82 | + } |
| 83 | + |
| 84 | + public String[] getChannelNames() { |
| 85 | + String[] names = new String[getTotalChannelCount()]; |
| 86 | + Arrays.fill(names, "Other"); |
| 87 | + |
| 88 | + names[getTimestampChannel()] = "Timestamp"; |
| 89 | + names[getSampleIndexChannel()] = "Sample Index"; |
| 90 | + |
| 91 | + int[] exgChannels = getEXGChannels(); |
| 92 | + for (int i=0; i<exgChannels.length; i++) { |
| 93 | + names[exgChannels[i]] = "EXG Channel " + i; |
| 94 | + } |
| 95 | + |
| 96 | + addChannelNamesInternal(names); |
| 97 | + return names; |
| 98 | + } |
| 99 | + |
| 100 | + public PacketLossTracker getPacketLossTracker() { |
| 101 | + return packetLossTracker; |
| 102 | + } |
| 103 | + |
| 104 | + public abstract boolean isConnected(); |
| 105 | + |
| 106 | + public abstract boolean isStreaming(); |
| 107 | + |
| 108 | + public abstract Pair <Boolean, String> sendCommand(String command); |
| 109 | + |
| 110 | + public abstract void insertMarker(int value); |
| 111 | + |
| 112 | + public abstract void insertMarker(double value); |
| 113 | + |
| 114 | + // *************************************** |
| 115 | + // protected methods implemented by board |
| 116 | + |
| 117 | + // implemented by each board class and used internally here to accumulate the FixedStack |
| 118 | + // and provide with public interfaces getFrameData() and getData(int) |
| 119 | + protected abstract double[][] getNewDataInternal(); |
| 120 | + |
| 121 | + protected abstract boolean initializeInternal(); |
| 122 | + |
| 123 | + protected abstract void uninitializeInternal(); |
| 124 | + |
| 125 | + protected abstract void updateInternal(); |
| 126 | + |
| 127 | + protected abstract void addChannelNamesInternal(String[] channelNames); |
| 128 | + |
| 129 | + protected abstract PacketLossTracker setupPacketLossTracker(); |
| 130 | +}; |
0 commit comments