mattjlewis / diozero

Java Device I/O library that is portable across Single Board Computers and microcontrollers. Tested with Raspberry Pi, Odroid C2, BeagleBone Black, Next Thing CHIP, Asus Tinker Board and Arduinos / Pico. Supports GPIO, I2C, SPI as well as Serial communication. Also known to work with Udoo Quad.
https://www.diozero.com
MIT License
261 stars 59 forks source link

Enhancement: Abstract I2CBuilder and SPIBuilder #209

Open EAGrahamJr opened 1 month ago

EAGrahamJr commented 1 month ago

To ensure more consistency across devices, I would suggest that the I2CBuilder and SPIBuilder classes get some abstraction. For example:

public abstract class AbstractI2CBuilder<I2CThing> {
    protected int controller;
    protected int address;
    protected I2CDeviceInterface device;
    protected I2CThing thing;

    protected abstract I2CThing buildThing(I2CDeviceInterface i2CDeviceInterface);

    public I2CThing build() {
        if (thing == null) {
            if (device == null) device = I2CDevice.builder(address).setController(controller).build();
            thing = buildThing(device);
        }
        return thing;
    }
}

This would help ensure behavior across all the device classes using these builders, as there currently is some variation depending upon when the class was added to the code base (i.e. some builders throw exceptions when the device is already created and others do not)1.

I didn't find a whole lot of current devices that have "dual interfaces", so there may not be a lot of refactoring involved, but would likely help for future additions.

1Not a criticism by any means -- my own stuff gets out of date within days. :smirk: