diff --git a/ShimmerBluetooth/ShimmerBluetooth/ConfigByteLayoutShimmer3.swift b/ShimmerBluetooth/ShimmerBluetooth/ConfigByteLayoutShimmer3.swift index 8d7d2d3..8e297ea 100644 --- a/ShimmerBluetooth/ShimmerBluetooth/ConfigByteLayoutShimmer3.swift +++ b/ShimmerBluetooth/ShimmerBluetooth/ConfigByteLayoutShimmer3.swift @@ -66,6 +66,9 @@ public class ConfigByteLayoutShimmer3{ static let bitShiftBMPX80PressureResolution = 4; static let maskBMPX80PressureResolution = 0x03; + + static let bitShiftBMP390PressureResolution = 0; + static let maskBMP390PressureResolution = 0x01; static let bitShiftLSM303DLHCMagRange = 5; static let maskLSM303DLHCMagRange = 0x07; diff --git a/ShimmerBluetooth/ShimmerBluetooth/PressureTempSensor.swift b/ShimmerBluetooth/ShimmerBluetooth/PressureTempSensor.swift index bc4ca59..3adb5b3 100644 --- a/ShimmerBluetooth/ShimmerBluetooth/PressureTempSensor.swift +++ b/ShimmerBluetooth/ShimmerBluetooth/PressureTempSensor.swift @@ -35,6 +35,38 @@ public class PressureTempSensor: Sensor , SensorProcessing{ } } + /// The pressure sensor fitted. The raw values are the sensor IDs the firmware + /// sends in the 0xA6 reply to 0xA7 (PRESSURE_SENSOR_BMP180..BMP581). + public enum PressureSensorType: UInt8 { + case BMP180 = 0 + case BMP280 = 1 + case BMP390 = 2 + case BMP581 = 3 + + /// Number of calibration coefficient bytes that follow the sensor ID in the 0xA6 reply. + public var calibrationByteCount: Int { + switch self { + case .BMP180: + return 22 + case .BMP280: + return 24 + case .BMP390: + return 21 + case .BMP581: + return 0 + } + } + } + + /// Set from the 0xA6 reply, or from the SR-number rule when 0xA7 gets no usable reply. + /// nil (not yet known) keeps the BMP390 path on a Shimmer3R. + public var pressureSensorType: PressureSensorType? = nil + + /// BMP581 oversampling settings, indexed by the 3-bit pressure resolution field. + public static let ListOfPressureResolutionBMP581 = ["Lowest Power", "Low", "Standard", "High", "High Res", "Very High Res", "Ultra High Res", "Highest Res"] + + static let FW_ID_LOGANDSTREAM = 3 + var CurrentResolution = Resolution.RES_LOW var pressureResolution = 0 public static let TEMPERATURE = "Temperature" @@ -119,6 +151,23 @@ public class PressureTempSensor: Sensor , SensorProcessing{ unitName: SensorUnits.noUnit.rawValue, value: Double(rawP)) + if pressureSensorType == .BMP581 { + // The BMP581 outputs compensated values, so there are no coefficients to apply + let (pressurePa, temperatureC) = PressureTempSensor.calibratePressure581(rawP: Int(rawP), + rawT: Int(rawT)) + + objectCluster.addData(sensorName: PressureTempSensor.TEMPERATURE, + formatName: SensorFormats.Calibrated.rawValue, + unitName: SensorUnits.degreescelcius.rawValue, + value: temperatureC) + + objectCluster.addData(sensorName: PressureTempSensor.PRESSURE, + formatName: SensorFormats.Calibrated.rawValue, + unitName: SensorUnits.kpascal.rawValue, + value: pressurePa / 1000.0) + return objectCluster + } + let (compPress, tLin) = calibratePressure390(UP: Double(rawP), UT: Double(rawT)) @@ -294,6 +343,92 @@ public class PressureTempSensor: Sensor , SensorProcessing{ } } + /// Decodes the body of a 0xA6 reply, [len = 1 + n][sensorId][n coefficient bytes], + /// with the ACK, response byte and CRC already removed. + /// Returns nil when the sensor ID is unknown or len does not match it. + public static func decodePressureCalibrationResponse(_ payload: [UInt8]) -> (sensorType: PressureSensorType, coefficients: [UInt8])? { + guard payload.count >= 2, let sensorType = PressureSensorType(rawValue: payload[1]) else { + return nil + } + let length = Int(payload[0]) + guard length == 1 + sensorType.calibrationByteCount, payload.count >= 1 + length else { + return nil + } + return (sensorType, Array(payload[2..<(1 + length)])) + } + + /// Stores the sensor type from a 0xA6 reply and parses its coefficients. + /// Returns false, changing nothing, when the reply is malformed. + public func applyPressureCalibrationResponse(_ payload: [UInt8]) -> Bool { + guard let decoded = PressureTempSensor.decodePressureCalibrationResponse(payload) else { + print("Invalid pressure calibration response: \(payload.map{ String(format: "%02X", $0) }.joined(separator: " "))") + return false + } + pressureSensorType = decoded.sensorType + print("Pressure sensor: \(decoded.sensorType)") + if decoded.sensorType == .BMP390 { + parseCalParamByteArray(pressureResoRes: payload) + } + // The BMP581 has no coefficients. BMP180/BMP280 coefficients are read with their own commands. + return true + } + + /// True when the expansion board SR number is one that carries the BMP581 on a Shimmer3R, + /// following the firmware's ShimBrd_isBmp581PresentPerSrNumber(). ">=" compares rev, then special rev. + public static func isBmp581PresentPerSrNumber(hardwareVersion: Int, expansionBoardId: Int, expansionBoardRev: Int, expansionBoardRevSpecial: Int) -> Bool { + // An unprogrammed daughter card reads 0x00 or 0xFF + if hardwareVersion != Shimmer3Protocol.HardwareType.Shimmer3R.rawValue + || expansionBoardId == 0x00 || expansionBoardId == 0xFF { + return false + } + func isSrNumberGte(_ id: Shimmer3Protocol.ExpansionBoardDetectShimmer3, _ rev: Int, _ revSpecial: Int) -> Bool { + return expansionBoardId == id.rawValue + && (expansionBoardRev > rev || (expansionBoardRev == rev && expansionBoardRevSpecial >= revSpecial)) + } + // SR48 has two BMP581 ranges, 7.2 to 7.x and 8.2 onwards: SR48-8.0 and 8.1 are BMP390 + return isSrNumberGte(.SHIMMER3, 11, 2) + || isSrNumberGte(.EXPANSION_PROTO3_DELUXE, 4, 2) + || isSrNumberGte(.EXP_BRD_EXG_UNIFIED, 8, 2) + || (isSrNumberGte(.EXP_BRD_GSR_UNIFIED, 7, 2) && !isSrNumberGte(.EXP_BRD_GSR_UNIFIED, 8, 0)) + || isSrNumberGte(.EXP_BRD_GSR_UNIFIED, 8, 2) + || isSrNumberGte(.EXP_BRD_BR_AMP_UNIFIED, 4, 2) + } + + /// True for LogAndStream v1.01.006 onwards, the first firmware that can drive a BMP581. + public static func isBmp581SupportedByFirmware(firmwareIdentifier: Int, major: Int, minor: Int, internalVersion: Int) -> Bool { + if firmwareIdentifier != PressureTempSensor.FW_ID_LOGANDSTREAM { + return false + } + if major != 1 { + return major > 1 + } + if minor != 1 { + return minor > 1 + } + return internalVersion >= 6 + } + + /// BMP581 conversion: pressure is unsigned, Pa = raw / 64; temperature is signed, degC = raw / 65536. + /// Returns (Pa, degC). No clamp is applied. + public static func calibratePressure581(rawP: Int, rawT: Int) -> (Double, Double) { + let pressurePa = Double(rawP & 0xFFFFFF) / 64.0 + let temperatureC = Double(signExtend24(rawT)) / 65536.0 + return (pressurePa, temperatureC) + } + + /// Reads a 24-bit field as two's complement. Masks to 24 bits first, so an + /// already sign-extended value passes through unchanged. + public static func signExtend24(_ raw: Int) -> Int { + let bits = raw & 0xFFFFFF + return (bits & 0x800000) != 0 ? bits - 0x1000000 : bits + } + + /// The 3-bit pressure resolution (oversampling) setting read from InfoMem. + /// On a BMP581 it indexes ListOfPressureResolutionBMP581. + public func getPressureResolutionIndex() -> Int { + return pressureResolution + } + public func updateInfoMemPressureResolution(infomem: [UInt8],res: Resolution) -> [UInt8]{ var infomemtoupdate = infomem print("oriinfomem: \(infomemtoupdate)") @@ -336,6 +471,11 @@ public class PressureTempSensor: Sensor , SensorProcessing{ sensorEnabled = false } pressureResolution = (Int(infomem[ConfigByteLayoutShimmer3.idxConfigSetupByte3]>>ConfigByteLayoutShimmer3.bitShiftBMPX80PressureResolution) & ConfigByteLayoutShimmer3.maskBMPX80PressureResolution) + if (HardwareVersion == Shimmer3Protocol.HardwareType.Shimmer3R.rawValue && infomem.count > ConfigByteLayoutShimmer3.idxConfigSetupByte4){ + // The BMP390/BMP581 setting is 3 bits: its MSB is ConfigSetupByte4 bit 0 + let msb = Int(infomem[ConfigByteLayoutShimmer3.idxConfigSetupByte4]>>ConfigByteLayoutShimmer3.bitShiftBMP390PressureResolution) & ConfigByteLayoutShimmer3.maskBMP390PressureResolution + pressureResolution = pressureResolution | (msb << 2) + } if (pressureResolution == 0){ CurrentResolution = Resolution.RES_LOW diff --git a/ShimmerBluetooth/ShimmerBluetooth/Shimmer3Protocol.swift b/ShimmerBluetooth/ShimmerBluetooth/Shimmer3Protocol.swift index 0e5481e..5a108b5 100644 --- a/ShimmerBluetooth/ShimmerBluetooth/Shimmer3Protocol.swift +++ b/ShimmerBluetooth/ShimmerBluetooth/Shimmer3Protocol.swift @@ -349,7 +349,17 @@ public class Shimmer3Protocol : NSObject, ShimmerProtocol { if (REV_HW_MAJOR==HardwareType.Shimmer3.rawValue){ await sendBMP280PressureCalibCoefficientsCommand() } else if (REV_HW_MAJOR==HardwareType.Shimmer3R.rawValue){ - await sendPressureCalibCoefficientsCommand() + let pressureSensorIdReceived = await sendPressureCalibCoefficientsCommand() ?? false + if (!pressureSensorIdReceived){ + // No usable 0xA6 reply: v1.01.006 refuses 0xA7 on a BMP581, so + // fall back to the board SR number and firmware version. + // Also taken for a malformed reply. A NACK only reaches here once + // NACK handling resumes the command with a failure; until then it + // leaves this await suspended. + let sensorType = isShimmer3RwithBmp581() ? PressureTempSensor.PressureSensorType.BMP581 : PressureTempSensor.PressureSensorType.BMP390 + pressureTempSensor.pressureSensorType = sensorType + print("Pressure sensor from SR number: \(sensorType)") + } } await sendInquiryCommand() @@ -362,6 +372,19 @@ public class Shimmer3Protocol : NSObject, ShimmerProtocol { return true } + /// True when a Shimmer3R's expansion board SR number and firmware version indicate + /// a BMP581. Used only when the device does not report its pressure sensor in-band. + public func isShimmer3RwithBmp581() -> Bool { + return PressureTempSensor.isBmp581PresentPerSrNumber(hardwareVersion: REV_HW_MAJOR, + expansionBoardId: EXPANSION_BOARD_ID, + expansionBoardRev: EXPANSION_BOARD_REV, + expansionBoardRevSpecial: EXPANSION_BOARD_REV_SPECIAL) + && PressureTempSensor.isBmp581SupportedByFirmware(firmwareIdentifier: REV_FW_IDENTIFIER, + major: REV_FW_MAJOR, + minor: REV_FW_MINOR, + internalVersion: REV_FW_INTERNAL) + } + func parseCalibrationDump(_ bytes:[UInt8]){ let length = Int(bytes[0]) + (Int(bytes[1])<<8) var calibrationBytes = Array(bytes.prefix(length+2)) @@ -971,23 +994,28 @@ public class Shimmer3Protocol : NSObject, ShimmerProtocol { //self.receivedBytes.removeAll() } else if (self.commandSent == PacketTypeShimmer.getpressureCalibrationCoefficientsCommand) { - if self.receivedBytes[1] == PacketTypeShimmer.pressureCalibrationCoefficientsResponse.rawValue { + if self.receivedBytes.count > 2 && self.receivedBytes[1] == PacketTypeShimmer.pressureCalibrationCoefficientsResponse.rawValue { + + // [ACK][0xA6][len = 1 + n][sensorId][n coefficient bytes][CRC] + // Wait for the whole reply so its length can be checked against the sensor ID + let length = 1 + 1 + 1 + Int(self.receivedBytes[2]) + Int(self.CRCMode.rawValue) + if self.receivedBytes.count >= length { + // Remove ACK + response type + var data = Array(self.receivedBytes.prefix(length)) + data.removeFirst(2) + + // Remove CRC + if self.CRCMode != .OFF { + data.removeLast(Int(self.CRCMode.rawValue)) + } - // Remove ACK + response type - var data = self.receivedBytes - data.removeFirst(2) + // Sets the sensor type; parses the coefficients for a BMP390 + let valid = self.pressureTempSensor.applyPressureCalibrationResponse(data) - // Remove CRC - if self.CRCMode != .OFF { - data.removeLast(Int(self.CRCMode.rawValue)) + self.continuation?.resume(returning: valid) + self.continuation = nil + self.receivedBytes.removeAll() } - - // Forward to same PressureTempSensor - self.pressureTempSensor.parseCalParamByteArray(pressureResoRes: data) - - self.continuation?.resume(returning: true) - self.continuation = nil - self.receivedBytes.removeAll() } } else if (self.commandSent == PacketTypeShimmer.setSensorsCommand) { diff --git a/ShimmerBluetooth/ShimmerBluetoothTests/SensorTest.swift b/ShimmerBluetooth/ShimmerBluetoothTests/SensorTest.swift index 4846cf6..136e9c6 100644 --- a/ShimmerBluetooth/ShimmerBluetoothTests/SensorTest.swift +++ b/ShimmerBluetooth/ShimmerBluetoothTests/SensorTest.swift @@ -96,6 +96,149 @@ class SensorTest: XCTestCase { pressureResolution = (Int(infompressuretemp[ConfigByteLayoutShimmer3.idxConfigSetupByte3]>>ConfigByteLayoutShimmer3.bitShiftBMPX80PressureResolution) & ConfigByteLayoutShimmer3.maskBMPX80PressureResolution) XCTAssertEqual(pressureResolution, 3, "Failed to set pressure resolution") } + + // BMP581 conversion vectors shared by every host API: pressure is unsigned, temperature is signed 24-bit + func testBMP581Conversion(){ + var (pressurePa, temperatureC) = PressureTempSensor.calibratePressure581(rawP: 6400000, rawT: 1638400) + XCTAssertEqual(pressurePa / 1000.0, 100.0, accuracy: 1e-9, "Failed BMP581 pressure") + XCTAssertEqual(temperatureC, 25.0, accuracy: 1e-9, "Failed BMP581 temperature") + + (pressurePa, temperatureC) = PressureTempSensor.calibratePressure581(rawP: 0xFFFFFF, rawT: 1600000) + XCTAssertEqual(pressurePa / 1000.0, 262.143984375, accuracy: 1e-9, "Failed BMP581 full-scale pressure") + XCTAssertEqual(temperatureC, 24.4140625, accuracy: 1e-9, "Failed BMP581 temperature") + + XCTAssertEqual(PressureTempSensor.calibratePressure581(rawP: 0, rawT: 0x7FFFFF).1, 127.9999847, accuracy: 1e-7, "Failed BMP581 maximum temperature") + XCTAssertEqual(PressureTempSensor.calibratePressure581(rawP: 0, rawT: 0xFFFFFF).1, -1.0 / 65536.0, accuracy: 1e-12, "Failed BMP581 temperature just below zero") + // DEV-1102: wire bytes 56 55 FE read as ~254 degC when the temperature was decoded unsigned + XCTAssertEqual(PressureTempSensor.calibratePressure581(rawP: 0, rawT: 0xFE5556).1, -1.6666565, accuracy: 1e-7, "Failed BMP581 sub-zero temperature") + XCTAssertEqual(PressureTempSensor.calibratePressure581(rawP: 0, rawT: 0x800000).1, -128.0, accuracy: 1e-12, "Failed BMP581 minimum temperature") + + // Masked before extending, so an already-signed value passes through unchanged + XCTAssertEqual(PressureTempSensor.signExtend24(-109226), -109226, "Failed sign extension of a signed value") + XCTAssertEqual(PressureTempSensor.signExtend24(0xFE5556), -109226, "Failed sign extension") + } + + func testBMP581ProcessData(){ + var pressTempSensor: PressureTempSensor = PressureTempSensor(hwid: Shimmer3Protocol.HardwareType.Shimmer3R.rawValue) + pressTempSensor.pressureSensorType = PressureTempSensor.PressureSensorType.BMP581 + // Channel order [0x1B pressure, 0x1A temperature], each 3 bytes little-endian + pressTempSensor.packetIndexPressure = 0 + pressTempSensor.packetIndexTemp = 3 + var objectCluster = pressTempSensor.processData(sensorPacket: [0x00, 0xA8, 0x61, 0x00, 0x00, 0x19], objectCluster: ObjectCluster(deviceName: "test")) + + let pressureName = [PressureTempSensor.PRESSURE, Sensor.SensorFormats.Calibrated.rawValue, Sensor.SensorUnits.kpascal.rawValue].joined(separator: "_") + let temperatureName = [PressureTempSensor.TEMPERATURE, Sensor.SensorFormats.Calibrated.rawValue, Sensor.SensorUnits.degreescelcius.rawValue].joined(separator: "_") + let rawPressureName = [PressureTempSensor.PRESSURE, Sensor.SensorFormats.Raw.rawValue, Sensor.SensorUnits.noUnit.rawValue].joined(separator: "_") + XCTAssertEqual(objectCluster.SignalData[objectCluster.SignalNames.firstIndex(of: pressureName)!], 100.0, accuracy: 1e-9, "Failed BMP581 pressure") + XCTAssertEqual(objectCluster.SignalData[objectCluster.SignalNames.firstIndex(of: temperatureName)!], 25.0, accuracy: 1e-9, "Failed BMP581 temperature") + XCTAssertEqual(objectCluster.SignalData[objectCluster.SignalNames.firstIndex(of: rawPressureName)!], 6400000.0, "Failed BMP581 raw pressure") + + // Sub-zero temperature, wire bytes 56 55 FE + objectCluster = pressTempSensor.processData(sensorPacket: [0x00, 0xA8, 0x61, 0x56, 0x55, 0xFE], objectCluster: ObjectCluster(deviceName: "test")) + XCTAssertEqual(objectCluster.SignalData[objectCluster.SignalNames.firstIndex(of: temperatureName)!], -1.6666565, accuracy: 1e-7, "Failed BMP581 sub-zero temperature") + } + + // Body of the 0xA6 reply to 0xA7, after the ACK and response byte: [len = 1 + n][sensorId][n coefficient bytes] + func testPressureCalibrationResponse(){ + var decoded = PressureTempSensor.decodePressureCalibrationResponse([0x01, 0x03]) + XCTAssertEqual(decoded?.sensorType, PressureTempSensor.PressureSensorType.BMP581, "Failed to decode BMP581") + XCTAssertEqual(decoded?.coefficients.count, 0, "BMP581 has no coefficients") + + // A length that does not match the sensor ID is rejected + XCTAssertNil(PressureTempSensor.decodePressureCalibrationResponse([0x04, 0x03, 0x11, 0x22, 0x33]), "Accepted a BMP581 reply with coefficients") + XCTAssertNil(PressureTempSensor.decodePressureCalibrationResponse([0x01, 0x02]), "Accepted a BMP390 reply without coefficients") + XCTAssertNil(PressureTempSensor.decodePressureCalibrationResponse([0x01, 0x04]), "Accepted an unknown sensor ID") + XCTAssertNil(PressureTempSensor.decodePressureCalibrationResponse([0x01]), "Accepted a truncated reply") + + let bmp390Coefficients: [UInt8] = [0x6B, 0x6A, 0x8E, 0x49, 0xF8, 0x7F, 0xF5, 0xA2, 0x05, 0x19, 0x05, 0x68, 0x44, 0x36, 0x61, 0xFC, 0x07, 0x3A, 0xF3, 0xB8, 0x0E] + let bmp390Payload: [UInt8] = [0x16, 0x02] + bmp390Coefficients + decoded = PressureTempSensor.decodePressureCalibrationResponse(bmp390Payload) + XCTAssertEqual(decoded?.sensorType, PressureTempSensor.PressureSensorType.BMP390, "Failed to decode BMP390") + XCTAssertEqual(decoded?.coefficients ?? [], bmp390Coefficients, "Failed to decode BMP390 coefficients") + XCTAssertNil(PressureTempSensor.decodePressureCalibrationResponse(Array(bmp390Payload.dropLast())), "Accepted a short BMP390 reply") + + // The BMP581 reply sets the type and leaves the coefficients alone + var pressTempSensor: PressureTempSensor = PressureTempSensor(hwid: Shimmer3Protocol.HardwareType.Shimmer3R.rawValue) + XCTAssertTrue(pressTempSensor.applyPressureCalibrationResponse([0x01, 0x03]), "Rejected a BMP581 reply") + XCTAssertEqual(pressTempSensor.pressureSensorType, PressureTempSensor.PressureSensorType.BMP581, "Failed to set BMP581") + XCTAssertEqual(pressTempSensor.par_T1, 0, "BMP581 reply changed the coefficients") + XCTAssertFalse(pressTempSensor.applyPressureCalibrationResponse([0x04, 0x03, 0x11, 0x22, 0x33]), "Accepted a malformed reply") + XCTAssertEqual(pressTempSensor.pressureSensorType, PressureTempSensor.PressureSensorType.BMP581, "A malformed reply changed the sensor type") + + // The BMP390 reply parses the coefficients exactly as before + pressTempSensor = PressureTempSensor(hwid: Shimmer3Protocol.HardwareType.Shimmer3R.rawValue) + let reference: PressureTempSensor = PressureTempSensor(hwid: Shimmer3Protocol.HardwareType.Shimmer3R.rawValue) + reference.parseCalParamByteArray(pressureResoRes: bmp390Payload) + XCTAssertTrue(pressTempSensor.applyPressureCalibrationResponse(bmp390Payload), "Rejected a BMP390 reply") + XCTAssertEqual(pressTempSensor.pressureSensorType, PressureTempSensor.PressureSensorType.BMP390, "Failed to set BMP390") + XCTAssertEqual(pressTempSensor.par_T1, reference.par_T1, "Failed BMP390 coefficients") + XCTAssertEqual(pressTempSensor.par_P5, reference.par_P5, "Failed BMP390 coefficients") + XCTAssertEqual(pressTempSensor.par_P11, reference.par_P11, "Failed BMP390 coefficients") + let (pressure, temperature) = pressTempSensor.calibratePressure390(UP: 6400000, UT: 8400000) + let (referencePressure, referenceTemperature) = reference.calibratePressure390(UP: 6400000, UT: 8400000) + XCTAssertEqual(pressure, referencePressure, "Failed BMP390 pressure") + XCTAssertEqual(temperature, referenceTemperature, "Failed BMP390 temperature") + } + + // Cases copied from the firmware's test_bmp581_gate (log-and-stream-common Test/host/test_boards.c) + func testBMP581SrNumberRule(){ + let imu = Shimmer3Protocol.ExpansionBoardDetectShimmer3.SHIMMER3.rawValue + let proto3Deluxe = Shimmer3Protocol.ExpansionBoardDetectShimmer3.EXPANSION_PROTO3_DELUXE.rawValue + let exg = Shimmer3Protocol.ExpansionBoardDetectShimmer3.EXP_BRD_EXG_UNIFIED.rawValue + let brAmp = Shimmer3Protocol.ExpansionBoardDetectShimmer3.EXP_BRD_BR_AMP_UNIFIED.rawValue + let gsr = Shimmer3Protocol.ExpansionBoardDetectShimmer3.EXP_BRD_GSR_UNIFIED.rawValue + let proto3Mini = Shimmer3Protocol.ExpansionBoardDetectShimmer3.EXPANSION_PROTO3_MINI.rawValue + let cases: [(Int, Int, Int, Bool)] = [ + (imu, 11, 1, false), (imu, 11, 2, true), (imu, 11, 3, true), (imu, 12, 0, true), (imu, 10, 9, false), + (proto3Deluxe, 4, 1, false), (proto3Deluxe, 4, 2, true), (proto3Deluxe, 5, 0, true), + (exg, 7, 2, false), (exg, 8, 1, false), (exg, 8, 2, true), (exg, 9, 0, true), + (brAmp, 4, 1, false), (brAmp, 4, 2, true), + (gsr, 6, 0, false), (gsr, 7, 0, false), (gsr, 7, 1, false), (gsr, 7, 2, true), (gsr, 7, 3, true), + (gsr, 8, 0, false), (gsr, 8, 1, false), (gsr, 8, 2, true), (gsr, 8, 3, true), (gsr, 9, 0, true), + (proto3Mini, 9, 9, false) + ] + for (srId, rev, revSpecial, expected) in cases { + XCTAssertEqual(PressureTempSensor.isBmp581PresentPerSrNumber(hardwareVersion: Shimmer3Protocol.HardwareType.Shimmer3R.rawValue, expansionBoardId: srId, expansionBoardRev: rev, expansionBoardRevSpecial: revSpecial), expected, "SR\(srId)-\(rev).\(revSpecial)") + } + + // Shimmer3R only: the same daughter card on a Shimmer3 never reports a BMP581 + XCTAssertFalse(PressureTempSensor.isBmp581PresentPerSrNumber(hardwareVersion: Shimmer3Protocol.HardwareType.Shimmer3.rawValue, expansionBoardId: gsr, expansionBoardRev: 8, expansionBoardRevSpecial: 2), "A Shimmer3 reported a BMP581") + // An unprogrammed card must not either + XCTAssertFalse(PressureTempSensor.isBmp581PresentPerSrNumber(hardwareVersion: Shimmer3Protocol.HardwareType.Shimmer3R.rawValue, expansionBoardId: 0xFF, expansionBoardRev: 0xFF, expansionBoardRevSpecial: 0xFF), "An unprogrammed card reported a BMP581") + + // LogAndStream (firmware identifier 3) v1.01.006 onwards + XCTAssertFalse(PressureTempSensor.isBmp581SupportedByFirmware(firmwareIdentifier: 3, major: 1, minor: 1, internalVersion: 5), "v1.01.005 has no BMP581 support") + XCTAssertTrue(PressureTempSensor.isBmp581SupportedByFirmware(firmwareIdentifier: 3, major: 1, minor: 1, internalVersion: 6), "Failed v1.01.006") + XCTAssertTrue(PressureTempSensor.isBmp581SupportedByFirmware(firmwareIdentifier: 3, major: 1, minor: 2, internalVersion: 0), "Failed v1.02.000") + XCTAssertTrue(PressureTempSensor.isBmp581SupportedByFirmware(firmwareIdentifier: 3, major: 2, minor: 0, internalVersion: 0), "Failed v2.00.000") + XCTAssertFalse(PressureTempSensor.isBmp581SupportedByFirmware(firmwareIdentifier: 3, major: 0, minor: 16, internalVersion: 0), "v0.16.000 has no BMP581 support") + XCTAssertFalse(PressureTempSensor.isBmp581SupportedByFirmware(firmwareIdentifier: 1, major: 1, minor: 1, internalVersion: 6), "Not LogAndStream") + } + + func testBMP581PressureResolution(){ + var infomem = [UInt8](repeating: 0, count: 384) + infomem[ConfigByteLayoutShimmer3.idxConfigSetupByte3] = 0x30 + infomem[ConfigByteLayoutShimmer3.idxConfigSetupByte4] = 0x01 + XCTAssertEqual(ConfigByteLayoutShimmer3.idxConfigSetupByte4, 130, "ConfigSetupByte4 moved") + + var pressTempSensor: PressureTempSensor = PressureTempSensor(hwid: Shimmer3Protocol.HardwareType.Shimmer3R.rawValue) + pressTempSensor.setInfoMom(infomem: infomem) + XCTAssertEqual(pressTempSensor.getPressureResolutionIndex(), 7, "Failed to read the 3-bit pressure resolution") + XCTAssertEqual(PressureTempSensor.ListOfPressureResolutionBMP581[pressTempSensor.getPressureResolutionIndex()], "Highest Res", "Failed BMP581 resolution label") + + infomem[ConfigByteLayoutShimmer3.idxConfigSetupByte3] = 0x10 + pressTempSensor.setInfoMom(infomem: infomem) + XCTAssertEqual(pressTempSensor.getPressureResolutionIndex(), 5, "Failed to read the 3-bit pressure resolution") + XCTAssertEqual(PressureTempSensor.ListOfPressureResolutionBMP581[pressTempSensor.getPressureResolutionIndex()], "Very High Res", "Failed BMP581 resolution label") + + XCTAssertEqual(PressureTempSensor.ListOfPressureResolutionBMP581, ["Lowest Power", "Low", "Standard", "High", "High Res", "Very High Res", "Ultra High Res", "Highest Res"], "Failed BMP581 resolution labels") + + // A Shimmer3 has a 2-bit setting and ignores ConfigSetupByte4 + pressTempSensor = PressureTempSensor(hwid: Shimmer3Protocol.HardwareType.Shimmer3.rawValue) + pressTempSensor.setInfoMom(infomem: infomem) + XCTAssertEqual(pressTempSensor.getPressureResolutionIndex(), 1, "Shimmer3 read ConfigSetupByte4") + } + func testSensorEcgRespGain(){ var exgSensor: EXGSensor = EXGSensor() var infomexg:[UInt8] = [0x80, 0x02, 0x01, 0x00, 0x00, 0x18, 0x01, 0xFF, 0x01, 0x09, 0x00, 0xA8, 0x10, 0x40, 0x40, 0x2D, 0x00, 0x00, 0x02, 0x03, 0x00, 0xA0, 0x10, 0x40, 0x47, 0x00, 0x00, 0x00, 0x02, 0x01, 0x09, 0x00, 0x00, 0x00, 0x08, 0xCD, 0x08, 0xCD, 0x08, 0xCD, 0x00, 0x5C, 0x00, 0x5C, 0x00, 0x5C, 0x00, 0x9C, 0x00, 0x9C, 0x00, 0x00, 0x00, 0x00, 0x9C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0x96, 0x19, 0x96, 0x19, 0x96, 0x00, 0x9C, 0x00, 0x9C, 0x00, 0x00, 0x00, 0x00, 0x9C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x9B, 0x02, 0x9B, 0x02, 0x9B, 0x00, 0x9C, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x9C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x87, 0x06, 0x87, 0x06, 0x87, 0x00, 0x9C, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x9C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,