Works with 8g
Fork of MMA8452 by
Diff: MMA8452.cpp
- Revision:
- 9:dfb0f6a7a455
- Parent:
- 8:89272163f395
- Child:
- 10:ca9ba7ad4e94
--- a/MMA8452.cpp Thu Oct 17 10:08:51 2013 +0000 +++ b/MMA8452.cpp Thu Oct 17 10:21:37 2013 +0000 @@ -50,25 +50,9 @@ // Get 'Fast Read Mode' called F_READ. If bit 1 is set '1' then F_READ is active. Fast read will skip LSB when reading xyz // resisters from 0x01 to 0x06. When F_READ is '0' then all 6 registers will be read. -int Accelerometer_MMA8452::get_CTRL_Reg1(int* CTRL_Reg) -{ - m_i2c.start(); - if( m_i2c.write(_writeAddress) == 0) - { - return 1; // we failed to write the mcu address on the bus to initiate dialogue - } - if( m_i2c.write( CTRL_REG_1) == 0) - { - return 1; // we failed to write 'status' to the chip - } - m_i2c.start(); - if( m_i2c.write(_readAddress) == 0) // this is asking to read the slave mcu address - even though it's a 'write' method!!! Crap API... - { - return 1; // we failed to request a read from that mcu - this really is just writing the mcu vaule on the bus - } - *CTRL_Reg = m_i2c.read(0); - m_i2c.stop(); - return 0; +int Accelerometer_MMA8452::get_CTRL_Reg1(int* dst) +{ + return read_reg(CTRL_REG_1,dst); } // Setting the control register bit 1 to true to activate the MMA8452 @@ -94,77 +78,24 @@ } // Get real time status of device - it can be STANDBY, WAKE or SLEEP -int Accelerometer_MMA8452::get_SystemMode(int& deviceSystemMode) +int Accelerometer_MMA8452::get_SystemMode(int *dst) { - m_i2c.start(); - if( m_i2c.write(_writeAddress) == 0) - { - return 1; // we failed to write the mcu address on the bus to initiate dialogue - } - if( m_i2c.write( SYSMOD) == 0) - { - return 1; // we failed to write 'status' to the chip - } - m_i2c.start(); - if( m_i2c.write(_readAddress) == 0) // this is asking to read the slave mcu address - even though it's a 'write' method!!! Crap API... - { - return 1; // we failed to request a read from that mcu - this really is just writing the mcu vaule on the bus - } - deviceSystemMode = m_i2c.read(0); - m_i2c.stop(); - return 0; - - + return read_reg(SYSMOD,dst); } // Get real time status of device - it can be STANDBY, WAKE or SLEEP -int Accelerometer_MMA8452::get_Status(int& deviceStatus) +int Accelerometer_MMA8452::get_Status(int* dst) { - m_i2c.start(); - if( m_i2c.write(_writeAddress) == 0) // tell the bus this is a write - { - return 1; // we failed to write the mcu address on the bus to initiate dialogue - } - if( m_i2c.write(STATUS) == 0) - { - return 1; // we failed to write 'status' to the chip - } - m_i2c.start(); - if( m_i2c.write(_readAddress) == 0) // this is asking to read the slave mcu address - even though it's a 'write' method!!! Crap API... - { - return 1; // we failed to request a read from that mcu - this really is just writing the mcu vaule on the bus - } - deviceStatus = m_i2c.read(0); - m_i2c.stop(); - return 0; - - + return read_reg(STATUS,dst); } // Get device ID -int Accelerometer_MMA8452::get_DeviceID(int& deviceID) +int Accelerometer_MMA8452::get_DeviceID(int *dst) { - m_i2c.start(); - if( m_i2c.write(_writeAddress) == 0) // just good practice to force bit 1 to a '0' by ANDing with 0xFE - { - return 1; // we failed to write the mcu address on the bus to initiate dialogue - } - if( m_i2c.write( WHO_AM_I) == 0) - { - return 1; // we failed to write 'who am i' to the chip - } - m_i2c.start(); - if( m_i2c.write(_readAddress) == 0) // this is asking to read the slave mcu address - even though it's a 'write' method!!! Crap API... - { - return 1; // we failed to request a read from that mcu - this really is just writing the mcu vaule on the bus - } - deviceID = m_i2c.read(0); - m_i2c.stop(); - return 0; - + return read_reg(WHO_AM_I,dst); } @@ -220,22 +151,7 @@ //int Accelerometer_MMA8452::read_x(int& xaxisLSB) int Accelerometer_MMA8452::read_x_raw(char *xaxis) { - // this is the register we want to get data from - char xaxis_register[1] = {OUT_X_MSB}; - //signed short s = 0; - - if(m_i2c.write(_writeAddress,xaxis_register,1,true) == 0) - { - if(m_i2c.read(_readAddress,xaxis,2)==0) - { - return 0; - } - } - - // failure case - xaxis[0] = 0x00; // make sure the array is set to zero - xaxis[1] = 0x00; - return 1; // failed to write and request the OUT_X_MSB bit + return read_raw(OUT_X_MSB,xaxis,2); } @@ -353,7 +269,7 @@ // Read from specified MMA7660FC register -char Accelerometer_MMA8452::read_reg(char addr) +int Accelerometer_MMA8452::read_reg(char addr, int *dst) { m_i2c.start(); @@ -361,7 +277,7 @@ { return 1; // we failed to write the mcu address on the bus to initiate dialogue } - if( m_i2c.write( addr) == 0) + if( m_i2c.write(addr) == 0) { return 1; // we failed to write 'status' to the chip } @@ -370,10 +286,10 @@ { return 1; // we failed to request a read from that mcu - this really is just writing the mcu vaule on the bus } - char c = m_i2c.read(0); + *dst = m_i2c.read(0); m_i2c.stop(); - return c; + return 0; }