Skip to content

cap12xx - support for setting i2c address #111

@Gadgetoid

Description

@Gadgetoid

While sitting down with @proppy yesterday and getting something of a crash course in building drivers for Things, we ran into an omission in the cap1xxx library.

The cap1xxx range support at least 5 different i2c address that I'm aware of, via the use of a pull-down address select resistor:

Resistor Address
82k 0x2c
100k 0x2b
120k 0x2a
150k 0x29
VDD 0x28

The hard-coded address needs substituting with an optional constructor for supplying one of the above resistor-selected values:

I2cDevice device = manager.openI2cDevice(i2cName, I2C_ADDRESS);

I have local code working with alternate i2c addresses (Pimoroni Drum HAT uses 0x2c) which looks like this:

    public Cap12xx(String i2cName, String alertName, Configuration chip, Handler handler) throws IOException {
        this(i2cName, I2C_ADDRESS, alertName, chip, handler);
    }

    /**
     * Create a new Cap12xx controller.
     *
     * @param i2cName I2C port name where the controller is attached. Cannot be null.
     * @param alertName optional GPIO pin name connected to the controller's
     *                  alert interrupt signal. Can be null.
     * @param chip identifier for the connected controller device chip.
     * @param handler optional {@link Handler} for software polling and callback events.
     * @throws IOException
     */
    public Cap12xx(String i2cName, int i2cAddress, String alertName, Configuration chip, Handler handler)
            throws IOException {
        mChipConfiguration = chip;
        try {
            PeripheralManager manager = PeripheralManager.getInstance();
            I2cDevice device = manager.openI2cDevice(i2cName, i2cAddress);
            Gpio alertPin = null;
            if (alertName != null) {
                alertPin = manager.openGpio(alertName);
            }
            init(device, alertPin, chip, handler);
        } catch (IOException|RuntimeException e) {
            // Close the peripherals if an init error occurs
            try {
                close();
            } catch (IOException|RuntimeException ignored) {
            }
            throw e;
        }
    }

I'm happy to raise a PR, but since this is my first Java rodeo my code may be a little rough around the edges and I'm currently missing tests for this feature.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions