Skip to content

Custom I2C port #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions src/SparkFun_LIS331.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ void LIS331::begin(comm_mode mode)
for (int i = 0x30; i < 0x37; i++) LIS331_write(i,&data,1);
}

void LIS331::setI2CPort(TwoWire &wirePort)
{
this->wirePort = &wirePort;
}

void LIS331::setI2CAddr(uint8_t address)
{
this->address = address;
Expand Down Expand Up @@ -363,13 +368,13 @@ void LIS331::LIS331_write(uint8_t reg_address, uint8_t *data, uint8_t len)
if (mode == USE_I2C)
{
// I2C write handling code
Wire.beginTransmission(address);
Wire.write(reg_address);
this->wirePort->beginTransmission(address);
this->wirePort->write(reg_address);
for(int i = 0; i<len; i++)
{
Wire.write(data[i]);
this->wirePort->write(data[i]);
}
Wire.endTransmission();
this->wirePort->endTransmission();
}
else
{
Expand All @@ -389,13 +394,13 @@ void LIS331::LIS331_read(uint8_t reg_address, uint8_t *data, uint8_t len)
if (mode == USE_I2C)
{
// I2C read handling code
Wire.beginTransmission(address);
Wire.write(reg_address);
Wire.endTransmission();
Wire.requestFrom(address, len);
this->wirePort->beginTransmission(address);
this->wirePort->write(reg_address);
this->wirePort->endTransmission();
this->wirePort->requestFrom(address, len);
for (int i = 0; i<len; i++)
{
data[i] = Wire.read();
data[i] = this->wirePort->read();
}
}
else
Expand Down
7 changes: 5 additions & 2 deletions src/SparkFun_LIS331.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define __sparkfun_lis331_h__

#include <stdint.h>
#include <Wire.h>

#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
Expand Down Expand Up @@ -35,7 +36,7 @@ class LIS331
LOW_POWER_2HZ, LOW_POWER_5HZ, LOW_POWER_10HZ} power_mode;
typedef enum {DR_50HZ, DR_100HZ, DR_400HZ, DR_1000HZ} data_rate;
typedef enum {HPC_8, HPC_16, HPC_32, HPC_64} high_pass_cutoff_freq_cfg;
typedef enum {PUSH_PULL, OPEN_DRAIN} pp_od;
typedef enum {PUSH_PULL, OPEN_DRAIN_LIS} pp_od;
typedef enum {INT_SRC, INT1_2_SRC, DRDY, BOOT} int_sig_src;
typedef enum {LOW_RANGE, MED_RANGE, NO_RANGE, HIGH_RANGE} fs_range;
typedef enum {X_AXIS, Y_AXIS, Z_AXIS} int_axis;
Expand All @@ -44,6 +45,7 @@ class LIS331
// public functions
LIS331(); // Constructor. Defers all functionality to .begin()
void begin(comm_mode mode);
void setI2CPort(TwoWire &wirePort);
void setI2CAddr(uint8_t address);
void setSPICSPin(uint8_t pin);
void axesEnable(bool enable);
Expand Down Expand Up @@ -71,10 +73,11 @@ class LIS331
private:

comm_mode mode; // comms mode, I2C or SPI
TwoWire *wirePort = &Wire;
uint8_t address; // I2C address
uint8_t CSPin;
void LIS331_write(uint8_t address, uint8_t *data, uint8_t len);
void LIS331_read(uint8_t address, uint8_t *data, uint8_t len);
};

#endif
#endif