summaryrefslogtreecommitdiff
path: root/tmk_core/tool/mbed/mbed-sdk/libraries/tests/mbed/spi_ADXL345/main.cpp
blob: 11055ebfa17967592e811f2884ca412683eaf218 (plain)
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
#include "test_env.h"
#include "ADXL345.h"

#if defined(TARGET_LPC812)
ADXL345 accelerometer(D10, D11, D12, D13);

#else
ADXL345 accelerometer(p5, p6, p7, p8);
#endif

// Assume test configuration on a plane (small x and y, z ~ g)
#define MAX_X_Y     (50)
#define MIN_Z       (200)
#define MAX_Z       (300)

void check_X_Y(int v) {
    int16_t a = (int16_t)v;
    if (abs(a) > MAX_X_Y) {
        printf("X/Y acceleration is too big: %d\n", a);
        notify_completion(false);
    }
}


int main() {
    int readings[3] = {0, 0, 0};

    printf("Starting ADXL345 test...\n");
    printf("Device ID is: 0x%02x\n", accelerometer.getDevId());

    //Go into standby mode to configure the device.
    accelerometer.setPowerControl(0x00);

    //Full resolution, +/-16g, 4mg/LSB.
    accelerometer.setDataFormatControl(0x0B);

    //3.2kHz data rate.
    accelerometer.setDataRate(ADXL345_3200HZ);

    //Measurement mode.
    accelerometer.setPowerControl(0x08);

    for (int i=0; i<3; i++) {
        wait(0.1);

        //13-bit, sign extended values.
        accelerometer.getOutput(readings);

        // X and Y
        check_X_Y(readings[0]);
        check_X_Y(readings[1]);

        // Z
        int16_t z = (int16_t)readings[2];
        if ((z < MIN_Z) || (z > MAX_Z)) {
            printf("Z acceleration not within expected range\n", z);
            notify_completion(false);
        }
    }

    notify_completion(true);
}