com.google.android.things.pio.Gpio Java Examples
The following examples show how to use
com.google.android.things.pio.Gpio.
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: BlinkActivity.java From sample-simplepio with Apache License 2.0 | 6 votes |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.i(TAG, "Starting BlinkActivity"); try { String pinName = BoardDefaults.getGPIOForLED(); mLedGpio = PeripheralManager.getInstance().openGpio(pinName); mLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW); Log.i(TAG, "Start blinking LED GPIO pin"); // Post a Runnable that continuously switch the state of the GPIO, blinking the // corresponding LED mHandler.post(mBlinkRunnable); } catch (IOException e) { Log.e(TAG, "Error on PeripheralIO API", e); } }
Example #2
Source File: Button.java From contrib-drivers with Apache License 2.0 | 6 votes |
@Override public boolean onGpioEdge(Gpio gpio) { try { boolean currentState = gpio.getValue(); if (mDebounceDelay == 0) { // Trigger event immediately performButtonEvent(currentState); } else { // Pass trigger state forward if a check was pending boolean trigger = (mPendingCheckDebounce == null) ? currentState : mPendingCheckDebounce.getTriggerState(); // Clear any pending checks removeDebounceCallback(); // Set a new pending check mPendingCheckDebounce = new CheckDebounce(trigger); mDebounceHandler.postDelayed(mPendingCheckDebounce, mDebounceDelay); } } catch (IOException e) { Log.e(TAG, "Error reading button state", e); } return true; }
Example #3
Source File: ButtonTestActivity.java From contrib-drivers with Apache License 2.0 | 6 votes |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mGpio = Mockito.mock(Gpio.class); try { mButton = new Button(mGpio, LogicState.PRESSED_WHEN_HIGH); } catch (IOException e) { throw new RuntimeException("Failed to initialize Button", e); } mInputDriver = new ButtonInputDriver(mButton, KEYCODE); mInputDriver.register(); mKeyDownEvents = new LinkedBlockingQueue<>(); mKeyUpEvents = new LinkedBlockingQueue<>(); }
Example #4
Source File: I2cBitBangDevice.java From contrib-drivers with Apache License 2.0 | 6 votes |
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 #5
Source File: MatrixKeypad.java From contrib-drivers with Apache License 2.0 | 6 votes |
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 #6
Source File: ButtonActivity.java From sample-simplepio with Apache License 2.0 | 6 votes |
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.i(TAG, "Starting ButtonActivity"); try { String pinName = BoardDefaults.getGPIOForButton(); mButtonGpio = PeripheralManager.getInstance().openGpio(pinName); mButtonGpio.setDirection(Gpio.DIRECTION_IN); mButtonGpio.setEdgeTriggerType(Gpio.EDGE_FALLING); mButtonGpio.registerGpioCallback(new GpioCallback() { @Override public boolean onGpioEdge(Gpio gpio) { Log.i(TAG, "GPIO changed, button pressed"); // Return true to continue listening to events return true; } }); } catch (IOException e) { Log.e(TAG, "Error on PeripheralIO API", e); } }
Example #7
Source File: MatrixKeypad.java From contrib-drivers with Apache License 2.0 | 6 votes |
MatrixKeypad(Gpio[] rowGpio, Gpio[] colGpio, int[] keyCodes, Handler handler) throws IOException { mGpioRows = rowGpio; mGpioCols = colGpio; mKeyCodes = keyCodes; mMatrix = new MatrixKey[rowGpio.length][colGpio.length]; // Initialize Gpio and keys for (int r = 0; r < rowGpio.length; r++) { mGpioRows[r].setDirection(Gpio.DIRECTION_IN); for (int c = 0; c < colGpio.length; c++) { if (mGpioCols[c] == null) { mGpioCols[c].setDirection(Gpio.DIRECTION_IN); } mMatrix[r][c] = new MatrixKey(keyCodes[r * colGpio.length + c]); } } mKeyScanHandler = handler; initKeyScanHandler(handler); }
Example #8
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 #9
Source File: ImageClassifierActivity.java From sample-tensorflow-imageclassifier with Apache License 2.0 | 6 votes |
/** * 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 #10
Source File: Lcd1602.java From 1602A-androidthings with Apache License 2.0 | 5 votes |
/** * <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 #11
Source File: Button.java From contrib-drivers with Apache License 2.0 | 5 votes |
private void connect(Gpio buttonGpio, LogicState logicLevel) throws IOException { mButtonGpio = buttonGpio; mButtonGpio.setDirection(Gpio.DIRECTION_IN); mButtonGpio.setEdgeTriggerType(Gpio.EDGE_BOTH); // Configure so pressed is always true mButtonGpio.setActiveType(logicLevel == LogicState.PRESSED_WHEN_LOW ? Gpio.ACTIVE_LOW : Gpio.ACTIVE_HIGH); mButtonGpio.registerGpioCallback(mInterruptCallback); mDebounceHandler = new Handler(); }
Example #12
Source File: VoiceHatPeripheralInstrumentationTest.java From contrib-drivers with Apache License 2.0 | 5 votes |
/** * Verify that the LED is null if not passed as a parameter. */ @Test @UiThreadTest public void testOpenLed() throws IOException { InstrumentationTestUtils.assertRaspberryPiOnly(); Gpio led = VoiceHat.openLed(); Assert.assertNotNull(led); led.close(); }
Example #13
Source File: Max98357A.java From contrib-drivers with Apache License 2.0 | 5 votes |
/** * 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 #14
Source File: MatrixKeypad.java From contrib-drivers with Apache License 2.0 | 5 votes |
@Override public void run() { if (mKeyEventListener != null) { try { // Provide voltage to each column line separately, keeping the others at HIGH-Z // as inputs. for (int c = 0; c < mGpioCols.length; c++) { Gpio colPin = mGpioCols[c]; colPin.setDirection(Gpio.DIRECTION_OUT_INITIALLY_HIGH); for (int r = 0; r < mGpioRows.length; r++) { Gpio rowPin = mGpioRows[r]; if (rowPin.getValue() && !mMatrix[r][c].isPressed()) { mMatrix[r][c].setPressed(true); keyDown(mMatrix[r][c]); } else if (!rowPin.getValue() && mMatrix[r][c].isPressed()) { mMatrix[r][c].setPressed(false); keyUp(mMatrix[r][c]); } } colPin.setDirection(Gpio.DIRECTION_IN); } } catch (IOException e) { Log.e(TAG, e.getMessage()); } } mKeyScanHandler.postDelayed(this, SOFTWAREPOLL_DELAY_MS); }
Example #15
Source File: ButtonTest.java From contrib-drivers with Apache License 2.0 | 5 votes |
@Before public void setup() throws Exception { ViewConfigurationMock.mockStatic(); // Note: we need PowerMockito, so instantiate mocks here instead of using a MockitoRule mGpio = PowerMockito.mock(Gpio.class); mGpioCallback = PowerMockito.mock(InterruptCallback.class); PowerMockito.doNothing().when(mGpio).registerGpioCallback(any(GpioCallback.class)); PowerMockito.whenNew(InterruptCallback.class).withNoArguments().thenReturn(mGpioCallback); }
Example #16
Source File: Cap1xxxTest.java From contrib-drivers with Apache License 2.0 | 5 votes |
@Before public void setup() throws Exception { mGpio = PowerMockito.mock(Gpio.class); mGpioCallback = PowerMockito.mock(AlertCallback.class); PowerMockito.doNothing().when(mGpio).registerGpioCallback(any(GpioCallback.class)); PowerMockito.whenNew(AlertCallback.class).withNoArguments().thenReturn(mGpioCallback); mDriver = new Cap1xxx(mI2c, mGpio, CONFIGURATION); // clear invocations from driver init Mockito.reset(mGpio); Mockito.reset(mI2c); }
Example #17
Source File: VoiceHat.java From contrib-drivers with Apache License 2.0 | 5 votes |
/** * 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 #18
Source File: Hcsr04.java From androidthings-drivers with Apache License 2.0 | 5 votes |
/** * 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 #19
Source File: Hcsr04.java From androidthings-drivers with Apache License 2.0 | 5 votes |
private void connect(Gpio trigGpio, Gpio echoGpio) throws IOException { mTrigGpio = trigGpio; mEchoGpio = echoGpio; mTrigGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW); mEchoGpio.setDirection(Gpio.DIRECTION_IN); mTrigGpio.setActiveType(Gpio.ACTIVE_HIGH); mEchoGpio.setActiveType(Gpio.ACTIVE_HIGH); mEchoGpio.setEdgeTriggerType(Gpio.EDGE_BOTH); }
Example #20
Source File: Epd.java From androidthings-drivers with Apache License 2.0 | 5 votes |
protected void configure() throws IOException { // Note: You may need to set bit justification for your board. // mSpiDevice.setBitJustification(SPI_BITJUST); mSpiDevice.setFrequency(SPI_FREQUENCY); mSpiDevice.setMode(SPI_MODE); mSpiDevice.setBitsPerWord(SPI_BPW); mResetPin.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW); mDataCommandPin.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW); mBusyPin.setDirection(Gpio.DIRECTION_IN); }
Example #21
Source File: Lcd1602.java From 1602A-androidthings with Apache License 2.0 | 5 votes |
/** * <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 #22
Source File: TricolorLed.java From robocar with Apache License 2.0 | 5 votes |
private Gpio createGpio(String pin) { try { Gpio gpio = PeripheralManager.getInstance().openGpio(pin); gpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_HIGH); return gpio; } catch (IOException e) { Log.e(TAG, "Error creating GPIO for pin " + pin, e); } return null; }
Example #23
Source File: TricolorLed.java From robocar with Apache License 2.0 | 5 votes |
private void closeGpio(Gpio gpio) { if (gpio != null) { try { gpio.close(); } catch (IOException e) { Log.e(TAG, "Error closing gpio", e); } } }
Example #24
Source File: TricolorLed.java From robocar with Apache License 2.0 | 5 votes |
private void setGpioValue(Gpio gpio, boolean value) { if (gpio != null) { try { gpio.setValue(value); } catch (IOException ignored) { } } }
Example #25
Source File: FaBoThingsDeviceControl.java From DeviceConnect-Android with MIT License | 5 votes |
@Override public void setPinMode(final FaBoShield.Pin pin, final FaBoShield.Mode mode) { mHandler.post(new Runnable() { @Override public void run() { try { Gpio gpio = mGpioMap.get(pin.getPinNumber()); if (gpio != null && pin.getMode() != mode) { switch (mode) { case GPIO_IN: gpio.setEdgeTriggerType(Gpio.EDGE_NONE); gpio.setDirection(Gpio.DIRECTION_IN); gpio.setActiveType(Gpio.ACTIVE_HIGH); gpio.setEdgeTriggerType(Gpio.EDGE_BOTH); break; case GPIO_OUT: gpio.setEdgeTriggerType(Gpio.EDGE_NONE); gpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW); gpio.setActiveType(Gpio.ACTIVE_HIGH); break; } pin.setMode(mode); } } catch (IOException e) { if (DEBUG) { Log.w(TAG, "setPinMode", e); } } } }); }
Example #26
Source File: FaBoThingsDeviceControl.java From DeviceConnect-Android with MIT License | 5 votes |
@Override public FaBoShield.Level getDigital(final FaBoShield.Pin pin) { try { Gpio gpio = mGpioMap.get(pin.getPinNumber()); if (gpio != null) { return gpio.getValue() ? FaBoShield.Level.HIGH : FaBoShield.Level.LOW; } return null; } catch (Exception e) { return null; } }
Example #27
Source File: RainbowHatInstrumentationTest.java From contrib-drivers with Apache License 2.0 | 5 votes |
/** * Verify that the LED can be opened and closed. */ @Test @UiThreadTest public void testOpenLed() throws IOException { Gpio red = RainbowHat.openLedRed(); Gpio green = RainbowHat.openLedGreen(); Gpio blue = RainbowHat.openLedBlue(); Assert.assertNotNull(red); Assert.assertNotNull(green); Assert.assertNotNull(blue); red.close(); green.close(); blue.close(); }
Example #28
Source File: ZXGestureSensorI2c.java From contrib-drivers with Apache License 2.0 | 5 votes |
/** * Creates new sensor driver for given I2C port. * @param port I2C port identifier * @param i2cAddress I2C address that sensor listens on. either 0x10 or 0x11 * @param gpioPin gpio pin with interrupt enabled that will listen to DR line * @throws IOException * * currently interrupt with <code>gpioPin</code> does not work; * register value for REG_DRE is not set */ private void connectI2c(String port, int i2cAddress, String gpioPin) throws IOException { mDevice = pioService.openI2cDevice(port, i2cAddress); if (gpioPin != null) { mDevice.writeRegByte(REG_DRE, GESTURE_DR_MASK); mGpio = pioService.openGpio(gpioPin); mGpio.setDirection(Gpio.DIRECTION_IN); mGpio.setActiveType(Gpio.ACTIVE_HIGH); mGpio.setEdgeTriggerType(Gpio.EDGE_RISING); mGpio.registerGpioCallback(onGpioDataReady); } else { mHandler.post(mI2cTimer); } }
Example #29
Source File: Button.java From contrib-drivers with Apache License 2.0 | 5 votes |
/** * 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 #30
Source File: FaBoThingsDeviceControl.java From DeviceConnect-Android with MIT License | 5 votes |
@Override public void writeDigital(final FaBoShield.Pin pin, final FaBoShield.Level hl) { try { Gpio gpio = mGpioMap.get(pin.getPinNumber()); if (gpio != null) { gpio.setValue(hl == FaBoShield.Level.HIGH); } } catch (Exception e) { if (DEBUG) { Log.w(TAG, "", e); } } }