com.google.android.things.pio.I2cDevice Java Examples
The following examples show how to use
com.google.android.things.pio.I2cDevice.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FaBoThingsDeviceControl.java From DeviceConnect-Android with MIT License | 6 votes |
/** * I2cDeviceを取得します. * <p> * 取得できなかった場合はnullを返却します。 * </p> * <p> * 一度開いているI2cDeviceは、マップで管理して、取得します。 * </p> * @param address アドレス * @return I2cDeviceのインスタンス */ I2cDevice getI2cDevice(final int address) { if (mI2cDeviceMap.containsKey(address)) { return mI2cDeviceMap.get(address); } List<String> i2cList = mManagerService.getI2cBusList(); if (i2cList.isEmpty()) { if (DEBUG) { Log.i(TAG, "No I2C port available on this device."); } return null; } else { try { I2cDevice device = mManagerService.openI2cDevice(i2cList.get(0), address); if (device != null) { mI2cDeviceMap.put(address, device); } return device; } catch (IOException e) { return null; } } }
Example #2
Source File: Sh1106.java From androidthings-drivers with Apache License 2.0 | 6 votes |
/** * Recommended start sequence for initializing the communications with the OLED display. * WARNING: If you change this code, power cycle your display before testing. * * @throws IOException */ private void init(I2cDevice device, int width, int height) throws IOException { mI2cDevice = device; mWidth = width; mHeight = height; mBuffer = new byte[PAGES][(((mWidth * mHeight) / VERTICAL_PIXEL_PER_PAGE) / PAGES) + DATA_OFFSET]; BitmapHelper.bmpToBytes( mBuffer, DATA_OFFSET, Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888), false); for (byte[] page : mBuffer) { page[0] = (byte) COMMAND_DISPLAY_START_LINE; } mI2cDevice.write(INIT_PAYLOAD, INIT_PAYLOAD.length); }
Example #3
Source File: Cap1xxx.java From contrib-drivers with Apache License 2.0 | 6 votes |
/** * Create a new Cap1xxx controller. * * @param i2cName I2C port name where the controller is attached. Cannot be null. * @param i2cAddress 7-bit I2C address for the attached controller. * @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 Cap1xxx(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; } }
Example #4
Source File: MotorHat.java From contrib-drivers with Apache License 2.0 | 5 votes |
public MotorHat(String i2cBusName, int i2cAddress) throws IOException { PeripheralManager pioService = PeripheralManager.getInstance(); I2cDevice device = pioService.openI2cDevice(i2cBusName, i2cAddress); try { initialize(device); } catch (IOException | RuntimeException e) { try { close(); } catch (IOException | RuntimeException ignored) { } throw e; } }
Example #5
Source File: Mma7660Fc.java From contrib-drivers with Apache License 2.0 | 5 votes |
private void connect(I2cDevice device) throws IOException { if (mDevice != null) { throw new IllegalStateException("device already connected"); } mDevice = device; setSamplingRate(RATE_120HZ); }
Example #6
Source File: Mma7660Fc.java From contrib-drivers with Apache License 2.0 | 5 votes |
/** * Create a new MMA7660FC driver connected to the given I2C bus. * @param bus * @throws IOException */ public Mma7660Fc(String bus) throws IOException { PeripheralManager pioService = PeripheralManager.getInstance(); I2cDevice device = pioService.openI2cDevice(bus, I2C_ADDRESS); try { connect(device); } catch (IOException|RuntimeException e) { try { close(); } catch (IOException|RuntimeException ignored) { } throw e; } }
Example #7
Source File: Vcnl4200.java From contrib-drivers with Apache License 2.0 | 5 votes |
private void connect(I2cDevice device, Configuration configuration) throws IOException { if (mDevice != null) { throw new IllegalStateException("device already connected"); } mDevice = device; applyConfiguration(configuration); }
Example #8
Source File: Tsl256x.java From androidthings-drivers with Apache License 2.0 | 5 votes |
/** * Create a new TSL2561 driver connected to the given I2C bus. * * @param i2cName I2C bus name the display is connected to * @param i2cAddress I2C address of the display * @throws IOException */ public Tsl256x(String i2cName, int i2cAddress) throws IOException { PeripheralManager pioService = PeripheralManager.getInstance(); I2cDevice device = pioService.openI2cDevice(i2cName, i2cAddress); try { connect(device); } catch (IOException | RuntimeException e) { try { close(); } catch (IOException | RuntimeException ignored) { } throw e; } }
Example #9
Source File: MotorHat.java From contrib-drivers with Apache License 2.0 | 5 votes |
private void initialize(I2cDevice device) throws IOException { mI2cDevice = device; // reset mI2cDevice.writeRegByte(REG_ALL_LED_ON_L, (byte) 0); mI2cDevice.writeRegByte(REG_ALL_LED_ON_H, (byte) 0); mI2cDevice.writeRegByte(REG_ALL_LED_OFF_L, (byte) 0); mI2cDevice.writeRegByte(REG_ALL_LED_OFF_H, (byte) 0); mI2cDevice.writeRegByte(REG_MODE_2, OUTDRV); mI2cDevice.writeRegByte(REG_MODE_1, ALLCALL); byte mode1 = mI2cDevice.readRegByte(REG_MODE_1); // Remove the restart and sleep bits. mode1 = (byte) (mode1 & ~(RESTART | SLEEP)); // Sleep while we set the prescale value. mI2cDevice.writeRegByte(REG_MODE_1, (byte) (mode1 | SLEEP)); float prescaleval = 25000000f // 25MHz / 4096 // 12-bit / 1600 // motor frequency - 1; // pineapple byte prescale = (byte) (prescaleval + 0.5f); mI2cDevice.writeRegByte(REG_PRESCALE, prescale); // Restart: clear the sleep bit, wait for the oscillator to stabilize, set the restart bit. // https://cdn-shop.adafruit.com/datasheets/PCA9685.pdf (page 15) mI2cDevice.writeRegByte(REG_MODE_1, (byte) (mode1 & ~SLEEP)); try { TimeUnit.MICROSECONDS.sleep(500); } catch (InterruptedException ignored) { } mI2cDevice.writeRegByte(REG_MODE_1, (byte) (mode1 | RESTART)); mMotors = new DcMotor[MAX_DC_MOTORS]; mMotors[0] = new DcMotor(8, 9, 10); mMotors[1] = new DcMotor(13, 12, 11); mMotors[2] = new DcMotor(2, 3, 4); mMotors[3] = new DcMotor(7, 6, 5); }
Example #10
Source File: Vcnl4200.java From contrib-drivers with Apache License 2.0 | 5 votes |
/** * Create a new VCNL4200 sensor driver connected to the given I2C bus. * @param bus I2C bus the sensor is connected to. * @param configuration Initial configuration of the sensor. * @throws IOException */ public Vcnl4200(String bus, Configuration configuration) throws IOException { PeripheralManager pioService = PeripheralManager.getInstance(); I2cDevice device = pioService.openI2cDevice(bus, I2C_ADDRESS); try { connect(device, configuration); } catch (IOException|RuntimeException e) { try { close(); } catch (IOException|RuntimeException ignored) { } throw e; } }
Example #11
Source File: Bmx280.java From contrib-drivers with Apache License 2.0 | 5 votes |
/** * Create a new BMP/BME280 sensor driver connected on the given bus and address. * @param bus I2C bus the sensor is connected to. * @param address I2C address of the sensor. * @throws IOException */ public Bmx280(String bus, int address) throws IOException { PeripheralManager pioService = PeripheralManager.getInstance(); I2cDevice device = pioService.openI2cDevice(bus, address); try { connect(device); } catch (IOException|RuntimeException e) { try { close(); } catch (IOException|RuntimeException ignored) { } throw e; } }
Example #12
Source File: Bmx280.java From contrib-drivers with Apache License 2.0 | 5 votes |
private void connect(I2cDevice device) throws IOException { mDevice = device; mChipId = mDevice.readRegByte(BMX280_REG_ID); mHasHumiditySensor = mChipId == CHIP_ID_BME280; // Read temperature calibration data (3 words). First value is unsigned. mTempCalibrationData[0] = mDevice.readRegWord(BMX280_REG_TEMP_CALIB_1) & 0xffff; mTempCalibrationData[1] = mDevice.readRegWord(BMX280_REG_TEMP_CALIB_2); mTempCalibrationData[2] = mDevice.readRegWord(BMX280_REG_TEMP_CALIB_3); // Read pressure calibration data (9 words). First value is unsigned. mPressureCalibrationData[0] = mDevice.readRegWord(BMX280_REG_PRESS_CALIB_1) & 0xffff; mPressureCalibrationData[1] = mDevice.readRegWord(BMX280_REG_PRESS_CALIB_2); mPressureCalibrationData[2] = mDevice.readRegWord(BMX280_REG_PRESS_CALIB_3); mPressureCalibrationData[3] = mDevice.readRegWord(BMX280_REG_PRESS_CALIB_4); mPressureCalibrationData[4] = mDevice.readRegWord(BMX280_REG_PRESS_CALIB_5); mPressureCalibrationData[5] = mDevice.readRegWord(BMX280_REG_PRESS_CALIB_6); mPressureCalibrationData[6] = mDevice.readRegWord(BMX280_REG_PRESS_CALIB_7); mPressureCalibrationData[7] = mDevice.readRegWord(BMX280_REG_PRESS_CALIB_8); mPressureCalibrationData[8] = mDevice.readRegWord(BMX280_REG_PRESS_CALIB_9); if (mHasHumiditySensor) { // Read humidity calibration data mHumCalibrationData[0] = mDevice.readRegByte(BME280_REG_HUM_CALIB_1) & 0xff; // dig_H1 mHumCalibrationData[1] = mDevice.readRegWord(BME280_REG_HUM_CALIB_2); // dig_H2 mHumCalibrationData[2] = mDevice.readRegByte(BME280_REG_HUM_CALIB_3) & 0xff; // dig_H3 int E4 = mDevice.readRegByte(BME280_REG_HUM_CALIB_4) & 0xff; int E5 = mDevice.readRegByte(BME280_REG_HUM_CALIB_5) & 0xff; int E6 = mDevice.readRegByte(BME280_REG_HUM_CALIB_6) & 0xff; int E7 = mDevice.readRegByte(BME280_REG_HUM_CALIB_7); mHumCalibrationData[3] = (E4 << 4) | (E5 & 0x0F); // dig_H4 mHumCalibrationData[4] = (E6 << 4) | (E5 >> 4); // dig_H5 mHumCalibrationData[5] = E7; // dig_H6 } }
Example #13
Source File: Adcv2x.java From contrib-drivers with Apache License 2.0 | 5 votes |
private void connect(I2cDevice device) throws IOException { if (mDevice != null) { throw new IllegalStateException("device already connected"); } mDevice = device; // set the range here to 4 volts even tho we know we're only feeding it ~3.3 // the dial turned all the way up will read ~3.2 setRange(RANGE_4_096V); }
Example #14
Source File: Ssd1306.java From contrib-drivers with Apache License 2.0 | 5 votes |
/** * Recommended start sequence for initializing the communications with the OLED display. * WARNING: If you change this code, power cycle your display before testing. * @throws IOException */ private void init(I2cDevice device, int width, int height) throws IOException { mI2cDevice = device; mWidth = width; mHeight = height; mBuffer = new byte[((mWidth * mHeight) / 8) + 1]; BitmapHelper.bmpToBytes(mBuffer, DATA_OFFSET, Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888), false); mBuffer[0] = (byte) COMMAND_START_LINE; // Recommended initialization sequence based on http://goo.gl/VSu0C8 mI2cDevice.write(INIT_PAYLOAD, INIT_PAYLOAD.length); stopScroll(); }
Example #15
Source File: Ssd1306.java From contrib-drivers with Apache License 2.0 | 5 votes |
/** * Create a new Ssd1306 driver connected to the named I2C bus and address * with the given dimensions. * @param i2cName I2C bus name the display is connected to * @param i2cAddress I2C address of the display * @param width display width in pixels. * @param height display height in pixels. * @throws IOException */ public Ssd1306(String i2cName, int i2cAddress, int width, int height) throws IOException { I2cDevice device = PeripheralManager.getInstance().openI2cDevice(i2cName, i2cAddress); try { init(device, width, height); } catch (IOException | RuntimeException e) { try { close(); } catch (IOException | RuntimeException ignored) { } throw e; } }
Example #16
Source File: Sh1106.java From androidthings-drivers with Apache License 2.0 | 5 votes |
/** * Create a new Sh1106 driver connected to the named I2C bus and address * with the given dimensions. * * @param i2cName I2C bus name the display is connected to * @param i2cAddress I2C address of the display * @param width display width in pixels. * @param height display height in pixels. * @throws IOException */ public Sh1106(String i2cName, int i2cAddress, int width, int height) throws IOException { I2cDevice device = PeripheralManager.getInstance().openI2cDevice(i2cName, i2cAddress); try { init(device, width, height); } catch (IOException | RuntimeException e) { try { close(); } catch (IOException | RuntimeException ignored) { } throw e; } }
Example #17
Source File: Ds3231.java From androidthings-drivers with Apache License 2.0 | 5 votes |
/** * Create a new Ds3231 driver connected to the named I2C bus and address * with the given dimensions. * * @param i2cName I2C bus name the RTC is connected to * @param i2cAddress I2C address of the RTC * @throws IOException */ public Ds3231(String i2cName, int i2cAddress) throws IOException { I2cDevice device = PeripheralManager.getInstance().openI2cDevice(i2cName, i2cAddress); try { connect(device); } catch (IOException | RuntimeException e) { try { close(); } catch (IOException | RuntimeException ignored) { } throw e; } }
Example #18
Source File: Lsm9ds1.java From androidthings-drivers with Apache License 2.0 | 5 votes |
private void connect(Builder builder, I2cDevice accelGyroDevice, I2cDevice magDevice) throws IOException { mAccelGyroDevice = accelGyroDevice; mMagDevice = magDevice; resetAndReboot(SENSOR_XG, false); resetAndReboot(SENSOR_MAG, true); byte idXg = readRegByte(SENSOR_XG, REGISTER_WHO_AM_I_XG); byte idMag = readRegByte(SENSOR_MAG, REGISTER_WHO_AM_I_M); if (idXg != XG_ID || idMag != MAG_ID) { throw new IllegalStateException("Could not find LSM9DS1, check wiring!"); } // FIFO configuration setFifoModeAndTreshold(builder.mFifoMode, builder.mFifoThreshold); setFifoMemoryEnabled(builder.mFifoMemoryEnabled); // Accelerometer configuration setAccelerometerOdr(builder.mAccelerometerOdr); setAccelerometerEnabledAxes(builder.mAccelerometerEnabledAxes); setAccelerometerDecimation(builder.mAccelerometerDecimation); setAccelerometerHighResolution(builder.mAccelerometerHighResolution); setAccelerometerRange(builder.mAccelerometerRange); // Gyroscope configuration setGyroscopeOdr(builder.mGyroscopeOdr); setGyroscopeScale(builder.mGyroscopeScale); // Magnetometer configuration setMagnetometerTemperatureCompensation(builder.mMagnetometerTemperatureCompensation); setMagnetometerXYOperatingMode(builder.mMagnetometerXYOperatingMode); setMagnetometerZOperatingMode(builder.mMagnetometerZOperatingMode); setMagnetometerSystemOperatingMode(builder.mMagnetometerSystemOperatingMode); setMagnetometerGain(builder.mMagnetometerGain); }
Example #19
Source File: Lsm9ds1.java From androidthings-drivers with Apache License 2.0 | 5 votes |
/** * Use the {@link Builder} to create a new LSM9DS1 sensor driver instance. * * @throws IOException */ private Lsm9ds1(Builder builder) throws IOException { PeripheralManager pioService = PeripheralManager.getInstance(); I2cDevice accelGyroDevice = pioService.openI2cDevice(builder.mI2cBus, builder.mI2cAddressAccelGyro); I2cDevice magDevice = pioService.openI2cDevice(builder.mI2cBus, builder.mI2cAddressMag); try { connect(builder, accelGyroDevice, magDevice); } catch (IOException | RuntimeException e) { try { close(); } catch (IOException | RuntimeException ignored) { } throw e; } }
Example #20
Source File: Cap1xxxTestActivity.java From contrib-drivers with Apache License 2.0 | 5 votes |
private static I2cDevice initMockI2cDevice() throws IOException { I2cDevice i2c = Mockito.mock(I2cDevice.class); // set input thresholds Mockito.doAnswer(sInputThresholdAnswer) .when(i2c) .readRegBuffer(eq(0x30), any(byte[].class), eq(CONFIGURATION.channelCount)); return i2c; }
Example #21
Source File: Hd44780.java From androidthings-drivers with Apache License 2.0 | 5 votes |
/** * Create a new Hd44780 driver connected to the named I2C bus and address * with the given geometry. * * @param i2cName I2C bus name the display is connected to * @param i2cAddress I2C address of the display * @param geometry geometry of the LCD. See {@link Geometry}. * @param use5x10Dots True to use a 10 pixel high font, false for the 8 pixel (default). It only works * for some 1 line displays. * @throws IOException */ public Hd44780(String i2cName, int i2cAddress, @Geometry int geometry, boolean use5x10Dots) throws IOException { mLcdGeometry = GEOMETRIES[geometry]; I2cDevice device = PeripheralManager.getInstance().openI2cDevice(i2cName, i2cAddress); try { init(device, use5x10Dots); } catch (IOException | RuntimeException e) { try { close(); } catch (IOException | RuntimeException ignored) { } throw e; } }
Example #22
Source File: Cap1xxx.java From contrib-drivers with Apache License 2.0 | 4 votes |
/** * Constructor invoked from unit tests. */ @VisibleForTesting /*package*/ Cap1xxx(I2cDevice i2cDevice, Gpio alertPin, Configuration chip) throws IOException { mChipConfiguration = chip; init(i2cDevice, alertPin, chip, null); }
Example #23
Source File: LedMatrix.java From contrib-drivers with Apache License 2.0 | 4 votes |
LedMatrix(I2cDevice device) { mDevice = device; }
Example #24
Source File: Cap1xxx.java From contrib-drivers with Apache License 2.0 | 4 votes |
/** * Initialize peripheral defaults from the constructor. */ private void init(I2cDevice i2cDevice, Gpio alertPin, Configuration chip, Handler handler) throws IOException { if (i2cDevice == null) { throw new IllegalArgumentException("Must provide I2C device"); } if (chip == null) { throw new IllegalArgumentException("Must provide a valid chip configuration"); } mDevice = i2cDevice; mAlertPin = alertPin; // Create handler for polling and callbacks mInputHandler = new Handler(handler == null ? Looper.myLooper() : handler.getLooper()); mInputStatus = new boolean[mChipConfiguration.channelCount]; mInputDeltas = new byte[mChipConfiguration.channelCount]; mInputThresholds = new byte[mChipConfiguration.channelCount]; if (mAlertPin != null) { // Configure hardware interrupt trigger mAlertPin.setDirection(Gpio.DIRECTION_IN); mAlertPin.setEdgeTriggerType(Gpio.EDGE_FALLING); mAlertPin.registerGpioCallback(mInputHandler, mAlertPinCallback); } else { // Begin software interrupt polling mInputHandler.post(mPollingCallback); } // Initialize device defaults setInputsEnabled(true); // Enable all inputs setInterruptsEnabled(true); // Enable all interrupts setMultitouchInputMax(mChipConfiguration.maxTouch); // Enable multitouch setRepeatRate(REPEAT_NORMAL); setSensitivity(SENSITIVITY_NORMAL); if (mChipConfiguration.ledCount > 0) { setLedFade(LED_FADE_INSTANT); // Turn off LED fading setLedBrightness(1); // Set LEDs to max brightness setLedInputLinkEnabled(false); // Unlink LEDs and sensor inputs mDevice.writeRegByte(REG_LED_CONTROL, (byte) 0x00); // Turn LEDs off } // These configs are not exposed yet. Reduce cycle time to 35ms and sampling time to 640us. mDevice.writeRegByte(REG_SAMPLING_CFG, (byte) 0b00110100); }
Example #25
Source File: MotorHat.java From contrib-drivers with Apache License 2.0 | 4 votes |
@VisibleForTesting /*package*/ MotorHat(I2cDevice device) throws IOException { initialize(device); }
Example #26
Source File: Vcnl4200.java From contrib-drivers with Apache License 2.0 | 4 votes |
/** * Package constructor with the default configuration parameters. */ /*package*/ Vcnl4200(I2cDevice device) throws IOException { this(device, new Configuration.Builder().build()); }
Example #27
Source File: AlphanumericDisplay.java From contrib-drivers with Apache License 2.0 | 4 votes |
/** * Create a new driver for a HT16K33 based alphanumeric display from a given I2C device. * @param device */ @VisibleForTesting /*package*/ AlphanumericDisplay(I2cDevice device) { super(device); }
Example #28
Source File: Ht16k33.java From contrib-drivers with Apache License 2.0 | 4 votes |
private void connect(I2cDevice device) { mDevice = device; }
Example #29
Source File: Ht16k33.java From contrib-drivers with Apache License 2.0 | 4 votes |
/** * Create a new driver for a HT16K33 peripheral from a given I2C device. * @param device */ @VisibleForTesting /*package*/ Ht16k33(I2cDevice device) { connect(device); }
Example #30
Source File: Ds3231.java From androidthings-drivers with Apache License 2.0 | 4 votes |
private void connect(I2cDevice device) throws IOException { mI2cDevice = device; // Try to read a register isTimekeepingDataValid(); }