-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathADXL345.cpp
387 lines (340 loc) · 12.5 KB
/
ADXL345.cpp
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
/**************************************************************************/
/*!
@file ADXL345.cpp
@author K.Townsend (Adafruit Industries)
@author profi248
*/
/**************************************************************************/
#include <cmath>
#include <cstring>
#include "ADXL345.h"
/**************************************************************************/
/*!
@brief Initializes I2C device
*/
/**************************************************************************/
bool I2CDevice::begin()
{
i2c_init(m_i2c_bus, 100 * 1000);
gpio_set_function(m_sda_pin, GPIO_FUNC_I2C);
gpio_set_function(m_scl_pin, GPIO_FUNC_I2C);
gpio_pull_up(m_sda_pin);
gpio_pull_up(m_scl_pin);
return true;
}
/**************************************************************************/
/*!
@brief Reads a buffer from a I2C bus
@param buffer The buffer to read into
@param len The number of bytes to read
*/
/**************************************************************************/
void I2CDevice::read(uint8_t* buf, size_t len)
{
i2c_read_blocking(m_i2c_bus, m_addr, buf, len, true);
}
/**************************************************************************/
/*!
@brief Writes a buffer to a I2C bus
@param buffer The buffer to write to
@param len The number of bytes to write
*/
/**************************************************************************/
void I2CDevice::write(const uint8_t* buf, size_t len)
{
i2c_write_blocking(m_i2c_bus, m_addr, buf, len, true);
}
/**************************************************************************/
/*!
@brief Writes one byte to the specified destination register
@param reg The address of the register to write to
@param value The value to set the register to
*/
/**************************************************************************/
void ADXL345::writeRegister(uint8_t reg, uint8_t value)
{
uint8_t buffer[2] = { reg, value };
i2c_dev->write(buffer, 2);
}
/**************************************************************************/
/*!
@brief Reads one byte from the specified register
@param reg The address of the register to read from
@returns The single byte value of the requested register
*/
/**************************************************************************/
uint8_t ADXL345::readRegister(uint8_t reg)
{
uint8_t buffer[1] = { i2c_dev ? reg : reg | 0x80 };
i2c_dev->write(buffer, 1);
i2c_dev->read(buffer, 1);
return buffer[0];
}
/**************************************************************************/
/*!
@brief Reads two bytes from the specified register
@param reg The address of the register to read from
@return The two bytes read from the sensor starting at the given address
*/
/**************************************************************************/
int16_t ADXL345::read16(uint8_t reg)
{
uint8_t buffer[2] = { i2c_dev ? reg : reg | 0x80 | 0x40, 0 };
i2c_dev->write(buffer, 1);
i2c_dev->read(buffer, 2);
return uint16_t(buffer[1]) << 8 | uint16_t(buffer[0]);
}
/**************************************************************************/
/*!
@brief Reads the device ID (can be used to check connection)
@return The Device ID of the connected sensor
*/
/**************************************************************************/
uint8_t ADXL345::getDeviceID(void)
{
// Check device ID register
return readRegister(ADXL345_REG_DEVID);
}
/**************************************************************************/
/*!
@brief Gets the most recent X axis value
@return The raw `int16_t` unscaled x-axis acceleration value
*/
/**************************************************************************/
int16_t ADXL345::getX(void)
{
return read16(ADXL345_REG_DATAX0);
}
/**************************************************************************/
/*!
@brief Gets the most recent Y axis value
@return The raw `int16_t` unscaled y-axis acceleration value
*/
/**************************************************************************/
int16_t ADXL345::getY(void)
{
return read16(ADXL345_REG_DATAY0);
}
/**************************************************************************/
/*!
@brief Gets the most recent Z axis value
@return The raw `int16_t` unscaled z-axis acceleration value
*/
/**************************************************************************/
int16_t ADXL345::getZ(void)
{
return read16(ADXL345_REG_DATAZ0);
}
/**************************************************************************/
/*!
@brief Instantiates a new ADXL345 class
@param sensorID A unique ID to use to differentiate the sensor from others
*/
/**************************************************************************/
ADXL345::ADXL345(int32_t sensorID)
{
_sensorID = sensorID;
_range = ADXL345_RANGE_2_G;
}
/**************************************************************************/
/*!
@brief Setups the HW (reads coefficients values, etc.)
@param i2caddr The I2C address to begin communication with
@return true: success false: a sensor with the correct ID was not found
*/
/**************************************************************************/
bool ADXL345::begin(uint8_t i2caddr, i2c_inst_t* i2c_bus, uint8_t sda_pin, uint8_t scl_pin)
{
i2c_dev = new I2CDevice(i2caddr, i2c_bus, sda_pin, scl_pin);
if (!i2c_dev->begin())
return false;
/* Check connection */
uint8_t deviceid = getDeviceID();
if (deviceid != 0xE5) {
/* No ADXL345 detected ... return false */
return false;
}
// Enable measurements
writeRegister(ADXL345_REG_POWER_CTL, 0x08);
return true;
}
/**************************************************************************/
/*!
@brief Sets the g range for the accelerometer
@param range The new `range_t` to set the accelerometer to
*/
/**************************************************************************/
void ADXL345::setRange(range_t range)
{
/* Read the data format register to preserve bits */
uint8_t format = readRegister(ADXL345_REG_DATA_FORMAT);
/* Update the data rate */
format &= ~0x0F;
format |= range;
/* Make sure that the FULL-RES bit is enabled for range scaling */
format |= 0x08;
/* Write the register back to the IC */
writeRegister(ADXL345_REG_DATA_FORMAT, format);
/* Keep track of the current range (to avoid readbacks) */
_range = range;
}
/**************************************************************************/
/*!
@brief Gets the g range for the accelerometer
@return The current `range_t` value
*/
/**************************************************************************/
range_t ADXL345::getRange(void)
{
/* Read the data format register to preserve bits */
return (range_t)(readRegister(ADXL345_REG_DATA_FORMAT) & 0x03);
}
/**************************************************************************/
/*!
@brief Sets the data rate for the ADXL345 (controls power consumption)
@param dataRate The `dataRate_t` to set
*/
/**************************************************************************/
void ADXL345::setDataRate(dataRate_t dataRate)
{
/* Note: The LOW_POWER bits are currently ignored and we always keep
the device in 'normal' mode */
writeRegister(ADXL345_REG_BW_RATE, dataRate);
}
/**************************************************************************/
/*!
@brief Gets the data rate for the ADXL345 (controls power consumption)
@return The current data rate
*/
/**************************************************************************/
dataRate_t ADXL345::getDataRate(void)
{
return (dataRate_t)(readRegister(ADXL345_REG_BW_RATE) & 0x0F);
}
/**************************************************************************/
/*!
@brief Gets the most recent sensor event
@param event Pointer to the event object to fill
@return true: success
*/
/**************************************************************************/
bool ADXL345::getEvent(sensors_event_t* event)
{
/* Clear the event */
memset(event, 0, sizeof(sensors_event_t));
event->version = sizeof(sensors_event_t);
event->sensor_id = _sensorID;
event->type = SENSOR_TYPE_ACCELEROMETER;
event->timestamp = 0;
event->acceleration.x = getX() * ADXL345_MG2G_MULTIPLIER * SENSORS_GRAVITY_STANDARD;
event->acceleration.y = getY() * ADXL345_MG2G_MULTIPLIER * SENSORS_GRAVITY_STANDARD;
event->acceleration.z = getZ() * ADXL345_MG2G_MULTIPLIER * SENSORS_GRAVITY_STANDARD;
return true;
}
/**************************************************************************/
/*!
@brief Set interrupt enabled/disabled
@param interrupt Interrupt type
@param enable true to enable, false to disable
*/
/**************************************************************************/
void ADXL345::setInterrupt(interrupt_t interrupt, bool state)
{
uint8_t reg = readRegister(ADXL345_REG_INT_ENABLE);
if (state)
reg |= interrupt;
else
reg &= ~interrupt;
writeRegister(ADXL345_REG_INT_ENABLE, reg);
}
/**************************************************************************/
/*!
@brief Set interrupt pin to either INT1 or INT2
@param interrupt Interrupt type
@param pin Interrupt pin
*/
/**************************************************************************/
void ADXL345::setInterruptMap(interrupt_t interrupt, interruptPin_t pin)
{
uint8_t reg = readRegister(ADXL345_REG_INT_MAP);
if (pin == ADXL345_INT2)
reg |= interrupt;
else
reg &= ~interrupt;
writeRegister(ADXL345_REG_INT_MAP, reg);
}
/**************************************************************************/
/*!
@brief Get interrupt source(s) and clear latched motion detection intrrupts
@return Bitmap with triggered interrupts set to 1
*/
/**************************************************************************/
uint8_t ADXL345::getInterruptSources()
{
return readRegister(ADXL345_REG_INT_SOURCE);
}
/**************************************************************************/
/*!
@brief Is given interrupt a source?
@param interrupt Interrupt type
@param sources_bitmap Interrupt sources bitmap
@return true if interrupt is a source, false otherwise
*/
/**************************************************************************/
bool ADXL345::isInterruptSource(interrupt_t interrupt, uint8_t sources_bitmap)
{
return (sources_bitmap & interrupt) != 0;
}
/**************************************************************************/
/*!
@brief Set force treshold for triggering free fall detection
@param threshold_mg threshold value in mg
@note Threshold is rounded to nearest multiple of 62.5 mg
@note Threshold is clamped to a maximum of 15937.5 mg
*/
/**************************************************************************/
void ADXL345::setFreefallInterruptTreshold(uint16_t threshold_mg)
{
if (threshold_mg < 0)
threshold_mg = 0;
if (threshold_mg > 15937.5)
threshold_mg = 15937.5;
uint8_t threshold = (uint8_t) round(threshold_mg / 62.5);
writeRegister(ADXL345_REG_THRESH_FF, threshold);
}
/**************************************************************************/
/*!
@brief Set duration required to trigger free fall interrupt
@param time_ms duration in ms
@note Duration is rounded to the nearest multiple of 5ms
@note Duration is clamped to a maximum of 1275ms
*/
/**************************************************************************/
void ADXL345::setFreefallInterruptTime(uint16_t time_ms)
{
if (time_ms > 1275)
time_ms = 1275;
uint8_t time = (uint8_t) round(time_ms / 5);
writeRegister(ADXL345_REG_TIME_FF, time);
}
/**************************************************************************/
/*!
@brief Fill a `sensor_t` struct with information about the sensor
@param sensor Pointer to a `sensor_t` struct to fill
*/
/**************************************************************************/
void ADXL345::getSensor(sensor_t* sensor)
{
/* Clear the sensor_t object */
memset(sensor, 0, sizeof(sensor_t));
/* Insert the sensor name in the fixed length char array */
strncpy(sensor->name, "ADXL345", sizeof(sensor->name) - 1);
sensor->name[sizeof(sensor->name) - 1] = 0;
sensor->version = 1;
sensor->sensor_id = _sensorID;
sensor->type = SENSOR_TYPE_ACCELEROMETER;
sensor->min_delay = 0;
sensor->max_value = -156.9064F; /* -16g = 156.9064 m/s^2 */
sensor->min_value = 156.9064F; /* 16g = 156.9064 m/s^2 */
sensor->resolution = 0.03923F; /* 4mg = 0.0392266 m/s^2 */
}