Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Sensor.cs 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. using System;
  2. using UnityEngine.InputSystem.Controls;
  3. using UnityEngine.InputSystem.Layouts;
  4. using UnityEngine.InputSystem.LowLevel;
  5. using UnityEngine.InputSystem.Utilities;
  6. ////TODO: make the sensors return values through their device
  7. //// (e.g. GravitySensor should itself be an InputControl returning a Vector3 value which is the gravity value)
  8. ////REVIEW: Is there a better way than having all the sensor classes?
  9. namespace UnityEngine.InputSystem.LowLevel
  10. {
  11. internal struct AccelerometerState : IInputStateTypeInfo
  12. {
  13. public static FourCC kFormat => new FourCC('A', 'C', 'C', 'L');
  14. [InputControl(displayName = "Acceleration", processors = "CompensateDirection", noisy = true)]
  15. public Vector3 acceleration;
  16. public FourCC format => kFormat;
  17. }
  18. internal struct GyroscopeState : IInputStateTypeInfo
  19. {
  20. public static FourCC kFormat => new FourCC('G', 'Y', 'R', 'O');
  21. [InputControl(displayName = "Angular Velocity", processors = "CompensateDirection", noisy = true)]
  22. public Vector3 angularVelocity;
  23. public FourCC format => kFormat;
  24. }
  25. internal struct GravityState : IInputStateTypeInfo
  26. {
  27. public static FourCC kFormat => new FourCC('G', 'R', 'V', ' ');
  28. [InputControl(displayName = "Gravity", processors = "CompensateDirection", noisy = true)]
  29. public Vector3 gravity;
  30. public FourCC format => kFormat;
  31. }
  32. internal struct AttitudeState : IInputStateTypeInfo
  33. {
  34. public static FourCC kFormat => new FourCC('A', 'T', 'T', 'D');
  35. [InputControl(displayName = "Attitude", processors = "CompensateRotation", noisy = true)]
  36. public Quaternion attitude;
  37. public FourCC format => kFormat;
  38. }
  39. internal struct LinearAccelerationState : IInputStateTypeInfo
  40. {
  41. public static FourCC kFormat => new FourCC('L', 'A', 'A', 'C');
  42. [InputControl(displayName = "Acceleration", processors = "CompensateDirection", noisy = true)]
  43. public Vector3 acceleration;
  44. public FourCC format => kFormat;
  45. }
  46. }
  47. namespace UnityEngine.InputSystem
  48. {
  49. /// <summary>
  50. /// Base class representing any sensor kind of input device.
  51. /// </summary>
  52. /// <remarks>
  53. /// Sensors represent device environmental sensors, such as <see cref="Accelerometer"/>s, <see cref="Gyroscope"/>s,
  54. /// <see cref="GravitySensor"/>s and others.
  55. ///
  56. /// Unlike other devices, sensor devices usually start out in a disabled state in order to reduce energy
  57. /// consumption (i.e. preserve battery life) when the sensors are not in fact used. To enable a specific sensor,
  58. /// call <see cref="InputSystem.EnableDevice"/> on the device instance.
  59. ///
  60. /// <example>
  61. /// <code>
  62. /// // Enable the gyroscope.
  63. /// InputSystem.EnableDevice(Gyroscope.current);
  64. /// </code>
  65. /// </example>
  66. ///
  67. /// Sensors are usually sampled automatically by the platform at regular intervals. For example, if a sensor
  68. /// is sampled at 50Hz, the platform will queue an event with an update at a rate of roughly 50 events per
  69. /// second. The default sampling rate for a sensor is usually platform-specific. A custom sampling frequency
  70. /// can be set through <see cref="samplingFrequency"/> but be aware that there may be limitations for how fast
  71. /// a given sensor can be sampled.
  72. /// </remarks>
  73. [InputControlLayout(isGenericTypeOfDevice = true)]
  74. public class Sensor : InputDevice
  75. {
  76. /// <summary>
  77. /// The frequency (in Hertz) at which the underlying sensor will be refreshed and at which update
  78. /// events for it will be queued.
  79. /// </summary>
  80. /// <value>Times per second at which the sensor is refreshed.</value>
  81. /// <remarks>
  82. /// Note that when setting sampling frequencies, there may be limits on the range of frequencies
  83. /// supported by the underlying hardware/platform.
  84. ///
  85. /// To support querying sampling frequencies, a sensor device must implement <see cref="QuerySamplingFrequencyCommand"/>.
  86. /// To support setting frequencies, it must implemenet <see cref="SetSamplingFrequencyCommand"/>.
  87. /// </remarks>
  88. /// <exception cref="NotSupportedException">Thrown when reading the property and the underlying
  89. /// sensor does not support querying of sampling frequencies.</exception>
  90. public float samplingFrequency
  91. {
  92. get
  93. {
  94. var command = QuerySamplingFrequencyCommand.Create();
  95. if (ExecuteCommand(ref command) >= 0)
  96. return command.frequency;
  97. throw new NotSupportedException($"Device '{this}' does not support querying sampling frequency");
  98. }
  99. set
  100. {
  101. ////REVIEW: should this throw NotSupportedException, too?
  102. var command = SetSamplingFrequencyCommand.Create(value);
  103. ExecuteCommand(ref command);
  104. }
  105. }
  106. }
  107. /// <summary>
  108. /// Input device representing an accelerometer sensor.
  109. /// </summary>
  110. /// <remarks>
  111. /// An accelerometer let's you measure the acceleration of a device, and can be useful to control content by moving a device around.
  112. /// Note that the accelerometer will report the acceleration measured on a device both due to moving the device around, and due gravity
  113. /// pulling the device down. You can use <see cref="GravitySensor"/> and <see cref="LinearAccelerationSensor"/> to get decoupled values
  114. /// for these.
  115. ///
  116. /// <example>
  117. /// <code>
  118. /// class MyBehavior : MonoBehaviour
  119. /// {
  120. /// protected void OnEnable()
  121. /// {
  122. /// // All sensors start out disabled so they have to manually be enabled first.
  123. /// InputSystem.EnableDevice(Accelerometer.current);
  124. /// }
  125. ///
  126. /// protected void OnDisable()
  127. /// {
  128. /// InputSystem.DisableDevice(Accelerometer.current);
  129. /// }
  130. ///
  131. /// protected void Update()
  132. /// {
  133. /// var acceleration = Accelerometer.current.acceleration.ReadValue();
  134. /// //...
  135. /// }
  136. /// }
  137. /// </code>
  138. /// </example>
  139. /// </remarks>
  140. [InputControlLayout(stateType = typeof(AccelerometerState))]
  141. public class Accelerometer : Sensor
  142. {
  143. public Vector3Control acceleration { get; protected set; }
  144. /// <summary>
  145. /// The accelerometer that was last added or had activity last.
  146. /// </summary>
  147. /// <value>Current accelerometer or <c>null</c>.</value>
  148. public static Accelerometer current { get; private set; }
  149. /// <inheritdoc />
  150. public override void MakeCurrent()
  151. {
  152. base.MakeCurrent();
  153. current = this;
  154. }
  155. /// <inheritdoc />
  156. protected override void OnRemoved()
  157. {
  158. base.OnRemoved();
  159. if (current == this)
  160. current = null;
  161. }
  162. /// <inheritdoc />
  163. protected override void FinishSetup()
  164. {
  165. acceleration = GetChildControl<Vector3Control>("acceleration");
  166. base.FinishSetup();
  167. }
  168. }
  169. /// <summary>
  170. /// Input device representing a gyroscope sensor.
  171. /// </summary>
  172. /// <remarks>
  173. /// A gyroscope let's you measure the angular velocity of a device, and can be useful to control content by rotating a device.
  174. /// </remarks>
  175. [InputControlLayout(stateType = typeof(GyroscopeState))]
  176. public class Gyroscope : Sensor
  177. {
  178. public Vector3Control angularVelocity { get; protected set; }
  179. /// <summary>
  180. /// The gyroscope that was last added or had activity last.
  181. /// </summary>
  182. /// <value>Current gyroscope or <c>null</c>.</value>
  183. public static Gyroscope current { get; private set; }
  184. /// <inheritdoc />
  185. public override void MakeCurrent()
  186. {
  187. base.MakeCurrent();
  188. current = this;
  189. }
  190. /// <inheritdoc />
  191. protected override void OnRemoved()
  192. {
  193. base.OnRemoved();
  194. if (current == this)
  195. current = null;
  196. }
  197. /// <inheritdoc />
  198. protected override void FinishSetup()
  199. {
  200. angularVelocity = GetChildControl<Vector3Control>("angularVelocity");
  201. base.FinishSetup();
  202. }
  203. }
  204. /// <summary>
  205. /// Input device representing a gravity sensor.
  206. /// </summary>
  207. /// <remarks>
  208. /// A gravity sensor let's you determine the direction of the gravity vector relative to a device, and can be useful to control content by device orientation.
  209. /// This is usually derived from a hardware <see cref="Accelerometer"/>, by subtracting the effect of linear acceleration (see <see cref="LinearAccelerationSensor"/>).
  210. /// </remarks>
  211. [InputControlLayout(stateType = typeof(GravityState), displayName = "Gravity")]
  212. public class GravitySensor : Sensor
  213. {
  214. public Vector3Control gravity { get; protected set; }
  215. /// <summary>
  216. /// The gravity sensor that was last added or had activity last.
  217. /// </summary>
  218. /// <value>Current gravity sensor or <c>null</c>.</value>
  219. public static GravitySensor current { get; private set; }
  220. /// <inheritdoc />
  221. protected override void FinishSetup()
  222. {
  223. gravity = GetChildControl<Vector3Control>("gravity");
  224. base.FinishSetup();
  225. }
  226. /// <inheritdoc />
  227. public override void MakeCurrent()
  228. {
  229. base.MakeCurrent();
  230. current = this;
  231. }
  232. /// <inheritdoc />
  233. protected override void OnRemoved()
  234. {
  235. base.OnRemoved();
  236. if (current == this)
  237. current = null;
  238. }
  239. }
  240. //// REVIEW: Is this name good enough, possible other name RotationVector, here's how Android docs describe it. "A rotation vector sensor reports the orientation of the device relative to the East-North-Up coordinates frame."
  241. //// This is the same as https://docs.unity3d.com/ScriptReference/Gyroscope-attitude.html
  242. /// <summary>
  243. /// Input device representing an attitude sensor.
  244. /// </summary>
  245. /// <remarks>
  246. /// An attitude sensor let's you determine the orientation of a device, and can be useful to control content by rotating a device.
  247. /// </remarks>
  248. [InputControlLayout(stateType = typeof(AttitudeState), displayName = "Attitude")]
  249. public class AttitudeSensor : Sensor
  250. {
  251. public QuaternionControl attitude { get; protected set; }
  252. /// <summary>
  253. /// The attitude sensor that was last added or had activity last.
  254. /// </summary>
  255. /// <value>Current attitude sensor or <c>null</c>.</value>
  256. public static AttitudeSensor current { get; private set; }
  257. /// <inheritdoc />
  258. public override void MakeCurrent()
  259. {
  260. base.MakeCurrent();
  261. current = this;
  262. }
  263. /// <inheritdoc />
  264. protected override void OnRemoved()
  265. {
  266. base.OnRemoved();
  267. if (current == this)
  268. current = null;
  269. }
  270. /// <inheritdoc />
  271. protected override void FinishSetup()
  272. {
  273. attitude = GetChildControl<QuaternionControl>("attitude");
  274. base.FinishSetup();
  275. }
  276. }
  277. /// <summary>
  278. /// Input device representing linear acceleration affecting the device playing the content.
  279. /// </summary>
  280. /// <remarks>
  281. /// An accelerometer let's you measure the acceleration of a device, and can be useful to control content by moving a device around.
  282. /// Linear acceleration is the acceleration of a device unaffected by gravity forces.
  283. /// This is usually derived from a hardware <see cref="Accelerometer"/>, by subtracting the effect of gravity (see <see cref="GravitySensor"/>).
  284. /// </remarks>
  285. [InputControlLayout(stateType = typeof(LinearAccelerationState), displayName = "Linear Acceleration")]
  286. public class LinearAccelerationSensor : Sensor
  287. {
  288. public Vector3Control acceleration { get; protected set; }
  289. /// <summary>
  290. /// The linear acceleration sensor that was last added or had activity last.
  291. /// </summary>
  292. /// <value>Current linear acceleration sensor or <c>null</c>.</value>
  293. public static LinearAccelerationSensor current { get; private set; }
  294. /// <inheritdoc />
  295. public override void MakeCurrent()
  296. {
  297. base.MakeCurrent();
  298. current = this;
  299. }
  300. /// <inheritdoc />
  301. protected override void OnRemoved()
  302. {
  303. base.OnRemoved();
  304. if (current == this)
  305. current = null;
  306. }
  307. /// <inheritdoc />
  308. protected override void FinishSetup()
  309. {
  310. acceleration = GetChildControl<Vector3Control>("acceleration");
  311. base.FinishSetup();
  312. }
  313. }
  314. /// <summary>
  315. /// Input device representing the magnetic field affecting the device playing the content.
  316. /// </summary>
  317. [InputControlLayout(displayName = "Magnetic Field")]
  318. public class MagneticFieldSensor : Sensor
  319. {
  320. /// <summary>
  321. /// Strength of the magnetic field reported by the sensor.
  322. /// </summary>
  323. /// <value>Control representing the strength of the magnetic field.</value>
  324. /// <remarks>
  325. /// Values are in micro-Tesla (uT) and measure the ambient magnetic field in the X, Y and Z axis.
  326. /// </remarks>
  327. [InputControl(displayName = "Magnetic Field", noisy = true)]
  328. public Vector3Control magneticField { get; protected set; }
  329. /// <summary>
  330. /// The linear acceleration sensor that was last added or had activity last.
  331. /// </summary>
  332. /// <value>Current linear acceleration sensor or <c>null</c>.</value>
  333. public static MagneticFieldSensor current { get; private set; }
  334. /// <inheritdoc />
  335. public override void MakeCurrent()
  336. {
  337. base.MakeCurrent();
  338. current = this;
  339. }
  340. /// <inheritdoc />
  341. protected override void OnRemoved()
  342. {
  343. base.OnRemoved();
  344. if (current == this)
  345. current = null;
  346. }
  347. /// <inheritdoc />
  348. protected override void FinishSetup()
  349. {
  350. magneticField = GetChildControl<Vector3Control>("magneticField");
  351. base.FinishSetup();
  352. }
  353. }
  354. /// <summary>
  355. /// Input device representing the ambient light measured by the device playing the content.
  356. /// </summary>
  357. [InputControlLayout(displayName = "Light")]
  358. public class LightSensor : Sensor
  359. {
  360. /// <summary>
  361. /// Light level in SI lux units.
  362. /// </summary>
  363. [InputControl(displayName = "Light Level", noisy = true)]
  364. public AxisControl lightLevel { get; protected set; }
  365. /// <summary>
  366. /// The light sensor that was last added or had activity last.
  367. /// </summary>
  368. /// <value>Current light sensor or <c>null</c>.</value>
  369. public static LightSensor current { get; private set; }
  370. /// <inheritdoc />
  371. public override void MakeCurrent()
  372. {
  373. base.MakeCurrent();
  374. current = this;
  375. }
  376. /// <inheritdoc />
  377. protected override void OnRemoved()
  378. {
  379. base.OnRemoved();
  380. if (current == this)
  381. current = null;
  382. }
  383. /// <inheritdoc />
  384. protected override void FinishSetup()
  385. {
  386. lightLevel = GetChildControl<AxisControl>("lightLevel");
  387. base.FinishSetup();
  388. }
  389. }
  390. /// <summary>
  391. /// Input device representing the atmospheric pressure measured by the device playing the content.
  392. /// </summary>
  393. [InputControlLayout(displayName = "Pressure")]
  394. public class PressureSensor : Sensor
  395. {
  396. /// <summary>
  397. /// Atmospheric pressure in hPa (millibar).
  398. /// </summary>
  399. [InputControl(displayName = "Atmospheric Pressure", noisy = true)]
  400. public AxisControl atmosphericPressure { get; protected set; }
  401. /// <summary>
  402. /// The pressure sensor that was last added or had activity last.
  403. /// </summary>
  404. /// <value>Current pressure sensor or <c>null</c>.</value>
  405. public static PressureSensor current { get; private set; }
  406. /// <inheritdoc />
  407. public override void MakeCurrent()
  408. {
  409. base.MakeCurrent();
  410. current = this;
  411. }
  412. /// <inheritdoc />
  413. protected override void OnRemoved()
  414. {
  415. base.OnRemoved();
  416. if (current == this)
  417. current = null;
  418. }
  419. /// <inheritdoc />
  420. protected override void FinishSetup()
  421. {
  422. atmosphericPressure = GetChildControl<AxisControl>("atmosphericPressure");
  423. base.FinishSetup();
  424. }
  425. }
  426. /// <summary>
  427. /// Input device representing the proximity of the device playing the content to the user.
  428. /// </summary>
  429. /// <remarks>
  430. /// The proximity sensor is usually used by phones to determine if the user is holding the phone to their ear or not.
  431. /// </remarks>
  432. [InputControlLayout(displayName = "Proximity")]
  433. public class ProximitySensor : Sensor
  434. {
  435. /// <summary>
  436. /// Proximity sensor distance measured in centimeters.
  437. /// </summary>
  438. [InputControl(displayName = "Distance", noisy = true)]
  439. public AxisControl distance { get; protected set; }
  440. /// <summary>
  441. /// The proximity sensor that was last added or had activity last.
  442. /// </summary>
  443. /// <value>Current proximity sensor or <c>null</c>.</value>
  444. public static ProximitySensor current { get; private set; }
  445. /// <inheritdoc />
  446. public override void MakeCurrent()
  447. {
  448. base.MakeCurrent();
  449. current = this;
  450. }
  451. /// <inheritdoc />
  452. protected override void OnRemoved()
  453. {
  454. base.OnRemoved();
  455. if (current == this)
  456. current = null;
  457. }
  458. /// <inheritdoc />
  459. protected override void FinishSetup()
  460. {
  461. distance = GetChildControl<AxisControl>("distance");
  462. base.FinishSetup();
  463. }
  464. }
  465. /// <summary>
  466. /// Input device representing the ambient air humidity measured by the device playing the content.
  467. /// </summary>
  468. [InputControlLayout(displayName = "Humidity")]
  469. public class HumiditySensor : Sensor
  470. {
  471. /// <summary>
  472. /// Relative ambient air humidity in percent.
  473. /// </summary>
  474. [InputControl(displayName = "Relative Humidity", noisy = true)]
  475. public AxisControl relativeHumidity { get; protected set; }
  476. /// <summary>
  477. /// The humidity sensor that was last added or had activity last.
  478. /// </summary>
  479. /// <value>Current humidity sensor or <c>null</c>.</value>
  480. public static HumiditySensor current { get; private set; }
  481. /// <inheritdoc />
  482. public override void MakeCurrent()
  483. {
  484. base.MakeCurrent();
  485. current = this;
  486. }
  487. /// <inheritdoc />
  488. protected override void OnRemoved()
  489. {
  490. base.OnRemoved();
  491. if (current == this)
  492. current = null;
  493. }
  494. /// <inheritdoc />
  495. protected override void FinishSetup()
  496. {
  497. relativeHumidity = GetChildControl<AxisControl>("relativeHumidity");
  498. base.FinishSetup();
  499. }
  500. }
  501. /// <summary>
  502. /// Input device representing the ambient air temperature measured by the device playing the content.
  503. /// </summary>
  504. [InputControlLayout(displayName = "Ambient Temperature")]
  505. public class AmbientTemperatureSensor : Sensor
  506. {
  507. /// <summary>
  508. /// Temperature in degree Celsius.
  509. /// </summary>
  510. [InputControl(displayName = "Ambient Temperature", noisy = true)]
  511. public AxisControl ambientTemperature { get; protected set; }
  512. /// <summary>
  513. /// The ambient temperature sensor that was last added or had activity last.
  514. /// </summary>
  515. /// <value>Current ambient temperature sensor or <c>null</c>.</value>
  516. public static AmbientTemperatureSensor current { get; private set; }
  517. /// <inheritdoc />
  518. public override void MakeCurrent()
  519. {
  520. base.MakeCurrent();
  521. current = this;
  522. }
  523. /// <inheritdoc />
  524. protected override void OnRemoved()
  525. {
  526. base.OnRemoved();
  527. if (current == this)
  528. current = null;
  529. }
  530. /// <inheritdoc />
  531. protected override void FinishSetup()
  532. {
  533. ambientTemperature = GetChildControl<AxisControl>("ambientTemperature");
  534. base.FinishSetup();
  535. }
  536. }
  537. /// <summary>
  538. /// Input device representing the foot steps taken by the user as measured by the device playing the content.
  539. /// </summary>
  540. /// <remarks>
  541. /// On iOS, access to the step counter must be enabled via <see cref="InputSettings.iOSSettings.motionUsage"/>.
  542. /// </remarks>
  543. [InputControlLayout(displayName = "Step Counter")]
  544. public class StepCounter : Sensor
  545. {
  546. /// <summary>
  547. /// The number of steps taken by the user since the last reboot while activated.
  548. /// </summary>
  549. [InputControl(displayName = "Step Counter", noisy = true)]
  550. public IntegerControl stepCounter { get; protected set; }
  551. /// <summary>
  552. /// The step counter that was last added or had activity last.
  553. /// </summary>
  554. /// <value>Current step counter or <c>null</c>.</value>
  555. public static StepCounter current { get; private set; }
  556. /// <inheritdoc />
  557. public override void MakeCurrent()
  558. {
  559. base.MakeCurrent();
  560. current = this;
  561. }
  562. /// <inheritdoc />
  563. protected override void OnRemoved()
  564. {
  565. base.OnRemoved();
  566. if (current == this)
  567. current = null;
  568. }
  569. /// <inheritdoc />
  570. protected override void FinishSetup()
  571. {
  572. stepCounter = GetChildControl<IntegerControl>("stepCounter");
  573. base.FinishSetup();
  574. }
  575. }
  576. }