cvbxcvxcv
Lis2dh12.cpp
- Committer:
- jurica238814
- Date:
- 2018-02-28
- Revision:
- 4:043396f22548
- Parent:
- 3:07ddb07f8b7f
- Child:
- 5:15d1c73137e8
File content as of revision 4:043396f22548:
/* * * LIS2DH12 MEMS digital output motion sensor * More info @ aconno.de * Made by Jurica Resetar * jurica_resetar@yahoo.com * */ #include "Lis2dh12.h" #include "Lis2dh12_regs.h" Lis2dh12::Lis2dh12(I2C *i2c_, char address) : i2c(i2c_, address){ } uint8_t Lis2dh12::whoIAm(){ char regAddr = (char)WHO_AM_I; char regData; i2c.readFromReg(regAddr, ®Data, 1); return (uint8_t)regData; } uint8_t Lis2dh12::setMode(Mode mode){ char ctrl1Copy; char ctrl4Copy; uint8_t success; i2c.readFromReg((char)CTRL_REG1, &ctrl1Copy, 1); i2c.readFromReg((char)CTRL_REG4, &ctrl4Copy, 1); switch(mode){ case HIGH_RES: ctrl1Copy &= 0xF7; ctrl4Copy |= 0x08; break; case NORMAL: ctrl1Copy &= 0xF7; ctrl4Copy &= 0xF7; break; case LOW_POWER: ctrl1Copy |= 0x08; ctrl4Copy &= 0xF7; break; default: return 0; } i2c.writeToReg((char)CTRL_REG1, &ctrl1Copy, 1); success = i2c.writeToReg((char)CTRL_REG4, &ctrl4Copy, 1); return success; } uint8_t Lis2dh12::enableAxes(Axis axis){ char ctrl1Copy; i2c.readFromReg((char)CTRL_REG1, &ctrl1Copy, 1); ctrl1Copy |= axis; i2c.writeToReg((char)CTRL_REG1, &ctrl1Copy, 1); return 0; } uint8_t Lis2dh12::disableAxes(Axis axis){ char ctrl1Copy; i2c.readFromReg((char)CTRL_REG1, &ctrl1Copy, 1); ctrl1Copy &= ~(1 << axis); i2c.writeToReg((char)CTRL_REG1, &ctrl1Copy, 1); return 0; } int16_t Lis2dh12::readXAxis(){ int16_t rawData; char tempData; // Make sure new data is ready do{ i2c.readFromReg((char)STATUS, &tempData, 1); }while(!(tempData & 0x08)); do{ i2c.readFromReg((char)STATUS, &tempData, 1); }while(!(tempData & 0x80)); // Same data have been overwritten //i2c.readFromReg((char)OUT_X_H, &tempData, 1); //rawData = (int8_t)tempData << 8; i2c.readFromReg((char)OUT_X_H, &tempData, 1); rawData = (int8_t)tempData; return rawData; } int16_t Lis2dh12::readYAxis(){ int16_t rawData; char tempData; //i2c.readFromReg((char)OUT_Y_H, &tempData, 1); //rawData = (int8_t)tempData << 8; i2c.readFromReg((char)OUT_Y_H, &tempData, 1); rawData = (int8_t)tempData; return rawData; } int16_t Lis2dh12::readZAxis(){ int16_t rawData = 0; char tempData; //i2c.readFromReg((char)OUT_Z_H, &tempData, 1); //rawData = (int8_t)tempData << 8; i2c.readFromReg((char)OUT_Z_H, &tempData, 1); rawData = (int8_t)tempData; return rawData; } uint8_t Lis2dh12::setODR(Odr odr){ char ctrl1Copy; i2c.readFromReg((char)CTRL_REG1, &ctrl1Copy, 1); ctrl1Copy |= (odr << 4); i2c.writeToReg((char)CTRL_REG1, &ctrl1Copy, 1); return 0; } uint8_t Lis2dh12::setScale(Scale scale){ char ctrl4Copy; i2c.readFromReg((char)CTRL_REG4, &ctrl4Copy, 1); ctrl4Copy |= (scale << 4); i2c.writeToReg((char)CTRL_REG4, &ctrl4Copy, 1); return 0; } /* Interrupt activity 1 driven to INT1 pad */ // TODO: Napraviti tako da postoji samo jedna metoda int1Setup koja prima gro // parametara: THS, DUR, Latch... uint8_t Lis2dh12::int1Setup(uint8_t setup){ char data = setup; i2c.writeToReg((char)CTRL_REG3, &data, 1); return 0; } uint8_t Lis2dh12::int1Latch(uint8_t enable){ char ctrl5Copy; i2c.readFromReg((char)CTRL_REG5, &ctrl5Copy, 1); ctrl5Copy |= enable; i2c.writeToReg((char)CTRL_REG5, &ctrl5Copy, 1); return 0; } uint8_t Lis2dh12::int1Duration(uint8_t duration){ char copy = duration; i2c.writeToReg((char)INT1_DURATION, ©, 1); return 0; } uint8_t Lis2dh12::int1Threshold(uint8_t threshold){ char copy = threshold; i2c.writeToReg((char)INT1_THS, ©, 1); return 0; } uint8_t Lis2dh12::int1Config(uint8_t config){ char copy = config; i2c.writeToReg((char)INT1_CFG, ©, 1); return 0; } void Lis2dh12::clearIntFlag(){ char data; i2c.readFromReg((char)INT1_SRC, (char*)&data, 1); }