Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
libraries/tests/mbed/i2c_MMA8451Q/main.cpp@0:6c56fb4bc5f0, 2016-11-04 (annotated)
- Committer:
- nexpaq
- Date:
- Fri Nov 04 20:27:58 2016 +0000
- Revision:
- 0:6c56fb4bc5f0
Moving to library for sharing updates
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| nexpaq | 0:6c56fb4bc5f0 | 1 | #include "mbed.h" |
| nexpaq | 0:6c56fb4bc5f0 | 2 | #include "MMA8451Q.h" |
| nexpaq | 0:6c56fb4bc5f0 | 3 | #include "test_env.h" |
| nexpaq | 0:6c56fb4bc5f0 | 4 | |
| nexpaq | 0:6c56fb4bc5f0 | 5 | #ifdef TARGET_KL05Z |
| nexpaq | 0:6c56fb4bc5f0 | 6 | #define SDA PTB4 |
| nexpaq | 0:6c56fb4bc5f0 | 7 | #define SCL PTB3 |
| nexpaq | 0:6c56fb4bc5f0 | 8 | |
| nexpaq | 0:6c56fb4bc5f0 | 9 | #elif TARGET_K20D50M |
| nexpaq | 0:6c56fb4bc5f0 | 10 | #define SDA PTB1 |
| nexpaq | 0:6c56fb4bc5f0 | 11 | #define SCL PTB0 |
| nexpaq | 0:6c56fb4bc5f0 | 12 | |
| nexpaq | 0:6c56fb4bc5f0 | 13 | #else |
| nexpaq | 0:6c56fb4bc5f0 | 14 | #define SDA PTE25 |
| nexpaq | 0:6c56fb4bc5f0 | 15 | #define SCL PTE24 |
| nexpaq | 0:6c56fb4bc5f0 | 16 | #endif |
| nexpaq | 0:6c56fb4bc5f0 | 17 | |
| nexpaq | 0:6c56fb4bc5f0 | 18 | namespace { |
| nexpaq | 0:6c56fb4bc5f0 | 19 | const int MMA8451_I2C_ADDRESS = 0x1D << 1; // I2C bus address |
| nexpaq | 0:6c56fb4bc5f0 | 20 | const float MMA8451_DIGITAL_SENSITIVITY = 4096.0; // Counts/g |
| nexpaq | 0:6c56fb4bc5f0 | 21 | } |
| nexpaq | 0:6c56fb4bc5f0 | 22 | |
| nexpaq | 0:6c56fb4bc5f0 | 23 | float calc_3d_vector_len(float x, float y, float z) { |
| nexpaq | 0:6c56fb4bc5f0 | 24 | return sqrt(x*x + y*y + z*z); |
| nexpaq | 0:6c56fb4bc5f0 | 25 | } |
| nexpaq | 0:6c56fb4bc5f0 | 26 | |
| nexpaq | 0:6c56fb4bc5f0 | 27 | #define TEST_ITERATIONS 25 |
| nexpaq | 0:6c56fb4bc5f0 | 28 | #define TEST_ITERATIONS_SKIP 5 |
| nexpaq | 0:6c56fb4bc5f0 | 29 | #define MEASURE_DEVIATION_TOLERANCE 0.025 // 2.5% |
| nexpaq | 0:6c56fb4bc5f0 | 30 | |
| nexpaq | 0:6c56fb4bc5f0 | 31 | int main(void) { |
| nexpaq | 0:6c56fb4bc5f0 | 32 | MBED_HOSTTEST_TIMEOUT(15); |
| nexpaq | 0:6c56fb4bc5f0 | 33 | MBED_HOSTTEST_SELECT(default_auto); |
| nexpaq | 0:6c56fb4bc5f0 | 34 | MBED_HOSTTEST_DESCRIPTION(MMA8451Q accelerometer); |
| nexpaq | 0:6c56fb4bc5f0 | 35 | MBED_HOSTTEST_START("KL25Z_5"); |
| nexpaq | 0:6c56fb4bc5f0 | 36 | |
| nexpaq | 0:6c56fb4bc5f0 | 37 | DigitalOut led(LED_GREEN); |
| nexpaq | 0:6c56fb4bc5f0 | 38 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
| nexpaq | 0:6c56fb4bc5f0 | 39 | bool result = true; |
| nexpaq | 0:6c56fb4bc5f0 | 40 | printf("WHO AM I: 0x%2X\r\n\n", acc.getWhoAmI()); |
| nexpaq | 0:6c56fb4bc5f0 | 41 | |
| nexpaq | 0:6c56fb4bc5f0 | 42 | for (int i = 0; i < TEST_ITERATIONS; i++) { |
| nexpaq | 0:6c56fb4bc5f0 | 43 | if (i < TEST_ITERATIONS_SKIP) { |
| nexpaq | 0:6c56fb4bc5f0 | 44 | // Skip first 5 measurements |
| nexpaq | 0:6c56fb4bc5f0 | 45 | continue; |
| nexpaq | 0:6c56fb4bc5f0 | 46 | } |
| nexpaq | 0:6c56fb4bc5f0 | 47 | const float g_vect_len = calc_3d_vector_len(acc.getAccX(), acc.getAccY(), acc.getAccZ()) / MMA8451_DIGITAL_SENSITIVITY; |
| nexpaq | 0:6c56fb4bc5f0 | 48 | const float deviation = fabs(g_vect_len - 1.0); |
| nexpaq | 0:6c56fb4bc5f0 | 49 | const char *succes_str = deviation <= MEASURE_DEVIATION_TOLERANCE ? "[OK]" : "[FAIL]"; |
| nexpaq | 0:6c56fb4bc5f0 | 50 | result = result && (deviation <= MEASURE_DEVIATION_TOLERANCE); |
| nexpaq | 0:6c56fb4bc5f0 | 51 | printf("X:% 6d Y:% 6d Z:% 5d GF:%0.3fg, dev:%0.3f ... %s\r\n", acc.getAccX(), acc.getAccY(), acc.getAccZ(), g_vect_len, deviation, succes_str); |
| nexpaq | 0:6c56fb4bc5f0 | 52 | wait(0.5); |
| nexpaq | 0:6c56fb4bc5f0 | 53 | led = !led; |
| nexpaq | 0:6c56fb4bc5f0 | 54 | } |
| nexpaq | 0:6c56fb4bc5f0 | 55 | MBED_HOSTTEST_RESULT(result); |
| nexpaq | 0:6c56fb4bc5f0 | 56 | } |