df

Dependencies:   mbed

Fork of APP1 by Team APP

Committer:
dupm2216
Date:
Sat Jan 14 22:37:34 2017 +0000
Revision:
3:1a9d0f0a50bf
Parent:
1:7becb0e903e3
Child:
4:303fb83498fd
Convert raw axis data to int; ; Agreed, it kind of looks dumb; ; Also:; - Move Tests in TestAccelerometer; - Accelerometer constructor now takes PinName's instead of a I2C device

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 3:1a9d0f0a50bf 25 Accelerometer::Accelerometer(
dupm2216 3:1a9d0f0a50bf 26 PinName sda_pin,
dupm2216 3:1a9d0f0a50bf 27 PinName scl_pin,
dupm2216 3:1a9d0f0a50bf 28 const int slave_address
dupm2216 3:1a9d0f0a50bf 29 ) :
dupm2216 3:1a9d0f0a50bf 30 device(sda_pin, scl_pin),
dupm2216 1:7becb0e903e3 31 slave_address(slave_address)
dupm2216 1:7becb0e903e3 32 {
dupm2216 1:7becb0e903e3 33 }
dupm2216 1:7becb0e903e3 34
dupm2216 1:7becb0e903e3 35 void Accelerometer::write_register(const char register_address, const char new_value)
dupm2216 1:7becb0e903e3 36 {
dupm2216 1:7becb0e903e3 37 const int left_shifted_slave_address = slave_address << 1;
dupm2216 1:7becb0e903e3 38 char data[2];
dupm2216 1:7becb0e903e3 39 data[0] = register_address;
dupm2216 1:7becb0e903e3 40 data[1] = new_value;
dupm2216 1:7becb0e903e3 41
dupm2216 1:7becb0e903e3 42 const int write_return = device.write(left_shifted_slave_address, data, 2);
dupm2216 1:7becb0e903e3 43 if(write_return < 0)
dupm2216 1:7becb0e903e3 44 {
dupm2216 1:7becb0e903e3 45 printf("Write error: I2C error");
dupm2216 1:7becb0e903e3 46 }
dupm2216 1:7becb0e903e3 47 }
dupm2216 1:7becb0e903e3 48
dupm2216 1:7becb0e903e3 49 char Accelerometer::read_register(const char register_address)
dupm2216 1:7becb0e903e3 50 {
dupm2216 1:7becb0e903e3 51 char result;
dupm2216 1:7becb0e903e3 52 const int left_shifted_slave_address = slave_address << 1;
dupm2216 1:7becb0e903e3 53
dupm2216 1:7becb0e903e3 54 const int write_return = device.write(left_shifted_slave_address, &register_address, 1, true);
dupm2216 1:7becb0e903e3 55 if(write_return < 0)
dupm2216 1:7becb0e903e3 56 {
dupm2216 1:7becb0e903e3 57 printf("Write error: I2C error");
dupm2216 1:7becb0e903e3 58 }
dupm2216 1:7becb0e903e3 59
dupm2216 1:7becb0e903e3 60 const int read_return = device.read(left_shifted_slave_address, &result, 1);
dupm2216 1:7becb0e903e3 61 if(read_return != 0)
dupm2216 1:7becb0e903e3 62 {
dupm2216 1:7becb0e903e3 63 printf("Read error: I2C error (nack)");
dupm2216 1:7becb0e903e3 64 }
dupm2216 1:7becb0e903e3 65
dupm2216 1:7becb0e903e3 66 return result;
dupm2216 1:7becb0e903e3 67 }
dupm2216 1:7becb0e903e3 68
dupm2216 1:7becb0e903e3 69 //axis_data must be an array of 6 bytes
dupm2216 3:1a9d0f0a50bf 70 void Accelerometer::read_all_axis(signed char* axis_data)
dupm2216 1:7becb0e903e3 71 {
dupm2216 1:7becb0e903e3 72 for(int i = 0; i < NUMBER_OF_DATA_REGISTERS; ++i)
dupm2216 1:7becb0e903e3 73 {
dupm2216 1:7becb0e903e3 74 const char current_register = OUT_X_MSB_REGISTER + i;
dupm2216 1:7becb0e903e3 75 axis_data[i] = read_register(current_register);
dupm2216 1:7becb0e903e3 76 }
dupm2216 1:7becb0e903e3 77 }
dupm2216 1:7becb0e903e3 78
dupm2216 1:7becb0e903e3 79 void Accelerometer::print_all_axis_data()
dupm2216 1:7becb0e903e3 80 {
dupm2216 3:1a9d0f0a50bf 81 signed char axis_data[NUMBER_OF_DATA_REGISTERS];
dupm2216 1:7becb0e903e3 82 for(int i=0; i<NUMBER_OF_DATA_REGISTERS; i++)
dupm2216 1:7becb0e903e3 83 {
dupm2216 1:7becb0e903e3 84 axis_data[i] = 0;
dupm2216 1:7becb0e903e3 85 }
dupm2216 1:7becb0e903e3 86
dupm2216 1:7becb0e903e3 87 read_all_axis(axis_data);
dupm2216 1:7becb0e903e3 88
dupm2216 1:7becb0e903e3 89 printf("Register content: ");
dupm2216 1:7becb0e903e3 90 for(int i=0; i<NUMBER_OF_DATA_REGISTERS; i++)
dupm2216 1:7becb0e903e3 91 {
dupm2216 1:7becb0e903e3 92 const int current_data = (int)(axis_data[i]);
dupm2216 1:7becb0e903e3 93 printf("%d, ", current_data);
dupm2216 1:7becb0e903e3 94 }
dupm2216 1:7becb0e903e3 95 printf("\r\n");
dupm2216 1:7becb0e903e3 96 }
dupm2216 1:7becb0e903e3 97
dupm2216 1:7becb0e903e3 98 void Accelerometer::set_standby()
dupm2216 1:7becb0e903e3 99 {
dupm2216 1:7becb0e903e3 100 const char previous_ctrl_reg1 = read_register(CTRL_REG1_REGISTER_ADDRESS);
dupm2216 1:7becb0e903e3 101 const char new_ctrl_reg1_value = previous_ctrl_reg1 & ~(0x01);
dupm2216 1:7becb0e903e3 102 write_register(CTRL_REG1_REGISTER_ADDRESS, new_ctrl_reg1_value);
dupm2216 1:7becb0e903e3 103 }
dupm2216 1:7becb0e903e3 104
dupm2216 1:7becb0e903e3 105 void Accelerometer::set_active()
dupm2216 1:7becb0e903e3 106 {
dupm2216 1:7becb0e903e3 107 const char previous_ctrl_reg1 = read_register(CTRL_REG1_REGISTER_ADDRESS);
dupm2216 1:7becb0e903e3 108 const char new_ctrl_reg1_value = previous_ctrl_reg1 | 0x01;
dupm2216 1:7becb0e903e3 109 write_register(CTRL_REG1_REGISTER_ADDRESS, new_ctrl_reg1_value);
dupm2216 1:7becb0e903e3 110 }
dupm2216 1:7becb0e903e3 111
dupm2216 1:7becb0e903e3 112 void Accelerometer::init()
dupm2216 1:7becb0e903e3 113 {
dupm2216 1:7becb0e903e3 114 set_active();
dupm2216 3:1a9d0f0a50bf 115 }
dupm2216 3:1a9d0f0a50bf 116
dupm2216 3:1a9d0f0a50bf 117 int Accelerometer::read_axis_data_8_bits(Axis axis)
dupm2216 3:1a9d0f0a50bf 118 {
dupm2216 3:1a9d0f0a50bf 119 const char axis_register = get_axis_register(axis);
dupm2216 3:1a9d0f0a50bf 120 const char register_value = read_register(axis_register);
dupm2216 3:1a9d0f0a50bf 121 return raw_axis_data_to_int(register_value);
dupm2216 1:7becb0e903e3 122 }