COSA
An Object-Oriented Platform for Arduino Programming
ADXL345.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_ADXL345_HH
22 #define COSA_ADXL345_HH
23 
24 #include "Cosa/TWI.hh"
25 #include "Cosa/IOStream.hh"
26 
49 class ADXL345 : private TWI::Driver {
50 public:
51 
56  ADXL345(bool use_alt_address = false) :
57  TWI::Driver(use_alt_address ? 0x53 : 0x1d)
58  {}
59 
67  bool begin();
68 
73  bool end();
74 
78  struct offset_t {
79  int8_t x;
80  int8_t y;
81  int8_t z;
82  };
83 
90  void calibrate(int8_t x, int8_t y, int8_t z)
91  __attribute__((always_inline))
92  {
93  offset_t ofs;
94  ofs.x = x;
95  ofs.y = y;
96  ofs.z = z;
97  write(OFS, &ofs, sizeof(ofs));
98  }
99 
105  void calibrate();
106 
110  struct sample_t {
111  int x;
112  int y;
113  int z;
114  };
115 
120  void sample(sample_t& s)
121  __attribute__((always_inline))
122  {
123  read(DATA, &s, sizeof(s));
124  }
125 
129  enum {
133  ACT = 4,
134  INACT = 3,
135  FREE_FALL = 2,
136  WATERMARK = 1,
137  OVERRUN = 0
138  } __attribute__((packed));
139 
145  uint8_t is_activity();
146 
147 protected:
151  enum Register {
152  DEVID = 0x00,
153  THRESH_TAP = 0x1D,
154  OFS = 0x1E,
155  DUR = 0x21,
156  LATENT = 0x22,
157  WINDOW = 0x23,
158  THRESH_ACT = 0x24,
159  THRESH_INACT = 0x25,
160  TIME_INACT = 0x26,
161  ACT_INACT_CTL = 0x27,
162  THRESH_FF = 0x28,
163  TIME_FF = 0x29,
164  TAP_AXES = 0x2A,
165  ACT_TAP_STATUS = 0x2B,
166  BW_RATE = 0x2C,
167  POWER_CTL = 0x2D,
168  INT_ENABLE = 0x2E,
169  INT_MAP = 0x2F,
170  INT_SOURCE = 0x30,
171  DATA_FORMAT = 0x31,
172  DATA = 0x32,
173  FIFO_CTL = 0x38,
174  FIFO_STATUS = 0x39
175  } __attribute__((packed));
176 
178  static const uint8_t ID = 0xe5;
179 
183  enum {
184  ACT_AC_DC = 7,
185  ACT_X_EN = 6,
186  ACT_Y_EN = 5,
187  ACT_Z_EN = 4,
192  } __attribute__((packed));
193 
197  enum {
198  SUPPRES = 3,
199  TAP_X_EN = 2,
200  TAP_Y_EN = 1,
201  TAP_Z_EN = 0,
202  } __attribute__((packed));
203 
207  enum {
208  ACT_X_SRC = 6,
209  ACT_Y_SRC = 5,
210  ACT_Z_SRC = 4,
211  ASLEEP = 3,
212  TAP_X_SRC = 2,
213  TAP_Y_SRC = 1,
215  } __attribute__((packed));
216 
220  enum {
221  LOW_POWER = 4,
222  RATE = 0,
223  RATE_MASK = 0x0f
224  } __attribute__((packed));
225 
229  enum {
230  LINK = 5,
232  MEASURE = 3,
233  SLEEP = 2,
234  WAKEUP = 0,
235  } __attribute__((packed));
236  enum {
242  };
243 
247  enum {
248  SELF_TEST = 7,
251  FULL_RES = 3,
252  JUSTIFY = 2,
253  RANGE = 0
254  } __attribute__((packed));
255  enum {
256  RANGE_2G = 0,
257  RANGE_4G = 1,
258  RANGE_8G = 2,
259  RANGE_16G = 3,
261  } __attribute__((packed));
262 
266  enum {
267  FIFO_MODE = 6,
268  FIFO_MASK = 0xc0,
269  TRIG = 5,
270  SAMPLES = 0,
271  SAMPLES_MASK = 0x1f
272  } __attribute__((packed));
273  enum {
274  BYPASS = 0x00,
275  FIFO = 0x40,
276  STREAM = 0x80,
277  TRIGGER = 0xc0
278  };
279 
283  enum {
284  FIFO_TRIG = 7,
285  ENTRIES = 0,
286  ENTRIES_MASK = 0x3f
287  } __attribute__((packed));
288 
294  void write(Register reg, uint8_t value);
295 
302  void write(Register reg, void* buffer, uint8_t count);
303 
309  uint8_t read(Register reg)
310  {
311  uint8_t res;
312  read(reg, &res, sizeof(res));
313  return (res);
314  }
315 
322  void read(Register reg, void* buffer, uint8_t count);
323 
325  enum {
326  X = 4,
327  Y = 2,
328  Z = 1
329  } __attribute__((packed));
330 };
331 
338 extern IOStream& operator<<(IOStream& outs, ADXL345& accelerometer);
339 
340 #endif
Trigger event to interrupt pin.
Definition: ADXL345.hh:269
Definition: TWI.hh:51
Data format control.
Definition: ADXL345.hh:171
Range 2-16 g (2 bits).
Definition: ADXL345.hh:253
Tap x event.
Definition: ADXL345.hh:212
Link mode select.
Definition: ADXL345.hh:230
Number of entries in FIFO (6 bits).
Definition: ADXL345.hh:285
Device is asleep.
Definition: ADXL345.hh:211
Activity x axis enable.
Definition: ADXL345.hh:185
Number of samples (5 bits).
Definition: ADXL345.hh:270
uint8_t read(Register reg)
Definition: ADXL345.hh:309
Holds the latest 32 values.
Definition: ADXL345.hh:276
FIFO status.
Definition: ADXL345.hh:174
Data rate (4 bits).
Definition: ADXL345.hh:222
Data (x, y, z).
Definition: ADXL345.hh:172
Inactivity x axis enable.
Definition: ADXL345.hh:189
Overrun interrupt enable/map/source.
Definition: ADXL345.hh:137
Tap duration.
Definition: ADXL345.hh:155
Double tap.
Definition: ADXL345.hh:132
Activity threshold.
Definition: ADXL345.hh:158
Inactivity threshold.
Definition: ADXL345.hh:159
void calibrate()
Definition: ADXL345.cpp:93
void sample(sample_t &s)
Definition: ADXL345.hh:120
static const uint8_t ID
Definition: ADXL345.hh:178
Free-fall time.
Definition: ADXL345.hh:163
Data ready interrupt enable/map/source.
Definition: ADXL345.hh:130
FIFO is bypassed.
Definition: ADXL345.hh:274
Wakeup frequency mask.
Definition: ADXL345.hh:241
Activity x axis enable.
Definition: ADXL345.hh:187
Device ID.
Definition: ADXL345.hh:152
Activity z event.
Definition: ADXL345.hh:210
Mask range field.
Definition: ADXL345.hh:260
bool end()
Definition: ADXL345.cpp:61
Data rate and power mode control.
Definition: ADXL345.hh:166
Inactivity.
Definition: ADXL345.hh:134
Interrupt enable control.
Definition: ADXL345.hh:168
Full resolution.
Definition: ADXL345.hh:251
Tap y event.
Definition: ADXL345.hh:213
Measurement mode.
Definition: ADXL345.hh:232
Activity.
Definition: ADXL345.hh:133
Tap z enable.
Definition: ADXL345.hh:201
FIFO control.
Definition: ADXL345.hh:173
Left justified/sign extend.
Definition: ADXL345.hh:252
void write(Register reg, uint8_t value)
Definition: ADXL345.cpp:68
Activity x event.
Definition: ADXL345.hh:208
Latest 32 values before trigger.
Definition: ADXL345.hh:277
Tap window.
Definition: ADXL345.hh:157
Tap y enable.
Definition: ADXL345.hh:200
FIFO Mode.
Definition: ADXL345.hh:267
Collects up to 32 values.
Definition: ADXL345.hh:275
Axis enable control for activity detection.
Definition: ADXL345.hh:161
Single tap.
Definition: ADXL345.hh:131
Auto sleep enable.
Definition: ADXL345.hh:231
Interrupt mapping control.
Definition: ADXL345.hh:169
Low power move.
Definition: ADXL345.hh:221
Sleep mode.
Definition: ADXL345.hh:233
IOStream & operator<<(IOStream &outs, ADXL345 &accelerometer)
Definition: ADXL345.cpp:117
Activity x axis enable.
Definition: ADXL345.hh:186
ADXL345(bool use_alt_address=false)
Definition: ADXL345.hh:56
Activity y event.
Definition: ADXL345.hh:209
Self-test force.
Definition: ADXL345.hh:248
Driver(uint8_t addr)
Definition: TWI.hh:70
Tap z event.
Definition: ADXL345.hh:214
Source of interrupts.
Definition: ADXL345.hh:170
SPI wire mode.
Definition: ADXL345.hh:249
uint8_t is_activity()
Definition: ADXL345.cpp:102
Dito Mask.
Definition: ADXL345.hh:268
Inactivity time.
Definition: ADXL345.hh:160
Tap x enable.
Definition: ADXL345.hh:199
Free-fall threshold.
Definition: ADXL345.hh:162
AC/DC coupled activity.
Definition: ADXL345.hh:184
FIFO trigger event occuring.
Definition: ADXL345.hh:284
bool begin()
Definition: ADXL345.cpp:24
Tap latency.
Definition: ADXL345.hh:156
Suppress.
Definition: ADXL345.hh:198
Axis control for single tap/double tap.
Definition: ADXL345.hh:164
Tap threshold.
Definition: ADXL345.hh:153
Power-saving features control.
Definition: ADXL345.hh:167
void calibrate(int8_t x, int8_t y, int8_t z)
Definition: ADXL345.hh:90
Wakeup frequency (2 bits).
Definition: ADXL345.hh:234
Inactivity x axis enable.
Definition: ADXL345.hh:191
AC/DC coupled inactivity.
Definition: ADXL345.hh:188
Interrupt active high/low.
Definition: ADXL345.hh:250
Offset (x, y, z).
Definition: ADXL345.hh:154
Inactivity x axis enable.
Definition: ADXL345.hh:190
Free fall.
Definition: ADXL345.hh:135
Source of single tap/double tap.
Definition: ADXL345.hh:165
Watermark.
Definition: ADXL345.hh:136
Data rate mask.
Definition: ADXL345.hh:223