Skip to main content

MagneticFieldLevel

A sensor that measures strength and polarity of magnetic field.

import { MagneticFieldLevel } from "@devicescript/core"

const magneticFieldLevel = new MagneticFieldLevel()

Registers

reading

Indicates the strength of magnetic field between -1 and 1. When no magnet is present the value should be around 0. For analog sensors, when the north pole of the magnet is on top of the module and closer than south pole, then the value should be positive. For digital sensors, the value should either 0 or 1, regardless of polarity.

  • type: Register<number> (packing format i1.15)

  • read only

import { MagneticFieldLevel } from "@devicescript/core"

const magneticFieldLevel = new MagneticFieldLevel()
// ...
const value = await magneticFieldLevel.reading.read()
  • track incoming values
import { MagneticFieldLevel } from "@devicescript/core"

const magneticFieldLevel = new MagneticFieldLevel()
// ...
magneticFieldLevel.reading.subscribe(async (value) => {
...
})
note

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

detected

Determines if the magnetic field is present. If the event active is observed, detected is true; if inactive is observed, detected is false.

  • type: Register<boolean> (packing format u8)

  • read only

import { MagneticFieldLevel } from "@devicescript/core"

const magneticFieldLevel = new MagneticFieldLevel()
// ...
const value = await magneticFieldLevel.detected.read()
  • track incoming values
import { MagneticFieldLevel } from "@devicescript/core"

const magneticFieldLevel = new MagneticFieldLevel()
// ...
magneticFieldLevel.detected.subscribe(async (value) => {
...
})
note

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

variant

Determines which magnetic poles the sensor can detected, and whether or not it can measure their strength or just presence.

  • type: Register<number> (packing format u8)

  • optional: this register may not be implemented

  • constant: the register value will not change (until the next reset)

  • read only

import { MagneticFieldLevel } from "@devicescript/core"

const magneticFieldLevel = new MagneticFieldLevel()
// ...
const value = await magneticFieldLevel.variant.read()
note

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

Events

active

Emitted when strong-enough magnetic field is detected.

magneticFieldLevel.active.subscribe(() => {

})

inactive

Emitted when strong-enough magnetic field is no longer detected.

magneticFieldLevel.inactive.subscribe(() => {

})