Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Feature request: Allow using secondary Wire buses #23

@matthijskooijman

Description

@matthijskooijman

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions