LIS3MDL I2C Mbed class with compass heading function
Revision 1:fe199024ddfe, committed 2021-04-13
- Comitter:
- gpmbed
- Date:
- Tue Apr 13 13:52:14 2021 +0000
- Parent:
- 0:4aa6b3804281
- Commit message:
- comments removed
Changed in this revision
LIS3MDL.cpp | Show annotated file Show diff for this revision Revisions of this file |
LIS3MDL.h | Show annotated file Show diff for this revision Revisions of this file |
--- 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); -} - - - -
--- a/LIS3MDL.h Tue Apr 13 13:46:21 2021 +0000 +++ b/LIS3MDL.h Tue Apr 13 13:52:14 2021 +0000 @@ -123,77 +123,7 @@ bool whoAmI(void); - bool getTempEnabled(void); - bool setTempEnabled(bool enable); - uint8_t getOperativeMode(void); - bool setOperativeMode(uint8_t mode); - uint8_t getDataRate(void); - bool setDataRate(uint8_t rate); - bool getSelfTestEnabled(void); - bool setSelfTestEnabled(bool enable); - - uint8_t getFullScale(void); - bool setFullScale(uint8_t sccale); - bool reboot(void); - bool softReset(void); - - bool getLowPowerMode(void); - bool setLowPowerMode(bool mode); - bool getSPIMode(void); - bool setSPIMode(bool mode); - uint8_t getOperatingMode(void); - bool setOperatingMode(uint8_t mode); - - uint8_t getOMZ(void); - bool setOMZ(uint8_t mode); - bool getEndian(void); - bool setEndian(bool selection); - - bool disableDataUpdate(void); - bool enableDataUpdate(void); - bool getDataUpdateStatus(void); - - bool getXYZOverrun(void); - bool getZOverrun(void); - bool getYOverrun(void); - bool getXOverrun(void); - - bool getXYZDataAvailable(void); - bool getZDataAvailable(void); - bool getYDataAvailabl(void); - bool getXDataAvailable(void); - - bool enableXInterupt(void); - bool disableXInterupt(void); - bool isXIntEnabled(void); - bool enableYInterupt(void); - bool disableYInterupt(void); - bool isYIntEnabled(void); - bool enableZInterupt(void); - bool disableZInterupt(void); - bool isZIntEnabled(void); - bool setIntHigh(void); - bool setIntLow(void); - bool checkIntConfig(void); - bool setInteruptLatch(bool latch); - bool checkInteruptLatch(void); - bool enableIntInterupt(void); - bool disableIntInterupt(void); - bool checkIntInterupt(void); - - bool posXThreshold(void); - bool posYThreshold(void); - bool posZThreshold(void); - bool negXThreshold(void); - bool negYThreshold(void); - bool negZThreshold(void); - bool measurementOverflow(void); - bool checkInteruptEvent(void); - void getMotion(int16_t* ax, int16_t* ay, int16_t* az); - - bool setInteruptThreshold(uint16_t threshold); - uint16_t getInteruptThreshold(void); private: I2C i2c;