diff --git a/src/LIDARLite.cpp b/src/LIDARLite.cpp index eff4ad5..84f55dd 100644 --- a/src/LIDARLite.cpp +++ b/src/LIDARLite.cpp @@ -125,6 +125,44 @@ void LIDARLite::configure(int configuration, char lidarliteAddress) } } /* LIDARLite::configure */ +/*------------------------------------------------------------------------------ + Set I2C Address + + Set Alternate I2C Device Address. See Operation Manual for additional info. + + Parameters + ------------------------------------------------------------------------------ + newAddress: desired secondary I2C device address + disableDefault: a non-zero value here means the default 0x62 I2C device + address will be disabled. + lidarliteAddress: Default 0x62. Fill in new address here if changed. See + operating manual for instructions. +------------------------------------------------------------------------------*/ +void LIDARLite::setI2Caddr(char newAddress, char disableDefault, char lidarliteAddress) +{ + byte dataBytes[2]; + + // Read UNIT_ID serial number bytes and write them into I2C_ID byte locations + read ((0x16 | 0x80), 2, dataBytes, false, lidarliteAddress); + write(0x18, dataBytes[0], lidarliteAddress); + write(0x19, dataBytes[1], lidarliteAddress); + + // Write the new I2C device address to registers + dataBytes[0] = newAddress; + write(0x1a, dataBytes[0], lidarliteAddress); + + // Enable the new I2C device address using the default I2C device address + dataBytes[0] = 0; + write(0x1e, dataBytes[0], lidarliteAddress); + + // If desired, disable default I2C device address (using the new I2C device address) + if (disableDefault) + { + dataBytes[0] = (1 << 3); // set bit to disable default address + write(0x1e, dataBytes[0], newAddress); + } +} /* LIDARLite::setI2Caddr */ + /*------------------------------------------------------------------------------ Reset diff --git a/src/LIDARLite.h b/src/LIDARLite.h index 0fd8a3f..551d0c1 100644 --- a/src/LIDARLite.h +++ b/src/LIDARLite.h @@ -35,6 +35,7 @@ class LIDARLite LIDARLite(); void begin(int = 0, bool = false, char = LIDARLITE_ADDR_DEFAULT); void configure(int = 0, char = LIDARLITE_ADDR_DEFAULT); + void setI2Caddr(char, char, char = LIDARLITE_ADDR_DEFAULT); void reset(char = LIDARLITE_ADDR_DEFAULT); int distance(bool = true, char = LIDARLITE_ADDR_DEFAULT); void write(char, char, char = LIDARLITE_ADDR_DEFAULT);