COSA
An Object-Oriented Platform for Arduino Programming
ADXL345.cpp
Go to the documentation of this file.
1 
21 #include "ADXL345.hh"
22 
23 bool
25 {
26  // Read device register and santity check
27  uint8_t id = read(DEVID);
28  if (UNLIKELY(id != ID)) return (false);
29 
30  // Data format
32 
33  // Single and double tap detection
34  write(THRESH_TAP, 0x40);
35  write(DUR, 0x30);
36  write(LATENT, 0x40);
37  write(WINDOW, 0xFF);
38  write(TAP_AXES, X | Y | Z);
39 
40  // Activity/inactivity detect
41  write(THRESH_ACT, 6);
42  write(THRESH_INACT, 3);
43  write(TIME_INACT, 6);
44  write(ACT_INACT_CTL, 0xff);
45 
46  // Free fall detect
47  write(THRESH_FF, 0x05);
48  write(TIME_FF, 0x14);
49 
50  // Power control
52 
53  // Interrupt enable
54  write(INT_MAP, 0);
55  write(INT_ENABLE, _BV(ACT) | _BV(SINGLE_TAP) | _BV(DOUBLE_TAP) | _BV(FREE_FALL));
56 
57  return (true);
58 }
59 
60 bool
62 {
63  write(POWER_CTL, 0);
64  return (true);
65 }
66 
67 void
68 ADXL345::write(Register reg, uint8_t value)
69 {
70  twi.acquire(this);
71  twi.write((uint8_t) reg, &value, sizeof(value));
72  twi.release();
73 }
74 
75 void
76 ADXL345::write(Register reg, void* buffer, uint8_t count)
77 {
78  twi.acquire(this);
79  twi.write((uint8_t) reg, buffer, count);
80  twi.release();
81 }
82 
83 void
84 ADXL345::read(Register reg, void* buffer, uint8_t count)
85 {
86  twi.acquire(this);
87  twi.write((uint8_t) reg);
88  twi.read(buffer, count);
89  twi.release();
90 }
91 
92 void
94 {
95  sample_t value;
96  calibrate(0, 0, 0);
97  sample(value);
98  calibrate(-value.x/4, -value.y/4, -value.z/4);
99 }
100 
101 uint8_t
103 {
104  uint8_t source = read(INT_SOURCE);
105  if (source & _BV(INACT)) {
106  uint8_t rate = read(BW_RATE);
107  write(BW_RATE, rate | _BV(LOW_POWER));
108  }
109  if (source & _BV(ACT)) {
110  uint8_t rate = read(BW_RATE);
111  write(BW_RATE, rate & ~_BV(LOW_POWER));
112  }
113  return (source & 0x74);
114 }
115 
116 IOStream&
117 operator<<(IOStream& outs, ADXL345& accelerometer)
118 {
119  ADXL345::sample_t value;
120  accelerometer.sample(value);
121  outs << PSTR("ADXL345(x = ") << (value.x << 2)
122  << PSTR(", y = ") << (value.y << 2)
123  << PSTR(", z = ") << (value.z << 2)
124  << PSTR(")");
125  return (outs);
126 }
127 
TWI twi
Definition: TWI.cpp:27
Data format control.
Definition: ADXL345.hh:171
uint8_t read(Register reg)
Definition: ADXL345.hh:309
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
#define PSTR(s)
Definition: Types.h:202
Device ID.
Definition: ADXL345.hh:152
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
Measurement mode.
Definition: ADXL345.hh:232
Activity.
Definition: ADXL345.hh:133
void write(Register reg, uint8_t value)
Definition: ADXL345.cpp:68
void release()
Definition: TWI.cpp:58
Tap window.
Definition: ADXL345.hh:157
Axis enable control for activity detection.
Definition: ADXL345.hh:161
Single tap.
Definition: ADXL345.hh:131
Auto sleep enable.
Definition: ADXL345.hh:231
int read(void *buf, size_t size)
Definition: TWI.hh:326
Interrupt mapping control.
Definition: ADXL345.hh:169
int write(void *buf, size_t size)
Definition: TWI.hh:282
Low power move.
Definition: ADXL345.hh:221
IOStream & operator<<(IOStream &outs, ADXL345 &accelerometer)
Definition: ADXL345.cpp:117
void acquire(TWI::Driver *dev)
Definition: TWI.cpp:36
Source of interrupts.
Definition: ADXL345.hh:170
uint8_t is_activity()
Definition: ADXL345.cpp:102
Inactivity time.
Definition: ADXL345.hh:160
Free-fall threshold.
Definition: ADXL345.hh:162
bool begin()
Definition: ADXL345.cpp:24
Tap latency.
Definition: ADXL345.hh:156
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
#define UNLIKELY(x)
Definition: Types.h:153
Interrupt active high/low.
Definition: ADXL345.hh:250
Free fall.
Definition: ADXL345.hh:135