-
Notifications
You must be signed in to change notification settings - Fork 17
Feature request: Allow using secondary Wire buses #23
Description
On some boards, such as STM32 boards (and probably SAMD boards too), there are multiple I²C controllers available, typically named Wire, Wire1, etc.
Currently, this library always uses Wire hardcoded, but it would be convenient if a secondary bus could also be used.
There is already a sensirion_i2c_select_bus(uint8_t idx) function declared, but it is never defined anywhere. Also, since I'm not sure whether there is a standard way to detect how much Wire objects are defined, this API might be hard to implement on Arduino. Therefore, I'd suggest implementing a selection function that actually passes the Wire, Wire1, etc objects as a reference. The type of these seems to be TwoWire on most cores, but just in case cores use other names, I'd suggest using decltype(Wire) instead.
So, that would be something like this (untested, should probably be split between .h and .cpp files as well, but just to get the general idea):
using SensirionWireType = decltype(Wire);
SensirionWireType *SensirionWire = &Wire;
sensirion_i2c_select_bus(SensirionWireType& bus)
{
SensirionWire = &bus;
}
And then replace all other Wire. with SensirionWire-> too.
This allows a sketch to do e.g.:
sensirion_i2c_select_bus(Wire1);
sensirion_i2c_init();
It can even switch between multiple buses at runtime, as long as it does call sensiron_i2c_init() once for each bus beforehand.