Skip to main content

Accelerometer

A 3-axis accelerometer.

About

Orientation

An accelerometer module should translate acceleration values as follows:

OrientationX value (g)Y value (g)Z value (g)
Module lying flat00-1
Module on left edge-100
Module on bottom edge010

We recommend an orientation marking on the PCB so that users can mount modules without having to experiment with the device. Left/bottom can be determined by assuming text on silk runs left-to-right.

import { Accelerometer } from "@devicescript/core"

const accelerometer = new Accelerometer()

Registers

reading

Indicates the current forces acting on accelerometer.

  • type: Register<any[]> (packing format i12.20 i12.20 i12.20)

  • track incoming values

import { Accelerometer } from "@devicescript/core"

const accelerometer = new Accelerometer()
// ...
accelerometer.reading.subscribe(async (value) => {
...
})
note

write and read will block until a server is bound to the client.

readingError

Error on the reading value.

  • type: Register<number> (packing format u12.20)

  • optional: this register may not be implemented

  • read only

import { Accelerometer } from "@devicescript/core"

const accelerometer = new Accelerometer()
// ...
const value = await accelerometer.readingError.read()
  • track incoming values
import { Accelerometer } from "@devicescript/core"

const accelerometer = new Accelerometer()
// ...
accelerometer.readingError.subscribe(async (value) => {
...
})
note

write and read will block until a server is bound to the client.

readingRange

Configures the range forces detected. The value will be "rounded up" to one of max_forces_supported.

  • type: Register<number> (packing format u12.20)

  • optional: this register may not be implemented

  • read and write

import { Accelerometer } from "@devicescript/core"

const accelerometer = new Accelerometer()
// ...
const value = await accelerometer.readingRange.read()
await accelerometer.readingRange.write(value)
  • track incoming values
import { Accelerometer } from "@devicescript/core"

const accelerometer = new Accelerometer()
// ...
accelerometer.readingRange.subscribe(async (value) => {
...
})
note

write and read will block until a server is bound to the client.

supportedRanges

Lists values supported for writing max_force.

  • type: Register<any[]> (packing format r: u12.20)
  • optional: this register may not be implemented
  • constant: the register value will not change (until the next reset)
note

write and read will block until a server is bound to the client.

Events

tiltUp

Emitted when accelerometer is tilted in the given direction.

accelerometer.tiltUp.subscribe(() => {

})

tiltDown

Emitted when accelerometer is tilted in the given direction.

accelerometer.tiltDown.subscribe(() => {

})

tiltLeft

Emitted when accelerometer is tilted in the given direction.

accelerometer.tiltLeft.subscribe(() => {

})

tiltRight

Emitted when accelerometer is tilted in the given direction.

accelerometer.tiltRight.subscribe(() => {

})

faceUp

Emitted when accelerometer is laying flat in the given direction.

accelerometer.faceUp.subscribe(() => {

})

faceDown

Emitted when accelerometer is laying flat in the given direction.

accelerometer.faceDown.subscribe(() => {

})

freefall

Emitted when total force acting on accelerometer is much less than 1g.

accelerometer.freefall.subscribe(() => {

})

shake

Emitted when forces change violently a few times.

accelerometer.shake.subscribe(() => {

})

force2g

Emitted when force in any direction exceeds given threshold.

accelerometer.force2g.subscribe(() => {

})

force3g

Emitted when force in any direction exceeds given threshold.

accelerometer.force3g.subscribe(() => {

})

force6g

Emitted when force in any direction exceeds given threshold.

accelerometer.force6g.subscribe(() => {

})

force8g

Emitted when force in any direction exceeds given threshold.

accelerometer.force8g.subscribe(() => {

})