Java Code Examples for com.google.android.things.pio.PeripheralManager#getInstance()

The following examples show how to use com.google.android.things.pio.PeripheralManager#getInstance() . 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: Epd.java    From androidthings-drivers with Apache License 2.0 6 votes vote down vote up
public Epd(String spiBusPort, String resetPin, String dataCommandPin, String busyPin) throws IOException {
    PeripheralManager pioService = PeripheralManager.getInstance();
    mSpiDevice = pioService.openSpiDevice(spiBusPort);
    mResetPin = pioService.openGpio(resetPin);
    mDataCommandPin = pioService.openGpio(dataCommandPin);
    mBusyPin = pioService.openGpio(busyPin);
    try {
        configure();
    } catch (IOException | RuntimeException e) {
        try {
            close();
        } catch (IOException | RuntimeException ignored) {
        }
        throw e;
    }
}
 
Example 2
Source File: ImageClassifierActivity.java    From sample-tensorflow-imageclassifier with Apache License 2.0 6 votes vote down vote up
/**
 * This method should only be called when running on an Android Things device.
 */
private void initPIO() {
    PeripheralManager pioManager = PeripheralManager.getInstance();
    try {
        mReadyLED = pioManager.openGpio(BoardDefaults.getGPIOForLED());
        mReadyLED.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
        mButtonDriver = new ButtonInputDriver(
                BoardDefaults.getGPIOForButton(),
                Button.LogicState.PRESSED_WHEN_LOW,
                SHUTTER_KEYCODE);
        mButtonDriver.register();
    } catch (IOException e) {
        mButtonDriver = null;
        Log.w(TAG, "Could not open GPIO pins", e);
    }
}
 
Example 3
Source File: LedControl.java    From blefun-androidthings with Apache License 2.0 6 votes vote down vote up
public LedControl(String spiGpio) throws IOException {
    PeripheralManager manager = PeripheralManager.getInstance();
    spiDevice = manager.openSpiDevice(spiGpio);
    spiDevice.setMode(SpiDevice.MODE0);
    spiDevice.setFrequency(1000000); // 1MHz
    spiDevice.setBitsPerWord(8); // 8 BPW
    spiDevice.setBitJustification(SpiDevice.BIT_JUSTIFICATION_MSB_FIRST); // MSB first

    spiTransfer(OP_DECODEMODE, 0); // decodingļ¼š BCD
    setScanLimit(7); // scanlimit: 8 LEDs
    spiTransfer(OP_DISPLAYTEST, 0);

    shutdown(false);
    setIntensity(3);
    clearDisplay();
}
 
Example 4
Source File: MatrixKeypad.java    From contrib-drivers with Apache License 2.0 6 votes vote down vote up
public MatrixKeypad(String[] rowPins, String[] colPins, int[] keyCodes,
        Handler handler) throws IOException {
    PeripheralManager pioService = PeripheralManager.getInstance();
    mGpioRows = new Gpio[rowPins.length];
    mGpioCols = new Gpio[colPins.length];
    mKeyCodes = keyCodes;
    mMatrix = new MatrixKey[rowPins.length][colPins.length];

    // Initialize Gpio and keys
    for (int r = 0; r < rowPins.length; r++) {
        mGpioRows[r] = pioService.openGpio(rowPins[r]);
        mGpioRows[r].setDirection(Gpio.DIRECTION_IN);
        for (int c = 0; c < colPins.length; c++) {
            if (mGpioCols[c] == null) {
                mGpioCols[c] = pioService.openGpio(colPins[c]);
                mGpioCols[c].setDirection(Gpio.DIRECTION_IN);
            }
            mMatrix[r][c] = new MatrixKey(keyCodes[r * colPins.length + c]);
        }
    }

    initKeyScanHandler(handler);
}
 
Example 5
Source File: Cap1xxx.java    From contrib-drivers with Apache License 2.0 6 votes vote down vote up
/**
 * 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 6
Source File: I2cBitBangDevice.java    From contrib-drivers with Apache License 2.0 6 votes vote down vote up
public I2cBitBangDevice(int i2cAddress, String pinData, String pinClock) throws IOException {
    mAddress = i2cAddress;
    PeripheralManager pioService = PeripheralManager.getInstance();
    try {
        mData = pioService.openGpio(pinData);
        mData.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
        mClock = pioService.openGpio(pinClock);
        mClock.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
    } catch (IOException|RuntimeException e) {
        try {
            close();
        } catch (IOException|RuntimeException ignored) {
        }
        throw e;
    }
}
 
Example 7
Source File: UartLowpanModule.java    From contrib-drivers with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new UartLowpanModule.
 *
 * @param lowpanDriverCallback @see {@link LowpanDriverCallback}
 * @param uartName UART port name where the module is attached. Cannot be null.
 * @param baudRate Baud rate used for the module UART.
 * @param hardwareFlowControl hardware flow control setting for uart device
 *        {@link UartDevice#HW_FLOW_CONTROL_NONE},
 *        {@link UartDevice#HW_FLOW_CONTROL_AUTO_RTSCTS}
 *        @see UartDevice#HW_FLOW_CONTROL_NONE}
 *        @see UartDevice#HW_FLOW_CONTROL_AUTO_RTSCTS}
 * @param handler optional {@link Handler} for UartDevice.
 */
public UartLowpanModule(@NonNull LowpanDriverCallback lowpanDriverCallback, String uartName,
                        int baudRate, int hardwareFlowControl,
                        UnexpectedCloseListener unexpectedCloseListener,
                        @NonNull Handler handler) throws IOException {
    mLowpanDriverCallback = lowpanDriverCallback;
    mUartName = uartName;
    mHardwareFlowControl = hardwareFlowControl;
    mHandler = handler;
    mBaudRate = baudRate;
    mUnexpectedCloseListener = unexpectedCloseListener;

    final PeripheralManager manager = PeripheralManager.getInstance();

    resetFrameState();

    mDevice = manager.openUartDevice(mUartName);
    mDevice.setBaudrate(mBaudRate);
    mDevice.setDataSize(8);
    mDevice.setHardwareFlowControl(mHardwareFlowControl);
    mDevice.registerUartDeviceCallback(mHandler, mUartDeviceCallback);
}
 
Example 8
Source File: Vcnl4200.java    From contrib-drivers with Apache License 2.0 5 votes vote down vote up
/**
 * 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 9
Source File: Button.java    From contrib-drivers with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new Button driver for the given GPIO pin name.
 * @param pin GPIO pin where the button is attached.
 * @param logicLevel Logic level when the button is considered pressed.
 * @throws IOException
 */
public Button(String pin, LogicState logicLevel) throws IOException {
    PeripheralManager pioService = PeripheralManager.getInstance();
    Gpio buttonGpio = pioService.openGpio(pin);
    try {
        connect(buttonGpio, logicLevel);
    } catch (IOException|RuntimeException e) {
        close();
        throw e;
    }
}
 
Example 10
Source File: VoiceHat.java    From contrib-drivers with Apache License 2.0 5 votes vote down vote up
/**
 * Opens the LED using a specified pin.
 *
 * @return LED on the VoiceHat
 */
public static Gpio openLed(String ledGpioPin) throws IOException {
    PeripheralManager pioService = PeripheralManager.getInstance();
    Gpio led = pioService.openGpio(ledGpioPin);
    led.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
    return led;
}
 
Example 11
Source File: Lcd1602.java    From 1602A-androidthings with Apache License 2.0 5 votes vote down vote up
/**
 * <pre>
 * When the display powers up, it is configured as follows:
 * 1. Display clear
 * 2. Function set:
 *   DL = 1; 8-bit interface data
 *   N = 0; 1-line display
 *   F = 0; 5x8 dot character font
 * 3. Display on/off control:
 *   D = 0; Display off
 *   C = 0; Cursor off
 *   B = 0; Blinking off
 * 4. Entry mode set:
 *   I/D = 1; Increment by 1
 *   S = 0; No shift
 *
 * Note, however, that resetting the Arduino doesn't reset the LCD, so we
 * can't assume that it's in that state when a sketch starts (and the
 * Lcd1602a constructor is called).
 * </pre>
 */
public Lcd1602(boolean fourbitmode, String rs, String rw, String enable, String d0, String d1, String d2, String d3, String d4, String d5, String d6, String d7) throws IOException {
    PeripheralManager manager = PeripheralManager.getInstance();

    rsGpio = manager.openGpio(rs);
    rsGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);

    // we can save 1 pin by not using RW. Indicate by passing null instead of pin#
    if (rw != null) {
        rwGpio = manager.openGpio(rw);
        rwGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
    }

    enableGpio = manager.openGpio(enable);
    enableGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);

    String[] dataPins = new String[]{d0, d1, d2, d3, d4, d5, d6, d7};
    for (int i = 0; i < (fourbitmode ? 4 : 8); i++) {
        dataGpios[i] = manager.openGpio(dataPins[i]);
        dataGpios[i].setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
    }

    if (fourbitmode) {
        displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
    } else {
        displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS;
    }

    begin(16, 1);
}
 
Example 12
Source File: NmeaGpsModule.java    From contrib-drivers with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new NmeaGpsModule.
 *
 * @param uartName UART port name where the module is attached. Cannot be null.
 * @param baudRate Baud rate used for the module UART.
 * @param accuracy specified accuracy, in meters CEP.
 * @param handler optional {@link Handler} for software polling and callback events.
 */
public NmeaGpsModule(String uartName, int baudRate, float accuracy, Handler handler) throws IOException {
    try {
        PeripheralManager manager = PeripheralManager.getInstance();
        UartDevice device = manager.openUartDevice(uartName);
        init(device, baudRate, accuracy, handler);
    } catch (IOException | RuntimeException e) {
        close();
        throw e;
    }
}
 
Example 13
Source File: Tsl256x.java    From androidthings-drivers with Apache License 2.0 5 votes vote down vote up
/**
 * 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 14
Source File: Max98357A.java    From contrib-drivers with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an interface to the chip.
 *
 * @param notSdMode The name for the NOT_SD_MODE pin
 * @param gainSlot The name for the GAIN_SLOT pin
 */
public Max98357A(String notSdMode, String gainSlot) throws IOException {
    PeripheralManager pioService = PeripheralManager.getInstance();
    if (notSdMode != null) {
        mNotSdModeGpio = pioService.openGpio(notSdMode);
        mNotSdModeGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
    }
    if (gainSlot != null) {
        mGainSlotGpio = pioService.openGpio(gainSlot);
        mGainSlotGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
    }
}
 
Example 15
Source File: Lsm9ds1.java    From androidthings-drivers with Apache License 2.0 5 votes vote down vote up
/**
 * 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 16
Source File: Hcsr04.java    From androidthings-drivers with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new HC-SR04 ultrasonic ranging module driver.
 *
 * @param trigPin The Gpio name for the trigger pin
 * @param echoPin The Gpio name for the echo pin
 * @throws IOException
 */
public Hcsr04(String trigPin, String echoPin) throws IOException {
    PeripheralManager pioService = PeripheralManager.getInstance();
    Gpio trigGpio = pioService.openGpio(trigPin);
    Gpio echoGpio = pioService.openGpio(echoPin);
    try {
        connect(trigGpio, echoGpio);
    } catch (IOException | RuntimeException e) {
        close();
        throw e;
    }
    mHandlerThread = new Hcsr04HandlerThread();
}
 
Example 17
Source File: ZXGestureSensor.java    From contrib-drivers with Apache License 2.0 4 votes vote down vote up
protected ZXGestureSensor(Handler handler) {
    pioService = PeripheralManager.getInstance();
    mGestureDetector = new SimpleGestureDetector(handler);
}
 
Example 18
Source File: SerialPortFactoryAT.java    From jlibmodbus with Apache License 2.0 4 votes vote down vote up
@Override
public List<String> getPortIdentifiersImpl() {
    final PeripheralManager pm = PeripheralManager.getInstance();
    return pm.getUartDeviceList();
}
 
Example 19
Source File: Adcv2x.java    From contrib-drivers with Apache License 2.0 3 votes vote down vote up
/**
 * Create a new instance of a Sparkfun ADC V20 board. Read more about it at
 * https://learn.sparkfun.com/tutorials/sparkfun-blocks-for-intel-edison---adc-v20
 *
 * @param bus I2C Bus Address. Typically the default "I2C1", stored as {@link #DEFAULT_BUS},
 *            but could be something else. Find what's connected with
 *            {@link PeripheralManager#getI2cBusList()}.
 *
 * @param address The closed address jumper on the back of the board. Useful for connecting
 *                multiple boards. Use the static ints enclosed in this class.
 *
 *                {@link #I2C_ADDRESS_48} (default when purchased), {@link #I2C_ADDRESS_49},
 *                {@link #I2C_ADDRESS_4A}, or {@link #I2C_ADDRESS_4B},
 *
 * @throws IOException Connection error
 */
public Adcv2x(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 20
Source File: Ht16k33.java    From contrib-drivers with Apache License 2.0 2 votes vote down vote up
/**
 * Create a new driver for a HT16K33 peripheral connected on the given I2C bus and using the
 * given I2C address.
 * @param bus
 * @param i2cAddress
 */
public Ht16k33(String bus, int i2cAddress) throws IOException {
    PeripheralManager pioService = PeripheralManager.getInstance();
    I2cDevice device = pioService.openI2cDevice(bus, i2cAddress);
    connect(device);
}