]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/tests/mbed/spi_ADXL345/main.cpp
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / tests / mbed / spi_ADXL345 / main.cpp
1 #include "test_env.h"
2 #include "ADXL345.h"
3
4 #if defined(TARGET_LPC812)
5 ADXL345 accelerometer(D10, D11, D12, D13);
6
7 #else
8 ADXL345 accelerometer(p5, p6, p7, p8);
9 #endif
10
11 // Assume test configuration on a plane (small x and y, z ~ g)
12 #define MAX_X_Y (50)
13 #define MIN_Z (200)
14 #define MAX_Z (300)
15
16 void check_X_Y(int v) {
17 int16_t a = (int16_t)v;
18 if (abs(a) > MAX_X_Y) {
19 printf("X/Y acceleration is too big: %d\n", a);
20 notify_completion(false);
21 }
22 }
23
24
25 int main() {
26 int readings[3] = {0, 0, 0};
27
28 printf("Starting ADXL345 test...\n");
29 printf("Device ID is: 0x%02x\n", accelerometer.getDevId());
30
31 //Go into standby mode to configure the device.
32 accelerometer.setPowerControl(0x00);
33
34 //Full resolution, +/-16g, 4mg/LSB.
35 accelerometer.setDataFormatControl(0x0B);
36
37 //3.2kHz data rate.
38 accelerometer.setDataRate(ADXL345_3200HZ);
39
40 //Measurement mode.
41 accelerometer.setPowerControl(0x08);
42
43 for (int i=0; i<3; i++) {
44 wait(0.1);
45
46 //13-bit, sign extended values.
47 accelerometer.getOutput(readings);
48
49 // X and Y
50 check_X_Y(readings[0]);
51 check_X_Y(readings[1]);
52
53 // Z
54 int16_t z = (int16_t)readings[2];
55 if ((z < MIN_Z) || (z > MAX_Z)) {
56 printf("Z acceleration not within expected range\n", z);
57 notify_completion(false);
58 }
59 }
60
61 notify_completion(true);
62 }
Imprint / Impressum