Skip to content

Commit a34c7a4

Browse files
committed
Simplify Bno055 polling logic a bit
- Clear data array at the start of poll loop instead of conditional clearance of portions multiple times - Combined temperature and calibration read if requested
1 parent d43303c commit a34c7a4

File tree

1 file changed

+32
-40
lines changed

1 file changed

+32
-40
lines changed

OpenEphys.Onix1/PolledBno055Data.cs

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -115,56 +115,48 @@ public unsafe IObservable<Bno055DataFrame> Generate<TSource>(IObservable<TSource
115115
registeredValues.Add(deviceName);
116116
}
117117

118-
// NB: Preallocate the array to avoid unnecesary allocation each frame
119118
byte[] data = new byte[28];
119+
120120
var s = source.SubscribeSafe(observer, _ =>
121121
{
122122
Bno055DataFrame frame = default;
123123
device.Context.EnsureContext(() =>
124124
{
125-
if (polled.HasFlag(PolledBno055Registers.EulerAngle))
126-
{
127-
i2c.ReadWord(PolledBno055.EulerHeadingLsbAddress + 0, 4, data, 0);
128-
i2c.ReadWord(PolledBno055.EulerHeadingLsbAddress + 4, 2, data, 4);
129-
}
130-
else
131-
{
132-
Array.Clear(data, 0, 6);
133-
}
125+
Array.Clear(data, 0, data.Length);
134126

135-
if (polled.HasFlag(PolledBno055Registers.Quaternion))
136-
{
137-
i2c.ReadWord(PolledBno055.EulerHeadingLsbAddress + 6, 4, data, 6);
138-
i2c.ReadWord(PolledBno055.EulerHeadingLsbAddress + 10, 4, data, 10);
139-
}
140-
else
141-
{
142-
Array.Clear(data, 6, 8);
143-
}
127+
if (polled.HasFlag(PolledBno055Registers.EulerAngle))
128+
{
129+
i2c.ReadWord(PolledBno055.EulerHeadingLsbAddress + 0, 4, data, 0);
130+
i2c.ReadWord(PolledBno055.EulerHeadingLsbAddress + 4, 2, data, 4);
131+
}
144132

145-
if (polled.HasFlag(PolledBno055Registers.Acceleration))
146-
{
147-
i2c.ReadWord(PolledBno055.EulerHeadingLsbAddress + 14, 4, data, 14);
148-
i2c.ReadWord(PolledBno055.EulerHeadingLsbAddress + 18, 2, data, 18);
149-
}
150-
else
151-
{
152-
Array.Clear(data, 0, 6);
153-
}
133+
if (polled.HasFlag(PolledBno055Registers.Quaternion))
134+
{
135+
i2c.ReadWord(PolledBno055.EulerHeadingLsbAddress + 6, 4, data, 6);
136+
i2c.ReadWord(PolledBno055.EulerHeadingLsbAddress + 10, 4, data, 10);
137+
}
138+
139+
if (polled.HasFlag(PolledBno055Registers.Acceleration))
140+
{
141+
i2c.ReadWord(PolledBno055.EulerHeadingLsbAddress + 14, 4, data, 14);
142+
i2c.ReadWord(PolledBno055.EulerHeadingLsbAddress + 18, 2, data, 18);
143+
}
154144

155-
if (polled.HasFlag(PolledBno055Registers.Gravity))
156-
{
157-
i2c.ReadWord(PolledBno055.EulerHeadingLsbAddress + 20, 4, data, 20);
158-
i2c.ReadWord(PolledBno055.EulerHeadingLsbAddress + 24, 2, data, 24);
159-
}
160-
else
161-
{
162-
Array.Clear(data, 0, 6);
163-
}
145+
if (polled.HasFlag(PolledBno055Registers.Gravity))
146+
{
147+
i2c.ReadWord(PolledBno055.EulerHeadingLsbAddress + 20, 4, data, 20);
148+
i2c.ReadWord(PolledBno055.EulerHeadingLsbAddress + 24, 2, data, 24);
149+
}
164150

165-
//TODO: actual performance would be a little better if we merged these two so they could be read together
166-
data[26] = polled.HasFlag(PolledBno055Registers.Temperature) ? i2c.ReadByte(PolledBno055.EulerHeadingLsbAddress + 26) : (byte)0;
167-
data[27] = polled.HasFlag(PolledBno055Registers.Calibration) ? i2c.ReadByte(PolledBno055.EulerHeadingLsbAddress + 27) : (byte)0;
151+
if (polled.HasFlag(PolledBno055Registers.Temperature | PolledBno055Registers.Calibration))
152+
{
153+
i2c.ReadWord(PolledBno055.EulerHeadingLsbAddress + 26, 2, data, 26);
154+
}
155+
else
156+
{
157+
data[26] = polled.HasFlag(PolledBno055Registers.Temperature) ? i2c.ReadByte(PolledBno055.EulerHeadingLsbAddress + 26) : (byte)0;
158+
data[27] = polled.HasFlag(PolledBno055Registers.Calibration) ? i2c.ReadByte(PolledBno055.EulerHeadingLsbAddress + 27) : (byte)0;
159+
}
168160

169161
ulong clock = passthrough.ReadRegister(DS90UB9x.LASTI2CL);
170162
clock += (ulong)passthrough.ReadRegister(DS90UB9x.LASTI2CH) << 32;

0 commit comments

Comments
 (0)