From 6344f96225fd219e3260dd77217be6b8dc9354e2 Mon Sep 17 00:00:00 2001 From: Mark Nolan Date: Wed, 11 Nov 2026 16:25:05 +0000 Subject: [PATCH] DEV-1449: read the battery at its packet index, and turn it on from the inquiry BattVoltageSensor read bytes 3-4 whatever its place in the packet. VBATT (0x03) follows the low-noise accel on a Shimmer3 when that is on, and is the last channel on a Shimmer3R, so the raw value came from another sensor's bytes. It was not reached anyway. interpretDataPacketFormat starts with disableSensors(), and its VBatt case only counted the two bytes, so every inquiry left the battery off. connect(), writeInfoMem and setSensors all end with one, which undid the Shimmer3's InfoMem enable. On a Shimmer3R the sensor was not created, configured or disabled at all. - BattVoltageSensor gets a packetIndex and reads there. With no index yet, or one past the end of the packet, it adds nothing. - The VBatt case sets the index and enables the sensor. - The Shimmer3R branches of createSensors, initializeSensors and disableSensors handle it as the Shimmer3 ones do. - The calibration moves into BattVoltageSensor.calibrate(raw:), unchanged: U12 at 3 V times 2, as the Java driver and the C# API use on both generations. SensorTest decodes four Shimmer3R packets with the battery as the last channel. The Shimmer3Protocol wiring is not reachable from a unit test. Co-Authored-By: Claude Opus 5.5 --- .../ShimmerBluetooth/BattVoltageSensor.swift | 22 ++++- .../ShimmerBluetooth/Shimmer3Protocol.swift | 7 ++ .../ShimmerBluetoothTests/SensorTest.swift | 84 +++++++++++++++++++ 3 files changed, 111 insertions(+), 2 deletions(-) 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..