Library for LIS2DH12 acc.
Dependents: acnSensa_LIS aconnoCellularGnss Lizzy
Diff: Lis2dh12.cpp
- Revision:
- 0:cc5d477d5dbe
- Child:
- 1:d89f4b12116b
diff -r 000000000000 -r cc5d477d5dbe Lis2dh12.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lis2dh12.cpp Fri Dec 29 17:40:50 2017 +0000 @@ -0,0 +1,115 @@ +/* + * + * 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; + i2c.readFromReg((char)OUT_X_H, &tempData, 1); + rawData = (int8_t)tempData << 8; + i2c.readFromReg((char)OUT_X_L, &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_L, &tempData, 1); + rawData |= (int8_t)tempData; + return rawData; +} + +int16_t Lis2dh12::readZAxis(){ + int16_t rawData; + char tempData; + i2c.readFromReg((char)OUT_Z_H, &tempData, 1); + rawData = (int8_t)tempData << 8; + i2c.readFromReg((char)OUT_Z_L, &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; +} + + + \ No newline at end of file