Skip to content

Commit

Permalink
Merge pull request #12 from adafruit/multi_inits
Browse files Browse the repository at this point in the history
on feather sense we might try two inits, before it would cache the i2c addr
  • Loading branch information
ladyada authored Dec 28, 2023
2 parents 4e85d56 + 0c86770 commit e6e4051
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions Adafruit_LIS3MDL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@
*
*/

#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

#include <Adafruit_LIS3MDL.h>
#include <Wire.h>

Expand All @@ -45,10 +40,13 @@ Adafruit_LIS3MDL::Adafruit_LIS3MDL() {}
* @return True if initialization was successful, otherwise false.
*/
bool Adafruit_LIS3MDL::begin_I2C(uint8_t i2c_address, TwoWire *wire) {
if (!i2c_dev) {
i2c_dev = new Adafruit_I2CDevice(i2c_address, wire);
}
if (i2c_dev)
delete i2c_dev;
if (spi_dev)
delete spi_dev;

spi_dev = NULL;
i2c_dev = new Adafruit_I2CDevice(i2c_address, wire);

if (!i2c_dev->begin()) {
return false;
Expand All @@ -65,14 +63,17 @@ bool Adafruit_LIS3MDL::begin_I2C(uint8_t i2c_address, TwoWire *wire) {
*/
boolean Adafruit_LIS3MDL::begin_SPI(uint8_t cs_pin, SPIClass *theSPI,
uint32_t frequency) {
if (i2c_dev)
delete i2c_dev;
if (spi_dev)
delete spi_dev;

i2c_dev = NULL;
if (!spi_dev) {
spi_dev = new Adafruit_SPIDevice(cs_pin,
frequency, // frequency
SPI_BITORDER_MSBFIRST, // bit order
SPI_MODE0, // data mode
theSPI);
}
spi_dev = new Adafruit_SPIDevice(cs_pin,
frequency, // frequency
SPI_BITORDER_MSBFIRST, // bit order
SPI_MODE0, // data mode
theSPI);
if (!spi_dev->begin()) {
return false;
}
Expand All @@ -90,13 +91,18 @@ boolean Adafruit_LIS3MDL::begin_SPI(uint8_t cs_pin, SPIClass *theSPI,
*/
bool Adafruit_LIS3MDL::begin_SPI(int8_t cs_pin, int8_t sck_pin, int8_t miso_pin,
int8_t mosi_pin, uint32_t frequency) {

if (i2c_dev)
delete i2c_dev;
if (spi_dev)
delete spi_dev;

i2c_dev = NULL;
if (!spi_dev) {
spi_dev = new Adafruit_SPIDevice(cs_pin, sck_pin, miso_pin, mosi_pin,
frequency, // frequency
SPI_BITORDER_MSBFIRST, // bit order
SPI_MODE0); // data mode
}
spi_dev = new Adafruit_SPIDevice(cs_pin, sck_pin, miso_pin, mosi_pin,
frequency, // frequency
SPI_BITORDER_MSBFIRST, // bit order
SPI_MODE0); // data mode

if (!spi_dev->begin()) {
return false;
}
Expand Down

0 comments on commit e6e4051

Please sign in to comment.