Skip to content

Commit ec802f8

Browse files
committed
Add support for reading multiple bytes in one call
- Fixes #49
1 parent 8da6cda commit ec802f8

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

Source/Devices/PolledBno055.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,13 @@ void PolledBno055::addSourceBuffers(OwnedArray<DataBuffer>& sourceBuffers)
209209

210210
int16_t PolledBno055::readInt16(uint32_t startAddress)
211211
{
212-
oni_reg_val_t byte1 = 0, byte2 = 0;
212+
uint32_t value = 0;
213+
int rc = ReadWord(startAddress, 2, &value);
213214

214-
int rc = ReadByte(startAddress, &byte1);
215-
if (rc != ONI_ESUCCESS) return 0;
216-
rc = ReadByte(startAddress + 1, &byte2);
217-
if (rc != ONI_ESUCCESS) return 0;
215+
if (rc != ONI_ESUCCESS)
216+
return 0;
218217

219-
return (static_cast<int16_t>(byte2) << 8) | byte1;
218+
return static_cast<int16_t>(value);
220219
}
221220

222221
void PolledBno055::hiResTimerCallback()

Source/I2CRegisterContext.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,20 @@ int I2CRegisterContext::WriteByte(uint32_t address, uint32_t value, bool sixteen
4040

4141
int I2CRegisterContext::ReadByte(uint32_t address, oni_reg_val_t* value, bool sixteenBitAddress)
4242
{
43+
return ReadWord(address, 1, value, sixteenBitAddress);
44+
}
45+
46+
int I2CRegisterContext::ReadWord(uint32_t address, uint32_t numBytes, uint32_t* value, bool sixteenBitAddress)
47+
{
48+
if (numBytes < 1 || numBytes > 4)
49+
{
50+
LOGE("Invalid number of bytes requested when reading a word.");
51+
return 1;
52+
}
53+
4354
uint32_t registerAddress = (address << 7) | (i2cAddress & 0x7F);
4455
registerAddress |= sixteenBitAddress ? 0x80000000 : 0;
56+
registerAddress |= (numBytes - 1) << 28;
4557

4658
return i2cContext->readRegister(deviceIndex, registerAddress, value);
4759
}

Source/I2CRegisterContext.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ namespace OnixSourcePlugin
3939

4040
int WriteByte(uint32_t address, uint32_t value, bool sixteenBitAddress = false);
4141

42+
int WriteWord(uint32_t address, uint32_t value, uint32_t numBytes, bool sixteenBitAddress = false);
43+
4244
int ReadByte(uint32_t address, oni_reg_val_t* value, bool sixteenBitAddress = false);
4345

46+
int ReadWord(uint32_t address, uint32_t numBytes, uint32_t* value, bool sixteenBitAddress = false);
47+
4448
int set933I2cRate(double);
4549

4650
protected:

0 commit comments

Comments
 (0)