Skip to content
Open
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
4 changes: 4 additions & 0 deletions ShimmerBluetooth/ShimmerBluetooth.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -101,6 +102,7 @@
9299E50F2B850571001EEFE0 /* BattVoltageSensor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BattVoltageSensor.swift; sourceTree = "<group>"; };
92B106792B7DAFBC00AB9952 /* EXGSensor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EXGSensor.swift; sourceTree = "<group>"; };
92B4B54F2DF035C100225A97 /* HighGAccelSensor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HighGAccelSensor.swift; sourceTree = "<group>"; };
DE14130A2C0100000000001A /* SoundLevelSensor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SoundLevelSensor.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -146,6 +148,7 @@
isa = PBXGroup;
children = (
92B4B54F2DF035C100225A97 /* HighGAccelSensor.swift */,
DE14130A2C0100000000001A /* SoundLevelSensor.swift */,
451EC8192DACCFF900D8F269 /* ConcurrentQueue.swift */,
92150EF42DE461CE00FEA53B /* AltMagSensor.swift */,
3AEB12F82BCE6F7E00B5F6F1 /* Shimmer3SpeedTestProtocol.swift */,
Expand Down Expand Up @@ -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 */,
Expand Down
1 change: 1 addition & 0 deletions ShimmerBluetooth/ShimmerBluetooth/Sensor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions ShimmerBluetooth/ShimmerBluetooth/Shimmer3Protocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []
Expand Down Expand Up @@ -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(){
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}


Expand Down
44 changes: 44 additions & 0 deletions ShimmerBluetooth/ShimmerBluetooth/SoundLevelSensor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// 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. 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.
/// - 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..<packetIndex+2])
let raw = ShimmerUtilities.parseSensorData(sensorData: bytes, dataType: SensorDataType.i16)!
objectCluster.addData(sensorName: SoundLevelSensor.SOUND_LEVEL, formatName: SensorFormats.Raw.rawValue, unitName: SensorUnits.noUnit.rawValue, value: Double(raw))
if (calibrationEnabled) {
objectCluster.addData(sensorName: SoundLevelSensor.SOUND_LEVEL, formatName: SensorFormats.Calibrated.rawValue, unitName: SensorUnits.decibelsA.rawValue, value: SoundLevelSensor.calibrate(raw: raw))
}
return objectCluster
}
}
83 changes: 83 additions & 0 deletions ShimmerBluetooth/ShimmerBluetoothTests/SensorTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -609,4 +609,87 @@ class SensorTest: XCTestCase {
sensor.packetIndexHighGAccelZ = 4
return sensor.processData(sensorPacket: packet, objectCluster: ObjectCluster(deviceName: "test"))
}

// MARK: Sound level (Shimmer3R channel 0x38)

private func soundLevel(_ objectCluster: ObjectCluster, calibrated: Bool) -> 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..<j], radix: 16)!)
i = j
}
return out
}

func testSoundLevelCalibration() {
XCTAssertEqual(SoundLevelSensor.calibrate(raw: 4941), 49.41, accuracy: 1e-12)
XCTAssertEqual(SoundLevelSensor.calibrate(raw: 0), 0.0)
XCTAssertEqual(SoundLevelSensor.calibrate(raw: -1), -0.01, accuracy: 1e-12)
XCTAssertEqual(SoundLevelSensor.calibrate(raw: 32767), 327.67, accuracy: 1e-12)
XCTAssertTrue(SoundLevelSensor.calibrate(raw: SoundLevelSensor.NO_LEVEL).isNaN, "-32768 is no level, not -327.68 dB")
}

/// The level is a signed little-endian int16 at its packet index.
func testSoundLevelDecodesAtItsIndex() {
// Timestamp, then 0x134D (4941), then 0x8000 (no level)
let packet: [UInt8] = [0x25, 0x13, 0xF4, 0x4D, 0x13, 0x00, 0x80]
let level = soundLevelCluster(packet: packet, packetIndex: 3)
XCTAssertEqual(soundLevel(level, calibrated: false), 4941)
XCTAssertEqual(soundLevel(level, calibrated: true)!, 49.41, accuracy: 1e-12)

let noLevel = soundLevelCluster(packet: packet, packetIndex: 5)
XCTAssertEqual(soundLevel(noLevel, calibrated: false), -32768)
XCTAssertTrue(soundLevel(noLevel, calibrated: true)!.isNaN, "the calibrated signal is still there, as NaN")

let uncalibrated = soundLevelCluster(packet: packet, packetIndex: 3, calibrationEnabled: false)
XCTAssertEqual(soundLevel(uncalibrated, calibrated: false), 4941)
XCTAssertNil(soundLevel(uncalibrated, calibrated: true))
}

/// Packets a Shimmer3R streamed with the sound level on, as buildMsg gets them: without the
/// packet type byte. The firmware's own test rig decoded the same levels from the same bytes.
func testSoundLevelFromShimmer3RPackets() {
// 1024 Hz, every sensor: timestamp (3), mag, gyro, low-noise accel (6 each), pressure and
// temperature (3 each), high-g accel, wide-range accel, alt mag (6 each), then the level
// at 45, GSR, battery
let classic = [
"3c49a82e004c0010fdc9ff2f00010097f3e13cb4ef009d5e00848001200040ffe0ccc408f1fcf200000000000000807c0a330b",
"5c49a82b004d0012fdabff2000150077f3f83c85ef009d5e0084800140fff00020b8c400f128f30000000000004d137f0a380b",
]
let classicLevels: [Double] = [-32768, 4941]
// 512 Hz: timestamp (3), mag, gyro, low-noise accel (6 each), then the level at 21, battery
let imu = [
"e3852226004d0019fdbbff310012007ef3e83c8def00801f0b",
"23862226004d0019fdb0ff1f00190073f3f43c8eef54232d0b",
]
let imuLevels: [Double] = [-32768, 9044]

for (hex, want) in zip(classic, classicLevels) {
let packet = bytes(hex)
XCTAssertEqual(packet.count, 51)
XCTAssertEqual(soundLevel(soundLevelCluster(packet: packet, packetIndex: 45), calibrated: false), want)
}
for (hex, want) in zip(imu, imuLevels) {
let packet = bytes(hex)
XCTAssertEqual(packet.count, 25)
XCTAssertEqual(soundLevel(soundLevelCluster(packet: packet, packetIndex: 21), calibrated: false), want)
}
}
}
Loading