LIS3MDL I2C Mbed class with compass heading function
Diff: LIS3MDL.cpp
- Revision:
- 1:fe199024ddfe
- Parent:
- 0:4aa6b3804281
diff -r 4aa6b3804281 -r fe199024ddfe LIS3MDL.cpp --- a/LIS3MDL.cpp Tue Apr 13 13:46:21 2021 +0000 +++ b/LIS3MDL.cpp Tue Apr 13 13:52:14 2021 +0000 @@ -50,41 +50,14 @@ // Choose device mode (bits 1:0 = 00 = continuous data read, 01 = single conversion, 10 & 11 = default power down) //writeByte(LIS3MDL_ADDRESS, LIS3MDL_CTRL_REG3, 0x00); // Enable continuous data read mode (bits 1:0 = 00) i2c_write(LIS3MDL_ADDRESS, LIS3MDL_CTRL_REG3,0x00,1); - // Enable temperature sensor (bit 7 = 1) - // Set magnetometer operative mode for x and y axes (bits 6:5) - // Set magnetometer ODR (bits 4:2) - //writeByte(LIS3MDL_ADDRESS, LIS3MDL_CTRL_REG1, 0x80 | Mopmode << 5 | Modr << 2); - //i2c_write(LIS3MDL_ADDRESS, LIS3MDL_CTRL_REG1, 0x80 | Mopmode << 5 | Modr << 2,1); - - //writeByte(LIS3MDL_ADDRESS, LIS3MDL_CTRL_REG2, Mscale << 5); // Set magnetometer full scale range - //i2c_write(LIS3MDL_ADDRESS, LIS3MDL_CTRL_REG2, Mscale << 5,1); - - //writeByte(LIS3MDL_ADDRESS, LIS3MDL_CTRL_REG4, Mopmode << 2); // Set magnetometer operative mode for z axis - //i2c_write(LIS3MDL_ADDRESS, LIS3MDL_CTRL_REG4, Mopmode << 2,1); // Set magnetometer operative mode for z axis - - //writeByte(LIS3MDL_ADDRESS, LIS3MDL_CTRL_REG5, 0x40); // output registers not updated until both data bytes have been read i2c_write(LIS3MDL_ADDRESS, LIS3MDL_CTRL_REG5, 0x40,1); // output registers not updated until both data bytes have been read - //Put into the correct operating mode - //disableLowPower(); - //enableAxisXYZ(); - //setDataRate(2); - return 1; } bool LIS3MDL::writeRegister(const uint8_t register_addr, const uint8_t value) { - //send write call to sensor address - //send register address to sensor - //send value to register - /* - bool write_status = 0; - Wire.beginTransmission(_address); //open communication with - Wire.write(register_addr); - Wire.write(value); - Wire.endTransmission(); - */ + i2c_write(_address,register_addr,value,1); return 1; //returns whether the write succeeded or failed @@ -207,314 +180,13 @@ return heading; } -void getMag(int16_t* ax, int16_t* ay, int16_t* az) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} -bool whoAmI(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool getTempEnabled(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool setTempEnabled(bool enable) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -uint8_t getOperativeMode(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool setOperativeMode(uint8_t mode) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -uint8_t getDataRate(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool setDataRate(uint8_t rate) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool getSelfTestEnabled(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool setSelfTestEnabled(bool enable) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -uint8_t getFullScale(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool setFullScale(uint8_t sccale) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool reboot(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool softReset(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool getLowPowerMode(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool setLowPowerMode(bool mode) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool getSPIMode(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool setSPIMode(bool mode) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -uint8_t getOperatingMode(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool setOperatingMode(uint8_t mode) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -uint8_t getOMZ(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool setOMZ(uint8_t mode) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool getEndian(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool setEndian(bool selection) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool disableDataUpdate(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool enableDataUpdate(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool getDataUpdateStatus(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool getXYZOverrun(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool getZOverrun(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool getYOverrun(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool getXOverrun(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool getXYZDataAvailable(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool getZDataAvailable(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool getYDataAvailabl(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool getXDataAvailable(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool enableXInterupt(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool disableXInterupt(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool isXIntEnabled(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool enableYInterupt(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool disableYInterupt(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool isYIntEnabled(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool enableZInterupt(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool disableZInterupt(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool isZIntEnabled(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool setIntHigh(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool setIntLow(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool checkIntConfig(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool setInteruptLatch(bool latch) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool checkInteruptLatch(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool enableIntInterupt(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool disableIntInterupt(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool checkIntInterupt(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - - -bool posXThreshold(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool posYThreshold(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool posZThreshold(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool negXThreshold(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool negYThreshold(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool negZThreshold(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool measurementOverflow(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -bool checkInteruptEvent(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - - -bool setInteruptThreshold(uint16_t threshold) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); -} - -uint16_t getInteruptThreshold(void) { - //return (readMaskedRegister(LIS3MDL_CTRL_REG2, LIS3MDL_HPIS2_MASK) != 0); +bool LIS3MDL::whoAmI(void) { + return (LIS3MDL_I_AM_MASK == readRegister(LIS3MDL_WHO_AM_I)); } -/** Read the all axis registers - * @see getAxisZ() - * @see getAxisY() - * @see getAxisZ() - */ -void LIS3MDL::getMotion(int16_t* ax, int16_t* ay, int16_t* az) { - *ax = getAxisX(); - *ay = getAxisY(); - *az = getAxisZ(); -} -//bool LIS3MDL::tempHasOverrun(void) { -// uint8_t overrun = readMaskedRegister(LIS3MDL_STATUS_REG_AUX, LIS3MDL_TOR_MASK); -// return (overrun != 0); -//} - -//bool LIS3MDL::tempDataAvailable(void) { -// uint8_t data = readMaskedRegister(LIS3MDL_STATUS_REG_AUX, LIS3MDL_TDA_MASK); -// return (data != 0); -//} - -//uint16_t LIS3MDL::getTemperature(void) { -// if(tempDataAvailable()){ -// return readRegisters(LIS3MDL_OUT_TEMP_H, LIS3MDL_OUT_TEMP_L); -// } else { -// //if new data isn't available -// return 0; -// } -//} - -bool LIS3MDL::whoAmI(void) { - return (LIS3MDL_I_AM_MASK == readRegister(LIS3MDL_WHO_AM_I)); -} - -bool LIS3MDL::getTempEnabled(void) { - //return (readMaskedRegister(LIS3MDL_TEMP_CFG_REG, LIS3MDL_TEMP_EN_MASK) != 0); -} - -bool LIS3MDL::setTempEnabled(bool enable) { - //return writeRegister(LIS3MDL_TEMP_CFG_REG, enable ? LIS3MDL_TEMP_EN_MASK : 0); -} - -uint8_t LIS3MDL::getDataRate(void) { - //return readMaskedRegister(LIS3MDL_CTRL_REG1, LIS3MDL_ODR_MASK); -} - -bool LIS3MDL::setDataRate(uint8_t rate) { - if(rate > 9) { - return 0; - } - uint8_t data_rate = rate << 4; - // return writeMaskedRegister(LIS3MDL_CTRL_REG1, LIS3MDL_ODR_MASK, data_rate); -} - - - -