-
Notifications
You must be signed in to change notification settings - Fork 239
/
Copy pathlis3dh.js
173 lines (152 loc) · 4.51 KB
/
lis3dh.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Copyright (c) 2016-2020 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK Runtime.
*
* The Moddable SDK Runtime is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Moddable SDK Runtime is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
Adafruit LIS3DH Accelerometer breakout
https://www.adafruit.com/product/2809
Implementation based on Adafruit https://github.com/adafruit/Adafruit_LIS3DH
*/
import SMBus from "pins/smbus";
const Register = {
STATUS1: 0x07,
OUTADC1_L: 0x08,
OUTADC1_H: 0x09,
OUTADC2_L: 0x0A,
OUTADC2_H: 0x0B,
OUTADC3_L: 0x0C,
OUTADC3_H: 0x0D,
INTCOUNT: 0x0E,
WHOAMI: 0x0F,
TEMPCFG: 0x1F,
CTRL1: 0x20,
CTRL2: 0x21,
CTRL3: 0x22,
CTRL4: 0x23,
CTRL5: 0x24,
CTRL6: 0x25,
REFERENCE: 0x26,
STATUS2: 0x27,
OUT_X_L: 0x28,
OUT_X_H: 0x29,
OUT_Y_L: 0x2A,
OUT_Y_H: 0x2B,
OUT_Z_L: 0x2C,
OUT_Z_H: 0x2D,
FIFOCTRL: 0x2E,
FIFOSRC: 0x2F,
INT1CFG: 0x30,
INT1SRC: 0x31,
INT1THS: 0x32,
INT1DUR: 0x33,
CLICKCFG: 0x38,
CLICKSRC: 0x39,
CLICKTHS: 0x3A,
TIMELIMIT: 0x3B,
TIMELATENCY: 0x3C,
TIMEWINDOW: 0x3D,
ACTTHS: 0x3E,
ACTDUR: 0x3F
};
Object.freeze(Register);
const Range = {
RANGE_16_G: 0b11, // +/- 16g
RANGE_8_G: 0b10, // +/- 8g
RANGE_4_G: 0b01, // +/- 4g
RANGE_2_G: 0b00 // +/- 2g (default value)
};
Object.freeze(Range);
// Used with register 0x2A (LIS3DH_REG_CTRL_REG1) to set bandwidth
const DataRate = {
DATARATE_400_HZ: 0b0111, // 400Hz
DATARATE_200_HZ: 0b0110, // 200Hz
DATARATE_100_HZ: 0b0101, // 100Hz
DATARATE_50_HZ: 0b0100, // 50Hz
DATARATE_25_HZ: 0b0011, // 25Hz
DATARATE_10_HZ: 0b0010, // 10 Hz
DATARATE_1_HZ: 0b0001, // 1 Hz
POWERDOWN: 0,
LOWPOWER_1K6HZ: 0b1000,
LOWPOWER_5KHZ: 0b1001,
};
Object.freeze(DataRate);
class Sensor extends SMBus {
#values = new Int16Array(3);
#multiplier;
#rate = DataRate.DATARATE_400_HZ;
#range = Range.RANGE_4_G;
constructor(dictionary) {
super({address: 0x18, throw: false, ...dictionary});
if (0x33 === this.readByte(Register.WHOAMI))
;
else if (0x33 !== this.readByte(Register.WHOAMI))
throw new Error("unexpected device ID");
this.configure({rate: dictionary.rate ?? this.#rate, range:dictionary.range ?? this.#range});
const interrupt = dictionary.interrupt;
if (interrupt) {
this.writeByte(Register.CTRL6, (interrupt.polarity ?? 1) ? 0b00000000 : 0b00000010);
this.writeByte(Register.INT1THS, (interrupt.threshold ?? 0) & 0x7f);
this.writeByte(Register.INT1DUR, (interrupt.duration ?? 0) & 0x7f);
this.writeByte(Register.INT1CFG, interrupt.enable);
this.writeByte(Register.CTRL2, 0x01); // HP filter for INT1
this.writeByte(Register.CTRL3, 0x40); // interrupt to INT1
this.writeByte(Register.CTRL5, interrupt.latch ? 0x08 : 0); // latch interrupt LIR_INT1
}
else {
this.writeByte(Register.INT1CFG, 0);
}
}
get irq_fired() { return 0 != (this.readByte(Register.INT1SRC) & 64); }
configure(dictionary) {
for (let property in dictionary) {
switch (property) {
case "rate":
this.#rate = parseInt(dictionary.rate);
break;
case "range":
this.#range = parseInt(dictionary.range);
break;
default:
throw new Error(`invalid property "${property}"`);
}
}
// Enable all axes, normal mode @ rate
this.writeByte(Register.CTRL1, 0x07 | (this.#rate << 4));
// High res & BDU enabled
this.writeByte(Register.CTRL4, 0x88 | (this.#range << 4));
if (this.#range === Range.RANGE_16_G)
this.#multiplier = 1 / 1365; // different sensitivity at 16g
else if (this.#range === Range.RANGE_8_G)
this.#multiplier = 1 / 4096;
else if (this.#range === Range.RANGE_4_G)
this.#multiplier = 1 / 8190;
else
this.#multiplier = 1 / 16380;
}
sample() {
const values = this.#values, multiplier = this.#multiplier;
this.readBlock(Register.OUT_X_L | 0x80, 6, values.buffer);
return {
x: values[0] * multiplier,
y: values[1] * multiplier,
z: values[2] * multiplier
};
}
}
Object.freeze(Sensor.prototype);
export {Sensor as default, Sensor, DataRate, Range};