Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Low-level Peripheral I/O with Android Things

Low-level Peripheral I/O with Android Things

Learn about Android Things and how to apply low-level peripheral I/O such as I2C and SPI.

Avatar for Dennis Geurts

Dennis Geurts

October 10, 2017

Other Decks in Programming

Transcript

  1. GPIO PWM I2C SPI UART Input Sensors GPS Peripheral Driver

    Library Peripheral I/O User Drivers
  2. // Open a peripheral connection PeripheralManagerService service = new PeripheralManagerService();

    Gpio button = service.openGpio(GPIO_PIN_NAME); // Configure the peripheral button.setDirection(Gpio.DIRECTION_IN); button.setEdgeTriggerType(Gpio.EDGE_FALLING); // Attach callback for input events button.registerGpioCallback(new GpioCallback() { @Override public boolean onGpioEdge(Gpio gpio) { Log.i(TAG, "GPIO changed"); // Return true to continue listening to events return true; } }); Simple Peripheral I/O
  3. private GpioCallback callback = new GpioCallback() { @Override public boolean

    onGpioEdge(Gpio gpio) { int keyAction = gpio.getValue() ? KeyEvent.ACTION_DOWN : KeyEvent.ACTION_UP; inputDriver.emit(new KeyEvent[]{ new KeyEvent(keyAction, KeyEvent.KEYCODE_SPACE)}); return true; } }; Button User Driver InputDriver inputDriver = InputDriver.builder(InputDevice.SOURCE_CLASS_BUTTON) .setName(DRIVER_NAME) .setVersion(DRIVER_VERSION) .setKeys(new int[]{KeyEvent.KEYCODE_SPACE}) .build(); UserDriverManager.getManager().registerInputDriver(inputDriver);
  4. dependencies { compile 'com.google.android.things.contrib:driver-button:...' } ButtonInputDriver inputDriver = new ButtonInputDriver(

    GPIO_PIN_NAME, Button.LogicState.PRESSED_WHEN_LOW, KeyEvent.KEYCODE_SPACE); inputDriver.register(); Button Library Driver “Hey, there’s a driver for that !”
  5. Digital I/O - PWM • Provide average voltage between min

    - max • Control servo • Control buzzer • Dim light intensity
  6. Higher-end protocols • UART • Inter-Integrated Circuit (I2C) • Serial

    Peripheral Interface (SPI) • Inter-IC Sound (I2S)
  7. Two wire - I2C • Synchronous • Master - Slave

    • Master provides the clock - multiple masters possible • 2- wires (3 with ground) • Devices have addresses
  8. Two wire - I2C Datasheets Datasheets Datasheets Datasheets Datasheets Datasheets

    Datasheets Datasheets Datasheets Datasheets Register 0xC1 Datasheets Register 0xA0
  9. Two wire - I2C • First read current register value

    • Then write to register (flipping a bit)
  10. SPI

  11. SPI • Synchronous/ Master - Slave/ Master provides clock •

    3 wires (4 with ground) • +1 wire for each device / to select it • High-speed 2Mbps
  12. Conclusion • Android ecosystem • OTA updates • Security •

    Ease of development - Android Studio • Driver ecosystem for peripherals • Feel free to build your own - they accept contributions (Currently working on) • RFM95 (LoRa / LoRaWAN - 868 MHz) • NRF24L01+ (2.4 GHz radio) • 433 MHz (low-level OOK)