|
| 1 | +// |
| 2 | +// SensorType.swift |
| 3 | +// SwiftAndroid |
| 4 | +// |
| 5 | +// Created by Alsey Coleman Miller on 7/6/25. |
| 6 | +// |
| 7 | + |
| 8 | +/// Android sensor type identifier. |
| 9 | +public struct SensorType: RawRepresentable, Equatable, Hashable, Sendable { |
| 10 | + |
| 11 | + public let rawValue: Int32 |
| 12 | + |
| 13 | + public init(rawValue: Int32) { |
| 14 | + self.rawValue = rawValue |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +// MARK: - Constants |
| 19 | + |
| 20 | +public extension SensorType { |
| 21 | + |
| 22 | + /// Measures the acceleration force in m/s² along x, y, z axes including gravity. |
| 23 | + static var accelerometer: Self { .init(rawValue: 1) } |
| 24 | + |
| 25 | + /// Measures the ambient geomagnetic field in μT for all three axes. |
| 26 | + static var magneticField: Self { .init(rawValue: 2) } |
| 27 | + |
| 28 | + /// Measures the orientation of the device in degrees around all three axes. |
| 29 | + static var orientation: Self { .init(rawValue: 3) } |
| 30 | + |
| 31 | + /// Measures the rate of rotation around x, y, z axes in rad/s. |
| 32 | + static var gyroscope: Self { .init(rawValue: 4) } |
| 33 | + |
| 34 | + /// Measures the ambient light level (illuminance) in lx. |
| 35 | + static var light: Self { .init(rawValue: 5) } |
| 36 | + |
| 37 | + /// Measures the ambient air pressure in hPa or mbar. |
| 38 | + static var pressure: Self { .init(rawValue: 6) } |
| 39 | + |
| 40 | + /// Measures the proximity of an object in cm relative to the viewing screen. |
| 41 | + static var proximity: Self { .init(rawValue: 8) } |
| 42 | + |
| 43 | + /// Measures the force of gravity in m/s² along x, y, z axes. |
| 44 | + static var gravity: Self { .init(rawValue: 9) } |
| 45 | + |
| 46 | + /// Measures the acceleration force in m/s² along x, y, z axes, excluding gravity. |
| 47 | + static var linearAcceleration: Self { .init(rawValue: 10) } |
| 48 | + |
| 49 | + /// Measures the orientation of the device as a combination of angle and axis. |
| 50 | + static var rotationVector: Self { .init(rawValue: 11) } |
| 51 | + |
| 52 | + /// Measures the relative ambient humidity in percent. |
| 53 | + static var relativeHumidity: Self { .init(rawValue: 12) } |
| 54 | + |
| 55 | + /// Measures the ambient air temperature in °C. |
| 56 | + static var ambientTemperature: Self { .init(rawValue: 13) } |
| 57 | + |
| 58 | + /// Measures the geomagnetic field for all three axes without hard iron calibration. |
| 59 | + static var magneticFieldUncalibrated: Self { .init(rawValue: 14) } |
| 60 | + |
| 61 | + /// Measures the rotation vector without the geomagnetic field component. |
| 62 | + static var gameRotationVector: Self { .init(rawValue: 15) } |
| 63 | + |
| 64 | + /// Measures the rate of rotation around each axis without drift compensation. |
| 65 | + static var gyroscopeUncalibrated: Self { .init(rawValue: 16) } |
| 66 | + |
| 67 | + /// Triggers an event each time a significant motion is detected. |
| 68 | + static var significantMotion: Self { .init(rawValue: 17) } |
| 69 | + |
| 70 | + /// Triggers an event each time a step is detected. |
| 71 | + static var stepDetector: Self { .init(rawValue: 18) } |
| 72 | + |
| 73 | + /// Reports the cumulative number of steps taken since the last reboot. |
| 74 | + static var stepCounter: Self { .init(rawValue: 19) } |
| 75 | + |
| 76 | + /// Measures the rotation vector based on the geomagnetic field and accelerometer. |
| 77 | + static var geomagneticRotationVector: Self { .init(rawValue: 20) } |
| 78 | + |
| 79 | + /// Measures the heart rate in beats per minute. |
| 80 | + static var heartRate: Self { .init(rawValue: 21) } |
| 81 | + |
| 82 | + /// Measures the pose of the device (rotation + translation) as a 6DoF value. |
| 83 | + static var pose6DOF: Self { .init(rawValue: 28) } |
| 84 | + |
| 85 | + /// Triggers an event when the device is stationary. |
| 86 | + static var stationaryDetect: Self { .init(rawValue: 29) } |
| 87 | + |
| 88 | + /// Triggers an event when the device starts moving. |
| 89 | + static var motionDetect: Self { .init(rawValue: 30) } |
| 90 | + |
| 91 | + /// Triggers an event each heartbeat. |
| 92 | + static var heartBeat: Self { .init(rawValue: 31) } |
| 93 | + |
| 94 | + /// Reports newly connected or disconnected dynamic sensors. |
| 95 | + static var dynamicSensorMeta: Self { .init(rawValue: 32) } |
| 96 | + |
| 97 | + /// Reports additional sensor information. |
| 98 | + static var additionalInfo: Self { .init(rawValue: 33) } |
| 99 | + |
| 100 | + /// Reports when the device transitions between on-body and off-body. |
| 101 | + static var lowLatencyOffbodyDetect: Self { .init(rawValue: 34) } |
| 102 | + |
| 103 | + /// Measures acceleration on all three axes without calibration. |
| 104 | + static var accelerometerUncalibrated: Self { .init(rawValue: 35) } |
| 105 | + |
| 106 | + /// Measures the hinge angle between two integral parts of the device. |
| 107 | + static var hingeAngle: Self { .init(rawValue: 36) } |
| 108 | + |
| 109 | + /// Tracks head orientation and motion. |
| 110 | + static var headTracker: Self { .init(rawValue: 37) } |
| 111 | + |
| 112 | + /// Measures acceleration along a subset of axes. |
| 113 | + static var accelerationLimitedAxes: Self { .init(rawValue: 38) } |
| 114 | + |
| 115 | + /// Measures rotation along a subset of axes. |
| 116 | + static var gyroscopeLimitedAxes: Self { .init(rawValue: 39) } |
| 117 | + |
| 118 | + /// Measures acceleration along a subset of axes without calibration. |
| 119 | + static var accelerationLimitedAxesUncalibrated: Self { .init(rawValue: 40) } |
| 120 | + |
| 121 | + /// Measures rotation along a subset of axes without calibration. |
| 122 | + static var gyroscopeLimitedAxesUncalibrated: Self { .init(rawValue: 41) } |
| 123 | + |
| 124 | + /// Measures the user's heading in degrees relative to magnetic north. |
| 125 | + static var heading: Self { .init(rawValue: 42) } |
| 126 | +} |
| 127 | + |
| 128 | +// MARK: - Supporting Types |
| 129 | + |
| 130 | +/// The accuracy of a sensor measurement. |
| 131 | +public enum SensorAccuracy: Int32, Sendable { |
| 132 | + |
| 133 | + /// The values returned are unreliable. |
| 134 | + case minimum = 0 |
| 135 | + |
| 136 | + /// The sensor has low accuracy. |
| 137 | + case low = 1 |
| 138 | + |
| 139 | + /// The sensor has medium accuracy. |
| 140 | + case medium = 2 |
| 141 | + |
| 142 | + /// The sensor has maximum accuracy. |
| 143 | + case high = 3 |
| 144 | +} |
| 145 | + |
| 146 | +/// Describes how a sensor reports data. |
| 147 | +public enum SensorReportingMode: Int32, Sendable { |
| 148 | + |
| 149 | + /// Reports data continuously at a requested rate. |
| 150 | + case continuous = 0 |
| 151 | + |
| 152 | + /// Reports data only when values change. |
| 153 | + case onChange = 1 |
| 154 | + |
| 155 | + /// Reports a single data sample and then disables itself. |
| 156 | + case oneShot = 2 |
| 157 | + |
| 158 | + /// Reports data in a mode specific to the sensor type. |
| 159 | + case special = 3 |
| 160 | +} |
| 161 | + |
| 162 | +/// Standard sensor sampling delays. |
| 163 | +public enum SensorDelay { |
| 164 | + |
| 165 | + /// Rate suited for the user interface (200ms). |
| 166 | + public static var normal: Int32 { 200_000 } |
| 167 | + |
| 168 | + /// Rate suited for UI (60ms). |
| 169 | + public static var ui: Int32 { 60_000 } |
| 170 | + |
| 171 | + /// Rate suited for games (20ms). |
| 172 | + public static var game: Int32 { 20_000 } |
| 173 | + |
| 174 | + /// Fastest rate supported by the sensor. |
| 175 | + public static var fastest: Int32 { 0 } |
| 176 | +} |
0 commit comments