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 1:7becb0e903e3 1 #include "Accelerometer.hpp"
dupm2216 1:7becb0e903e3 2
dupm2216 3:1a9d0f0a50bf 3 //Compute inverse two's complement to obtain a signed value
dupm2216 3:1a9d0f0a50bf 4 //See page 21: https://www.gel.usherbrooke.ca/s5info/h17/doc/app1/file/MMA8452Q.pdf
dupm2216 3:1a9d0f0a50bf 5 //See: https://en.wikipedia.org/wiki/Two's_complement
dupm2216 3:1a9d0f0a50bf 6 //Turns out, the signed char does it for free
dupm2216 3:1a9d0f0a50bf 7 //We have to specify "signed char" because the "standard does not specify if plain char is signed or unsigned"
dupm2216 3:1a9d0f0a50bf 8 //http://stackoverflow.com/a/2054941/3212785
dupm2216 3:1a9d0f0a50bf 9 int raw_axis_data_to_int(signed char raw_axis_data)
dupm2216 3:1a9d0f0a50bf 10 {
dupm2216 3:1a9d0f0a50bf 11 return raw_axis_data;
dupm2216 3:1a9d0f0a50bf 12 }
dupm2216 3:1a9d0f0a50bf 13
dupm2216 3:1a9d0f0a50bf 14 char get_axis_register(Axis axis)
dupm2216 3:1a9d0f0a50bf 15 {
dupm2216 3:1a9d0f0a50bf 16 switch(axis)
dupm2216 3:1a9d0f0a50bf 17 {
dupm2216 3:1a9d0f0a50bf 18 case AXIS_X: return OUT_X_MSB_REGISTER;
dupm2216 3:1a9d0f0a50bf 19 case AXIS_Y: return OUT_Y_MSB_REGISTER;
dupm2216 3:1a9d0f0a50bf 20 case AXIS_Z: return OUT_Z_MSB_REGISTER;
dupm2216 3:1a9d0f0a50bf 21 default: return AXIS_INVALID;
dupm2216 3:1a9d0f0a50bf 22 }
dupm2216 3:1a9d0f0a50bf 23 }
dupm2216 3:1a9d0f0a50bf 24
dupm2216 4:303fb83498fd 25 bool is_almost_equal(double a, double b, double tolerance)
dupm2216 4:303fb83498fd 26 {
dupm2216 4:303fb83498fd 27 double difference = std::abs(a-b);
dupm2216 4:303fb83498fd 28 return (difference <= tolerance);
dupm2216 4:303fb83498fd 29 }
dupm2216 4:303fb83498fd 30
dupm2216 3:1a9d0f0a50bf 31 Accelerometer::Accelerometer(
dupm2216 3:1a9d0f0a50bf 32 PinName sda_pin,
dupm2216 3:1a9d0f0a50bf 33 PinName scl_pin,
dupm2216 3:1a9d0f0a50bf 34 const int slave_address
dupm2216 3:1a9d0f0a50bf 35 ) :
dupm2216 3:1a9d0f0a50bf 36 device(sda_pin, scl_pin),
dupm2216 1:7becb0e903e3 37 slave_address(slave_address)
dupm2216 1:7becb0e903e3 38 {
dupm2216 1:7becb0e903e3 39 }
dupm2216 1:7becb0e903e3 40
dupm2216 1:7becb0e903e3 41 void Accelerometer::write_register(const char register_address, const char new_value)
dupm2216 1:7becb0e903e3 42 {
dupm2216 1:7becb0e903e3 43 const int left_shifted_slave_address = slave_address << 1;
dupm2216 1:7becb0e903e3 44 char data[2];
dupm2216 1:7becb0e903e3 45 data[0] = register_address;
dupm2216 1:7becb0e903e3 46 data[1] = new_value;
dupm2216 1:7becb0e903e3 47
dupm2216 1:7becb0e903e3 48 const int write_return = device.write(left_shifted_slave_address, data, 2);
dupm2216 1:7becb0e903e3 49 if(write_return < 0)
dupm2216 1:7becb0e903e3 50 {
dupm2216 1:7becb0e903e3 51 printf("Write error: I2C error");
dupm2216 1:7becb0e903e3 52 }
dupm2216 1:7becb0e903e3 53 }
dupm2216 1:7becb0e903e3 54
dupm2216 1:7becb0e903e3 55 char Accelerometer::read_register(const char register_address)
dupm2216 1:7becb0e903e3 56 {
dupm2216 1:7becb0e903e3 57 char result;
dupm2216 1:7becb0e903e3 58 const int left_shifted_slave_address = slave_address << 1;
dupm2216 1:7becb0e903e3 59
dupm2216 1:7becb0e903e3 60 const int write_return = device.write(left_shifted_slave_address, &register_address, 1, true);
dupm2216 1:7becb0e903e3 61 if(write_return < 0)
dupm2216 1:7becb0e903e3 62 {
dupm2216 1:7becb0e903e3 63 printf("Write error: I2C error");
dupm2216 1:7becb0e903e3 64 }
dupm2216 1:7becb0e903e3 65
dupm2216 1:7becb0e903e3 66 const int read_return = device.read(left_shifted_slave_address, &result, 1);
dupm2216 1:7becb0e903e3 67 if(read_return != 0)
dupm2216 1:7becb0e903e3 68 {
dupm2216 1:7becb0e903e3 69 printf("Read error: I2C error (nack)");
dupm2216 1:7becb0e903e3 70 }
dupm2216 1:7becb0e903e3 71
dupm2216 1:7becb0e903e3 72 return result;
dupm2216 1:7becb0e903e3 73 }
dupm2216 1:7becb0e903e3 74
dupm2216 1:7becb0e903e3 75 //axis_data must be an array of 6 bytes
dupm2216 3:1a9d0f0a50bf 76 void Accelerometer::read_all_axis(signed char* axis_data)
dupm2216 1:7becb0e903e3 77 {
dupm2216 1:7becb0e903e3 78 for(int i = 0; i < NUMBER_OF_DATA_REGISTERS; ++i)
dupm2216 1:7becb0e903e3 79 {
dupm2216 1:7becb0e903e3 80 const char current_register = OUT_X_MSB_REGISTER + i;
dupm2216 1:7becb0e903e3 81 axis_data[i] = read_register(current_register);
dupm2216 1:7becb0e903e3 82 }
dupm2216 1:7becb0e903e3 83 }
dupm2216 1:7becb0e903e3 84
dupm2216 1:7becb0e903e3 85 void Accelerometer::print_all_axis_data()
dupm2216 1:7becb0e903e3 86 {
dupm2216 3:1a9d0f0a50bf 87 signed char axis_data[NUMBER_OF_DATA_REGISTERS];
dupm2216 1:7becb0e903e3 88 for(int i=0; i<NUMBER_OF_DATA_REGISTERS; i++)
dupm2216 1:7becb0e903e3 89 {
dupm2216 1:7becb0e903e3 90 axis_data[i] = 0;
dupm2216 1:7becb0e903e3 91 }
dupm2216 1:7becb0e903e3 92
dupm2216 1:7becb0e903e3 93 read_all_axis(axis_data);
dupm2216 1:7becb0e903e3 94
dupm2216 1:7becb0e903e3 95 printf("Register content: ");
dupm2216 1:7becb0e903e3 96 for(int i=0; i<NUMBER_OF_DATA_REGISTERS; i++)
dupm2216 1:7becb0e903e3 97 {
dupm2216 1:7becb0e903e3 98 const int current_data = (int)(axis_data[i]);
dupm2216 1:7becb0e903e3 99 printf("%d, ", current_data);
dupm2216 1:7becb0e903e3 100 }
dupm2216 1:7becb0e903e3 101 printf("\r\n");
dupm2216 1:7becb0e903e3 102 }
dupm2216 1:7becb0e903e3 103
dupm2216 1:7becb0e903e3 104 void Accelerometer::set_standby()
dupm2216 1:7becb0e903e3 105 {
dupm2216 1:7becb0e903e3 106 const char previous_ctrl_reg1 = read_register(CTRL_REG1_REGISTER_ADDRESS);
dupm2216 1:7becb0e903e3 107 const char new_ctrl_reg1_value = previous_ctrl_reg1 & ~(0x01);
dupm2216 1:7becb0e903e3 108 write_register(CTRL_REG1_REGISTER_ADDRESS, new_ctrl_reg1_value);
dupm2216 1:7becb0e903e3 109 }
dupm2216 1:7becb0e903e3 110
dupm2216 1:7becb0e903e3 111 void Accelerometer::set_active()
dupm2216 1:7becb0e903e3 112 {
dupm2216 1:7becb0e903e3 113 const char previous_ctrl_reg1 = read_register(CTRL_REG1_REGISTER_ADDRESS);
dupm2216 1:7becb0e903e3 114 const char new_ctrl_reg1_value = previous_ctrl_reg1 | 0x01;
dupm2216 1:7becb0e903e3 115 write_register(CTRL_REG1_REGISTER_ADDRESS, new_ctrl_reg1_value);
dupm2216 1:7becb0e903e3 116 }
dupm2216 1:7becb0e903e3 117
dupm2216 1:7becb0e903e3 118 void Accelerometer::init()
dupm2216 1:7becb0e903e3 119 {
dupm2216 1:7becb0e903e3 120 set_active();
dupm2216 3:1a9d0f0a50bf 121 }
dupm2216 3:1a9d0f0a50bf 122
dupm2216 3:1a9d0f0a50bf 123 int Accelerometer::read_axis_data_8_bits(Axis axis)
dupm2216 3:1a9d0f0a50bf 124 {
dupm2216 3:1a9d0f0a50bf 125 const char axis_register = get_axis_register(axis);
dupm2216 3:1a9d0f0a50bf 126 const char register_value = read_register(axis_register);
dupm2216 3:1a9d0f0a50bf 127 return raw_axis_data_to_int(register_value);
dupm2216 1:7becb0e903e3 128 }