diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java b/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java index 32c08f967..e0f241f43 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java @@ -146,6 +146,8 @@ public abstract class ShimmerBluetooth extends ShimmerObject implements Serializ protected long mSetEnabledSensors = SENSOR_ACCEL; // Only used during the initialization process, see initialize(); private int mNumberofTXRetriesCount=1; + private static final int AUDIO_STATUS_LENGTH = 32; + private static final byte AUDIO_STATE_RECORDING = 2; private final static int NUMBER_OF_TX_RETRIES_LIMIT = 0; private class DUMMY_READ_WAIT_TIME_MS { private final static int START = 50; @@ -416,6 +418,8 @@ public enum SHIMMER_FEATURE { aMap.put(GET_RWC_COMMAND, new BtCommandDetails(GET_RWC_COMMAND, "GET_RWC_COMMAND", RWC_RESPONSE)); aMap.put(GET_VBATT_COMMAND, new BtCommandDetails(GET_VBATT_COMMAND, "GET_VBATT_COMMAND", VBATT_RESPONSE)); aMap.put(GET_BT_FW_VERSION_STR_COMMAND, new BtCommandDetails(GET_BT_FW_VERSION_STR_COMMAND, "GET_BT_FW_VERSION_STR_COMMAND", BT_FW_VERSION_STR_RESPONSE)); + aMap.put(GET_AUDIO_STATUS_COMMAND, new BtCommandDetails(GET_AUDIO_STATUS_COMMAND, "GET_AUDIO_STATUS_COMMAND", AUDIO_STATUS_RESPONSE)); + aMap.put(GET_EVENT_CHANNEL_STATUS_COMMAND, new BtCommandDetails(GET_EVENT_CHANNEL_STATUS_COMMAND, "GET_EVENT_CHANNEL_STATUS_COMMAND", EVENT_CHANNEL_STATUS_RESPONSE)); mBtGetCommandMap = Collections.unmodifiableMap(aMap); @@ -538,6 +542,8 @@ public enum SHIMMER_FEATURE { aMap.put(RWC_RESPONSE, new BtCommandDetails(RWC_RESPONSE, "RWC_RESPONSE", 8)); aMap.put(VBATT_RESPONSE, new BtCommandDetails(VBATT_RESPONSE, "VBATT_RESPONSE", 3)); aMap.put(BT_FW_VERSION_STR_RESPONSE, new BtCommandDetails(BT_FW_VERSION_STR_RESPONSE, "BT_FW_VERSION_STR_RESPONSE", -1)); + aMap.put(AUDIO_STATUS_RESPONSE, new BtCommandDetails(AUDIO_STATUS_RESPONSE, "AUDIO_STATUS_RESPONSE", AUDIO_STATUS_LENGTH)); + aMap.put(EVENT_CHANNEL_STATUS_RESPONSE, new BtCommandDetails(EVENT_CHANNEL_STATUS_RESPONSE, "EVENT_CHANNEL_STATUS_RESPONSE", EventChannelStatus.LENGTH)); mBtResponseMap = Collections.unmodifiableMap(aMap); @@ -2382,6 +2388,14 @@ else if(responseCommand==BT_FW_VERSION_STR_RESPONSE) { } } } + + else if(responseCommand==AUDIO_STATUS_RESPONSE) { + byte[] responseData = readBytes(AUDIO_STATUS_LENGTH, responseCommand); + if(responseData!=null) { + parseAudioStatus(responseData); + } + } + else if(responseCommand==EVENT_CHANNEL_STATUS_RESPONSE) { byte[] responseData = readBytes(EventChannelStatus.LENGTH, responseCommand); @@ -3070,9 +3084,16 @@ protected void parseStatusByte(byte[] statusByte){ if(statusByte.length > 1) { setIsUsbPluggedIn(((statusByte[1] & (0x01 << 0)) > 0)? true:false); - - consolePrintLn("\nStatus Response = \n" + UtilShimmer.byteToHexStringFormatted(statusByte[1]) - + "\t" + "IsUsbPluggedIn = " + isUsbPluggedIn()); + // earlier firmware sends 0 in the audio bits + if(isSupportedAudioStatus()) { + setIsAudioRecording((statusByte[1] & (0x01 << 1)) > 0); + setIsAudioFault((statusByte[1] & (0x01 << 2)) > 0); + } + + consolePrintLn("\nStatus Response = \n" + UtilShimmer.byteToHexStringFormatted(statusByte[1]) + + "\t" + "IsUsbPluggedIn = " + isUsbPluggedIn() + + "\t" + "IsAudioRecording = " + isAudioRecording() + + "\t" + "IsAudioFault = " + isAudioFault()); } @@ -3096,6 +3117,11 @@ public boolean isSupportedSdInfoInStatus() { public boolean isSupportedUSBPluggedInStatus() { return isThisVerCompatibleWith(HW_ID.SHIMMER_3R, FW_ID.LOGANDSTREAM, 1,0, 24); } + + /** Status byte 1 bits 1-2, audio recording and audio fault: LogAndStream_Shimmer3R v1.01.026 or later */ + public boolean isSupportedAudioStatus() { + return isVerCompatibleWithAnyOf(Configuration.Shimmer3.CompatibilityInfoForMaps.listOfCompatibleVersionInfoSoundLevel); + } protected boolean isSupportedInStreamCmds() { if((getFirmwareIdentifier()==FW_ID.LOGANDSTREAM @@ -3327,6 +3353,10 @@ private void initializeShimmer3or3R(int hardwareVersion){ readBtFwVersion(); } + if(isAudioStatusToAsk()) { + readAudioStatus(); + } + //TODO this could actually be causing a lot of misunderstanding and a disconnect between what the user thinks is set and what is actually set. // Only read calibration dump over bluetooth if the Shimmer3 is not // docked as the Shimmer3 won't have access to the SD card. The Shimmer3R does. @@ -3622,6 +3652,19 @@ else if(mCurrentCommand==STOP_LOGGING_ONLY_COMMAND){ } + //firmware without audio leaves it unanswered, which says the unit records none + if(mCurrentCommand==GET_AUDIO_STATUS_COMMAND){ + setIsAudioStatusAnswered(false); + mWaitForAck=false; + mWaitForResponse=false; + + removeInstruction(0); + mTransactionCompleted = true; + setInstructionStackLock(false); + + return; + } + if(mCurrentCommand==GET_FW_VERSION_COMMAND){ setShimmerVersionObjectAndCreateSensorMap(new ShimmerVerObject(HW_ID.SHIMMER_2R, FW_ID.BOILER_PLATE, 0, 1, 0)); @@ -4140,6 +4183,22 @@ public void readStatusLogAndStream(){ } } + /** v1.01.025 development builds may record audio; later releases do by version, so are not asked */ + public boolean isAudioStatusToAsk() { + return isVerCompatibleWithAnyOf(Configuration.Shimmer3.configOptionAudioMode.mListOfCompatibleVersionInfo) && !isSupportedAudioStatus(); + } + + public void readAudioStatus() { + writeInstruction(GET_AUDIO_STATUS_COMMAND); + } + + /** AUDIO_STATUS_RESPONSE (log-and-stream-common SHIMMER3R_AUDIO.md §5): byte 2 the state, 2 recording; byte 3 the fault, 0 none */ + protected void parseAudioStatus(byte[] audioStatus) { + setIsAudioStatusAnswered(true); + setIsAudioRecording(audioStatus[2] == AUDIO_STATE_RECORDING); + setIsAudioFault(audioStatus[3] != 0); + } + public void readBtFwVersion() { if (getShimmerVerObject().isSupportedBtFwVerRequest()) { writeInstruction(GET_BT_FW_VERSION_STR_COMMAND); @@ -5343,8 +5402,8 @@ public void writeConfigBytes(boolean readBackAfter){ if(!isButtonLogModeKnown()){ skipIndexes.add(configByteLayoutCast.idxButtonLogMode); } - // Byte 233 (DEV-99, DEV-1413) on a Shimmer3R, not read - // and the sound level not turned on + // Byte 233 (DEV-99, DEV-1413) on a Shimmer3R, neither read + // nor set (the sound level or the audio mode) if(configByteLayoutCast.idxAudioConfig>0 && !isAudioConfigKnown()){ skipIndexes.add(configByteLayoutCast.idxAudioConfig); } diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/driver/Configuration.java b/ShimmerDriver/src/main/java/com/shimmerresearch/driver/Configuration.java index a09bb6b68..6c412e5c6 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/driver/Configuration.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/driver/Configuration.java @@ -907,6 +907,7 @@ public class GuiLabelConfig{ public static final String ENABLE_ERROR_LEDS_RTC = "RTC Error LEDs"; public static final String ENABLE_ERROR_LEDS_SD = "SD Error LEDs"; public static final String LOW_POWER_AUTOSTOP = "Low-power Autostop"; + public static final String AUDIO_MODE = "Audio Mode"; /** Configuration byte 232 bits 0-2 (DEV-971), an Integer, see ButtonLogMode.BUTTON_MODE_* */ public static final String BUTTON_MODE = "Button Mode"; /** Configuration byte 232 bits 3-4 (DEV-971), an Integer, see ButtonLogMode.LOG_TRIGGERS_* */ @@ -2023,6 +2024,21 @@ public static class CompatibilityInfoForMaps{ ConfigOptionDetailsSensor.GUI_COMPONENT_TYPE.CHECKBOX, Arrays.asList(new ShimmerVerObject(FW_ID.LOGANDSTREAM, 0, 9, 6), new ShimmerVerObject(FW_ID.SDLOG, 0, 17, 3))); + + /** What a Shimmer3R records from its microphone while logging to SD (DEV-99, config byte 233). */ + public static final int AUDIO_MODE_OFF = 0; + public static final int AUDIO_MODE_FINGERPRINT = 1; + public static final int AUDIO_MODE_FINGERPRINT_AND_RAW = 2; + public static final int AUDIO_MODE_RAW = 3; + public static final String[] ListofAudioMode = {"Off", "Fingerprint only", "Fingerprint and raw audio", "Raw audio only"}; + public static final Integer[] ListofAudioModeConfigValues = {AUDIO_MODE_OFF, AUDIO_MODE_FINGERPRINT, AUDIO_MODE_FINGERPRINT_AND_RAW, AUDIO_MODE_RAW}; + public static final ConfigOptionDetailsSensor configOptionAudioMode = new ConfigOptionDetailsSensor( + Configuration.Shimmer3.GuiLabelConfig.AUDIO_MODE, + ShimmerDevice.DatabaseConfigHandle.AUDIO_MODE, + ListofAudioMode, + ListofAudioModeConfigValues, + ConfigOptionDetailsSensor.GUI_COMPONENT_TYPE.COMBOBOX, + Arrays.asList(new ShimmerVerObject(HW_ID.SHIMMER_3R, FW_ID.LOGANDSTREAM, 1, 1, 25))); public static final Map mConfigOptionsMapRef; static { @@ -2296,6 +2312,7 @@ public static class CompatibilityInfoForMaps{ CompatibilityInfoForMaps.listOfCompatibleVersionInfoAnyExpBoardAndFw)); aMap.put(Configuration.Shimmer3.GuiLabelConfig.LOW_POWER_AUTOSTOP, configOptionLowPowerAutoStop); + aMap.put(Configuration.Shimmer3.GuiLabelConfig.AUDIO_MODE, configOptionAudioMode); mConfigOptionsMapRef3r = Collections.unmodifiableMap(aMap); } diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/driver/ShimmerDevice.java b/ShimmerDriver/src/main/java/com/shimmerresearch/driver/ShimmerDevice.java index e606e38ac..2c75eed07 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/driver/ShimmerDevice.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/driver/ShimmerDevice.java @@ -150,6 +150,11 @@ public abstract class ShimmerDevice extends BasicProcessWithCallBack implements protected boolean mIsInitialised = false; private boolean mIsDocked = false; private boolean mIsUsbPluggedIn = false; + /** Shimmer3R microphone, from the status: audio captured for the current log, and this session's audio failed or degraded */ + private boolean mIsAudioRecording = false; + private boolean mIsAudioFault = false; + /** A v1.01.025 unit's answer to GET_AUDIO_STATUS: null until asked */ + private Boolean mIsAudioStatusAnswered = null; private boolean mIsDockCommsSuccessful = false; public boolean mIsUsbDfuMode= false; protected boolean mHaveAttemptedToReadConfig = false; @@ -324,6 +329,7 @@ public static final class DatabaseConfigHandle{ public static final String TRIAL_ID = "Trial_Id"; public static final String N_SHIMMER = "NShimmer"; public static final String LOW_POWER_AUTOSTOP = "Low_Power_Autostop"; + public static final String AUDIO_MODE = "Audio_Mode"; public static final String SHIMMER_VERSION = "Shimmer_Version"; public static final String FW_VERSION = "FW_ID"; @@ -1081,6 +1087,38 @@ public boolean isDocked() { public boolean isUsbPluggedIn() { return mIsUsbPluggedIn; } + + public void setIsAudioRecording(boolean isAudioRecording) { + mIsAudioRecording = isAudioRecording; + } + + public boolean isAudioRecording() { + return mIsAudioRecording; + } + + public void setIsAudioFault(boolean isAudioFault) { + mIsAudioFault = isAudioFault; + } + + public boolean isAudioFault() { + return mIsAudioFault; + } + + public void setIsAudioStatusAnswered(boolean isAnswered) { + mIsAudioStatusAnswered = isAnswered; + } + + /** + * Whether this unit records audio: LogAndStream_Shimmer3R v1.01.026 or later by its version; a v1.01.025 + * development build only once it has answered GET_AUDIO_STATUS (SHIMMER3R_AUDIO.md §5). + */ + public boolean isAudioSupported() { + if (isVerCompatibleWithAnyOf(Configuration.Shimmer3.CompatibilityInfoForMaps.listOfCompatibleVersionInfoSoundLevel)) { + return true; + } + return isVerCompatibleWithAnyOf(Configuration.Shimmer3.configOptionAudioMode.mListOfCompatibleVersionInfo) + && Boolean.TRUE.equals(mIsAudioStatusAnswered); + } public boolean isUsbDfuMode(){ return mIsUsbDfuMode; diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/driver/ShimmerObject.java b/ShimmerDriver/src/main/java/com/shimmerresearch/driver/ShimmerObject.java index dcf10063e..8d450164b 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/driver/ShimmerObject.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/driver/ShimmerObject.java @@ -511,6 +511,10 @@ public static final class PRESSURE_SENSOR_ID { public static final byte EVENT_CHANNEL_STATUS_RESPONSE = (byte) 0xBA; /** An event channel record, [0x8A][0xBB][len][record][CRC-16], sent only to a host that opted in (DEV-971) */ public static final byte EVENT_RECORD_RESPONSE = (byte) 0xBB; + + /** Shimmer3R audio (DEV-99): firmware without audio leaves it unanswered */ + public static final byte GET_AUDIO_STATUS_COMMAND = (byte) 0xBC; + public static final byte AUDIO_STATUS_RESPONSE = (byte) 0xBD; public static final int MAX_NUMBER_OF_SIGNALS = 77;//50; //used to be 11 but now 13 because of the SR30 + 8 for 3d orientation public static final int MAX_INQUIRY_PACKET_SIZE = 47; @@ -582,6 +586,9 @@ public String toString() { private boolean isOverrideShowErrorLedsSd = false; private int mShowErrorLedsSd = 0; private boolean mLowBattAutoStop = false; + private int mAudioMode = Configuration.Shimmer3.AUDIO_MODE_OFF; + /** Only internal apps may switch raw audio on; any other app writes it off. */ + private static boolean sIsRawAudioAllowed = false; /** Configuration byte 232 as read from the Shimmer or set by the host (DEV-971), see {@link ButtonLogMode} */ private int mButtonLogMode = 0; @@ -5033,6 +5040,7 @@ public void setDefaultShimmerConfiguration() { setSingleTouch(false); setTCXO(false); setLowBattAutoStop(false); + setAudioMode(Configuration.Shimmer3.AUDIO_MODE_OFF); setPacketSize(0); mConfigByte0=0; @@ -6276,6 +6284,34 @@ private boolean isLowBattAutoStop() { return mLowBattAutoStop; } + public void setAudioMode(int audioMode) { + mAudioMode = audioMode; + } + + public int getAudioMode() { + return mAudioMode; + } + + public static boolean isRawAudioMode(int audioMode) { + return audioMode == Configuration.Shimmer3.AUDIO_MODE_FINGERPRINT_AND_RAW || audioMode == Configuration.Shimmer3.AUDIO_MODE_RAW; + } + + /** What a config write saves: a raw audio mode drops to Fingerprint only or Off unless raw audio is allowed. */ + public int getAudioModeToWrite() { + if (sIsRawAudioAllowed || !isRawAudioMode(mAudioMode)) { + return mAudioMode; + } + return mAudioMode == Configuration.Shimmer3.AUDIO_MODE_FINGERPRINT_AND_RAW ? Configuration.Shimmer3.AUDIO_MODE_FINGERPRINT : Configuration.Shimmer3.AUDIO_MODE_OFF; + } + + public static void setRawAudioAllowed(boolean isAllowed) { + sIsRawAudioAllowed = isAllowed; + } + + public static boolean isRawAudioAllowed() { + return sIsRawAudioAllowed; + } + /** * @return the mTrialNumberOfShimmers */ @@ -6776,12 +6812,12 @@ public boolean isButtonLogModeKnown() { /** * Whether the driver has the Shimmer3R's own byte 233, the microphone's - * configuration ({@link SensorSoundLevel#isAudioConfigKnown()}). True - * where there is no such byte. + * configuration ({@link SensorSoundLevel#isAudioConfigKnown(ShimmerDevice)}). + * True where there is no such byte. */ public boolean isAudioConfigKnown() { AbstractSensor abstractSensor = getSensorClass(AbstractSensor.SENSORS.SOUND_LEVEL); - return !(abstractSensor instanceof SensorSoundLevel) || ((SensorSoundLevel) abstractSensor).isAudioConfigKnown(); + return !(abstractSensor instanceof SensorSoundLevel) || ((SensorSoundLevel) abstractSensor).isAudioConfigKnown(this); } /** @@ -9970,6 +10006,9 @@ public Object getConfigValueUsingConfigLabel(String identifier, String configLab case(Configuration.Shimmer3.GuiLabelConfig.LOW_POWER_AUTOSTOP): returnValue = isLowBattAutoStop(); break; + case(Configuration.Shimmer3.GuiLabelConfig.AUDIO_MODE): + returnValue = getAudioMode(); + break; case(Configuration.Shimmer3.GuiLabelConfig.BLUETOOTH_BAUD_RATE): returnValue = getBluetoothBaudRate(); @@ -10147,6 +10186,9 @@ public Object setConfigValueUsingConfigLabel(String identifier, String configLab case(Configuration.Shimmer3.GuiLabelConfig.LOW_POWER_AUTOSTOP): setLowBattAutoStop((boolean)valueToSet); break; + case(Configuration.Shimmer3.GuiLabelConfig.AUDIO_MODE): + setAudioMode(isVerCompatibleWithAnyOf(Configuration.Shimmer3.configOptionAudioMode.mListOfCompatibleVersionInfo) ? (int)valueToSet : Configuration.Shimmer3.AUDIO_MODE_OFF); + break; case(Configuration.Shimmer3.GuiLabelConfig.BLUETOOTH_BAUD_RATE): setBluetoothBaudRate((int)valueToSet); @@ -10395,6 +10437,7 @@ public LinkedHashMap generateConfigMap(COMMUNICATION_TYPE commTy configMapForDb.put(DatabaseConfigHandleShimmerObject.TRIAL_DURATION_MAXIMUM, (double) getTrialDurationMaximumInSecs()); configMapForDb.put(ShimmerDevice.DatabaseConfigHandle.LOW_POWER_AUTOSTOP, (double) (isLowBattAutoStop()? 1:0)); + configMapForDb.put(ShimmerDevice.DatabaseConfigHandle.AUDIO_MODE, (double) getAudioMode()); //Now handled in the algorithm class in super /** @@ -10444,6 +10487,9 @@ public void parseConfigMap(ShimmerVerObject svo, LinkedHashMap m if(mapOfConfigPerShimmer.containsKey(DatabaseConfigHandle.LOW_POWER_AUTOSTOP)){ setLowBattAutoStop(((Double) mapOfConfigPerShimmer.get(DatabaseConfigHandle.LOW_POWER_AUTOSTOP))>0? true:false); } + if(mapOfConfigPerShimmer.containsKey(DatabaseConfigHandle.AUDIO_MODE)){ + setAudioMode(((Double) mapOfConfigPerShimmer.get(DatabaseConfigHandle.AUDIO_MODE)).intValue()); + } //EXG Configuration byte[] exg1Bytes = SensorEXG.parseExgConfigFromDb(mapOfConfigPerShimmer, EXG_CHIP_INDEX.CHIP1, 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 0e0cb2f2c..57c76ecfd 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/driver/shimmer2r3/ConfigByteLayoutShimmer3.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/driver/shimmer2r3/ConfigByteLayoutShimmer3.java @@ -177,6 +177,7 @@ public class ConfigByteLayoutShimmer3 extends ConfigByteLayout implements Serial //as 0 when bit 7 is set, as it is in erased flash (0xFF). public int idxAudioConfig = 0; public int maskAudioModeAndGain = 0x0F; + public int maskAudioMode = 0x03; public int bitShiftSoundLevel = 4; public int maskSoundLevel = 0x01; public int maskAudioConfigInvalid = 0x80; diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/sensors/soundlevel/SensorSoundLevel.java b/ShimmerDriver/src/main/java/com/shimmerresearch/sensors/soundlevel/SensorSoundLevel.java index 092877676..7dbb508b8 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/sensors/soundlevel/SensorSoundLevel.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/sensors/soundlevel/SensorSoundLevel.java @@ -13,6 +13,7 @@ import com.shimmerresearch.driver.FormatCluster; import com.shimmerresearch.driver.ObjectCluster; import com.shimmerresearch.driver.ShimmerDevice; +import com.shimmerresearch.driver.ShimmerObject; import com.shimmerresearch.driver.shimmer2r3.ConfigByteLayoutShimmer3; import com.shimmerresearch.driverUtilities.ChannelDetails; import com.shimmerresearch.driverUtilities.ChannelDetails.CHANNEL_DATA_ENDIAN; @@ -206,6 +207,12 @@ private static ConfigByteLayoutShimmer3 getAudioConfigLayout(ShimmerDevice shimm return null; } + /** Whether this firmware records audio (DEV-99): LogAndStream_Shimmer3R v1.01.025 or later */ + private static boolean isAudioModeSupported(ShimmerDevice shimmerDevice) { + return shimmerDevice instanceof ShimmerObject + && shimmerDevice.isVerCompatibleWithAnyOf(Configuration.Shimmer3.configOptionAudioMode.mListOfCompatibleVersionInfo); + } + /** Whether this firmware has the channel: LogAndStream_Shimmer3R v1.01.026 or later */ private static boolean isChannelSupported(ShimmerDevice shimmerDevice) { return shimmerDevice.isVerCompatibleWithAnyOf(CompatibilityInfoForMaps.listOfCompatibleVersionInfoSoundLevel); @@ -221,6 +228,10 @@ public void configBytesGenerate(ShimmerDevice shimmerDevice, byte[] configBytes, ConfigByteLayoutShimmer3 configByteLayoutCast = getAudioConfigLayout(shimmerDevice, configBytes); if(configByteLayoutCast!=null){ int value = mAudioModeAndGain & configByteLayoutCast.maskAudioModeAndGain; + // a mode changed here goes through the raw audio rule; one set elsewhere goes back as read + if(isAudioModeSupported(shimmerDevice) && ((ShimmerObject) shimmerDevice).getAudioMode()!=(value & configByteLayoutCast.maskAudioMode)){ + value = (value & ~configByteLayoutCast.maskAudioMode) | (((ShimmerObject) shimmerDevice).getAudioModeToWrite() & configByteLayoutCast.maskAudioMode); + } if(isChannelSupported(shimmerDevice) && isSensorEnabled(Configuration.Shimmer3.SENSOR_ID.SHIMMER_SOUND_LEVEL)){ value |= configByteLayoutCast.maskSoundLevel << configByteLayoutCast.bitShiftSoundLevel; } @@ -243,6 +254,9 @@ public void configBytesParse(ShimmerDevice shimmerDevice, byte[] configBytes, CO value = 0; } mAudioModeAndGain = value & configByteLayoutCast.maskAudioModeAndGain; + if(isAudioModeSupported(shimmerDevice)){ + ((ShimmerObject) shimmerDevice).setAudioMode(mAudioModeAndGain & configByteLayoutCast.maskAudioMode); + } mIsAudioConfigRead = true; SensorDetails sensorDetails = mSensorMap.get(Configuration.Shimmer3.SENSOR_ID.SHIMMER_SOUND_LEVEL); @@ -269,14 +283,22 @@ public int getAudioModeAndGain() { /** * Whether a configuration write has a byte 233 to give: it was read from - * the Shimmer, or the sound level is on, the one part of it set here. - * Before a read, bits 0-3 are not known, and writing them as 0 would turn - * the Shimmer's audio mode off. With the sound level turned on before a - * read they are written as 0 all the same, since one bit cannot be written - * on its own. + * the Shimmer, or a part of it was set here, the sound level turned on or + * the audio mode (DEV-1459) changed from the one read. Before a read, bits + * 0-3 are not known, and writing them as 0 would turn the Shimmer's audio + * mode off. With the sound level or an audio mode set before a read, the + * rest of bits 0-3 are written as 0 all the same, since one bit cannot be + * written on its own. An audio mode set back to Off before a read looks + * like one never set, so the Shimmer keeps its own. */ - public boolean isAudioConfigKnown() { - return mIsAudioConfigRead || isSensorEnabled(Configuration.Shimmer3.SENSOR_ID.SHIMMER_SOUND_LEVEL); + public boolean isAudioConfigKnown(ShimmerDevice shimmerDevice) { + if(mIsAudioConfigRead || isSensorEnabled(Configuration.Shimmer3.SENSOR_ID.SHIMMER_SOUND_LEVEL)){ + return true; + } + // As configBytesGenerate() decides whether to write the mode set here + ConfigByteLayout configByteLayout = shimmerDevice.getConfigByteLayout(); + return isAudioModeSupported(shimmerDevice) && configByteLayout instanceof ConfigByteLayoutShimmer3 + && ((ShimmerObject) shimmerDevice).getAudioMode()!=(mAudioModeAndGain & ((ConfigByteLayoutShimmer3) configByteLayout).maskAudioMode); } @Override diff --git a/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00047_AudioConfigWriteTest.java b/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00047_AudioConfigWriteTest.java index caa774473..c8f2e6a89 100644 --- a/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00047_AudioConfigWriteTest.java +++ b/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00047_AudioConfigWriteTest.java @@ -19,11 +19,12 @@ /** * Configuration byte 233 on a Shimmer3R (DEV-99 audio mode and gain in bits * 0-3, DEV-1413 sound level in bit 4) through a Bluetooth configuration write. - * The host can set bit 4 but never bits 0-3, which it only gives back as read. - * So before a read, the write leaves the byte out, as it does byte 232 - * (API_00046), and the Shimmer keeps its own. The one exception is the sound - * level turned on before a read: the byte is then written with bits 0-3 at 0, - * since a SET_INFOMEM cannot write one bit. + * The host can set bit 4 and the audio mode (bits 0-1, DEV-1459), but not the + * gain preset (bits 2-3), which it only gives back as read. So before a read, + * the write leaves the byte out, as it does byte 232 (API_00046), and the + * Shimmer keeps its own. The exceptions are the sound level turned on, or an + * audio mode set, before a read: the byte is then written with the rest of + * bits 0-3 at 0, since a SET_INFOMEM cannot write one bit. *

* No hardware. It lives in this package because the instruction list is a * protected member of ShimmerBluetooth. @@ -147,4 +148,59 @@ public void byte232SetBeforeAReadIsWrittenWithout233() { assertEquals(0x01, writtenAt(shimmer, IDX_BUTTON_LOG_MODE)); assertEquals(-1, writtenAt(shimmer, IDX_AUDIO_CONFIG)); } + + /** DEV-1459: the audio mode, bits 0-1, can be set here too */ + @Test + public void anAudioModeSetBeforeAReadIsWritten() { + ShimmerPC shimmer = shimmer(); + shimmer.setConfigValueUsingConfigLabel(Configuration.Shimmer3.GuiLabelConfig.AUDIO_MODE, Configuration.Shimmer3.AUDIO_MODE_FINGERPRINT); + shimmer.writeConfigBytes(false); + assertEquals("the mode set, with the rest of the byte at 0", Configuration.Shimmer3.AUDIO_MODE_FINGERPRINT, writtenAt(shimmer, IDX_AUDIO_CONFIG)); + } + + @Test + public void anAudioModeLeftAsItIsBeforeAReadIsLeftOut() { + ShimmerPC shimmer = shimmer(); + shimmer.setConfigValueUsingConfigLabel(Configuration.Shimmer3.GuiLabelConfig.AUDIO_MODE, Configuration.Shimmer3.AUDIO_MODE_OFF); + shimmer.writeConfigBytes(false); + assertEquals("Off is the default, so it says nothing about the Shimmer's own", -1, writtenAt(shimmer, IDX_AUDIO_CONFIG)); + } + + /** + * A raw audio mode is written only by an app allowed to (ShimmerObject.setRawAudioAllowed). + * Otherwise it drops to Fingerprint only or Off, before a read as after one. So Raw audio + * only, set before a read, writes Off over the Shimmer's own mode. + */ + @Test + public void aRawModeSetBeforeAReadIsWrittenWithoutTheRawAudio() { + boolean wasAllowed = ShimmerObject.isRawAudioAllowed(); + ShimmerObject.setRawAudioAllowed(false); + try { + ShimmerPC shimmer = shimmer(); + shimmer.setConfigValueUsingConfigLabel(Configuration.Shimmer3.GuiLabelConfig.AUDIO_MODE, Configuration.Shimmer3.AUDIO_MODE_FINGERPRINT_AND_RAW); + shimmer.writeConfigBytes(false); + assertEquals("Fingerprint and raw audio drops to Fingerprint only", Configuration.Shimmer3.AUDIO_MODE_FINGERPRINT, writtenAt(shimmer, IDX_AUDIO_CONFIG)); + + shimmer = shimmer(); + shimmer.setConfigValueUsingConfigLabel(Configuration.Shimmer3.GuiLabelConfig.AUDIO_MODE, Configuration.Shimmer3.AUDIO_MODE_RAW); + shimmer.writeConfigBytes(false); + assertEquals("Raw audio only drops to Off, and the byte is still written", Configuration.Shimmer3.AUDIO_MODE_OFF, writtenAt(shimmer, IDX_AUDIO_CONFIG)); + } finally { + ShimmerObject.setRawAudioAllowed(wasAllowed); + } + } + + @Test + public void aRawModeSetBeforeAReadIsWrittenWhereRawAudioIsAllowed() { + boolean wasAllowed = ShimmerObject.isRawAudioAllowed(); + ShimmerObject.setRawAudioAllowed(true); + try { + ShimmerPC shimmer = shimmer(); + shimmer.setConfigValueUsingConfigLabel(Configuration.Shimmer3.GuiLabelConfig.AUDIO_MODE, Configuration.Shimmer3.AUDIO_MODE_RAW); + shimmer.writeConfigBytes(false); + assertEquals(Configuration.Shimmer3.AUDIO_MODE_RAW, writtenAt(shimmer, IDX_AUDIO_CONFIG)); + } finally { + ShimmerObject.setRawAudioAllowed(wasAllowed); + } + } } diff --git a/ShimmerDriverPC/src/test/java/com/shimmerresearch/driver/API_00029_Shimmer3rAudioModeConfigByteTest.java b/ShimmerDriverPC/src/test/java/com/shimmerresearch/driver/API_00029_Shimmer3rAudioModeConfigByteTest.java new file mode 100644 index 000000000..e148af1f7 --- /dev/null +++ b/ShimmerDriverPC/src/test/java/com/shimmerresearch/driver/API_00029_Shimmer3rAudioModeConfigByteTest.java @@ -0,0 +1,80 @@ +package com.shimmerresearch.driver; + +import static org.junit.Assert.assertEquals; + +import org.junit.After; +import org.junit.Test; + +import com.shimmerresearch.driver.Configuration.COMMUNICATION_TYPE; +import com.shimmerresearch.driverUtilities.ShimmerVerDetails.FW_ID; +import com.shimmerresearch.driverUtilities.ShimmerVerDetails.HW_ID; +import com.shimmerresearch.driverUtilities.ShimmerVerObject; +import com.shimmerresearch.pcDriver.ShimmerPC; + +/** + * DEV-99: the Shimmer3R audio mode in config byte 233 (log-and-stream-common SHIMMER3R_AUDIO.md §1). + * Bits 0-1 are the mode, bits 2-3 a gain preset (only 0 so far) and bit 7 must be clear; the firmware + * runs anything else as off, so the driver reads it as off too. Raw audio is written only by an app + * allowed to record it, otherwise the fingerprint is kept and the raw audio dropped. + */ +public class API_00029_Shimmer3rAudioModeConfigByteTest { + + private static final int IDX_AUDIO_MODE = 233; + + private static ShimmerPC device(int fwInternal) { + ShimmerPC device = new ShimmerPC("COM99"); + device.setShimmerVersionObjectAndCreateSensorMap(new ShimmerVerObject(HW_ID.SHIMMER_3R, FW_ID.LOGANDSTREAM, 1, 1, fwInternal)); + return device; + } + + private static int parse(int audioModeByte) { + ShimmerPC device = device(25); + byte[] configBytes = device.configBytesGenerate(true, COMMUNICATION_TYPE.SD); + configBytes[IDX_AUDIO_MODE] = (byte) audioModeByte; + device.configBytesParse(configBytes, COMMUNICATION_TYPE.SD); + return device.getAudioMode(); + } + + private static int written(int audioMode) { + ShimmerPC device = device(25); + device.setConfigValueUsingConfigLabel(Configuration.Shimmer3.GuiLabelConfig.AUDIO_MODE, audioMode); + return device.configBytesGenerate(true, COMMUNICATION_TYPE.SD)[IDX_AUDIO_MODE] & 0xFF; + } + + @After + public void resetRawAudioAllowed() { + ShimmerObject.setRawAudioAllowed(false); + } + + @Test + public void readsTheFourModes() { + for (int mode = Configuration.Shimmer3.AUDIO_MODE_OFF; mode <= Configuration.Shimmer3.AUDIO_MODE_RAW; mode++) { + assertEquals(mode, parse(mode)); + } + } + + @Test + public void erasedReadsAsOffAndGainKeepsTheMode() { + assertEquals(Configuration.Shimmer3.AUDIO_MODE_OFF, parse(0xFF)); // erased + assertEquals(Configuration.Shimmer3.AUDIO_MODE_OFF, parse(0x81)); + assertEquals(Configuration.Shimmer3.AUDIO_MODE_FINGERPRINT, parse(0x05)); // a gain preset leaves the mode as read + } + + @Test + public void rawAudioIsDroppedUnlessAllowed() { + assertEquals(Configuration.Shimmer3.AUDIO_MODE_FINGERPRINT, written(Configuration.Shimmer3.AUDIO_MODE_FINGERPRINT)); + assertEquals(Configuration.Shimmer3.AUDIO_MODE_FINGERPRINT, written(Configuration.Shimmer3.AUDIO_MODE_FINGERPRINT_AND_RAW)); + assertEquals(Configuration.Shimmer3.AUDIO_MODE_OFF, written(Configuration.Shimmer3.AUDIO_MODE_RAW)); + ShimmerObject.setRawAudioAllowed(true); + assertEquals(Configuration.Shimmer3.AUDIO_MODE_FINGERPRINT_AND_RAW, written(Configuration.Shimmer3.AUDIO_MODE_FINGERPRINT_AND_RAW)); + assertEquals(Configuration.Shimmer3.AUDIO_MODE_RAW, written(Configuration.Shimmer3.AUDIO_MODE_RAW)); + } + + @Test + public void olderFirmwareHasNoAudioMode() { + ShimmerPC device = device(24); + device.setConfigValueUsingConfigLabel(Configuration.Shimmer3.GuiLabelConfig.AUDIO_MODE, Configuration.Shimmer3.AUDIO_MODE_FINGERPRINT); + assertEquals(Configuration.Shimmer3.AUDIO_MODE_OFF, device.getAudioMode()); + } + +} diff --git a/ShimmerDriverPC/src/test/java/com/shimmerresearch/pcDriver/API_00048_Shimmer3rAudioStatusTest.java b/ShimmerDriverPC/src/test/java/com/shimmerresearch/pcDriver/API_00048_Shimmer3rAudioStatusTest.java new file mode 100644 index 000000000..c93dafa8c --- /dev/null +++ b/ShimmerDriverPC/src/test/java/com/shimmerresearch/pcDriver/API_00048_Shimmer3rAudioStatusTest.java @@ -0,0 +1,122 @@ +package com.shimmerresearch.pcDriver; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.shimmerresearch.driverUtilities.ShimmerVerDetails.FW_ID; +import com.shimmerresearch.driverUtilities.ShimmerVerDetails.HW_ID; +import com.shimmerresearch.driverUtilities.ShimmerVerObject; + +/** + * DEV-1459: status byte 1 bits 1-2, audio recording and audio fault + * (log-and-stream-common SHIMMER3R_AUDIO.md §5). No hardware. + */ +public class API_00048_Shimmer3rAudioStatusTest { + + /** Reaches the status parser, which the driver calls on each status frame */ + private static class StatusShimmer extends ShimmerPC { + StatusShimmer(int major, int minor, int internal) { + super("COM99"); + setShimmerVersionObjectAndCreateSensorMap(new ShimmerVerObject(HW_ID.SHIMMER_3R, FW_ID.LOGANDSTREAM, major, minor, internal)); + } + + StatusShimmer parse(int statusByte1) { + parseStatusByte(new byte[] {0, (byte) statusByte1}); + return this; + } + + boolean mIsConnectionLost = false; + + @Override + protected void connectionLost() { + mIsConnectionLost = true; + } + + boolean isTransactionCompleted() { + return mTransactionCompleted; + } + + /** A command sent and left unanswered */ + void timeOut(byte command) throws InterruptedException { + mCurrentCommand = command; + mWaitForAck = true; + mTransactionCompleted = false; + startTimerCheckForAckOrResp(1); + Thread.sleep(2500); + } + + StatusShimmer answer(int state, int fault) { + byte[] audioStatus = new byte[32]; + audioStatus[0] = 1; + audioStatus[2] = (byte) state; + audioStatus[3] = (byte) fault; + parseAudioStatus(audioStatus); + return this; + } + } + + @Test + public void recordingAndFaultFromByte1() { + StatusShimmer shimmer = new StatusShimmer(1, 1, 26); + assertTrue(shimmer.parse(0x02).isAudioRecording()); + assertFalse(shimmer.isAudioFault()); + + shimmer.parse(0x04); + assertFalse(shimmer.isAudioRecording()); + assertTrue(shimmer.isAudioFault()); + + shimmer.parse(0x01); + assertTrue(shimmer.isUsbPluggedIn()); + assertFalse(shimmer.isAudioRecording()); + assertFalse(shimmer.isAudioFault()); + } + + @Test + public void ignoredBeforeV1_01_026() { + StatusShimmer shimmer = new StatusShimmer(1, 1, 25).parse(0x06); + assertFalse(shimmer.isAudioRecording()); + assertFalse(shimmer.isAudioFault()); + } + + /** v1.01.026 records audio by its version; v1.01.025 only if a development build answers GET_AUDIO_STATUS */ + @Test + public void audioSupportByVersionOrAnswer() { + assertTrue(new StatusShimmer(1, 1, 26).isAudioSupported()); + assertFalse(new StatusShimmer(1, 1, 26).isAudioStatusToAsk()); + assertFalse(new StatusShimmer(1, 1, 24).isAudioSupported()); + assertFalse(new StatusShimmer(1, 1, 24).isAudioStatusToAsk()); + + StatusShimmer devBuild = new StatusShimmer(1, 1, 25); + assertTrue(devBuild.isAudioStatusToAsk()); + assertFalse("unknown until it answers", devBuild.isAudioSupported()); + assertTrue(devBuild.answer(2, 0).isAudioSupported()); + assertTrue(devBuild.isAudioRecording()); + assertFalse(devBuild.isAudioFault()); + + StatusShimmer release = new StatusShimmer(1, 1, 25); + release.setIsAudioStatusAnswered(false); + assertFalse("no answer: no audio", release.isAudioSupported()); + } + + @Test + public void audioStatusFault() { + StatusShimmer shimmer = new StatusShimmer(1, 1, 25).answer(4, 3); + assertFalse(shimmer.isAudioRecording()); + assertTrue(shimmer.isAudioFault()); + } + + /** A public v1.01.025 leaves GET_AUDIO_STATUS unanswered: no audio, and the link stays up */ + @Test + public void unansweredAudioStatusKeepsTheConnection() throws InterruptedException { + StatusShimmer release = new StatusShimmer(1, 1, 25); + release.timeOut(ShimmerPC.GET_AUDIO_STATUS_COMMAND); + assertFalse("connection kept", release.mIsConnectionLost); + assertTrue("next command may go", release.isTransactionCompleted()); + assertFalse(release.isAudioSupported()); + + StatusShimmer control = new StatusShimmer(1, 1, 25); + control.timeOut(ShimmerPC.GET_STATUS_COMMAND); + assertTrue("any other unanswered command still drops the link", control.mIsConnectionLost); + } +}