|
| 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 | +} |
0 commit comments