Skip to content

Instantly share code, notes, and snippets.

@mikepyts
Last active November 4, 2018 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikepyts/a0508ef8e0017732d6cc8b8f42460862 to your computer and use it in GitHub Desktop.
Save mikepyts/a0508ef8e0017732d6cc8b8f42460862 to your computer and use it in GitHub Desktop.
Example how to use user-space driver for button events (Kotlin, Android Things)
package <package_name>
import android.app.Activity
import android.os.Bundle
import android.util.Log
import com.google.android.things.contrib.driver.button.Button
import java.io.IOException
class UserDriver : Activity() {
// Variables
val TAG = "Driver Activity"
lateinit var mButton: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Access the button and listen for events:
try {
mButton = Button(<GPIO_pin>, Button.LogicState.PRESSED_WHEN_HIGH)
mButton.setDebounceDelay(1)
// Set listener when key state changed
mButton.setOnButtonEventListener { button, pressed ->
Log.d(TAG, "Button is pressed")
Log.d(TAG, "Key state: " + pressed.toString())
}
} catch (e: IOException) {
Log.d(TAG, "Can't configure GPIO")
}
}
override fun onDestroy() {
super.onDestroy()
// Close the connection
mButton.close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment