forked from cagdasc/Linux-I2C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathADXL345.h
469 lines (302 loc) · 11.8 KB
/
ADXL345.h
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
Copyright (C) 2014 Cagdas Caglak [email protected] http://expcodes.blogspot.com.tr/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ADXL345_H
#define ADXL345_H
#include "I2cPort.h"
#define ADXL345_DEFAULT_ADDRESS 0x53
#define ADXL345_RA_DEVID 0x00
#define ADXL345_RA_RESERVED1 0x01
#define ADXL345_RA_THRESH_TAP 0x1D
#define ADXL345_RA_OFSX 0x1E
#define ADXL345_RA_OFSY 0x1F
#define ADXL345_RA_OFSZ 0x20
#define ADXL345_RA_DUR 0x21
#define ADXL345_RA_LATENT 0x22
#define ADXL345_RA_WINDOW 0x23
#define ADXL345_RA_THRESH_ACT 0x24
#define ADXL345_RA_THRESH_INACT 0x25
#define ADXL345_RA_TIME_INACT 0x26
#define ADXL345_RA_ACT_INACT_CTL 0x27
#define ADXL345_RA_THRESH_FF 0x28
#define ADXL345_RA_TIME_FF 0x29
#define ADXL345_RA_TAP_AXES 0x2A
#define ADXL345_RA_ACT_TAP_STATUS 0x2B
#define ADXL345_RA_BW_RATE 0x2C
#define ADXL345_RA_POWER_CTL 0x2D
#define ADXL345_RA_INT_ENABLE 0x2E
#define ADXL345_RA_INT_MAP 0x2F
#define ADXL345_RA_INT_SOURCE 0x30
#define ADXL345_RA_DATA_FORMAT 0x31
#define ADXL345_RA_DATAX0 0x32
#define ADXL345_RA_DATAX1 0x33
#define ADXL345_RA_DATAY0 0x34
#define ADXL345_RA_DATAY1 0x35
#define ADXL345_RA_DATAZ0 0x36
#define ADXL345_RA_DATAZ1 0x37
#define ADXL345_RA_FIFO_CTL 0x38
#define ADXL345_RA_FIFO_STATUS 0x39
#define ADXL345_AIC_ACT_AC_BIT 7
#define ADXL345_AIC_ACT_X_BIT 6
#define ADXL345_AIC_ACT_Y_BIT 5
#define ADXL345_AIC_ACT_Z_BIT 4
#define ADXL345_AIC_INACT_AC_BIT 3
#define ADXL345_AIC_INACT_X_BIT 2
#define ADXL345_AIC_INACT_Y_BIT 1
#define ADXL345_AIC_INACT_Z_BIT 0
#define ADXL345_TAPAXIS_SUP_BIT 3
#define ADXL345_TAPAXIS_X_BIT 2
#define ADXL345_TAPAXIS_Y_BIT 1
#define ADXL345_TAPAXIS_Z_BIT 0
#define ADXL345_TAPSTAT_ACTX_BIT 6
#define ADXL345_TAPSTAT_ACTY_BIT 5
#define ADXL345_TAPSTAT_ACTZ_BIT 4
#define ADXL345_TAPSTAT_ASLEEP_BIT 3
#define ADXL345_TAPSTAT_TAPX_BIT 2
#define ADXL345_TAPSTAT_TAPY_BIT 1
#define ADXL345_TAPSTAT_TAPZ_BIT 0
#define ADXL345_BW_LOWPOWER_BIT 0
#define ADXL345_BW_RATE_BIT 3
#define ADXL345_BW_RATE_LENGTH 4
#define ADXL345_RATE_3200 0b1111
#define ADXL345_RATE_1600 0b1110
#define ADXL345_RATE_800 0b1101
#define ADXL345_RATE_400 0b1100
#define ADXL345_RATE_200 0b1011
#define ADXL345_RATE_100 0b1010
#define ADXL345_RATE_50 0b1001
#define ADXL345_RATE_25 0b1000
#define ADXL345_RATE_12P5 0b0111
#define ADXL345_RATE_6P25 0b0110
#define ADXL345_RATE_3P13 0b0101
#define ADXL345_RATE_1P56 0b0100
#define ADXL345_RATE_0P78 0b0011
#define ADXL345_RATE_0P39 0b0010
#define ADXL345_RATE_0P20 0b0001
#define ADXL345_RATE_0P10 0b0000
#define ADXL345_PCTL_LINK_BIT 5
#define ADXL345_PCTL_AUTOSLEEP_BIT 4
#define ADXL345_PCTL_MEASURE_BIT 3
#define ADXL345_PCTL_SLEEP_BIT 2
#define ADXL345_PCTL_WAKEUP_BIT 0
#define ADXL345_PCTL_WAKEUP_LENGTH 2
#define ADXL345_WAKEUP_8HZ 0b00
#define ADXL345_WAKEUP_4HZ 0b01
#define ADXL345_WAKEUP_2HZ 0b10
#define ADXL345_WAKEUP_1HZ 0b11
#define ADXL345_INT_DATA_READY_BIT 7
#define ADXL345_INT_SINGLE_TAP_BIT 6
#define ADXL345_INT_DOUBLE_TAP_BIT 5
#define ADXL345_INT_ACTIVITY_BIT 4
#define ADXL345_INT_INACTIVITY_BIT 3
#define ADXL345_INT_FREE_FALL_BIT 2
#define ADXL345_INT_WATERMARK_BIT 1
#define ADXL345_INT_OVERRUN_BIT 0
#define ADXL345_FORMAT_SELFTEST_BIT 7
#define ADXL345_FORMAT_SPIMODE_BIT 6
#define ADXL345_FORMAT_INTMODE_BIT 5
#define ADXL345_FORMAT_FULL_RES_BIT 3
#define ADXL345_FORMAT_JUSTIFY_BIT 2
#define ADXL345_FORMAT_RANGE_BIT 0
#define ADXL345_FORMAT_RANGE_LENGTH 2
#define ADXL345_RANGE_2G 0b00
#define ADXL345_RANGE_4G 0b01
#define ADXL345_RANGE_8G 0b10
#define ADXL345_RANGE_16G 0b11
#define ADXL345_FIFO_MODE_BIT 6
#define ADXL345_FIFO_MODE_LENGTH 2
#define ADXL345_FIFO_TRIGGER_BIT 5
#define ADXL345_FIFO_SAMPLES_BIT 0
#define ADXL345_FIFO_SAMPLES_LENGTH 5
#define ADXL345_FIFO_MODE_BYPASS 0b00
#define ADXL345_FIFO_MODE_FIFO 0b01
#define ADXL345_FIFO_MODE_STREAM 0b10
#define ADXL345_FIFO_MODE_TRIGGER 0b11
#define ADXL345_FIFOSTAT_TRIGGER_BIT 7
#define ADXL345_FIFOSTAT_LENGTH_BIT 5
#define ADXL345_FIFOSTAT_BIT_START 6
using namespace cacaosd_i2cport;
namespace cacaosd_adxl345 {
class ADXL345 {
public:
ADXL345(I2cPort *i2c);
virtual ~ADXL345();
void initialize();
bool testConnection();
void setDeviceAddress(uint8_t DEV_ADD);
uint8_t getDeviceAddress();
// DEVID register
uint8_t getDeviceID();
// THRESH_TAP register
uint8_t getTapThreshold();
void setTapThreshold(uint8_t threshold);
// OFS* registers
void getOffset(int8_t *offsets);
void setOffset(int8_t x, int8_t y, int8_t z);
int8_t getOffsetX();
void setOffsetX(int8_t x);
int8_t getOffsetY();
void setOffsetY(int8_t y);
int8_t getOffsetZ();
void setOffsetZ(int8_t z);
// DUR register
uint8_t getTapDuration();
void setTapDuration(uint8_t duration);
// LATENT register
uint8_t getDoubleTapLatency();
void setDoubleTapLatency(uint8_t latency);
// WINDOW register
uint8_t getDoubleTapWindow();
void setDoubleTapWindow(uint8_t window);
// THRESH_ACT register
uint8_t getActivityThreshold();
void setActivityThreshold(uint8_t threshold);
// THRESH_INACT register
uint8_t getInactivityThreshold();
void setInactivityThreshold(uint8_t threshold);
// TIME_INACT register
uint8_t getInactivityTime();
void setInactivityTime(uint8_t time);
// ACT_INACT_CTL register
bool getActivityAC();
void setActivityAC(bool enabled);
bool getActivityXEnabled();
void setActivityXEnabled(bool enabled);
bool getActivityYEnabled();
void setActivityYEnabled(bool enabled);
bool getActivityZEnabled();
void setActivityZEnabled(bool enabled);
bool getInactivityAC();
void setInactivityAC(bool enabled);
bool getInactivityXEnabled();
void setInactivityXEnabled(bool enabled);
bool getInactivityYEnabled();
void setInactivityYEnabled(bool enabled);
bool getInactivityZEnabled();
void setInactivityZEnabled(bool enabled);
// THRESH_FF register
uint8_t getFreefallThreshold();
void setFreefallThreshold(uint8_t threshold);
// TIME_FF register
uint8_t getFreefallTime();
void setFreefallTime(uint8_t time);
// TAP_AXES register
bool getTapAxisSuppress();
void setTapAxisSuppress(bool enabled);
bool getTapAxisXEnabled();
void setTapAxisXEnabled(bool enabled);
bool getTapAxisYEnabled();
void setTapAxisYEnabled(bool enabled);
bool getTapAxisZEnabled();
void setTapAxisZEnabled(bool enabled);
// ACT_TAP_STATUS register
bool getActivitySourceX();
bool getActivitySourceY();
bool getActivitySourceZ();
bool getAsleep();
bool getTapSourceX();
bool getTapSourceY();
bool getTapSourceZ();
// BW_RATE register
bool getLowPowerEnabled();
void setLowPowerEnabled(bool enabled);
uint8_t getRate();
void setRate(uint8_t rate);
// POWER_CTL register
bool getLinkEnabled();
void setLinkEnabled(bool enabled);
bool getAutoSleepEnabled();
void setAutoSleepEnabled(bool enabled);
bool getMeasureEnabled();
void setMeasureEnabled(bool enabled);
bool getSleepEnabled();
void setSleepEnabled(bool enabled);
uint8_t getWakeupFrequency();
void setWakeupFrequency(uint8_t frequency);
// INT_ENABLE register
bool getIntDataReadyEnabled();
void setIntDataReadyEnabled(bool enabled);
bool getIntSingleTapEnabled();
void setIntSingleTapEnabled(bool enabled);
bool getIntDoubleTapEnabled();
void setIntDoubleTapEnabled(bool enabled);
bool getIntActivityEnabled();
void setIntActivityEnabled(bool enabled);
bool getIntInactivityEnabled();
void setIntInactivityEnabled(bool enabled);
bool getIntFreefallEnabled();
void setIntFreefallEnabled(bool enabled);
bool getIntWatermarkEnabled();
void setIntWatermarkEnabled(bool enabled);
bool getIntOverrunEnabled();
void setIntOverrunEnabled(bool enabled);
// INT_MAP register
uint8_t getIntDataReadyPin();
void setIntDataReadyPin(uint8_t pin);
uint8_t getIntSingleTapPin();
void setIntSingleTapPin(uint8_t pin);
uint8_t getIntDoubleTapPin();
void setIntDoubleTapPin(uint8_t pin);
uint8_t getIntActivityPin();
void setIntActivityPin(uint8_t pin);
uint8_t getIntInactivityPin();
void setIntInactivityPin(uint8_t pin);
uint8_t getIntFreefallPin();
void setIntFreefallPin(uint8_t pin);
uint8_t getIntWatermarkPin();
void setIntWatermarkPin(uint8_t pin);
uint8_t getIntOverrunPin();
void setIntOverrunPin(uint8_t pin);
// INT_SOURCE register
uint8_t getIntDataReadySource();
uint8_t getIntSingleTapSource();
uint8_t getIntDoubleTapSource();
uint8_t getIntActivitySource();
uint8_t getIntInactivitySource();
uint8_t getIntFreefallSource();
uint8_t getIntWatermarkSource();
uint8_t getIntOverrunSource();
// DATA_FORMAT register
uint8_t getSelfTestEnabled();
void setSelfTestEnabled(uint8_t enabled);
uint8_t getSPIMode();
void setSPIMode(uint8_t mode);
uint8_t getInterruptMode();
void setInterruptMode(uint8_t mode);
uint8_t getFullResolution();
void setFullResolution(uint8_t resolution);
uint8_t getDataJustification();
void setDataJustification(uint8_t justification);
uint8_t getRange();
void setRange(uint8_t range);
// DATA* registers
void getAccelerations(int16_t *accels);
int16_t getAccelerationX();
int16_t getAccelerationY();
int16_t getAccelerationZ();
// FIFO_CTL register
uint8_t getFIFOMode();
void setFIFOMode(uint8_t mode);
uint8_t getFIFOTriggerInterruptPin();
void setFIFOTriggerInterruptPin(uint8_t interrupt);
uint8_t getFIFOSamples();
void setFIFOSamples(uint8_t size);
// FIFO_STATUS register
bool getFIFOTriggerOccurred();
uint8_t getFIFOLength();
private:
I2cPort *i2c;
uint8_t device_address;
};
} // namespace cacaosd_adxl345
#endif /* ADXL345_H */