Skip to content

Instantly share code, notes, and snippets.

@mikepyts
Last active November 4, 2018 23:24
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/3b2a6e08e9d1492270c78fc8f688509f to your computer and use it in GitHub Desktop.
Save mikepyts/3b2a6e08e9d1492270c78fc8f688509f to your computer and use it in GitHub Desktop.
Simply shows how to connect RGB LED to the button (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 com.google.android.things.pio.Gpio
import com.google.android.things.pio.PeripheralManager
import java.io.IOException
class RGB : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val manager = PeripheralManager.getInstance()
// Open GPIO pin
val blueGPIO = manager.openGpio(<GPIO_pin>)
// Set direction for the pin with initial value
blueGPIO.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW)
// Same steps should be done for other RGB LED colors
val button1 = Button(<GPIO_pin>, Button.LogicState.PRESSED_WHEN_HIGH)
// Set-up debaunce delay
button1.setDebounceDelay(10)
// Set button event listener which changes state of the LED GPIO signal
button1.setOnButtonEventListener { button, pressed ->
try {
if (pressed) {
Log.d("TAG", "Button is pressed")
Log.d("TAG", "Key state: " + pressed.toString())
blueGPIO.value = !blueGPIO.value
}
} catch (e: IOException) {
Log.d("Error:", e.toString())
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment