sensor.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package sensor provides sensor events from various movement sensors.
  5. package sensor
  6. import (
  7. "errors"
  8. "sync"
  9. "time"
  10. )
  11. // Type represents a sensor type.
  12. type Type int
  13. var sensorNames = map[Type]string{
  14. Accelerometer: "Accelerometer",
  15. Gyroscope: "Gyroscope",
  16. Magnetometer: "Magnetometer",
  17. }
  18. // String returns the string representation of the sensor type.
  19. func (t Type) String() string {
  20. if n, ok := sensorNames[t]; ok {
  21. return n
  22. }
  23. return "Unknown sensor"
  24. }
  25. const (
  26. Accelerometer = Type(0)
  27. Gyroscope = Type(1)
  28. Magnetometer = Type(2)
  29. nTypes = Type(3)
  30. )
  31. // Event represents a sensor event.
  32. type Event struct {
  33. // Sensor is the type of the sensor the event is coming from.
  34. Sensor Type
  35. // Timestamp is a device specific event time in nanoseconds.
  36. // Timestamps are not Unix times, they represent a time that is
  37. // only valid for the device's default sensor.
  38. Timestamp int64
  39. // Data is the event data.
  40. //
  41. // If the event source is Accelerometer,
  42. // - Data[0]: acceleration force in x axis in m/s^2
  43. // - Data[1]: acceleration force in y axis in m/s^2
  44. // - Data[2]: acceleration force in z axis in m/s^2
  45. //
  46. // If the event source is Gyroscope,
  47. // - Data[0]: rate of rotation around the x axis in rad/s
  48. // - Data[1]: rate of rotation around the y axis in rad/s
  49. // - Data[2]: rate of rotation around the z axis in rad/s
  50. //
  51. // If the event source is Magnetometer,
  52. // - Data[0]: force of gravity along the x axis in m/s^2
  53. // - Data[1]: force of gravity along the y axis in m/s^2
  54. // - Data[2]: force of gravity along the z axis in m/s^2
  55. //
  56. Data []float64
  57. }
  58. // TODO(jbd): Move Sender interface definition to a top-level package.
  59. var (
  60. // senderMu protects sender.
  61. senderMu sync.Mutex
  62. // sender is notified with the sensor data each time a new event is available.
  63. sender Sender
  64. )
  65. // Sender sends an event.
  66. type Sender interface {
  67. Send(event interface{})
  68. }
  69. // Notify registers a Sender and sensor events will be sent to s.
  70. // A typical example of Sender implementations is app.App.
  71. // Once you call Notify, you are not allowed to call it again.
  72. // You cannot call Notify with a nil Sender.
  73. func Notify(s Sender) {
  74. senderMu.Lock()
  75. defer senderMu.Unlock()
  76. if s == nil {
  77. panic("sensor: cannot set a nil sender")
  78. }
  79. if sender != nil {
  80. panic("sensor: another sender is being notified, cannot set s as the sender")
  81. }
  82. sender = s
  83. }
  84. // Enable enables the specified sensor type with the given delay rate.
  85. // Users must set a non-nil Sender via Notify before enabling a sensor,
  86. // otherwise an error will be returned.
  87. func Enable(t Type, delay time.Duration) error {
  88. if t < 0 || int(t) >= len(sensorNames) {
  89. return errors.New("sensor: unknown sensor type")
  90. }
  91. if err := validSender(); err != nil {
  92. return err
  93. }
  94. return enable(t, delay)
  95. }
  96. // Disable disables to feed the manager with the specified sensor.
  97. // Disable is not safe for concurrent use.
  98. func Disable(t Type) error {
  99. if t < 0 || int(t) >= len(sensorNames) {
  100. return errors.New("sensor: unknown sensor type")
  101. }
  102. return disable(t)
  103. }
  104. func validSender() error {
  105. senderMu.Lock()
  106. defer senderMu.Unlock()
  107. if sender == nil {
  108. return errors.New("sensor: no senders to be notified; cannot enable the sensor")
  109. }
  110. return nil
  111. }