Commented debug printfs
Dependents: LoRaWAN-NAMote72-Application-Demo_IoTium LoRaWAN-NAMote72-BVS-confirmed-tester-0-7v1_copy LoRaWAN-NAMote72-Application-Demo-good LoRaWAN-NAMote72-Application-Demo
Fork of lib_mpl3115a2 by
mpl3115a2.cpp
- Committer:
- dudmuck
- Date:
- 2015-03-18
- Revision:
- 0:6bba2efea51e
- Child:
- 1:3cd1f21925e8
File content as of revision 0:6bba2efea51e:
#include "mpl3115a2.h" #define MPL3115A_I2C_ADDRESS 0xc0 //0x60 MPL3115A2::MPL3115A2(I2C& r) : m_i2c(r) { } MPL3115A2::~MPL3115A2() { } void MPL3115A2::init() { uint8_t regVal; //MPL3115Reset( ); write(CTRL_REG1, 4); wait(0.05); do { // Wait for the RST bit to clear regVal = read( CTRL_REG1); //printf("ctrl_reg1:%02x\n", regVal); } while( regVal ); write( PT_DATA_CFG_REG, 0x07 ); // Enable data flags write( CTRL_REG3, 0x11 ); // Open drain, active low interrupts write( CTRL_REG4, 0x80 ); // Enable DRDY interrupt write( CTRL_REG5, 0x00 ); // DRDY interrupt routed to INT2 - PTD3 write( CTRL_REG1, 0xA9 ); // Active altitude mode, OSR = 32 write( OFF_H_REG, 0xB0 ); // Altitude data offset SetModeActive( ); } void MPL3115A2::SetModeActive( ) { uint8_t val = read(CTRL_REG1); val |= 1; // set SBYB write(CTRL_REG1, val); } void MPL3115A2::SetModeStandby( ) { uint8_t val = read(CTRL_REG1); val &= ~1; // clear SBYB write(CTRL_REG1, val); } void MPL3115A2::write(uint8_t a, uint8_t d) { char cmd[2]; cmd[0] = a; cmd[1] = d; if (m_i2c.write(MPL3115A_I2C_ADDRESS, cmd, 2)) printf("write-fail %02x %02x\n", cmd[0], cmd[1]); } uint8_t MPL3115A2::read(uint8_t a) { char cmd[2]; cmd[0] = a; if (m_i2c.write(MPL3115A_I2C_ADDRESS, cmd, 1, true)) printf("write-fail %02x\n", cmd[0]); if (m_i2c.read(MPL3115A_I2C_ADDRESS, cmd, 1)) printf("read-fail\n"); //printf("MPL3115::try_read: %x\n", cmd[0]); return cmd[0]; } /*void MPL3115A2::try_read() { char cmd[2]; cmd[0] = MPL3115_ID; if (m_i2c.write(MPL3115A_I2C_ADDRESS, cmd, 1, true)) printf("write-fail try_read\n"); if (m_i2c.read(MPL3115A_I2C_ADDRESS, cmd, 1)) printf("read-fail\n"); printf("MPL3115::try_read: %x\n", cmd[0]); }*/ float MPL3115A2::ReadAltitude( void ) { uint8_t counter = 0; uint8_t val = 0; uint8_t msb = 0, csb = 0, lsb = 0; float decimal = 0; /*if( MPL3115Initialized == false ) { return 0; }*/ SetModeAltimeter( ); ToggleOneShot( ); while( ( val & 0x04 ) != 0x04 ) { val = read( STATUS_REG); wait(0.01); //DelayMs( 10 ); counter++; if( counter > 20 ) { //MPL3115Initialized = false; init( ); SetModeAltimeter( ); ToggleOneShot( ); counter = 0; while( ( val & 0x04 ) != 0x04 ) { val = read( STATUS_REG); wait(0.01); //DelayMs( 10 ); counter++; if( counter > 20 ) { return( 0 ); //Error out after max of 512ms for a read } } } } msb = read( OUT_P_MSB_REG); // High byte of integer part of altitude, csb = read( OUT_P_CSB_REG); // Low byte of integer part of altitude lsb = read( OUT_P_LSB_REG); // Decimal part of altitude in bits 7-4 decimal = ( ( float )( lsb >> 4 ) ) / 16.0; //Altitude = ( float )( ( msb << 8 ) | csb ) + decimal; Altitude = ( float )( ( int16_t )( ( msb << 8 ) | csb ) ) + decimal; return( Altitude ); } void MPL3115A2::SetModeAltimeter( void ) { uint8_t val; SetModeStandby( ); val = read( CTRL_REG1); val |= 0x80; //Set ALT bit write( CTRL_REG1, val ); SetModeActive( ); } void MPL3115A2::ToggleOneShot( void ) { uint8_t val; SetModeStandby( ); val = read( CTRL_REG1); val &= ~(0x02); //Clear OST bit write( CTRL_REG1, val ); val = read( CTRL_REG1); val |= 0x02; //Set OST bit write( CTRL_REG1, val ); SetModeActive( ); } float MPL3115A2::ReadTemperature( void ) { uint8_t counter = 0; bool negSign = false; uint8_t val = 0; uint8_t msb = 0, lsb = 0; /*if( MPL3115Initialized == false ) { return 0; }*/ ToggleOneShot( ); while( ( val & 0x02 ) != 0x02 ) { val = read( STATUS_REG); wait(0.01); counter++; if( counter > 20 ) { //MPL3115Initialized = false; init( ); ToggleOneShot( ); counter = 0; while( ( val & 0x02 ) != 0x02 ) { val = read( STATUS_REG); wait(0.01); counter++; if( counter > 20 ) { return( 0 ); //Error out after max of 512ms for a read } } } } msb = read( OUT_T_MSB_REG); // Integer part of temperature lsb = read( OUT_T_LSB_REG); // Decimal part of temperature in bits 7-4 if( msb > 0x7F ) { val = ~( ( msb << 8 ) + lsb ) + 1; //2?s complement msb = val >> 8; lsb = val & 0x00F0; negSign = true; } if( negSign == true ) { Temperature = 0 - ( msb + ( float )( ( lsb >> 4 ) / 16.0 ) ); } else { Temperature = msb + ( float )( ( lsb >> 4 ) / 16.0 ); } ToggleOneShot( ); return( Temperature ); }