Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cap12xx - support for setting i2c address #111

Open
Gadgetoid opened this issue Jul 27, 2018 · 2 comments
Open

cap12xx - support for setting i2c address #111

Gadgetoid opened this issue Jul 27, 2018 · 2 comments

Comments

@Gadgetoid
Copy link
Contributor

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.

@proppy
Copy link
Contributor

proppy commented Jul 27, 2018

@Gadgetoid Thanks for the report! PR are always welcome.

Looking at the existing tests:
https://github.com/androidthings/contrib-drivers/blob/master/cap12xx/src/test/java/com/google/android/things/contrib/driver/cap12xx/Cap12xxTest.java
https://github.com/androidthings/contrib-drivers/blob/master/cap12xx/src/androidTest/java/com/google/android/things/contrib/driver/cap12xx/Cap12xxTestActivity.java
It doesn't seems that we have any coverage for the public constructors, so I'm not sure it would be required to add new ones, but @jdkoren is the authority there :)

@jdkoren
Copy link
Member

jdkoren commented Jul 27, 2018

I think for now don't worry about adding tests just for the constructors.

devunwired added a commit that referenced this issue Sep 20, 2018
cap1xxx - add support to specify alternate i2c address (#111)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants