-
Notifications
You must be signed in to change notification settings - Fork 9
/
AdafruitLSM303.jun
62 lines (56 loc) · 1.75 KB
/
AdafruitLSM303.jun
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
module AdafruitLSM303
open(Prelude, Time, Vector)
include("\"Adafruit_LSM303.h\"")
// p should be a Adafruit_LSM303 C++ object
alias accMag = { p : ptr, offset : vector<float, 3>, scale : vector<float, 3> }
alias accMagState = { timeState : timerState,
latchState : (vector<float, 3>, vector<float, 3>) }
fun make(p: ptr,
minX : float, minY : float, minZ : float,
maxX : float, maxY : float, maxZ : float) : accMag = {
let offset = [(minX + maxX)/-2.0, (minY + maxY)/-2.0, (minZ + maxZ)/-2.0]
let scale = [2.0/(maxX - minX), 2.0/(maxY - minY), 2.0/(maxZ - minZ)]
{
p := p,
offset := offset,
scale := scale
}
}
let state : accMagState = {
let z = Vector:zero()
{ timeState := Time:state,
latchState := (z, z) }
}
fun begin(am : accMag) : unit = {
let p = am.p
#((Adafruit_LSM303*) p)->begin()#
}
fun read(am : accMag) : (vector<float, 3>, vector<float, 3>) = {
let p = am.p
let mut accX = 0.0f
let mut accY = 0.0f
let mut accZ = 0.0f
let mut magX = 0.0f
let mut magY = 0.0f
let mut magZ = 0.0f
#
auto ptr = (Adafruit_LSM303*) p;
ptr->read();
accX = ptr->accelData.x;
accY = ptr->accelData.y;
accZ = ptr->accelData.z;
magX = ptr->magData.x;
magY = ptr->magData.y;
magZ = ptr->magData.z;
#
(
[accX, accY, accZ],
Vector:multiply(am.scale, Vector:add(am.offset, [magX, magY, magZ]))
)
}
fun getSignal(updateInterval : uint32,
accMag : accMag,
inout state : accMagState) : sig<(vector<float, 3>, vector<float, 3>)> =
Time:every(updateInterval, inout state.timeState) |>
Signal:map((t) => read(accMag)) |>
Signal:latch(inout state.latchState)