#include "imuData.h" #include "string.h" ImuDataClass ::ImuDataClass () { ucDevAddr =0x50; } void ImuDataClass::StartIIC() { ucDevAddr = 0x50; Wire.begin(); } void ImuDataClass::StartIIC(unsigned char ucAddr) { ucDevAddr = ucAddr; Wire.begin(); } void ImuDataClass ::CopeSerialData(unsigned char ucData) { static unsigned char ucRxBuffer[250]; static unsigned char ucRxCnt = 0; ucRxBuffer[ucRxCnt++]=ucData; if (ucRxBuffer[0]!=0x55) { ucRxCnt=0; return; } if (ucRxCnt<11) {return;} else { switch(ucRxBuffer[1]) { case 0x50: memcpy(&stcTime,&ucRxBuffer[2],8);break; case 0x51: memcpy(&stcAcc,&ucRxBuffer[2],8);break; case 0x52: memcpy(&stcGyro,&ucRxBuffer[2],8);break; case 0x53: memcpy(&stcAngle,&ucRxBuffer[2],8);break; case 0x54: memcpy(&stcMag,&ucRxBuffer[2],8);break; case 0x55: memcpy(&stcDStatus,&ucRxBuffer[2],8);break; case 0x56: memcpy(&stcPress,&ucRxBuffer[2],8);break; case 0x57: memcpy(&stcLonLat,&ucRxBuffer[2],8);break; case 0x58: memcpy(&stcGPSV,&ucRxBuffer[2],8);break; } ucRxCnt=0; } } void ImuDataClass::readRegisters(unsigned char deviceAddr,unsigned char addressToRead, unsigned char bytesToRead, char * dest) { Wire.beginTransmission(deviceAddr); Wire.write(addressToRead); Wire.endTransmission(false); //endTransmission but keep the connection active Wire.requestFrom(deviceAddr, bytesToRead); //Ask for bytes, once done, bus is released by default while(Wire.available() < bytesToRead); //Hang out until we get the # of bytes we expect for(int x = 0 ; x < bytesToRead ; x++) dest[x] = Wire.read(); } void ImuDataClass::writeRegister(unsigned char deviceAddr,unsigned char addressToWrite,unsigned char bytesToRead, char *dataToWrite) { Wire.beginTransmission(deviceAddr); Wire.write(addressToWrite); for(int i = 0 ; i < bytesToRead ; i++) Wire.write(dataToWrite[i]); Wire.endTransmission(); //Stop transmitting } short ImuDataClass::ReadWord(unsigned char ucAddr) { short sResult; readRegisters(ucDevAddr, ucAddr, 2, (char *)&sResult); return sResult; } void ImuDataClass::WriteWord(unsigned char ucAddr,short sData) { writeRegister(ucDevAddr, ucAddr, 2, (char *)&sData); } void ImuDataClass::ReadData(unsigned char ucAddr,unsigned char ucLength,char chrData[]) { readRegisters(ucDevAddr, ucAddr, ucLength, chrData); } void ImuDataClass::GetTime() { readRegisters(ucDevAddr, 0x30, 8, (char*)&stcTime); } void ImuDataClass::GetAcc() { readRegisters(ucDevAddr, AX, 6, (char *)&stcAcc); } void ImuDataClass::GetGyro() { readRegisters(ucDevAddr, GX, 6, (char *)&stcGyro); } void ImuDataClass::GetAngle() { readRegisters(ucDevAddr, Roll, 6, (char *)&stcAngle); } void ImuDataClass::GetMag() { readRegisters(ucDevAddr, HX, 6, (char *)&stcMag); } void ImuDataClass::GetPress() { readRegisters(ucDevAddr, PressureL, 8, (char *)&stcPress); } void ImuDataClass::GetDStatus() { readRegisters(ucDevAddr, D0Status, 8, (char *)&stcDStatus); } void ImuDataClass::GetLonLat() { readRegisters(ucDevAddr, LonL, 8, (char *)&stcLonLat); } void ImuDataClass::GetGPSV() { readRegisters(ucDevAddr, GPSHeight, 8, (char *)&stcGPSV); } void ImuDataClass::GetQuater() { readRegisters(ucDevAddr, QUATER, 8, (char *)&stcQuater); } ImuDataClass imuData = ImuDataClass();