Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
140 changes: 140 additions & 0 deletions ShimmerBluetooth/ShimmerBluetooth/PressureTempSensor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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)")
Expand Down Expand Up @@ -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
Expand Down
58 changes: 43 additions & 15 deletions ShimmerBluetooth/ShimmerBluetooth/Shimmer3Protocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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))
Expand Down Expand Up @@ -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) {
Expand Down
Loading
Loading