Skip to content

Commit 90f470a

Browse files
committed
Add SensorEvent
1 parent 58fb56a commit 90f470a

1 file changed

Lines changed: 114 additions & 0 deletions

File tree

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
//
2+
// SensorEvent.swift
3+
// SwiftAndroid
4+
//
5+
// Created by Alsey Coleman Miller on 7/6/25.
6+
//
7+
8+
/// A sensor event containing a measurement from a hardware sensor.
9+
public struct SensorEvent: Sendable {
10+
11+
private let raw: ASensorEvent
12+
13+
internal init(_ raw: ASensorEvent) {
14+
self.raw = raw
15+
}
16+
}
17+
18+
// MARK: - Properties
19+
20+
public extension SensorEvent {
21+
22+
/// The sensor identifier (matches `Sensor.handle`).
23+
var sensor: Int32 { raw.sensor }
24+
25+
/// The sensor type.
26+
var type: SensorType { SensorType(rawValue: raw.type) }
27+
28+
/// The time at which the event occurred, in nanoseconds since boot.
29+
var timestamp: Int64 { raw.timestamp }
30+
31+
/// Raw floating-point data values (up to 16 floats).
32+
///
33+
/// The layout depends on `type`. For most sensors the relevant values
34+
/// are in the first 3 elements (x, y, z).
35+
var data: [Float] {
36+
withUnsafeBytes(of: raw._data) { bytes in
37+
Array(bytes.bindMemory(to: Float.self))
38+
}
39+
}
40+
}
41+
42+
// MARK: - Convenience Accessors
43+
44+
public extension SensorEvent {
45+
46+
/// Acceleration vector in m/s² (x, y, z) — valid for `.accelerometer`,
47+
/// `.linearAcceleration`, and `.gravity` events.
48+
var acceleration: (x: Float, y: Float, z: Float) {
49+
withUnsafeBytes(of: raw._data) { bytes in
50+
let f = bytes.bindMemory(to: Float.self)
51+
return (f[0], f[1], f[2])
52+
}
53+
}
54+
55+
/// Rotation rate in rad/s (x, y, z) — valid for `.gyroscope` events.
56+
var angularVelocity: (x: Float, y: Float, z: Float) {
57+
withUnsafeBytes(of: raw._data) { bytes in
58+
let f = bytes.bindMemory(to: Float.self)
59+
return (f[0], f[1], f[2])
60+
}
61+
}
62+
63+
/// Magnetic field in μT (x, y, z) — valid for `.magneticField` events.
64+
var magneticField: (x: Float, y: Float, z: Float) {
65+
withUnsafeBytes(of: raw._data) { bytes in
66+
let f = bytes.bindMemory(to: Float.self)
67+
return (f[0], f[1], f[2])
68+
}
69+
}
70+
71+
/// Rotation vector (x, y, z, w) — valid for `.rotationVector` and
72+
/// related events.
73+
var rotationVector: (x: Float, y: Float, z: Float, w: Float) {
74+
withUnsafeBytes(of: raw._data) { bytes in
75+
let f = bytes.bindMemory(to: Float.self)
76+
return (f[0], f[1], f[2], f[3])
77+
}
78+
}
79+
80+
/// Illuminance in lx — valid for `.light` events.
81+
var light: Float {
82+
withUnsafeBytes(of: raw._data) { $0.bindMemory(to: Float.self)[0] }
83+
}
84+
85+
/// Distance in cm — valid for `.proximity` events.
86+
var distance: Float {
87+
withUnsafeBytes(of: raw._data) { $0.bindMemory(to: Float.self)[0] }
88+
}
89+
90+
/// Temperature in °C — valid for `.ambientTemperature` events.
91+
var temperature: Float {
92+
withUnsafeBytes(of: raw._data) { $0.bindMemory(to: Float.self)[0] }
93+
}
94+
95+
/// Pressure in hPa — valid for `.pressure` events.
96+
var pressure: Float {
97+
withUnsafeBytes(of: raw._data) { $0.bindMemory(to: Float.self)[0] }
98+
}
99+
100+
/// Relative humidity as a percentage — valid for `.relativeHumidity` events.
101+
var relativeHumidity: Float {
102+
withUnsafeBytes(of: raw._data) { $0.bindMemory(to: Float.self)[0] }
103+
}
104+
105+
/// Cumulative step count since last reboot — valid for `.stepCounter` events.
106+
var stepCount: UInt64 {
107+
withUnsafeBytes(of: raw._data) { $0.bindMemory(to: UInt64.self)[0] }
108+
}
109+
110+
/// Hinge angle in degrees — valid for `.hingeAngle` events.
111+
var hingeAngle: Float {
112+
withUnsafeBytes(of: raw._data) { $0.bindMemory(to: Float.self)[0] }
113+
}
114+
}

0 commit comments

Comments
 (0)