sensors

package
v1.17.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: BSD-3-Clause Imports: 8 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package sensors gives an app the motion sensors of the device (SPEC §28.4): the accelerometer and the gyroscope of a phone or of a browser on one, and the sensors of a gamepad (package gamepad). Flutter has no such package in its framework (the sensors_plus plugin is the closest); the names here are Glutter's.

A Sensor is opened with System.Open, keeps its latest reading, and calls its listeners with the readings that arrived since the last frame, once a frame, on the UI thread. The axes are SDL's: x to the right, y up and z out of the screen, for a device in its natural orientation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Feed

type Feed struct {
	// contains filtered or unexported fields
}

Feed gives readings to a Sensor that the platform's sensors service does not make: the sensors of a gamepad (package gamepad) are made this way. An app that wants a Sensor of its own (a recording that it plays back, a test) may make one too.

Flutter: no counterpart

func NewFeed

func NewFeed(b *widgets.WidgetsBinding, kind Kind, rate float64, onClose func() error) *Feed

NewFeed makes a Feed whose Sensor is of the kind, with the rate that the source makes (0 when it does not say). onClose, which may be nil, runs when the sensor is closed.

Flutter: no counterpart

func (*Feed) Put

func (f *Feed) Put(readings ...Reading)

Put gives readings to the sensor, from any goroutine. A closed sensor gets nothing.

Flutter: no counterpart

func (*Feed) Sensor

func (f *Feed) Sensor() *Sensor

Sensor is the sensor that the feed's readings reach.

Flutter: no counterpart

type Kind

type Kind uint8

Kind is the kind of a motion sensor.

Flutter: no counterpart

const (
	// Accelerometer measures the acceleration of the device with gravity included, in m/s². A device that lies flat on a table,
	// face up, reads about 9.8 on z.
	//
	// Flutter: no counterpart
	Accelerometer Kind = iota
	// Gyroscope measures the rate of rotation about the three axes, in rad/s, counterclockwise as seen from the positive end of
	// an axis.
	//
	// Flutter: no counterpart
	Gyroscope
)

func (Kind) String

func (k Kind) String() string

String returns "accelerometer" or "gyroscope".

Flutter: no counterpart

type Opts

type Opts struct {
	// Rate is the number of readings a second that the app would like. 0 leaves it to the platform (60 a second, or the fastest
	// that the sensor can do if that is slower); a browser sends events at its own rate whatever is asked.
	Rate float64
}

Opts are the options of Open.

Flutter: no counterpart

type Reading

type Reading struct {
	// Timestamp is when the reading was made, on a monotonic clock whose start is not defined: only the difference of two
	// readings of the same sensor means something.
	Timestamp time.Duration
	// Values are x, y and z, in the unit of the kind (m/s² or rad/s).
	Values [3]float64
}

Reading is one reading of a sensor.

Flutter: no counterpart

type Sensor

type Sensor struct {
	// contains filtered or unexported fields
}

Sensor is an open sensor. It is a platform.SensorHandler: the backend calls it on its own thread.

Flutter: no counterpart

func (*Sensor) AddListener

func (s *Sensor) AddListener(fn func(readings []Reading)) (remove func())

AddListener adds a function that is called, on the UI thread, once a frame, with the readings that arrived since the last call, oldest first. The slice is valid only during the call. It returns the function that removes the listener. A sensor with no listener schedules no frames.

Flutter: no counterpart

func (*Sensor) Close

func (s *Sensor) Close() error

Close ends the sensor: no listener is called after it returns. It may be called more than once.

Flutter: no counterpart

func (*Sensor) Dropped

func (s *Sensor) Dropped() int

Dropped is how many readings were thrown away because no frame ran to deliver them (see maxPending).

Flutter: no counterpart

func (*Sensor) Kind

func (s *Sensor) Kind() Kind

Kind says what the sensor measures.

Flutter: no counterpart

func (*Sensor) Latest

func (s *Sensor) Latest() (Reading, bool)

Latest returns the last reading and whether there is one yet. It may be called from any goroutine.

Flutter: no counterpart

func (*Sensor) Rate

func (s *Sensor) Rate() float64

Rate is the number of readings a second that the sensor was set to, or 0 when the platform does not say (a browser).

Flutter: no counterpart

func (*Sensor) SensorReadings

func (s *Sensor) SensorReadings(in []platform.SensorReading)

SensorReadings implements platform.SensorHandler. It runs on the backend's thread: it keeps the readings and asks the UI thread for a frame to deliver them in.

type System

type System struct {
	// contains filtered or unexported fields
}

System is the motion sensors of the app. Get it with Of. It is made when it is first asked for, and closed with the app.

Flutter: no counterpart

func ForBinding

func ForBinding(b *widgets.WidgetsBinding) *System

ForBinding returns the sensors of the binding, for code that has no BuildContext.

Flutter: no counterpart

func Of

func Of(bc widgets.BuildContext) *System

Of returns the sensors of the app (the ones of the binding). An app on a platform that has no sensors service gets a System whose calls fail with SensorUnavailable.

Flutter: no counterpart

func (*System) Available

func (s *System) Available() bool

Available says whether the platform has a sensors service. It does not say that the device has the sensor that the app wants: Has does, and Open fails when it has not.

Flutter: no counterpart

func (*System) Close

func (s *System) Close()

Close closes every sensor that the app left open. The engine does it when the app ends.

Flutter: no counterpart

func (*System) Has

func (s *System) Has(kind Kind) bool

Has says whether the device has a sensor of the kind. A browser cannot know before it sends the first reading, and answers yes where it has the events.

Flutter: no counterpart

func (*System) Open

func (s *System) Open(ctx context.Context, kind Kind, opts ...Opts) (*Sensor, error)

Open opens a sensor and starts it. It fails with SensorUnavailable when the platform has no sensors service or the device has no such sensor, SensorPermissionDenied when the user refuses it, and SensorOpenFailed for the rest (also when ctx ends first). Where the system asks the user (a browser on some systems) the call waits for the answer, until ctx ends: call it from a goroutine that is not the UI one. At most one Opts may be given.

Flutter: no counterpart

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL