Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1993 - 1
/**************************************************************************
2
 *                                                                         *
3
 * ADXL345 Driver for Arduino                                              *
4
 *                                                                         *
5
 ***************************************************************************
6
 *                                                                         *
7
 * This program is free software; you can redistribute it and/or modify    *
8
 * it under the terms of the GNU License.                                  *
9
 * This program is distributed in the hope that it will be useful,         *
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
12
 * GNU License V2 for more details.                                        *
13
 *                                                                         *
14
 ***************************************************************************/
15
#include "WProgram.h"
16
 
17
#ifndef ADXL345_h
18
#define ADXL345_h
19
 
20
/* -- ADXL345 addresses --*/
21
#define ADXL345_ADDR_ALT_HIGH 0x1D // ADXL345 address when ALT is connected to HIGH
22
#define ADXL345_ADDR_ALT_LOW  0x53 // ADXL345 address when ALT is connected to LOW
23
 
24
/* ------- Register names ------- */
25
#define ADXL345_DEVID 0x00
26
#define ADXL345_RESERVED1 0x01
27
#define ADXL345_THRESH_TAP 0x1d
28
#define ADXL345_OFSX 0x1e
29
#define ADXL345_OFSY 0x1f
30
#define ADXL345_OFSZ 0x20
31
#define ADXL345_DUR 0x21
32
#define ADXL345_LATENT 0x22
33
#define ADXL345_WINDOW 0x23
34
#define ADXL345_THRESH_ACT 0x24
35
#define ADXL345_THRESH_INACT 0x25
36
#define ADXL345_TIME_INACT 0x26
37
#define ADXL345_ACT_INACT_CTL 0x27
38
#define ADXL345_THRESH_FF 0x28
39
#define ADXL345_TIME_FF 0x29
40
#define ADXL345_TAP_AXES 0x2a
41
#define ADXL345_ACT_TAP_STATUS 0x2b
42
#define ADXL345_BW_RATE 0x2c
43
#define ADXL345_POWER_CTL 0x2d
44
#define ADXL345_INT_ENABLE 0x2e
45
#define ADXL345_INT_MAP 0x2f
46
#define ADXL345_INT_SOURCE 0x30
47
#define ADXL345_DATA_FORMAT 0x31
48
#define ADXL345_DATAX0 0x32
49
#define ADXL345_DATAX1 0x33
50
#define ADXL345_DATAY0 0x34
51
#define ADXL345_DATAY1 0x35
52
#define ADXL345_DATAZ0 0x36
53
#define ADXL345_DATAZ1 0x37
54
#define ADXL345_FIFO_CTL 0x38
55
#define ADXL345_FIFO_STATUS 0x39
56
 
57
#define ADXL345_BW_1600 0xF // 1111
58
#define ADXL345_BW_800  0xE // 1110
59
#define ADXL345_BW_400  0xD // 1101  
60
#define ADXL345_BW_200  0xC // 1100
61
#define ADXL345_BW_100  0xB // 1011  
62
#define ADXL345_BW_50   0xA // 1010 
63
#define ADXL345_BW_25   0x9 // 1001 
64
#define ADXL345_BW_12   0x8 // 1000 
65
#define ADXL345_BW_6    0x7 // 0111
66
#define ADXL345_BW_3    0x6 // 0110
67
 
68
 
69
/*
70
 Interrupt PINs
71
 INT1: 0
72
 INT2: 1
73
 */
74
#define ADXL345_INT1_PIN 0x00
75
#define ADXL345_INT2_PIN 0x01
76
 
77
/*
78
 Interrupt bit position
79
 */
80
#define ADXL345_INT_DATA_READY_BIT 0x07
81
#define ADXL345_INT_SINGLE_TAP_BIT 0x06
82
#define ADXL345_INT_DOUBLE_TAP_BIT 0x05
83
#define ADXL345_INT_ACTIVITY_BIT   0x04
84
#define ADXL345_INT_INACTIVITY_BIT 0x03
85
#define ADXL345_INT_FREE_FALL_BIT  0x02
86
#define ADXL345_INT_WATERMARK_BIT  0x01
87
#define ADXL345_INT_OVERRUNY_BIT   0x00
88
 
89
#define ADXL345_OK    1 // no error
90
#define ADXL345_ERROR 0 // indicates error is predent
91
 
92
#define ADXL345_NO_ERROR   0 // initial state
93
#define ADXL345_READ_ERROR 1 // problem reading accel
94
#define ADXL345_BAD_ARG    2 // bad method argument
95
 
96
class ADXL345
97
{
98
public:
99
  bool status;           // set when error occurs 
100
                         // see error code for details
101
  byte error_code;       // Initial state
102
  float gains[3];        // counts to Gs
103
 
104
  ADXL345();
105
  void init(int address);
106
  void powerOn();
107
  void readAccel(int* xyx);
108
  void readAccel(int* x, int* y, int* z);
109
  void get_Gxyz(float *xyz);
110
 
111
  void setTapThreshold(int tapThreshold);
112
  int getTapThreshold();
113
  void setAxisGains(float *_gains);
114
  void getAxisGains(float *_gains);
115
  void setAxisOffset(int x, int y, int z);
116
  void getAxisOffset(int* x, int* y, int*z);
117
  void setTapDuration(int tapDuration);
118
  int getTapDuration();
119
  void setDoubleTapLatency(int floatTapLatency);
120
  int getDoubleTapLatency();
121
  void setDoubleTapWindow(int floatTapWindow);
122
  int getDoubleTapWindow();
123
  void setActivityThreshold(int activityThreshold);
124
  int getActivityThreshold();
125
  void setInactivityThreshold(int inactivityThreshold);
126
  int getInactivityThreshold();
127
  void setTimeInactivity(int timeInactivity);
128
  int getTimeInactivity();
129
  void setFreeFallThreshold(int freeFallthreshold);
130
  int getFreeFallThreshold();
131
  void setFreeFallDuration(int freeFallDuration);
132
  int getFreeFallDuration();
133
 
134
  bool isActivityXEnabled();
135
  bool isActivityYEnabled();
136
  bool isActivityZEnabled();
137
  bool isInactivityXEnabled();
138
  bool isInactivityYEnabled();
139
  bool isInactivityZEnabled();
140
  bool isActivityAc();
141
  bool isInactivityAc();
142
  void setActivityAc(bool state);
143
  void setInactivityAc(bool state);
144
 
145
  bool getSuppressBit();
146
  void setSuppressBit(bool state);
147
  bool isTapDetectionOnX();
148
  void setTapDetectionOnX(bool state);
149
  bool isTapDetectionOnY();
150
  void setTapDetectionOnY(bool state);
151
  bool isTapDetectionOnZ();
152
  void setTapDetectionOnZ(bool state);
153
 
154
  void setActivityX(bool state);
155
  void setActivityY(bool state);
156
  void setActivityZ(bool state);
157
  void setInactivityX(bool state);
158
  void setInactivityY(bool state);
159
  void setInactivityZ(bool state);
160
 
161
  bool isActivitySourceOnX();
162
  bool isActivitySourceOnY();
163
  bool isActivitySourceOnZ();
164
  bool isTapSourceOnX();
165
  bool isTapSourceOnY();
166
  bool isTapSourceOnZ();
167
  bool isAsleep();
168
 
169
  bool isLowPower();
170
  void setLowPower(bool state);
171
  float getRate();
172
  void setRate(float rate);
173
  void set_bw(byte bw_code);
174
  byte get_bw_code();  
175
 
176
  byte getInterruptSource();
177
  bool getInterruptSource(byte interruptBit);
178
  bool getInterruptMapping(byte interruptBit);
179
  void setInterruptMapping(byte interruptBit, bool interruptPin);
180
  bool isInterruptEnabled(byte interruptBit);
181
  void setInterrupt(byte interruptBit, bool state);
182
 
183
  void getRangeSetting(byte* rangeSetting);
184
  void setRangeSetting(int val);
185
  bool getSelfTestBit();
186
  void setSelfTestBit(bool selfTestBit);
187
  bool getSpiBit();
188
  void setSpiBit(bool spiBit);
189
  bool getInterruptLevelBit();
190
  void setInterruptLevelBit(bool interruptLevelBit);
191
  bool getFullResBit();
192
  void setFullResBit(bool fullResBit);
193
  bool getJustifyBit();
194
  void setJustifyBit(bool justifyBit);
195
  void printAllRegister();
196
  void writeTo(byte address, byte val);
197
 
198
private:
199
  void readFrom(byte address, int num, byte buff[]);
200
  void setRegisterBit(byte regAdress, int bitPos, bool state);
201
  bool getRegisterBit(byte regAdress, int bitPos);  
202
  byte _buff[6] ;    //6 bytes buffer for saving data read from the device
203
  int _dev_address;
204
};
205
void print_byte(byte val);
206
#endif