From 58ab27de3ce954cd6342bc96d2ceadbd5044a488 Mon Sep 17 00:00:00 2001 From: Mark Nolan Date: Wed, 11 Nov 2026 15:41:43 +0000 Subject: [PATCH 1/2] DEV-1413: decode the Shimmer3R sound level channel (0x38) The Shimmer3R firmware adds channel 0x38, SOUND_LEVEL: the A-weighted level at the microphone as an int16, little-endian, in 0.01 dB(A). -32768 means no level. interpretDataPacketFormat already sized unknown IDs at two bytes, so packets stayed aligned, but the level was dropped. - SoundLevelSensor decodes it: RAW, and Calibrated in dB(A) (raw / 100, NaN for -32768) while calibration is on. SoundLevelSensor.calibrate() is public. - Shimmer3Protocol enables it when a Shimmer3R's inquiry lists 0x38 (it has no enabled-sensors bit), turns it off in disableSensors whatever the hardware, and runs it in buildMsg before the battery. - Sensor.SensorUnits.decibelsA, ChannelContentsShimmer3.SoundLevel. - SoundLevelSensor.swift has its four project.pbxproj entries. SensorTest covers the calibration, the decode at its index with and without calibration, and four real packets from a Shimmer3R around its first level. The wiring inside Shimmer3Protocol has no test: the class needs a live CBPeripheral. Written without Xcode, so CI is the first compile. Co-Authored-By: Claude Opus 5.5 --- .../project.pbxproj | 4 + .../ShimmerBluetooth/Sensor.swift | 1 + .../ShimmerBluetooth/Shimmer3Protocol.swift | 13 +++ .../ShimmerBluetooth/SoundLevelSensor.swift | 43 ++++++++++ .../ShimmerBluetoothTests/SensorTest.swift | 83 +++++++++++++++++++ 5 files changed, 144 insertions(+) create mode 100644 ShimmerBluetooth/ShimmerBluetooth/SoundLevelSensor.swift diff --git a/ShimmerBluetooth/ShimmerBluetooth.xcodeproj/project.pbxproj b/ShimmerBluetooth/ShimmerBluetooth.xcodeproj/project.pbxproj index 1774485..1dc48b2 100644 --- a/ShimmerBluetooth/ShimmerBluetooth.xcodeproj/project.pbxproj +++ b/ShimmerBluetooth/ShimmerBluetooth.xcodeproj/project.pbxproj @@ -47,6 +47,7 @@ 9299E5102B850571001EEFE0 /* BattVoltageSensor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9299E50F2B850571001EEFE0 /* BattVoltageSensor.swift */; }; 92B1067A2B7DAFBC00AB9952 /* EXGSensor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92B106792B7DAFBC00AB9952 /* EXGSensor.swift */; }; 92B4B5502DF035C100225A97 /* HighGAccelSensor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92B4B54F2DF035C100225A97 /* HighGAccelSensor.swift */; }; + DE14130B2C0100000000001B /* SoundLevelSensor.swift in Sources */ = {isa = PBXBuildFile; fileRef = DE14130A2C0100000000001A /* SoundLevelSensor.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -101,6 +102,7 @@ 9299E50F2B850571001EEFE0 /* BattVoltageSensor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BattVoltageSensor.swift; sourceTree = ""; }; 92B106792B7DAFBC00AB9952 /* EXGSensor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EXGSensor.swift; sourceTree = ""; }; 92B4B54F2DF035C100225A97 /* HighGAccelSensor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HighGAccelSensor.swift; sourceTree = ""; }; + DE14130A2C0100000000001A /* SoundLevelSensor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SoundLevelSensor.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -146,6 +148,7 @@ isa = PBXGroup; children = ( 92B4B54F2DF035C100225A97 /* HighGAccelSensor.swift */, + DE14130A2C0100000000001A /* SoundLevelSensor.swift */, 451EC8192DACCFF900D8F269 /* ConcurrentQueue.swift */, 92150EF42DE461CE00FEA53B /* AltMagSensor.swift */, 3AEB12F82BCE6F7E00B5F6F1 /* Shimmer3SpeedTestProtocol.swift */, @@ -340,6 +343,7 @@ 3A7A7F282AE77F0E001FD6BD /* Shimmer3Protocol.swift in Sources */, DE11460B2C0100000000001B /* Shimmer3DeviceStatus.swift in Sources */, 92B4B5502DF035C100225A97 /* HighGAccelSensor.swift in Sources */, + DE14130B2C0100000000001B /* SoundLevelSensor.swift in Sources */, 3AEB12F92BCE6F7E00B5F6F1 /* Shimmer3SpeedTestProtocol.swift in Sources */, 3AA359132AF34D89008AD334 /* Shimmer3InfoMem.swift in Sources */, 3A0B4B632B0DDD4500786295 /* ADCSensor.swift in Sources */, diff --git a/ShimmerBluetooth/ShimmerBluetooth/Sensor.swift b/ShimmerBluetooth/ShimmerBluetooth/Sensor.swift index 634b0d4..363331b 100644 --- a/ShimmerBluetooth/ShimmerBluetooth/Sensor.swift +++ b/ShimmerBluetooth/ShimmerBluetooth/Sensor.swift @@ -41,6 +41,7 @@ public class Sensor: NSObject{ case percent = "%" case kpascal = "kPa" case degreescelcius = "Degree Celsius" + case decibelsA = "dB(A)" } public var sensorEnabled:Bool = false public var calibrationEnabled:Bool = true diff --git a/ShimmerBluetooth/ShimmerBluetooth/Shimmer3Protocol.swift b/ShimmerBluetooth/ShimmerBluetooth/Shimmer3Protocol.swift index c2cf34d..835c96b 100644 --- a/ShimmerBluetooth/ShimmerBluetooth/Shimmer3Protocol.swift +++ b/ShimmerBluetooth/ShimmerBluetooth/Shimmer3Protocol.swift @@ -63,6 +63,7 @@ public class Shimmer3Protocol : NSObject, ShimmerProtocol { public var exgSensor: EXGSensor = EXGSensor() public var pressureTempSensor : PressureTempSensor = PressureTempSensor(hwid: HardwareType.UNKNOWN.rawValue) var battVoltageSensor : BattVoltageSensor = BattVoltageSensor() + public var soundLevelSensor: SoundLevelSensor = SoundLevelSensor() public var RTCErrorEnabled = false; var infoMem: [UInt8] = [] var inquiry: [UInt8] = [] @@ -503,6 +504,8 @@ public class Shimmer3Protocol : NSObject, ShimmerProtocol { adcExternalA1Sensor.sensorEnabled = false adcExternalA2Sensor.sensorEnabled = false } + // Whatever the hardware: interpretDataPacketFormat turns it back on if the channel is listed + soundLevelSensor.sensorEnabled = false } func initializeSensors(){ @@ -617,6 +620,9 @@ public class Shimmer3Protocol : NSObject, ShimmerProtocol { if pressureTempSensor.sensorEnabled{ ojc = pressureTempSensor.processData(sensorPacket: bytes, objectCluster: ojc) } + if soundLevelSensor.sensorEnabled { + ojc = soundLevelSensor.processData(sensorPacket: bytes, objectCluster: ojc) + } if battVoltageSensor.sensorEnabled{ ojc = battVoltageSensor.processData(sensorPacket: bytes, objectCluster: ojc) } @@ -1430,6 +1436,12 @@ public class Shimmer3Protocol : NSObject, ShimmerProtocol { pressureTempSensor.packetIndexPressure = packetSize packetSize += 3 enabledSensors |= Int(SensorBitmapShimmer3.SENSOR_PRESSURE.rawValue) + case ChannelContentsShimmer3.SoundLevel.rawValue where REV_HW_MAJOR == HardwareType.Shimmer3R.rawValue: + // No enabled-sensors bit: the firmware turns it on from configuration byte 233 + // bit 4, so the channel list is what says it is there + soundLevelSensor.sensorEnabled = true + soundLevelSensor.packetIndex = packetSize + packetSize += 2 default: packetSize += 2 } @@ -2248,6 +2260,7 @@ public class Shimmer3Protocol : NSObject, ShimmerProtocol { case STRAIN_HIGH = 0x27 case STRAIN_LOW = 0x28 case GsrRaw = 0x1C + case SoundLevel = 0x38 // Shimmer3R only } diff --git a/ShimmerBluetooth/ShimmerBluetooth/SoundLevelSensor.swift b/ShimmerBluetooth/ShimmerBluetooth/SoundLevelSensor.swift new file mode 100644 index 0000000..3c9cdab --- /dev/null +++ b/ShimmerBluetooth/ShimmerBluetooth/SoundLevelSensor.swift @@ -0,0 +1,43 @@ +// +// SoundLevelSensor.swift +// ShimmerBluetooth +// +// The Shimmer3R's sound level channel, 0x38. +// + +import Foundation + +/// The A-weighted sound level the Shimmer3R firmware computes from its microphone: channel 0x38, +/// an int16, little-endian, in 0.01 dB(A) steps. +/// +/// - `-32768` means no level: the firmware sends it before its first level after the microphone +/// capture starts, and while the capture is not running. It calibrates to NaN, not -327.68 dB. +/// - Each value covers 128 ms of audio and repeats until the next one. The middle of those 128 ms +/// is 64 to 192 ms before the packet's timestamp. +/// - The level is nominal: the firmware works it out from the microphone's datasheet, not from a +/// calibrated meter. +/// +/// The channel has no enabled-sensors bit; the firmware turns it on from configuration byte 233 +/// bit 4. So it is enabled here when the inquiry response lists it. +public class SoundLevelSensor: Sensor, SensorProcessing { + public static let SOUND_LEVEL = "Sound Level" + /// The raw value that means the firmware has no level to report. + public static let NO_LEVEL: Int = -32768 + + public var packetIndex: Int = -1 + + /// A raw sound level in dB(A): raw / 100, or NaN for `NO_LEVEL`. + public static func calibrate(raw: Int) -> Double { + return raw == NO_LEVEL ? Double.nan : Double(raw) / 100.0 + } + + public func processData(sensorPacket: [UInt8], objectCluster: ObjectCluster) -> ObjectCluster { + let bytes = Array(sensorPacket[packetIndex.. Double? { + let name = calibrated + ? [SoundLevelSensor.SOUND_LEVEL, Sensor.SensorFormats.Calibrated.rawValue, Sensor.SensorUnits.decibelsA.rawValue].joined(separator: "_") + : [SoundLevelSensor.SOUND_LEVEL, Sensor.SensorFormats.Raw.rawValue, Sensor.SensorUnits.noUnit.rawValue].joined(separator: "_") + guard let i = objectCluster.SignalNames.firstIndex(of: name) else { return nil } + return objectCluster.SignalData[i] + } + + private func soundLevelCluster(packet: [UInt8], packetIndex: Int, calibrationEnabled: Bool = true) -> ObjectCluster { + let sensor = SoundLevelSensor() + sensor.calibrationEnabled = calibrationEnabled + sensor.packetIndex = packetIndex + return sensor.processData(sensorPacket: packet, objectCluster: ObjectCluster(deviceName: "test")) + } + + private func bytes(_ hex: String) -> [UInt8] { + var out: [UInt8] = [] + var i = hex.startIndex + while i < hex.endIndex { + let j = hex.index(i, offsetBy: 2) + out.append(UInt8(hex[i.. Date: Wed, 11 Nov 2026 17:17:15 +0000 Subject: [PATCH 2/2] DEV-1413: name v1.01.026, the first firmware release with the sound level Co-Authored-By: Claude Opus 5.5 --- ShimmerBluetooth/ShimmerBluetooth/SoundLevelSensor.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ShimmerBluetooth/ShimmerBluetooth/SoundLevelSensor.swift b/ShimmerBluetooth/ShimmerBluetooth/SoundLevelSensor.swift index 3c9cdab..9eeb2d2 100644 --- a/ShimmerBluetooth/ShimmerBluetooth/SoundLevelSensor.swift +++ b/ShimmerBluetooth/ShimmerBluetooth/SoundLevelSensor.swift @@ -8,7 +8,8 @@ import Foundation /// The A-weighted sound level the Shimmer3R firmware computes from its microphone: channel 0x38, -/// an int16, little-endian, in 0.01 dB(A) steps. +/// an int16, little-endian, in 0.01 dB(A) steps. LogAndStream_Shimmer3R v1.01.026 is the first +/// release with it. /// /// - `-32768` means no level: the firmware sends it before its first level after the microphone /// capture starts, and while the capture is not running. It calibrates to NaN, not -327.68 dB.