Skip to content

Instantly share code, notes, and snippets.

@riggaroo
Created October 21, 2017 10:33
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 riggaroo/b4cad83be2f81d54fd854403af029b2d to your computer and use it in GitHub Desktop.
Save riggaroo/b4cad83be2f81d54fd854403af029b2d to your computer and use it in GitHub Desktop.
Motion sensing camera
package za.co.riggaroo.motionsense
import android.app.Activity
import android.graphics.Bitmap
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.widget.ImageView
import com.google.android.things.pio.Gpio
import com.google.android.things.pio.GpioCallback
import com.google.android.things.pio.PeripheralManagerService
/**
* Skeleton of an Android Things activity.
*
* Android Things peripheral APIs are accessible through the class
* PeripheralManagerService. For example, the snippet below will open a GPIO pin and
* set it to HIGH:
*
* <pre>{@code
* val service = PeripheralManagerService()
* val mLedGpio = service.openGpio("BCM6")
* mLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW)
* mLedGpio.value = true
* }</pre>
* <p>
* For more complex peripherals, look for an existing user-space driver, or implement one if none
* is available.
*
* @see <a href="https://github.com/androidthings/contrib-drivers#readme">https://github.com/androidthings/contrib-drivers#readme</a>
*
*/
class MainActivity : Activity() {
private val MOTION_SENSOR_PIN = "GPIO_35"
private var gpioMotionSensor: Gpio? = null
private var ledGpio: Gpio? = null
private val LED_GPIO_PIN: String = "GPIO_174"
private lateinit var camera: CustomCamera
private lateinit var motionImageView: ImageView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
gpioMotionSensor = PeripheralManagerService().openGpio(MOTION_SENSOR_PIN)
if (gpioMotionSensor == null) {
Log.d("MainActivity", "motion sensor is null")
}
gpioMotionSensor?.setDirection(Gpio.DIRECTION_IN)
gpioMotionSensor?.setActiveType(Gpio.ACTIVE_HIGH)
gpioMotionSensor?.setEdgeTriggerType(Gpio.EDGE_BOTH)
gpioMotionSensor?.registerGpioCallback(object : GpioCallback() {
override fun onGpioEdge(gpio: Gpio): Boolean {
if (gpio.value) {
ledGpio?.value = true
camera.takePicture()
Log.d("MainActivity", "onGpioEdge: motion detected ")
} else {
ledGpio?.value = false
Log.d("MainActivity", "onGpioEdge: NO MOTION detected ")
}
return true
}
})
ledGpio = PeripheralManagerService().openGpio(LED_GPIO_PIN)
ledGpio?.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW)
ledGpio?.value = true
motionImageView = findViewById(R.id.image_view_motion_photo)
setupCamera()
}
private fun setupCamera() {
camera = CustomCamera.getInstance()
camera.initializeCamera(this, Handler(), imageAvailableListener)
}
private val imageAvailableListener = object : CustomCamera.ImageCapturedListener {
override fun onImageCaptured(bitmap: Bitmap) {
motionImageView.setImageBitmap(bitmap)
}
}
override fun onDestroy() {
super.onDestroy()
gpioMotionSensor?.close()
ledGpio?.close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment