| ||||||
This module returns the measurements of the builtin magnetic field sensor. Please note that only a few devices have a such a sensor.
Each measurement corresponds to a vector in three dimensional space indicating the direction of the magnetic field, and a value indicating the device heading:
| Key | Meaning | Type |
| x | x-component of the magnetic field vector | Number |
| y | x-component of the magnetic field vector | Number |
| z | x-component of the magnetic field vector | Number |
| north | Number of degrees between device top and magnetic north (0 to 359). | Number |
Gets the current magnetometer measurement.
Throws ErrNotSupported if the device has no magnetic field sensor.
|
// Check whether the device heads north or south function headsNorth() c=compass.get(); return c["north"]<90 or c["north"]>270 end print headsNorth() → true
|
Waits until any component of the magnetic field vector changes by at least xyzdelta, or the magnetic north direction changes by at least northdelta, then returns the current measurement. If timeout>=0 and timeout milliseconds have passed without any change, null is returned.
Throws ErrNotSupported if the device has no magnetic field sensor.
The following example draws a blue line in magnetic north direction:
|
// compute the center and radius s=graph.size(); cx=s[0]/2; cy=s[1]/2; r=cx; if cy<r then r=cy end; // initially there is no line dx=0; dy=0; do graph.show(); c=compass.new(); // erase the old line graph.pen(graph.bg()); graph.line(cx, cy, cx+dx, cy+dy); // compute the new line alpha=c["north"]*math.pi/180; dx=-math.sin(alpha)*r; dy=-math.cos(alpha)*r; // draw the new line graph.pen(graph.blue); graph.line(cx, cy, cx+dx, cy+dy) // repeat forever until false |