Skip to content

Commit 43e2a97

Browse files
committed
Merge branch 'feature/session_builder' into feature/logging
2 parents 8418a75 + d1f1915 commit 43e2a97

File tree

7 files changed

+223
-1
lines changed

7 files changed

+223
-1
lines changed

SensorLib/sensorlib/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ android {
5656

5757
def versionMajor = 0
5858
def versionMinor = 4
59-
def versionPatch = 1
59+
def versionPatch = 2
6060

6161
versionCode versionMajor * 10000 + versionMinor * 100 + versionPatch
6262
versionName "${versionMajor}.${versionMinor}.${versionPatch}." + versionBuild + "_" + getGitDate() + "-" + getGitHash()

SensorLib/sensorlib/src/main/java/de/fau/sensorlib/widgets/StatusBar.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ public void setState(SensorState status) {
9797
setBackgroundColor(ContextCompat.getColor(mContext, R.color.status_bar_streaming));
9898
mStateTextView.setText(getResources().getString(R.string.status_bar_streaming, mSensorName).toUpperCase());
9999
break;
100+
case LOGGING:
101+
setBackgroundColor(ContextCompat.getColor(mContext, R.color.status_bar_logging));
102+
mStateTextView.setText(getResources().getString(R.string.status_bar_loggging, mSensorName).toUpperCase());
103+
break;
100104
case SIMULATING:
101105
setBackgroundColor(ContextCompat.getColor(mContext, R.color.status_bar_simulating));
102106
mStateTextView.setText(getResources().getString(R.string.status_bar_simulating, mSensorName).toUpperCase());

SensorLib/sensorlib/src/main/java/de/fau/sensorlib/widgets/StreamingFooter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,12 @@ public void onSensorStateChange(AbstractSensor sensor, SensorState state) {
211211
mStartStopButton.setText(R.string.start);
212212
}
213213
break;
214+
case LOGGING:
215+
if (mFabOpen) {
216+
mFab.performClick();
217+
mStartStopButton.setText(R.string.stop);
218+
}
219+
break;
214220
case CONNECTION_LOST:
215221
mFab.performClick();
216222
mDisconnectButton.performClick();

SensorLib/sensorlib/src/main/res/values/colors.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<color name="status_bar_connecting">#FF9800</color>
1919
<color name="status_bar_connected">#8BC34A</color>
2020
<color name="status_bar_streaming">#4CAF50</color>
21+
<color name="status_bar_logging">#009688</color>
2122
<color name="status_bar_simulating">#03A9F4</color>
2223

2324
<!-- Streaming Footer -->

SensorLib/sensorlib/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<string name="status_bar_connecting">%1$s connecting</string>
1818
<string name="status_bar_connected">%1$s connected</string>
1919
<string name="status_bar_streaming">%1$s streaming</string>
20+
<string name="status_bar_loggging">%1$s logging</string>
2021
<string name="status_bar_simulating">%1$s simulating</string>
2122

2223
<!-- Streaming Footer -->
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
/*
2+
* Copyright (C) 2019 Machine Learning and Data Analytics Lab, Friedrich-Alexander-Universität Erlangen-Nürnberg (FAU).
3+
* <p>
4+
* This file is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
5+
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. If you reuse
6+
* this code you have to keep or cite this comment.
7+
*/
8+
9+
package de.fau.sensorlib.sensors.logging;
10+
11+
import android.bluetooth.BluetoothGattCharacteristic;
12+
import android.util.Log;
13+
14+
import java.nio.BufferOverflowException;
15+
import java.nio.ByteBuffer;
16+
import java.util.Arrays;
17+
import java.util.Date;
18+
import java.util.HashMap;
19+
20+
import de.fau.sensorlib.enums.HardwareSensor;
21+
import de.fau.sensorlib.sensors.AbstractSensor;
22+
import de.fau.sensorlib.sensors.InsoleSensor;
23+
import de.fau.sensorlib.sensors.NilsPodSensor;
24+
import de.fau.sensorlib.sensors.enums.NilsPodRfGroup;
25+
import de.fau.sensorlib.sensors.enums.NilsPodSyncRole;
26+
import de.fau.sensorlib.sensors.enums.NilsPodTerminationSource;
27+
28+
public class SessionBuilder {
29+
30+
private static final String TAG = SessionBuilder.class.getSimpleName();
31+
32+
33+
private boolean mFirstPacketRead;
34+
35+
private int mHeaderSize;
36+
private int mSampleSize;
37+
private HashMap<HardwareSensor, Boolean> mEnabledSensorsMap = new HashMap<>();
38+
39+
private ByteBuffer mByteBuffer;
40+
41+
private AbstractSensor mSensor;
42+
private Session mSession;
43+
44+
public SessionBuilder(AbstractSensor sensor, Session session) {
45+
mSensor = sensor;
46+
mSession = session;
47+
}
48+
49+
public void nextPacket(byte[] values) {
50+
if (!mFirstPacketRead) {
51+
Log.e(TAG, Arrays.toString(values));
52+
mFirstPacketRead = true;
53+
mHeaderSize = values[0];
54+
byte[] header = new byte[mHeaderSize];
55+
byte[] data = new byte[values.length - mHeaderSize];
56+
System.arraycopy(values, 0, header, 0, header.length);
57+
System.arraycopy(values, mHeaderSize, data, 0, data.length);
58+
extractHeader(header);
59+
onNewData(data);
60+
} else {
61+
onNewData(values);
62+
}
63+
}
64+
65+
// TODO TEST!!!
66+
private synchronized void extractHeader(byte[] values) {
67+
BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(null, 0, 0);
68+
characteristic.setValue(values);
69+
70+
Log.e(TAG, "header: " + Arrays.toString(values));
71+
int offset = 1;
72+
mSampleSize = values[offset++];
73+
74+
mByteBuffer = ByteBuffer.allocate(mSampleSize * 1000);
75+
76+
int sensors = values[offset++];
77+
mEnabledSensorsMap.put(HardwareSensor.ACCELEROMETER, ((sensors & 0x01) != 0));
78+
mEnabledSensorsMap.put(HardwareSensor.GYROSCOPE, ((sensors & 0x01) != 0));
79+
mEnabledSensorsMap.put(HardwareSensor.FSR, ((sensors & 0x02) != 0));
80+
mEnabledSensorsMap.put(HardwareSensor.BAROMETER, ((sensors & 0x04) != 0));
81+
82+
83+
double samplingRate = NilsPodSensor.inferSamplingRate(values[offset] & 0x0F);
84+
NilsPodTerminationSource terminationSource = NilsPodTerminationSource.inferTerminationSource(values[offset++] & 0xF0);
85+
86+
NilsPodSyncRole syncRole = NilsPodSyncRole.values()[values[offset++]];
87+
int syncDistance = values[offset++] * 100;
88+
NilsPodRfGroup rfGroup = NilsPodRfGroup.values()[values[offset++]];
89+
90+
int accRange = values[offset++];
91+
int gyroRange = values[offset++];
92+
93+
// metadata
94+
int sensorPosition = values[offset++];
95+
int specialFunction = values[offset++];
96+
offset += 3;
97+
98+
99+
int tmpTime = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT32, offset);
100+
Date startTime = new Date(((long) tmpTime) * 1000);
101+
offset += 4;
102+
103+
tmpTime = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT32, offset);
104+
// little endian
105+
//tmpTime = (values[offset++] & 0xFF) | ((values[offset++] & 0xFF) << 8) | ((values[offset++] & 0xFF) << 16) | ((values[offset++] & 0xFF) << 24);
106+
Date endTime = new Date(((long) tmpTime) * 1000);
107+
offset += 4;
108+
109+
//int sessionSize = ((values[offset++] & 0xFF) << 24) | ((values[offset++] & 0xFF) << 16) | ((values[offset++] & 0xFF) << 8) | (values[offset++] & 0xFF);
110+
int sessionSize = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT32, offset);
111+
offset += 4;
112+
String firmwareVersion = values[offset++] + "." + values[offset++] + "." + values[offset];
113+
114+
String sb = "sample size: " + mSampleSize + "\n" +
115+
"enabled sensors: " + mEnabledSensorsMap + "\n" +
116+
"sampling rate: " + samplingRate + "\n" +
117+
"termination source: " + terminationSource + "\n" +
118+
"sync role: " + syncRole + "\n" +
119+
"sync distance: " + syncDistance + "\n" +
120+
"rf group: " + rfGroup + "\n" +
121+
"acc range: " + accRange + "\n" +
122+
"gyro range: " + gyroRange + "\n" +
123+
"sensor position: " + sensorPosition + "\n" +
124+
"special function: " + specialFunction + "\n" +
125+
"start Time: " + startTime + "\n" +
126+
"end Time: " + endTime + "\n" +
127+
"session size: " + sessionSize + "\n" +
128+
"firmware version: " + firmwareVersion + "\n";
129+
Log.e(TAG, sb);
130+
}
131+
132+
133+
private synchronized void onNewData(byte[] values) {
134+
try {
135+
mByteBuffer.put(values);
136+
} catch (BufferOverflowException e) {
137+
e.printStackTrace();
138+
}
139+
140+
byte[] sample;
141+
// flip buffer to start reading
142+
mByteBuffer.flip();
143+
while (mByteBuffer.remaining() / mSampleSize > 0) {
144+
sample = new byte[mSampleSize];
145+
// get one data sample
146+
mByteBuffer.get(sample);
147+
extractDataFrame(sample);
148+
}
149+
// compact buffer to shift remaining samples to beginning
150+
mByteBuffer.compact();
151+
}
152+
153+
154+
private void extractDataFrame(byte[] values) {
155+
BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(null, 0, 0);
156+
characteristic.setValue(values);
157+
int offset = 0;
158+
double[] gyro = new double[3];
159+
double[] accel = new double[3];
160+
double baro = 0;
161+
double[] pressure = new double[3];
162+
int timestamp;
163+
164+
if (isSensorEnabled(HardwareSensor.GYROSCOPE)) {
165+
// extract gyroscope data
166+
for (int j = 0; j < 3; j++) {
167+
gyro[j] = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, offset);
168+
offset += 2;
169+
}
170+
}
171+
// extract accelerometer data
172+
if (isSensorEnabled(HardwareSensor.ACCELEROMETER)) {
173+
for (int j = 0; j < 3; j++) {
174+
accel[j] = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, offset);
175+
offset += 2;
176+
}
177+
}
178+
179+
if (isSensorEnabled(HardwareSensor.BAROMETER)) {
180+
baro = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_SINT16, offset);
181+
baro = (baro + 101325.0) / 100.0;
182+
offset += 2;
183+
}
184+
185+
if (isSensorEnabled(HardwareSensor.FSR)) {
186+
for (int j = 0; j < 3; j++) {
187+
pressure[j] = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, offset);
188+
offset++;
189+
}
190+
}
191+
192+
timestamp = ((values[offset++] & 0xFF) << 24) | ((values[offset++] & 0xFF) << 16) | ((values[offset++] & 0xFF) << 8) | values[offset] & 0xFF;
193+
194+
InsoleSensor.InsoleDataFrame df = new InsoleSensor.InsoleDataFrame(mSensor, timestamp, accel, gyro, baro, pressure);
195+
196+
Log.d(TAG, df.toString());
197+
//mDataRecorder.writeData(df);
198+
}
199+
200+
public boolean isSensorEnabled(HardwareSensor sensor) {
201+
return (mEnabledSensorsMap.get(sensor) != null) && mEnabledSensorsMap.get(sensor);
202+
}
203+
204+
}

SensorLib/sls-portabiles/src/main/java/de/fau/sensorlib/sensors/logging/SessionDownloader.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class SessionDownloader {
2424
private Session mSession;
2525

2626
private SessionByteWriter mSessionWriter;
27+
private SessionBuilder mSessionBuilder;
2728

2829
// in Byte
2930
private int mProgress;
@@ -60,6 +61,10 @@ public void setSessionWriter() throws SensorException {
6061
mSessionWriter = new SessionByteWriter(mSensor, mSession, mSensor.getContext());
6162
}
6263

64+
public void setSessionBuilder() {
65+
mSessionBuilder = new SessionBuilder(mSensor, mSession);
66+
}
67+
6368
public void onNewData(byte[] values) {
6469
mProgress += values.length;
6570
mElapsedTime = System.currentTimeMillis() - mStartTime;
@@ -74,6 +79,7 @@ public void onNewData(byte[] values) {
7479
mEstimatedRemainingTime = (long) (remainingBytes / mDownloadRate) * 1000;
7580

7681
mSessionWriter.writeData(values);
82+
mSessionBuilder.nextPacket(values);
7783
}
7884

7985
public int getProgress() {

0 commit comments

Comments
 (0)