diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/driver/Configuration.java b/ShimmerDriver/src/main/java/com/shimmerresearch/driver/Configuration.java index bf813e203..0f2f44607 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/driver/Configuration.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/driver/Configuration.java @@ -94,6 +94,7 @@ import com.shimmerresearch.sensors.adxl371.SensorADXL371; import com.shimmerresearch.sensors.lsm6dsv.SensorLSM6DSV; import com.shimmerresearch.sensors.lis2dw12.SensorLIS2DW12; +import com.shimmerresearch.sensors.soundlevel.SensorSoundLevel; import com.shimmerresearch.verisense.sensors.SensorLSM6DS3; //import com.shimmerresearch.verisense.sensors.SensorLIS2DW12; import com.shimmerresearch.verisense.sensors.SensorBattVoltageVerisense; @@ -158,6 +159,8 @@ public static class CHANNEL_UNITS{ public static final String GRAVITY = "g"; public static final String RPM = "rpm"; public static final String POWER = "dB"; + /** A-weighted sound pressure level, the Shimmer3R sound level channel's unit (DEV-1413) */ + public static final String DECIBELS_A = "dB(A)"; public static final String SCORE = "Score"; public static final String ACCEL_CAL_UNIT = METER_PER_SECOND_SQUARE; @@ -735,6 +738,8 @@ public static class SENSOR_ID{ public static final int SHIMMER_LIS3MDL_MAG_ALT = 41; public static final int SHIMMER_BMP390_PRESSURE = 43; public static final int SHIMMER_BMP581_PRESSURE = 44; + /** Shimmer3R microphone sound level, channel 0x38 (DEV-1413): enabled through configuration byte 233 bit 4, not a sensor bitmap bit */ + public static final int SHIMMER_SOUND_LEVEL = 45; // public static final int SHIMMER_EXG1_24BIT = 3; // public static final int SHIMMER_EXG2_24BIT = 4; @@ -943,7 +948,8 @@ public enum LABEL_SENSOR_TILE{ MPU_OTHER(SensorMPU9X50.LABEL_SENSOR_TILE.MPU_OTHER), GPS("GPS"), STC3100_MONITORING (SensorSTC3100.LABEL_SENSOR_TILE.STC3100_MONITORING), - SWEATCH_ADC ("Sweatch ADC"); + SWEATCH_ADC ("Sweatch ADC"), + SOUND_LEVEL(SensorSoundLevel.LABEL_SENSOR_TILE.SOUND_LEVEL); private String tileText = ""; LABEL_SENSOR_TILE(String text){ @@ -1331,6 +1337,8 @@ public static class CompatibilityInfoForMaps{ private static final ShimmerVerObject svoShimmer3RImuAnyExpBrdLogAndStream = new ShimmerVerObject(HW_ID.SHIMMER_3R,FW_ID.LOGANDSTREAM,0,1,0,ShimmerVerDetails.ANY_VERSION, ShimmerVerDetails.ANY_VERSION, NEW_IMU_EXP_REV.IMU); public static final ShimmerVerObject svoShimmer3RLogAndStream = new ShimmerVerObject(HW_ID.SHIMMER_3R,FW_ID.LOGANDSTREAM,ShimmerVerDetails.ANY_VERSION,ShimmerVerDetails.ANY_VERSION,ShimmerVerDetails.ANY_VERSION,ShimmerVerDetails.ANY_VERSION); + /** LogAndStream_Shimmer3R v1.01.026 and later: the first release with the sound level channel (DEV-1413) */ + public static final ShimmerVerObject svoShimmer3RLogAndStreamSoundLevel = new ShimmerVerObject(HW_ID.SHIMMER_3R,FW_ID.LOGANDSTREAM,1,1,26,ShimmerVerDetails.ANY_VERSION); public static final ShimmerVerObject svoShimmer3LogAndStreamWithSDLogSyncSupport = new ShimmerVerObject(HW_ID.SHIMMER_3,FW_ID.LOGANDSTREAM,0,16,11,ShimmerVerDetails.ANY_VERSION); public static final ShimmerVerObject svoShimmer3RLogAndStreamWithSDLogSyncSupport = new ShimmerVerObject(HW_ID.SHIMMER_3R,FW_ID.LOGANDSTREAM,ShimmerVerDetails.ANY_VERSION,ShimmerVerDetails.ANY_VERSION,ShimmerVerDetails.ANY_VERSION,ShimmerVerDetails.ANY_VERSION); @@ -1538,6 +1546,13 @@ public static class CompatibilityInfoForMaps{ svoShimmer3RBrAmpUnifiedLogAndStream, svoShimmer3RProto3DeluxeLogAndStream); + //Shimmer3r sound level (DEV-1413), from LogAndStream_Shimmer3R v1.01.026, + //the first release with the channel. Earlier releases store + //configuration byte 233 untouched, so bit 4 would read back set there + //with no channel behind it: the tile is not offered on them. + public static final List listOfCompatibleVersionInfoSoundLevel = Arrays.asList( + svoShimmer3RLogAndStreamSoundLevel); + //TODO need to update this list and remove "any" public static final List listOfCompatibleVersionInfoKionixKXRB52042 = Arrays.asList( svoAnyIntExpBoardAndSdlog, svoAnyIntExpBoardAndBtStream, svoAnyIntExpBoardAndLogAndStream, diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/driver/ShimmerObject.java b/ShimmerDriver/src/main/java/com/shimmerresearch/driver/ShimmerObject.java index 2679bc312..bf568640c 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/driver/ShimmerObject.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/driver/ShimmerObject.java @@ -95,6 +95,7 @@ import com.shimmerresearch.sensors.shimmer2.SensorMMA736x; import com.shimmerresearch.sensors.shimmer2.SensorShimmer2Gyro; import com.shimmerresearch.sensors.shimmer2.SensorShimmer2Mag; +import com.shimmerresearch.sensors.soundlevel.SensorSoundLevel; import com.shimmerresearch.algorithms.orientation.GradDes3DOrientation; import com.shimmerresearch.algorithms.orientation.GradDes3DOrientation9DoF; import com.shimmerresearch.algorithms.orientation.Orientation3DObject; @@ -2378,6 +2379,23 @@ else if (isEXGUsingDefaultThreeUnipolarConfiguration()){ calibratedDataUnits[sigIndex]=CHANNEL_UNITS.BEATS_PER_MINUTE; } + // Sound level (DEV-1413, Shimmer3R). Enabled through configuration + // byte 233 bit 4 rather than a sensor bitmap bit, so it is decoded + // whenever the channel list carries it. 0x8000 (no level) is NaN. + int iSoundLevel = getSignalIndex(SensorSoundLevel.ObjectClusterSensorName.SOUND_LEVEL); + if (iSoundLevel!=-1){ + double soundLevelRaw = (double)newPacketInt[iSoundLevel]; + objectCluster.addDataToMap(SensorSoundLevel.ObjectClusterSensorName.SOUND_LEVEL,CHANNEL_TYPE.UNCAL.toString(),CHANNEL_UNITS.NO_UNITS,soundLevelRaw); + uncalibratedData[iSoundLevel]=soundLevelRaw; + uncalibratedDataUnits[iSoundLevel]=CHANNEL_UNITS.NO_UNITS; + if (mEnableCalibration){ + double soundLevelDbA = SensorSoundLevel.calibrateSoundLevel(soundLevelRaw); + objectCluster.addDataToMap(SensorSoundLevel.ObjectClusterSensorName.SOUND_LEVEL,CHANNEL_TYPE.CAL.toString(),CHANNEL_UNITS.DECIBELS_A,soundLevelDbA); + calibratedData[iSoundLevel]=soundLevelDbA; + calibratedDataUnits[iSoundLevel]=CHANNEL_UNITS.DECIBELS_A; + } + } + //Additional Channels Offset int additionalChannelsOffset = calibratedData.length-numAdditionalChannels; if(fwType == COMMUNICATION_TYPE.BLUETOOTH) { @@ -3631,6 +3649,14 @@ else if ((byte)signalId[i-iTS]==(byte)0x28){ enabledSensors |= SENSOR_BRIDGE_AMP; } } + else if ((byte)signalId[i-iTS]==SensorSoundLevel.CHANNEL_ID && getHardwareVersion()==HW_ID.SHIMMER_3R){ + // Sound level (DEV-1413): a signed 16-bit little-endian count of + // 0.01 dB(A). A Shimmer3 never sends 0x38 and keeps the unknown + // channel handling below. + signalNameArray[i+1]=SensorSoundLevel.ObjectClusterSensorName.SOUND_LEVEL; + signalDataTypeArray[i+1] = "i16"; + packetSize=packetSize+2; + } else{ signalNameArray[i+1]=Byte.toString(signalId[i-iTS]); signalDataTypeArray[i+1] = "u12"; @@ -5605,6 +5631,10 @@ else if(isSupportedNewImuSensors()){ mSensorLIS2MDL = new SensorLIS2MDL(this); addSensorClass(mSensorLIS2MDL); + + // Every Shimmer3R has the microphone; whether its firmware has the + // channel is learnt from configuration byte 233 (DEV-1413) + addSensorClass(new SensorSoundLevel(this)); } } @@ -5858,6 +5888,11 @@ else if(sensorId == Configuration.Shimmer3.SENSOR_ID.HOST_SHIMMER_STREAMING_PROP mSensorMap.get(sensorId).setIsEnabled(true); return true; } + // The sound level (DEV-1413) has no sensor bitmap bit, so the bitmap + // would always turn it off: its state comes from configuration byte 233 + else if(sensorId == Configuration.Shimmer3.SENSOR_ID.SHIMMER_SOUND_LEVEL){ + return true; + } return false; } diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/driver/shimmer2r3/ConfigByteLayoutShimmer3.java b/ShimmerDriver/src/main/java/com/shimmerresearch/driver/shimmer2r3/ConfigByteLayoutShimmer3.java index b9d1aa436..5f5b05854 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/driver/shimmer2r3/ConfigByteLayoutShimmer3.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/driver/shimmer2r3/ConfigByteLayoutShimmer3.java @@ -168,6 +168,17 @@ public class ConfigByteLayoutShimmer3 extends ConfigByteLayout implements Serial public int maskLIS2MDLMagRateMSB = 0x07; public int bitShiftLIS2MDLMagRateMSB = 3; + //Config Byte 233, Shimmer3R only (0 where there is none): the microphone. + //Bits 0-1 are the audio mode and bits 2-3 the gain preset (DEV-99), kept as + //read on a configuration write. Bit 4 turns on the sound level channel, + //0x38 (DEV-1413). The firmware clears bits 5-6, and reads the whole byte + //as 0 when bit 7 is set, as it is in erased flash (0xFF). + public int idxAudioConfig = 0; + public int maskAudioModeAndGain = 0x0F; + public int bitShiftSoundLevel = 4; + public int maskSoundLevel = 0x01; + public int maskAudioConfigInvalid = 0x80; + // Derived Channels - used by SW not FW public int maskDerivedChannelsByte = 0xFF; public int byteShiftDerivedSensors0 = 8*0; @@ -395,6 +406,12 @@ public ConfigByteLayoutShimmer3(int firmwareIdentifier, int firmwareVersionMajor maskShowErrorLedsSd = 0x01; } + // Any Shimmer3R firmware: older builds store the byte untouched, so + // writing back what was read is safe on them too + if(mShimmerVerObject.mHardwareVersion == HW_ID.SHIMMER_3R) { + idxAudioConfig = 128+105; + } + if(mShimmerVerObject.mHardwareVersion == HW_ID.SHIMMER_3R || compareVersions(FW_ID.LOGANDSTREAM,0,8,1)) { idxBtFactoryReset = 128+103; @@ -537,6 +554,9 @@ public HashMap getMapOfByteDescriptions(){ mapOfByteDescriptions.put(idxMaxExpLengthLsb, "idxMaxExpLengthLsb"); mapOfByteDescriptions.put(idxMacAddress, "idxMacAddress"); mapOfByteDescriptions.put(idxSDConfigDelayFlag, "idxSDConfigDelayFlag"); + if(idxAudioConfig>0){ + mapOfByteDescriptions.put(idxAudioConfig, "idxAudioConfig"); + } if(idxBtFactoryReset>0){ mapOfByteDescriptions.put(idxBtFactoryReset, "idxBtFactoryReset"); diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/sensors/AbstractSensor.java b/ShimmerDriver/src/main/java/com/shimmerresearch/sensors/AbstractSensor.java index c1049b229..890f78f42 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/sensors/AbstractSensor.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/sensors/AbstractSensor.java @@ -71,7 +71,8 @@ public enum SENSORS{ VD6283("VD6283"), MLX90632("MLX90632"), BMP390("BMP390"), - BMP581("BMP581"); + BMP581("BMP581"), + SOUND_LEVEL("Sound Level"); private final String text; diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/sensors/soundlevel/SensorSoundLevel.java b/ShimmerDriver/src/main/java/com/shimmerresearch/sensors/soundlevel/SensorSoundLevel.java new file mode 100644 index 000000000..8365b00c3 --- /dev/null +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/sensors/soundlevel/SensorSoundLevel.java @@ -0,0 +1,317 @@ +package com.shimmerresearch.sensors.soundlevel; + +import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; + +import com.shimmerresearch.driver.ConfigByteLayout; +import com.shimmerresearch.driver.Configuration; +import com.shimmerresearch.driver.Configuration.CHANNEL_UNITS; +import com.shimmerresearch.driver.Configuration.COMMUNICATION_TYPE; +import com.shimmerresearch.driver.Configuration.Shimmer3.CompatibilityInfoForMaps; +import com.shimmerresearch.driver.FormatCluster; +import com.shimmerresearch.driver.ObjectCluster; +import com.shimmerresearch.driver.ShimmerDevice; +import com.shimmerresearch.driver.shimmer2r3.ConfigByteLayoutShimmer3; +import com.shimmerresearch.driverUtilities.ChannelDetails; +import com.shimmerresearch.driverUtilities.ChannelDetails.CHANNEL_DATA_ENDIAN; +import com.shimmerresearch.driverUtilities.ChannelDetails.CHANNEL_DATA_TYPE; +import com.shimmerresearch.driverUtilities.ChannelDetails.CHANNEL_TYPE; +import com.shimmerresearch.driverUtilities.SensorDetails; +import com.shimmerresearch.driverUtilities.SensorDetailsRef; +import com.shimmerresearch.driverUtilities.SensorGroupingDetails; +import com.shimmerresearch.sensors.AbstractSensor; +import com.shimmerresearch.sensors.ActionSetting; + +/** + * The Shimmer3R sound level channel (DEV-1413): the A-weighted sound level + * from the microphone, LAeq over the most recent completed 128 ms block, in + * dB(A) SPL. LogAndStream_Shimmer3R v1.01.026 is the first release with it. + *

+ * Nominal. The firmware adds the microphone's datasheet sensitivity and + * nothing more; no unit has been checked against a calibrated sound level + * meter. + *

+ * On the wire. Channel ID 0x38, two bytes, a signed 16-bit + * little-endian count of 0.01 dB(A). 0x8000 (-32768) means there is no level: + * while capture settles (about the first 256 ms), or when it is stopped or + * has failed. {@link #calibrateSoundLevel(double)} makes that NaN, never + * -327.68. A value describes the block that ended 0-128 ms before the + * packet's timestamp, plus the main loop's latency (up to about 0.2 s more + * while an SD card is slow); nothing here corrects for that. The firmware + * places the channel just before the ADS7028 block (GSR, the ADCs and the + * battery), so the battery stays last, but the decoder takes its position + * from the channel list, as for any channel. A Shimmer3 never sends it. + *

+ * Enabling it. Not a sensor bitmap bit: configuration byte 233 bit 4. + * Byte 233 also holds the DEV-99 audio mode (bits 0-1) and gain preset (bits + * 2-3), which this class keeps as read so that a full configuration write, + * which builds its image from zeros, gives the Shimmer back its own, on any + * Shimmer3R firmware. + *

+ * Offered from v1.01.026. The sensor and its tile are only offered on + * LogAndStream_Shimmer3R v1.01.026 and later + * ({@link CompatibilityInfoForMaps#listOfCompatibleVersionInfoSoundLevel}). + * Releases before it (DEV-99) store byte 233 untouched, so there bit 4 would + * read back set while no channel is sent. On them, a bit 4 that is already + * set reads as off, and the next configuration write clears it, so a later + * firmware update does not turn the channel on unasked. The channel list, + * not the bit, is still what the decoder goes by. + *

+ * This class is only created for a Shimmer3R. A Shimmer3's byte 233 stays 0. + */ +public class SensorSoundLevel extends AbstractSensor { + + private static final long serialVersionUID = 6915432740389468093L; + + /** The channel ID in the inquiry response and in the SD header's channel list */ + public static final byte CHANNEL_ID = 0x38; + /** The raw count that means there is no level: 0x8000 as a signed 16-bit value */ + public static final int NO_LEVEL = -32768; + /** Counts per dB(A) */ + public static final double COUNTS_PER_DB = 100.0; + + /** Byte 233's audio mode and gain preset as read (bits 0-3, DEV-99), written back as they are */ + private int mAudioModeAndGain = 0; + + public class GuiLabelSensors{ + public static final String SOUND_LEVEL = "Sound Level"; + } + + public class LABEL_SENSOR_TILE{ + public static final String SOUND_LEVEL = GuiLabelSensors.SOUND_LEVEL; + } + + public static class ObjectClusterSensorName{ + public static final String SOUND_LEVEL = "Sound_Level"; + } + + public static class DatabaseChannelHandles{ + public static final String SOUND_LEVEL = "Sound_Level"; + } + + //--------- Sensor info start -------------- + /** No sensor bitmap bit: enabled through configuration byte 233 bit 4 */ + public static final SensorDetailsRef sensorSoundLevel = new SensorDetailsRef( + 0, + 0, + GuiLabelSensors.SOUND_LEVEL, + CompatibilityInfoForMaps.listOfCompatibleVersionInfoSoundLevel, + null, + Arrays.asList(ObjectClusterSensorName.SOUND_LEVEL)); + + public static final Map mSensorMapRef; + static { + Map aMap = new LinkedHashMap(); + aMap.put(Configuration.Shimmer3.SENSOR_ID.SHIMMER_SOUND_LEVEL, SensorSoundLevel.sensorSoundLevel); + mSensorMapRef = Collections.unmodifiableMap(aMap); + } + //--------- Sensor info end -------------- + + //--------- Channel info start -------------- + public static final ChannelDetails channelSoundLevel = new ChannelDetails( + ObjectClusterSensorName.SOUND_LEVEL, + ObjectClusterSensorName.SOUND_LEVEL, + DatabaseChannelHandles.SOUND_LEVEL, + CHANNEL_DATA_TYPE.INT16, 2, CHANNEL_DATA_ENDIAN.LSB, + CHANNEL_UNITS.DECIBELS_A, + Arrays.asList(CHANNEL_TYPE.CAL, CHANNEL_TYPE.UNCAL), + CHANNEL_ID); + + public static final Map mChannelMapRef; + static { + Map aMap = new LinkedHashMap(); + aMap.put(ObjectClusterSensorName.SOUND_LEVEL, SensorSoundLevel.channelSoundLevel); + mChannelMapRef = Collections.unmodifiableMap(aMap); + } + //--------- Channel info end -------------- + + public static final SensorGroupingDetails sensorGroupSoundLevel = new SensorGroupingDetails( + LABEL_SENSOR_TILE.SOUND_LEVEL, + Arrays.asList(Configuration.Shimmer3.SENSOR_ID.SHIMMER_SOUND_LEVEL), + CompatibilityInfoForMaps.listOfCompatibleVersionInfoSoundLevel); + + //--------- Constructors for this class start -------------- + public SensorSoundLevel(ShimmerDevice shimmerDevice) { + super(SENSORS.SOUND_LEVEL, shimmerDevice); + initialise(); + } + //--------- Constructors for this class end -------------- + + /** + * A raw sound level count in nominal dB(A) SPL, or NaN for the no-level + * value (0x8000) and for anything that is not a signed 16-bit count. + */ + public static double calibrateSoundLevel(double rawCount) { + if(Double.isNaN(rawCount) || rawCount<=NO_LEVEL || rawCount>Short.MAX_VALUE) { + return Double.NaN; + } + return rawCount/COUNTS_PER_DB; + } + + @Override + public void generateSensorMap() { + super.createLocalSensorMapWithCustomParser(mSensorMapRef, mChannelMapRef); + } + + @Override + public void generateConfigOptionsMap() { + // No configuration options: the channel is on or off + } + + @Override + public void generateSensorGroupMapping() { + mSensorGroupingMap = new LinkedHashMap(); + mSensorGroupingMap.put(Configuration.Shimmer3.LABEL_SENSOR_TILE.SOUND_LEVEL.ordinal(), sensorGroupSoundLevel); + super.updateSensorGroupingMap(); + } + + /** Used where a device parses through its sensor classes; the Shimmer3R stream and SD paths decode in ShimmerObject.buildMsg() */ + @Override + public ObjectCluster processDataCustom(SensorDetails sensorDetails, byte[] rawData, COMMUNICATION_TYPE commType, + ObjectCluster objectCluster, boolean isTimeSyncEnabled, double pcTimestampMs) { + objectCluster = sensorDetails.processDataCommon(rawData, commType, objectCluster, isTimeSyncEnabled, pcTimestampMs); + + if(mEnableCalibration){ + for(ChannelDetails channelDetails:sensorDetails.mListOfChannels){ + if(channelDetails.mObjectClusterName.equals(ObjectClusterSensorName.SOUND_LEVEL)){ + FormatCluster rawFormatCluster = (FormatCluster) ObjectCluster.returnFormatCluster( + objectCluster.getCollectionOfFormatClusters(channelDetails.mObjectClusterName), + channelDetails.mChannelFormatDerivedFromShimmerDataPacket.toString()); + if(rawFormatCluster!=null){ + objectCluster.addCalData(channelDetails, calibrateSoundLevel(rawFormatCluster.mData), objectCluster.getIndexKeeper()-1); + } + } + } + } + return objectCluster; + } + + @Override + public void checkShimmerConfigBeforeConfiguring() { + // Nothing depends on it + } + + private static ConfigByteLayoutShimmer3 getAudioConfigLayout(ShimmerDevice shimmerDevice, byte[] configBytes) { + ConfigByteLayout configByteLayout = shimmerDevice.getConfigByteLayout(); + if(configByteLayout instanceof ConfigByteLayoutShimmer3){ + ConfigByteLayoutShimmer3 configByteLayoutCast = (ConfigByteLayoutShimmer3) configByteLayout; + if(configByteLayoutCast.idxAudioConfig>0 && configBytes!=null && configBytes.length>configByteLayoutCast.idxAudioConfig){ + return configByteLayoutCast; + } + } + return null; + } + + /** Whether this firmware has the channel: LogAndStream_Shimmer3R v1.01.026 or later */ + private static boolean isChannelSupported(ShimmerDevice shimmerDevice) { + return shimmerDevice.isVerCompatibleWithAnyOf(CompatibilityInfoForMaps.listOfCompatibleVersionInfoSoundLevel); + } + + /** + * Byte 233: bits 0-3 as read (0 if the byte read was not valid) and bit 4 + * from this sensor's state, on firmware that has the channel; bits 5-7 are + * 0, as the firmware wants them. + */ + @Override + public void configBytesGenerate(ShimmerDevice shimmerDevice, byte[] configBytes, COMMUNICATION_TYPE commType) { + ConfigByteLayoutShimmer3 configByteLayoutCast = getAudioConfigLayout(shimmerDevice, configBytes); + if(configByteLayoutCast!=null){ + int value = mAudioModeAndGain & configByteLayoutCast.maskAudioModeAndGain; + if(isChannelSupported(shimmerDevice) && isSensorEnabled(Configuration.Shimmer3.SENSOR_ID.SHIMMER_SOUND_LEVEL)){ + value |= configByteLayoutCast.maskSoundLevel << configByteLayoutCast.bitShiftSoundLevel; + } + configBytes[configByteLayoutCast.idxAudioConfig] = (byte) value; + } + } + + /** + * Keeps byte 233's bits 0-3 for the next write and takes this sensor's + * state from bit 4, on firmware that has the channel; before v1.01.026 the + * sensor reads as off whatever bit 4 holds. Bit 7 set (erased flash reads + * 0xFF) makes the firmware read the whole byte as 0, and so does this. + */ + @Override + public void configBytesParse(ShimmerDevice shimmerDevice, byte[] configBytes, COMMUNICATION_TYPE commType) { + ConfigByteLayoutShimmer3 configByteLayoutCast = getAudioConfigLayout(shimmerDevice, configBytes); + if(configByteLayoutCast!=null){ + int value = configBytes[configByteLayoutCast.idxAudioConfig] & 0xFF; + if((value & configByteLayoutCast.maskAudioConfigInvalid)!=0){ + value = 0; + } + mAudioModeAndGain = value & configByteLayoutCast.maskAudioModeAndGain; + + SensorDetails sensorDetails = mSensorMap.get(Configuration.Shimmer3.SENSOR_ID.SHIMMER_SOUND_LEVEL); + if(sensorDetails!=null){ + sensorDetails.setIsEnabled(isChannelSupported(shimmerDevice) + && ((value >> configByteLayoutCast.bitShiftSoundLevel) & configByteLayoutCast.maskSoundLevel)!=0); + } + } + } + + /** + * The sensor bitmap has no bit for this sensor, so the bitmap-driven + * update would always turn it off. Its state comes from byte 233 instead. + */ + @Override + public boolean handleSpecCasesBeforeSensorMapUpdatePerSensor(ShimmerDevice shimmerDevice, Integer sensorId) { + return sensorId!=null && sensorId==Configuration.Shimmer3.SENSOR_ID.SHIMMER_SOUND_LEVEL; + } + + /** Byte 233's audio mode and gain preset (bits 0-3, DEV-99) as last read, which a configuration write gives back */ + public int getAudioModeAndGain() { + return mAudioModeAndGain; + } + + @Override + public Object setConfigValueUsingConfigLabel(Integer sensorId, String configLabel, Object valueToSet) { + return super.setConfigValueUsingConfigLabelCommon(sensorId, configLabel, valueToSet); + } + + @Override + public Object getConfigValueUsingConfigLabel(Integer sensorId, String configLabel) { + return super.getConfigValueUsingConfigLabelCommon(sensorId, configLabel); + } + + @Override + public void setSensorSamplingRate(double samplingRateHz) { + // One level per 128 ms block whatever the sampling rate + } + + @Override + public boolean setDefaultConfigForSensor(int sensorId, boolean isSensorEnabled) { + return mSensorMap.containsKey(sensorId); + } + + @Override + public boolean checkConfigOptionValues(String stringKey) { + return mConfigOptionsMap.containsKey(stringKey); + } + + @Override + public Object getSettings(String componentName, COMMUNICATION_TYPE commType) { + return null; + } + + @Override + public ActionSetting setSettings(String componentName, Object valueToSet, COMMUNICATION_TYPE commType) { + return new ActionSetting(commType); + } + + @Override + public boolean processResponse(int responseCommand, Object parsedResponse, COMMUNICATION_TYPE commType) { + return false; + } + + @Override + public LinkedHashMap generateConfigMap() { + return new LinkedHashMap(); + } + + @Override + public void parseConfigMap(LinkedHashMap mapOfConfigPerShimmer) { + // Nothing beyond the enabled state, which the sensor map carries + } + +} diff --git a/ShimmerDriver/src/test/java/com/shimmerresearch/driver/API_00040_SoundLevelChannelTest.java b/ShimmerDriver/src/test/java/com/shimmerresearch/driver/API_00040_SoundLevelChannelTest.java new file mode 100644 index 000000000..9e170d146 --- /dev/null +++ b/ShimmerDriver/src/test/java/com/shimmerresearch/driver/API_00040_SoundLevelChannelTest.java @@ -0,0 +1,357 @@ +package com.shimmerresearch.driver; + +import static org.junit.Assert.*; + +import java.util.Arrays; + +import org.junit.Test; + +import com.shimmerresearch.driver.Configuration.CHANNEL_UNITS; +import com.shimmerresearch.driver.Configuration.COMMUNICATION_TYPE; +import com.shimmerresearch.driver.shimmer2r3.ConfigByteLayoutShimmer3; +import com.shimmerresearch.driverUtilities.ChannelDetails.CHANNEL_TYPE; +import com.shimmerresearch.driverUtilities.ShimmerVerDetails.FW_ID; +import com.shimmerresearch.driverUtilities.ShimmerVerDetails.HW_ID; +import com.shimmerresearch.driverUtilities.ShimmerVerObject; +import com.shimmerresearch.driverUtilities.UtilParseData; +import com.shimmerresearch.sensors.AbstractSensor.SENSORS; +import com.shimmerresearch.sensors.soundlevel.SensorSoundLevel; + +/** + * DEV-1413: the Shimmer3R sound level channel, 0x38. Its decoding from a data + * packet, wherever the channel list puts it, and configuration byte 233, + * whose bit 4 turns it on. A full configuration write builds its image from + * zeros, so before DEV-1413 it wrote 0 to byte 233 and turned off a + * Shimmer3R's audio recording (DEV-99) as well; it now gives back bits 0-3 as + * read. A Shimmer3 has no microphone: it never sends 0x38 and its byte 233 + * stays 0. + *

+ * No hardware. + * + * @author Mark Nolan + */ +public class API_00040_SoundLevelChannelTest { + + private static final int IDX_AUDIO_CONFIG = 233; + + /** Shimmer3R LogAndStream v1.01.026, the first release with the channel: every Shimmer3R has the microphone */ + private static final ShimmerVerObject S3R = new ShimmerVerObject(HW_ID.SHIMMER_3R, FW_ID.LOGANDSTREAM, 1, 1, 26); + /** The release before it, which stores byte 233 untouched */ + private static final ShimmerVerObject S3R_BEFORE_CHANNEL = new ShimmerVerObject(HW_ID.SHIMMER_3R, FW_ID.LOGANDSTREAM, 1, 1, 25); + private static final ShimmerVerObject S3 = new ShimmerVerObject(HW_ID.SHIMMER_3, FW_ID.LOGANDSTREAM, 1, 0, 17); + + private static final byte LN_ACCEL_X = 0x00; + private static final byte LN_ACCEL_Y = 0x01; + private static final byte LN_ACCEL_Z = 0x02; + private static final byte VBATT = 0x03; + private static final byte GSR = 0x1C; + private static final byte SOUND_LEVEL = 0x38; + + /** The smallest concrete ShimmerObject: decoding and configuration, no connection */ + private static class TestShimmer extends ShimmerObject { + private static final long serialVersionUID = 1L; + + TestShimmer(ShimmerVerObject svo) { + setShimmerVersionObjectAndCreateSensorMap(svo); + setSamplingRateShimmer(128); + updateTimestampByteLength(); + } + + @Override + public void createConfigBytesLayout() { + mConfigByteLayout = new ConfigByteLayoutShimmer3(getFirmwareIdentifier(), getFirmwareVersionMajor(), getFirmwareVersionMinor(), getFirmwareVersionInternal(), mShimmerVerObject.mHardwareVersion); + } + + @Override + protected void checkBattery() { + } + + @Override + public ShimmerDevice deepClone() { + return null; + } + + @Override + protected void interpretDataPacketFormat(Object object, COMMUNICATION_TYPE commType) { + } + + @Override + protected void dataHandler(ObjectCluster ojc) { + } + + @Override + protected void processMsgFromCallback(ShimmerMsg shimmerMSG) { + } + + byte[] written() { + return generateConfigBytesForWritingToShimmer().clone(); + } + + boolean isSoundLevelEnabled() { + return isSensorEnabled(Configuration.Shimmer3.SENSOR_ID.SHIMMER_SOUND_LEVEL); + } + + void setSoundLevelEnabled(boolean state) { + assertTrue(setSensorEnabledState(Configuration.Shimmer3.SENSOR_ID.SHIMMER_SOUND_LEVEL, state)); + } + + /** Takes the channel list as an inquiry response or an SD header gives it */ + void channels(byte... channelIds) { + mNChannels = channelIds.length; + interpretDataPacketFormat(channelIds.length, channelIds); + } + } + + /** A configuration as a Shimmer on that firmware would answer it, with the given byte 233 */ + private static byte[] configFromShimmer(ShimmerVerObject svo, int audioConfig) { + byte[] config = new TestShimmer(svo).written(); + config[IDX_AUDIO_CONFIG] = (byte) audioConfig; + return config; + } + + private static TestShimmer readFrom(ShimmerVerObject svo, int audioConfig) { + TestShimmer shimmer = new TestShimmer(svo); + shimmer.configBytesParse(configFromShimmer(svo, audioConfig), COMMUNICATION_TYPE.BLUETOOTH); + return shimmer; + } + + private static int writtenAudioConfig(TestShimmer shimmer) { + return shimmer.written()[IDX_AUDIO_CONFIG] & 0xFF; + } + + private static String hex(int value) { + return String.format("0x%02X", value); + } + + /** A data packet as buildMsg gets it: the timestamp, then two bytes little-endian per channel */ + private static byte[] packet(TestShimmer shimmer, int... channelValues) { + byte[] packet = new byte[shimmer.mTimeStampPacketByteSize + 2*channelValues.length]; + packet[0] = 0x10; + int index = shimmer.mTimeStampPacketByteSize; + for(int value:channelValues){ + packet[index++] = (byte) (value & 0xFF); + packet[index++] = (byte) ((value >> 8) & 0xFF); + } + return packet; + } + + private static FormatCluster soundLevel(ObjectCluster objectCluster, CHANNEL_TYPE channelType) { + return objectCluster.getLastFormatCluster(SensorSoundLevel.ObjectClusterSensorName.SOUND_LEVEL, channelType.toString()); + } + + // ---------- Decoding ---------- + + /** Just before the ADS7028 block (GSR, then the battery last), as the firmware sends it */ + @Test + public void decodedAtTheSpecPosition() { + TestShimmer shimmer = new TestShimmer(S3R); + shimmer.channels(LN_ACCEL_X, LN_ACCEL_Y, LN_ACCEL_Z, SOUND_LEVEL, GSR, VBATT); + + assertEquals(shimmer.mTimeStampPacketByteSize + 12, shimmer.mPacketSize); + assertSame(SensorSoundLevel.ObjectClusterSensorName.SOUND_LEVEL, shimmer.mSignalNameArray[4]); + assertEquals("i16", shimmer.mSignalDataTypeArray[4]); + + byte[] packet = packet(shimmer, 0x0101, 0x0202, 0x0303, 6543, 0x0456, 0x0123); + ObjectCluster objectCluster = shimmer.buildMsg(packet, COMMUNICATION_TYPE.BLUETOOTH, false, 0); + + assertEquals(6543, soundLevel(objectCluster, CHANNEL_TYPE.UNCAL).mData, 0); + FormatCluster cal = soundLevel(objectCluster, CHANNEL_TYPE.CAL); + assertEquals(65.43, cal.mData, 1e-9); + assertEquals(CHANNEL_UNITS.DECIBELS_A, cal.mUnits); + + // The channels after it are still where the channel list says + long[] parsed = UtilParseData.parseData(packet, shimmer.mSignalDataTypeArray); + assertEquals(0x0303, parsed[3]); + assertEquals(6543, parsed[4]); + assertEquals(0x0456, parsed[5]); + assertEquals(0x0123, parsed[6]); + } + + /** The decoder goes by the channel list, not by a fixed position */ + @Test + public void decodedWhereverTheChannelListPutsIt() { + TestShimmer shimmer = new TestShimmer(S3R); + shimmer.channels(SOUND_LEVEL, VBATT); + ObjectCluster objectCluster = shimmer.buildMsg(packet(shimmer, 4012, 0x0123), COMMUNICATION_TYPE.BLUETOOTH, false, 0); + assertEquals(40.12, soundLevel(objectCluster, CHANNEL_TYPE.CAL).mData, 1e-9); + + shimmer = new TestShimmer(S3R); + shimmer.channels(VBATT, SOUND_LEVEL); + objectCluster = shimmer.buildMsg(packet(shimmer, 0x0123, 4012), COMMUNICATION_TYPE.BLUETOOTH, false, 0); + assertEquals(40.12, soundLevel(objectCluster, CHANNEL_TYPE.CAL).mData, 1e-9); + } + + /** 0x8000 says there is no level: NaN, never -327.68 */ + @Test + public void noLevelIsNaN() { + TestShimmer shimmer = new TestShimmer(S3R); + shimmer.channels(LN_ACCEL_X, SOUND_LEVEL, VBATT); + ObjectCluster objectCluster = shimmer.buildMsg(packet(shimmer, 0, 0x8000, 0x0123), COMMUNICATION_TYPE.BLUETOOTH, false, 0); + + assertEquals(SensorSoundLevel.NO_LEVEL, soundLevel(objectCluster, CHANNEL_TYPE.UNCAL).mData, 0); + assertTrue(Double.isNaN(soundLevel(objectCluster, CHANNEL_TYPE.CAL).mData)); + } + + /** Signed: a level below 0 dB(A) is a level, not a wrap */ + @Test + public void signedLittleEndian() { + TestShimmer shimmer = new TestShimmer(S3R); + shimmer.channels(SOUND_LEVEL); + ObjectCluster objectCluster = shimmer.buildMsg(packet(shimmer, 0xFF9C), COMMUNICATION_TYPE.BLUETOOTH, false, 0); + assertEquals(-100, soundLevel(objectCluster, CHANNEL_TYPE.UNCAL).mData, 0); + assertEquals(-1.0, soundLevel(objectCluster, CHANNEL_TYPE.CAL).mData, 1e-9); + + objectCluster = shimmer.buildMsg(packet(shimmer, 0x7FFF), COMMUNICATION_TYPE.BLUETOOTH, false, 0); + assertEquals(327.67, soundLevel(objectCluster, CHANNEL_TYPE.CAL).mData, 1e-9); + } + + /** Shimmer3R SD files carry the channel list in their header, decoded the same way */ + @Test + public void decodedFromAnSdFile() { + TestShimmer shimmer = new TestShimmer(S3R); + shimmer.channels(LN_ACCEL_X, SOUND_LEVEL, GSR, VBATT); + ObjectCluster objectCluster = shimmer.buildMsg(packet(shimmer, 0, 7210, 0x0456, 0x0123), COMMUNICATION_TYPE.SD, false, 0); + assertEquals(72.10, soundLevel(objectCluster, CHANNEL_TYPE.CAL).mData, 1e-9); + } + + @Test + public void calibration() { + assertEquals(65.43, SensorSoundLevel.calibrateSoundLevel(6543), 1e-12); + assertEquals(0.0, SensorSoundLevel.calibrateSoundLevel(0), 0); + assertEquals(-327.67, SensorSoundLevel.calibrateSoundLevel(-32767), 1e-12); + assertTrue(Double.isNaN(SensorSoundLevel.calibrateSoundLevel(-32768))); + assertTrue(Double.isNaN(SensorSoundLevel.calibrateSoundLevel(32768))); + assertTrue(Double.isNaN(SensorSoundLevel.calibrateSoundLevel(Double.NaN))); + } + + /** A Shimmer3 never sends 0x38: if one did, it would stay an unknown channel, as before */ + @Test + public void shimmer3KeepsTheUnknownChannelHandling() { + TestShimmer shimmer = new TestShimmer(S3); + shimmer.channels(LN_ACCEL_X, SOUND_LEVEL, VBATT); + assertEquals(Byte.toString(SOUND_LEVEL), shimmer.mSignalNameArray[2]); + assertEquals("u12", shimmer.mSignalDataTypeArray[2]); + + ObjectCluster objectCluster = shimmer.buildMsg(packet(shimmer, 0, 6543, 0x0123), COMMUNICATION_TYPE.BLUETOOTH, false, 0); + assertNull(soundLevel(objectCluster, CHANNEL_TYPE.CAL)); + assertNull(soundLevel(objectCluster, CHANNEL_TYPE.UNCAL)); + } + + // ---------- Configuration byte 233 ---------- + + @Test + public void onlyAShimmer3RHasTheSensor() { + TestShimmer s3r = new TestShimmer(S3R); + assertNotNull(s3r.getSensorClass(SENSORS.SOUND_LEVEL)); + assertNotNull(s3r.getSensorDetails(Configuration.Shimmer3.SENSOR_ID.SHIMMER_SOUND_LEVEL)); + assertFalse("off until the Shimmer says otherwise", s3r.isSoundLevelEnabled()); + + TestShimmer s3 = new TestShimmer(S3); + assertNull(s3.getSensorClass(SENSORS.SOUND_LEVEL)); + assertNull(s3.getSensorDetails(Configuration.Shimmer3.SENSOR_ID.SHIMMER_SOUND_LEVEL)); + } + + /** The tile is offered from v1.01.026, the first release with the channel, and not before */ + @Test + public void theTileIsOfferedFromV1_01_026() { + int tile = Configuration.Shimmer3.LABEL_SENSOR_TILE.SOUND_LEVEL.ordinal(); + assertTrue(new TestShimmer(S3R).getSensorGroupingMap().containsKey(tile)); + assertTrue(new TestShimmer(new ShimmerVerObject(HW_ID.SHIMMER_3R, FW_ID.LOGANDSTREAM, 1, 2, 0)).getSensorGroupingMap().containsKey(tile)); + assertFalse(new TestShimmer(S3R_BEFORE_CHANNEL).getSensorGroupingMap().containsKey(tile)); + } + + /** + * Before v1.01.026 the firmware stores byte 233 untouched, so a bit 4 read back + * there has no channel behind it: it reads as off, and the next write clears it, + * keeping the audio mode, so a later firmware update does not turn the channel on + */ + @Test + public void beforeTheChannelBit4ReadsOffAndIsClearedOnWrite() { + TestShimmer shimmer = readFrom(S3R_BEFORE_CHANNEL, 0x12); + assertFalse(shimmer.isSoundLevelEnabled()); + assertEquals("the audio mode kept, bit 4 cleared", 0x02, writtenAudioConfig(shimmer)); + } + + /** Before v1.01.026 enabling it writes nothing: the firmware would only store the bit */ + @Test + public void beforeTheChannelEnablingDoesNotSetBit4() { + TestShimmer shimmer = readFrom(S3R_BEFORE_CHANNEL, 0x01); + shimmer.setSensorEnabledState(Configuration.Shimmer3.SENSOR_ID.SHIMMER_SOUND_LEVEL, true); + assertEquals("the audio mode kept, bit 4 not set", 0x01, writtenAudioConfig(shimmer)); + } + + /** The DEV-99 audio mode went to 0 on every full write before DEV-1413 */ + @Test + public void theAudioModeReadGoesBackOnAFullWrite() { + for(int audioMode=0;audioMode<4;audioMode++){ + TestShimmer shimmer = readFrom(S3R, audioMode); + assertFalse(shimmer.isSoundLevelEnabled()); + assertEquals("mode " + audioMode, audioMode, writtenAudioConfig(shimmer)); + } + } + + /** + * Every value of the byte: bits 0-4 go back as read, bits 5-6 are cleared + * as the firmware clears them, and bit 7 set (erased flash) reads as 0 + */ + @Test + public void everyByteGoesBackAsTheFirmwareKeepsIt() { + for(int value=0;value<256;value++){ + TestShimmer shimmer = readFrom(S3R, value); + boolean valid = (value & 0x80)==0; + assertEquals(hex(value), valid && (value & 0x10)!=0, shimmer.isSoundLevelEnabled()); + assertEquals(hex(value), valid ? value & 0x1F : 0, writtenAudioConfig(shimmer)); + } + } + + /** Enabling the sensor sets byte 233 bit 4 and changes nothing else in the image */ + @Test + public void enablingSetsBit4AndNothingElse() { + TestShimmer shimmer = readFrom(S3R, 0x02); + byte[] off = shimmer.written(); + + shimmer.setSoundLevelEnabled(true); + byte[] on = shimmer.written(); + assertEquals(0x12, on[IDX_AUDIO_CONFIG] & 0xFF); + byte[] onWithByte233Off = on.clone(); + onWithByte233Off[IDX_AUDIO_CONFIG] = off[IDX_AUDIO_CONFIG]; + assertArrayEquals("no sensor bitmap bit, so no other byte moves", off, onWithByte233Off); + + shimmer.setSoundLevelEnabled(false); + assertTrue(Arrays.equals(off, shimmer.written())); + } + + /** A Shimmer3R never read writes 0, as the driver always did */ + @Test + public void unreadShimmer3RWritesZero() { + TestShimmer shimmer = new TestShimmer(S3R); + assertEquals(0, writtenAudioConfig(shimmer)); + shimmer.setSoundLevelEnabled(true); + assertEquals(0x10, writtenAudioConfig(shimmer)); + } + + /** The bitmap has no bit for it, so updating the sensor map from the bitmap must leave it alone */ + @Test + public void theSensorBitmapDoesNotTurnItOff() { + TestShimmer shimmer = readFrom(S3R, 0x11); + assertTrue(shimmer.isSoundLevelEnabled()); + long enabledSensors = shimmer.getEnabledSensors(); + + shimmer.setEnabledAndDerivedSensorsAndUpdateMaps(shimmer.getEnabledSensors(), shimmer.getDerivedSensors()); + assertTrue(shimmer.isSoundLevelEnabled()); + + shimmer.refreshEnabledSensorsFromSensorMap(); + assertEquals("adds no bitmap bit (0x4000 would read as ECG-to-HR on SD)", enabledSensors, shimmer.getEnabledSensors()); + assertEquals(0x11, writtenAudioConfig(shimmer)); + } + + /** A Shimmer3 has no microphone: byte 233 stays 0 whatever was read */ + @Test + public void shimmer3WritesZero() { + TestShimmer shimmer = readFrom(S3, 0x13); + assertFalse(shimmer.isSoundLevelEnabled()); + assertEquals(0, writtenAudioConfig(shimmer)); + assertFalse(shimmer.setSensorEnabledState(Configuration.Shimmer3.SENSOR_ID.SHIMMER_SOUND_LEVEL, true)); + assertEquals(0, writtenAudioConfig(shimmer)); + } +} diff --git a/ShimmerDriverPC/src/test/java/com/shimmerresearch/pcDriver/API_00041_SoundLevelShimmerPCTest.java b/ShimmerDriverPC/src/test/java/com/shimmerresearch/pcDriver/API_00041_SoundLevelShimmerPCTest.java new file mode 100644 index 000000000..9e6040c77 --- /dev/null +++ b/ShimmerDriverPC/src/test/java/com/shimmerresearch/pcDriver/API_00041_SoundLevelShimmerPCTest.java @@ -0,0 +1,88 @@ +package com.shimmerresearch.pcDriver; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.shimmerresearch.driver.Configuration; +import com.shimmerresearch.driver.Configuration.COMMUNICATION_TYPE; +import com.shimmerresearch.driver.ObjectCluster; +import com.shimmerresearch.driverUtilities.ChannelDetails.CHANNEL_TYPE; +import com.shimmerresearch.driverUtilities.ShimmerVerDetails.FW_ID; +import com.shimmerresearch.driverUtilities.ShimmerVerDetails.HW_ID; +import com.shimmerresearch.driverUtilities.ShimmerVerObject; +import com.shimmerresearch.sensors.soundlevel.SensorSoundLevel; + +/** + * DEV-1413: the Shimmer3R sound level channel through ShimmerPC, the class + * Consensys configures. Consensys edits a copy made by serialisation + * (deepClone()) and writes the configuration from it, so byte 233 as read has + * to survive the copy. API_00040 in ShimmerDriver covers the decoding and the + * byte itself. + *

+ * No hardware. + * + * @author Mark Nolan + */ +public class API_00041_SoundLevelShimmerPCTest { + + private static final int IDX_AUDIO_CONFIG = 233; + /** v1.01.026, the first release with the channel */ + private static final ShimmerVerObject S3R = new ShimmerVerObject(HW_ID.SHIMMER_3R, FW_ID.LOGANDSTREAM, 1, 1, 26); + + private static ShimmerPC shimmer() { + ShimmerPC shimmer = new ShimmerPC("COM99"); + shimmer.setShimmerVersionObjectAndCreateSensorMap(S3R); + return shimmer; + } + + private static ShimmerPC readFrom(int audioConfig) { + byte[] config = shimmer().generateConfigBytesForWritingToShimmer().clone(); + config[IDX_AUDIO_CONFIG] = (byte) audioConfig; + ShimmerPC shimmer = shimmer(); + shimmer.configBytesParse(config, COMMUNICATION_TYPE.BLUETOOTH); + return shimmer; + } + + private static int writtenAudioConfig(ShimmerPC shimmer) { + return shimmer.generateConfigBytesForWritingToShimmer()[IDX_AUDIO_CONFIG] & 0xFF; + } + + @Test + public void byte233SurvivesTheCopyConsensysWritesFrom() { + ShimmerPC shimmer = readFrom(0x13); + assertTrue(shimmer.isSensorEnabled(Configuration.Shimmer3.SENSOR_ID.SHIMMER_SOUND_LEVEL)); + + ShimmerPC clone = shimmer.deepClone(); + assertNotNull(clone); + assertTrue(clone.isSensorEnabled(Configuration.Shimmer3.SENSOR_ID.SHIMMER_SOUND_LEVEL)); + assertEquals(0x13, writtenAudioConfig(clone)); + + assertTrue(clone.setSensorEnabledState(Configuration.Shimmer3.SENSOR_ID.SHIMMER_SOUND_LEVEL, false)); + assertEquals("the audio mode stays", 0x03, writtenAudioConfig(clone)); + assertEquals("the original is untouched", 0x13, writtenAudioConfig(shimmer)); + } + + /** Consensys streams with the arrays data structure on */ + @Test + public void decodedWithTheArraysDataStructure() { + ShimmerPC shimmer = shimmer(); + shimmer.mNChannels = 2; + shimmer.interpretDataPacketFormat(2, new byte[] {SensorSoundLevel.CHANNEL_ID, 0x03}); + shimmer.enableArraysDataStructure(true); + + byte[] packet = new byte[shimmer.getPacketSize()]; + int iSoundLevel = packet.length - 4; + packet[iSoundLevel] = (byte) 0x8F; // 6543 = 0x198F + packet[iSoundLevel + 1] = (byte) 0x19; + ObjectCluster objectCluster = shimmer.buildMsg(packet, COMMUNICATION_TYPE.BLUETOOTH, false, 0); + assertEquals(65.43, objectCluster.getFormatClusterValue(SensorSoundLevel.ObjectClusterSensorName.SOUND_LEVEL, CHANNEL_TYPE.CAL.toString()), 1e-9); + assertEquals(6543, objectCluster.getFormatClusterValue(SensorSoundLevel.ObjectClusterSensorName.SOUND_LEVEL, CHANNEL_TYPE.UNCAL.toString()), 0); + + packet[iSoundLevel] = 0x00; // 0x8000, no level + packet[iSoundLevel + 1] = (byte) 0x80; + objectCluster = shimmer.buildMsg(packet, COMMUNICATION_TYPE.BLUETOOTH, false, 0); + assertTrue(Double.isNaN(objectCluster.getFormatClusterValue(SensorSoundLevel.ObjectClusterSensorName.SOUND_LEVEL, CHANNEL_TYPE.CAL.toString()))); + assertEquals(-32768, objectCluster.getFormatClusterValue(SensorSoundLevel.ObjectClusterSensorName.SOUND_LEVEL, CHANNEL_TYPE.UNCAL.toString()), 0); + } +}