df

Dependencies:   mbed

Fork of APP1 by Team APP

Committer:
dupm2216
Date:
Sun Jan 15 02:04:23 2017 +0000
Revision:
6:3facf0329142
Parent:
5:f59b51ac4b40
Child:
8:862e28c7f6f6
Move utility functions out of Accelerometer

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dupm2216 3:1a9d0f0a50bf 1 #include "TestAccelerometer.hpp"
dupm2216 3:1a9d0f0a50bf 2 #include "Accelerometer.hpp"
dupm2216 6:3facf0329142 3 #include "Utility.hpp"
dupm2216 3:1a9d0f0a50bf 4 #include "mbed.h"
dupm2216 3:1a9d0f0a50bf 5
dupm2216 3:1a9d0f0a50bf 6 #include <cassert>
dupm2216 5:f59b51ac4b40 7 #include <cmath>
dupm2216 3:1a9d0f0a50bf 8
dupm2216 3:1a9d0f0a50bf 9 namespace accelerometer
dupm2216 3:1a9d0f0a50bf 10 {
dupm2216 3:1a9d0f0a50bf 11 void run_all_tests()
dupm2216 3:1a9d0f0a50bf 12 {
dupm2216 3:1a9d0f0a50bf 13 test_raw_axis_data_to_int();
dupm2216 3:1a9d0f0a50bf 14 test_set_standby_and_active();
dupm2216 6:3facf0329142 15 test_g_force_from_int_axis_data();
dupm2216 6:3facf0329142 16 test_angle_from_int_axis_data();
dupm2216 3:1a9d0f0a50bf 17 }
dupm2216 3:1a9d0f0a50bf 18
dupm2216 3:1a9d0f0a50bf 19 void test_raw_axis_data_to_int()
dupm2216 3:1a9d0f0a50bf 20 {
dupm2216 3:1a9d0f0a50bf 21 assert(128 == (unsigned char)(0x80));
dupm2216 3:1a9d0f0a50bf 22 assert(-128 == (signed char)(0x80));
dupm2216 3:1a9d0f0a50bf 23
dupm2216 3:1a9d0f0a50bf 24 assert(0 == raw_axis_data_to_int(0x00));
dupm2216 3:1a9d0f0a50bf 25 assert(1 == raw_axis_data_to_int(0x01));
dupm2216 3:1a9d0f0a50bf 26 assert(127 == raw_axis_data_to_int(0x7F));
dupm2216 3:1a9d0f0a50bf 27 assert(-1 == raw_axis_data_to_int(0xFF));
dupm2216 3:1a9d0f0a50bf 28 assert(-128 == raw_axis_data_to_int(0x80));
dupm2216 3:1a9d0f0a50bf 29 }
dupm2216 3:1a9d0f0a50bf 30
dupm2216 3:1a9d0f0a50bf 31 void test_set_standby_and_active()
dupm2216 3:1a9d0f0a50bf 32 {
dupm2216 3:1a9d0f0a50bf 33 Accelerometer accelerometer(p9, p10, I2C_ACCELEROMETER_ADDRESS);
dupm2216 3:1a9d0f0a50bf 34
dupm2216 3:1a9d0f0a50bf 35 accelerometer.set_standby();
dupm2216 3:1a9d0f0a50bf 36 char value = accelerometer.read_register(CTRL_REG1_REGISTER_ADDRESS);
dupm2216 3:1a9d0f0a50bf 37 if(value % 2 != 0)
dupm2216 3:1a9d0f0a50bf 38 {
dupm2216 3:1a9d0f0a50bf 39 printf("Fail\r\n");
dupm2216 3:1a9d0f0a50bf 40 }
dupm2216 3:1a9d0f0a50bf 41
dupm2216 3:1a9d0f0a50bf 42 accelerometer.set_active();
dupm2216 3:1a9d0f0a50bf 43 value = accelerometer.read_register(CTRL_REG1_REGISTER_ADDRESS);
dupm2216 3:1a9d0f0a50bf 44 if(value % 2 != 1)
dupm2216 3:1a9d0f0a50bf 45 {
dupm2216 3:1a9d0f0a50bf 46 printf("Fail\r\n");
dupm2216 3:1a9d0f0a50bf 47 }
dupm2216 3:1a9d0f0a50bf 48 }
dupm2216 5:f59b51ac4b40 49
dupm2216 5:f59b51ac4b40 50 void test_angle_from_int_axis_data()
dupm2216 5:f59b51ac4b40 51 {
dupm2216 5:f59b51ac4b40 52 const double tolerance = 0.05;
dupm2216 6:3facf0329142 53 assert(utility::is_almost_equal(0, angle_from_int_axis_data(64), tolerance));
dupm2216 6:3facf0329142 54 assert(utility::is_almost_equal(0, angle_from_int_axis_data(-64), tolerance));
dupm2216 6:3facf0329142 55 assert(utility::is_almost_equal(60, angle_from_int_axis_data(32), tolerance));
dupm2216 6:3facf0329142 56 assert(utility::is_almost_equal(60, angle_from_int_axis_data(-32), tolerance));
dupm2216 5:f59b51ac4b40 57 }
dupm2216 5:f59b51ac4b40 58
dupm2216 5:f59b51ac4b40 59 void test_g_force_from_int_axis_data()
dupm2216 5:f59b51ac4b40 60 {
dupm2216 5:f59b51ac4b40 61 const double tolerance = 0.05;
dupm2216 6:3facf0329142 62 assert(utility::is_almost_equal(1, g_force_from_int_axis_data(64), tolerance));
dupm2216 6:3facf0329142 63 assert(utility::is_almost_equal(-1, g_force_from_int_axis_data(-64), tolerance));
dupm2216 6:3facf0329142 64 assert(utility::is_almost_equal(2, g_force_from_int_axis_data(128), tolerance));
dupm2216 6:3facf0329142 65 assert(utility::is_almost_equal(-2, g_force_from_int_axis_data(-128), tolerance));
dupm2216 6:3facf0329142 66 assert(utility::is_almost_equal(0.5, g_force_from_int_axis_data(32), tolerance));
dupm2216 6:3facf0329142 67 assert(utility::is_almost_equal(-0.5, g_force_from_int_axis_data(-32), tolerance));
dupm2216 5:f59b51ac4b40 68 }
dupm2216 3:1a9d0f0a50bf 69 }