Skip to content

Instantly share code, notes, and snippets.

@patrickhammond
Last active January 21, 2017 04:59
Show Gist options
  • Save patrickhammond/5b50b5d824abee940c12836abd3bf804 to your computer and use it in GitHub Desktop.
Save patrickhammond/5b50b5d824abee940c12836abd3bf804 to your computer and use it in GitHub Desktop.
Motor controlled via L293D.
package com.madebyatomicrobot.things
import android.app.Activity
import android.os.Bundle
import android.util.Log
import com.google.android.things.pio.Gpio
import com.google.android.things.pio.PeripheralManagerService
import java.io.IOException
class MainActivity : Activity() {
companion object {
private val TAG = MainActivity::class.java.simpleName
}
lateinit var gpio17: Gpio
lateinit var gpio18: Gpio
lateinit var gpio27: Gpio
var motorOn = false
var clockwise = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val manager = PeripheralManagerService()
try {
Log.i(TAG, "GPIO: " + manager.gpioList)
gpio17 = manager.openGpio("BCM17")
gpio17.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW)
gpio17.setActiveType(Gpio.ACTIVE_HIGH)
gpio17.value = false
gpio18 = manager.openGpio("BCM18")
gpio18.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW)
gpio18.setActiveType(Gpio.ACTIVE_HIGH)
gpio18.value = false
gpio27 = manager.openGpio("BCM27")
gpio27.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW)
gpio27.setActiveType(Gpio.ACTIVE_HIGH)
gpio27.value = false
} catch (ex: IOException) {
Log.w(TAG, "Error in onCreate", ex)
}
findViewById(R.id.toggle).setOnClickListener { toggleOn() }
findViewById(R.id.direction).setOnClickListener { toggleDirection() }
}
fun toggleOn() {
motorOn = !motorOn
updateMotor()
}
fun toggleDirection() {
clockwise = !clockwise
updateMotor()
}
fun updateMotor() {
gpio27.value = motorOn
gpio17.value = clockwise
gpio18.value = !clockwise
}
override fun onDestroy() {
try {
gpio17.close()
gpio18.close()
gpio27.close()
} catch (ex: IOException) {
Log.w(TAG, "Error in onDestroy", ex)
}
super.onDestroy()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment