Library for accelerometer KXR94-2050

KXR94_2050.cpp

Committer:
66keg
Date:
2015-01-03
Revision:
2:1e4a75b43643
Parent:
0:2700ada3a245

File content as of revision 2:1e4a75b43643:

#include "KXR94_2050.h"

KXR94_2050::KXR94_2050(PinName OutX, 
                       PinName OutY,
                       PinName OutZ) : _OutX(OutX), _OutY(OutY), _OutZ(OutZ) {    
    X_ZERO_VAL = 2097;
    Y_ZERO_VAL = 2050;
    Z_ZERO_VAL = 2052;
}

void KXR94_2050::setZeroVal(uint16_t x_zero, uint16_t y_zero, uint16_t z_zero) {
    X_ZERO_VAL = x_zero;
    Y_ZERO_VAL = y_zero;
    Z_ZERO_VAL = z_zero;
}

void KXR94_2050::getRawData(float *pitch, float *roll) {
    uint16_t Xacc = _OutX.read_u16();
    uint16_t Yacc = _OutY.read_u16();
    uint16_t Zacc = _OutZ.read_u16();
    
    int xval = Xacc - X_ZERO_VAL;
    int yval = Yacc - Y_ZERO_VAL;
    int zval = Zacc - Z_ZERO_VAL;
    
    *roll = atan2l(yval, zval) * 180.0 / PI;
    *pitch = atan2l(xval, zval) * 180.0 / PI;
}