diff --git a/ShimmerBluetooth/ShimmerBluetooth/BattVoltageSensor.swift b/ShimmerBluetooth/ShimmerBluetooth/BattVoltageSensor.swift index 0696e53..57cec78 100644 --- a/ShimmerBluetooth/ShimmerBluetooth/BattVoltageSensor.swift +++ b/ShimmerBluetooth/ShimmerBluetooth/BattVoltageSensor.swift @@ -7,18 +7,36 @@ import Foundation +/// The battery voltage, channel 0x03 (VBATT), on a Shimmer3 and a Shimmer3R. +/// +/// Where it sits in the packet depends on what else is on: on a Shimmer3 it follows the +/// low-noise accelerometer when that is enabled, and on a Shimmer3R it is the last channel. So +/// it is read at `packetIndex`, which interpretDataPacketFormat sets from the inquiry response's +/// channel list. public class BattVoltageSensor: Sensor , SensorProcessing{ public static let BATTERY = "Battery" public static let BATTERY_PERCENTAGE = "Battery Percentage" + public var packetIndex: Int = -1 + + /// A raw battery reading in mV: the 12-bit ADC value at a 3 V reference, times 2 for the + /// divider. The same on a Shimmer3 and a Shimmer3R, as in the Java and C# APIs. + public static func calibrate(raw: Double) -> Double { + return ((raw - 0) * ((3000/1)/4095)) * 2 + } + public func processData(sensorPacket: [UInt8], objectCluster: ObjectCluster) -> ObjectCluster { - let battery = Array(sensorPacket[3..<5]) + // No index yet (the inquiry response has not been read), or a packet too short for it + guard packetIndex >= 0, packetIndex + 2 <= sensorPacket.count else { + return objectCluster + } + let battery = Array(sensorPacket[packetIndex.. Double? { + let signal = [name, format.rawValue, unit.rawValue].joined(separator: "_") + guard let i = objectCluster.SignalNames.firstIndex(of: signal) else { return nil } + return objectCluster.SignalData[i] + } + + private func batteryCluster(packet: [UInt8], packetIndex: Int, calibrationEnabled: Bool = true) -> ObjectCluster { + let sensor = BattVoltageSensor() + sensor.calibrationEnabled = calibrationEnabled + sensor.packetIndex = packetIndex + return sensor.processData(sensorPacket: packet, objectCluster: ObjectCluster(deviceName: "test")) + } + + /// U12 at a 3 V reference, times 2, as the Java driver and the C# API do on both generations. + func testBatteryCalibration(){ + XCTAssertEqual(BattVoltageSensor.calibrate(raw: 0), 0.0) + XCTAssertEqual(BattVoltageSensor.calibrate(raw: 4095), 6000.0, accuracy: 1e-9) + XCTAssertEqual(BattVoltageSensor.calibrate(raw: 2847), 4171.428571428572, accuracy: 1e-9) + XCTAssertEqual(BattVoltageSensor.calibrate(raw: 2048), 3000.7326007326005, accuracy: 1e-9) + } + + /// The battery is read at its packet index, not at the first channel after the timestamp. + func testBatteryDecodesAtItsIndex(){ + // Timestamp, then 0x002E (46) where the old code read, then 0x0B1F (2847) + let packet: [UInt8] = [0xE3, 0x85, 0x22, 0x2E, 0x00, 0x1F, 0x0B] + let battery = batteryCluster(packet: packet, packetIndex: 5) + XCTAssertEqual(batterySignal(battery, BattVoltageSensor.BATTERY, .Raw, .noUnit), 2847) + XCTAssertEqual(batterySignal(battery, BattVoltageSensor.BATTERY, .Calibrated, .milliVolts)!, 4171.428571428572, accuracy: 1e-9) + + let uncalibrated = batteryCluster(packet: packet, packetIndex: 5, calibrationEnabled: false) + XCTAssertEqual(batterySignal(uncalibrated, BattVoltageSensor.BATTERY, .Raw, .noUnit), 2847) + XCTAssertNil(batterySignal(uncalibrated, BattVoltageSensor.BATTERY, .Calibrated, .milliVolts)) + XCTAssertNil(batterySignal(uncalibrated, BattVoltageSensor.BATTERY_PERCENTAGE, .Calibrated, .percent)) + } + + /// Without an index from the inquiry response, or with one past the end of the packet, the + /// sensor adds nothing rather than reading the wrong bytes or out of range. + func testBatteryWithoutAUsableIndexAddsNothing(){ + let packet: [UInt8] = [0xE3, 0x85, 0x22, 0x2E, 0x00, 0x1F, 0x0B] + for packetIndex in [-1, 6, 7] { + let cluster = batteryCluster(packet: packet, packetIndex: packetIndex) + XCTAssertTrue(cluster.SignalNames.isEmpty, "index \(packetIndex)") + } + } + + /// Packets a Shimmer3R streamed, as buildMsg gets them: without the packet type byte. The + /// battery is the last channel, and bytes 3-4 hold the magnetometer. + func testBatteryFromShimmer3RPackets(){ + func hex(_ s: String) -> [UInt8] { + var out: [UInt8] = [] + var i = s.startIndex + while i < s.endIndex { + let j = s.index(i, offsetBy: 2) + out.append(UInt8(s[i..