Bruno Allaire-Lemay
/
APP1test
df
Fork of APP1 by
Diff: Accelerometer.cpp
- Revision:
- 3:1a9d0f0a50bf
- Parent:
- 1:7becb0e903e3
- Child:
- 4:303fb83498fd
--- a/Accelerometer.cpp Sat Jan 14 18:53:39 2017 +0000 +++ b/Accelerometer.cpp Sat Jan 14 22:37:34 2017 +0000 @@ -1,10 +1,35 @@ #include "Accelerometer.hpp" -Accelerometer::Accelerometer(I2C& device, const int slave_address) : - device(device), +//Compute inverse two's complement to obtain a signed value +//See page 21: https://www.gel.usherbrooke.ca/s5info/h17/doc/app1/file/MMA8452Q.pdf +//See: https://en.wikipedia.org/wiki/Two's_complement +//Turns out, the signed char does it for free +//We have to specify "signed char" because the "standard does not specify if plain char is signed or unsigned" +//http://stackoverflow.com/a/2054941/3212785 +int raw_axis_data_to_int(signed char raw_axis_data) +{ + return raw_axis_data; +} + +char get_axis_register(Axis axis) +{ + switch(axis) + { + case AXIS_X: return OUT_X_MSB_REGISTER; + case AXIS_Y: return OUT_Y_MSB_REGISTER; + case AXIS_Z: return OUT_Z_MSB_REGISTER; + default: return AXIS_INVALID; + } +} + +Accelerometer::Accelerometer( + PinName sda_pin, + PinName scl_pin, + const int slave_address + ) : + device(sda_pin, scl_pin), slave_address(slave_address) { - } void Accelerometer::write_register(const char register_address, const char new_value) @@ -42,7 +67,7 @@ } //axis_data must be an array of 6 bytes -void Accelerometer::read_all_axis(char* axis_data) +void Accelerometer::read_all_axis(signed char* axis_data) { for(int i = 0; i < NUMBER_OF_DATA_REGISTERS; ++i) { @@ -53,7 +78,7 @@ void Accelerometer::print_all_axis_data() { - char axis_data[NUMBER_OF_DATA_REGISTERS]; + signed char axis_data[NUMBER_OF_DATA_REGISTERS]; for(int i=0; i<NUMBER_OF_DATA_REGISTERS; i++) { axis_data[i] = 0; @@ -87,4 +112,11 @@ void Accelerometer::init() { set_active(); +} + +int Accelerometer::read_axis_data_8_bits(Axis axis) +{ + const char axis_register = get_axis_register(axis); + const char register_value = read_register(axis_register); + return raw_axis_data_to_int(register_value); } \ No newline at end of file