df

Dependencies:   mbed

Fork of APP1 by Team APP

Committer:
dupm2216
Date:
Sat Jan 14 23:32:29 2017 +0000
Revision:
4:303fb83498fd
Parent:
3:1a9d0f0a50bf
Child:
5:f59b51ac4b40
Add function is_almost_equal

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 3:1a9d0f0a50bf 3 #include "mbed.h"
dupm2216 3:1a9d0f0a50bf 4
dupm2216 3:1a9d0f0a50bf 5 #include <cassert>
dupm2216 3:1a9d0f0a50bf 6
dupm2216 3:1a9d0f0a50bf 7 namespace accelerometer
dupm2216 3:1a9d0f0a50bf 8 {
dupm2216 3:1a9d0f0a50bf 9 void run_all_tests()
dupm2216 3:1a9d0f0a50bf 10 {
dupm2216 3:1a9d0f0a50bf 11 test_raw_axis_data_to_int();
dupm2216 3:1a9d0f0a50bf 12 test_set_standby_and_active();
dupm2216 4:303fb83498fd 13 test_is_almost_equal();
dupm2216 3:1a9d0f0a50bf 14 }
dupm2216 3:1a9d0f0a50bf 15
dupm2216 3:1a9d0f0a50bf 16 void test_raw_axis_data_to_int()
dupm2216 3:1a9d0f0a50bf 17 {
dupm2216 3:1a9d0f0a50bf 18 assert(128 == (unsigned char)(0x80));
dupm2216 3:1a9d0f0a50bf 19 assert(-128 == (signed char)(0x80));
dupm2216 3:1a9d0f0a50bf 20
dupm2216 3:1a9d0f0a50bf 21 assert(0 == raw_axis_data_to_int(0x00));
dupm2216 3:1a9d0f0a50bf 22 assert(1 == raw_axis_data_to_int(0x01));
dupm2216 3:1a9d0f0a50bf 23 assert(127 == raw_axis_data_to_int(0x7F));
dupm2216 3:1a9d0f0a50bf 24 assert(-1 == raw_axis_data_to_int(0xFF));
dupm2216 3:1a9d0f0a50bf 25 assert(-128 == raw_axis_data_to_int(0x80));
dupm2216 3:1a9d0f0a50bf 26 }
dupm2216 3:1a9d0f0a50bf 27
dupm2216 3:1a9d0f0a50bf 28 void test_set_standby_and_active()
dupm2216 3:1a9d0f0a50bf 29 {
dupm2216 3:1a9d0f0a50bf 30 Accelerometer accelerometer(p9, p10, I2C_ACCELEROMETER_ADDRESS);
dupm2216 3:1a9d0f0a50bf 31
dupm2216 3:1a9d0f0a50bf 32 accelerometer.set_standby();
dupm2216 3:1a9d0f0a50bf 33 char value = accelerometer.read_register(CTRL_REG1_REGISTER_ADDRESS);
dupm2216 3:1a9d0f0a50bf 34 if(value % 2 != 0)
dupm2216 3:1a9d0f0a50bf 35 {
dupm2216 3:1a9d0f0a50bf 36 printf("Fail\r\n");
dupm2216 3:1a9d0f0a50bf 37 }
dupm2216 3:1a9d0f0a50bf 38
dupm2216 3:1a9d0f0a50bf 39 accelerometer.set_active();
dupm2216 3:1a9d0f0a50bf 40 value = accelerometer.read_register(CTRL_REG1_REGISTER_ADDRESS);
dupm2216 3:1a9d0f0a50bf 41 if(value % 2 != 1)
dupm2216 3:1a9d0f0a50bf 42 {
dupm2216 3:1a9d0f0a50bf 43 printf("Fail\r\n");
dupm2216 3:1a9d0f0a50bf 44 }
dupm2216 3:1a9d0f0a50bf 45 }
dupm2216 4:303fb83498fd 46
dupm2216 4:303fb83498fd 47 void test_is_almost_equal()
dupm2216 4:303fb83498fd 48 {
dupm2216 4:303fb83498fd 49 assert(is_almost_equal(0, 0, 0.01));
dupm2216 4:303fb83498fd 50 assert(is_almost_equal(0.50, 0.51, 0.02));
dupm2216 4:303fb83498fd 51 assert(!is_almost_equal(0.50, 0.52, 0.01));
dupm2216 4:303fb83498fd 52 assert(!is_almost_equal(0, 0.50, 0.1));
dupm2216 4:303fb83498fd 53 }
dupm2216 3:1a9d0f0a50bf 54 }