Last active
December 25, 2017 13:32
-
-
Save akimach/7d4411b4323fac1939eb98de39756141 to your computer and use it in GitHub Desktop.
Android Thingsの概要とKotlinでのLチカ ref: https://qiita.com/akimach/items/07cc61b16068336be86a
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity | |
import android.os.Build | |
import android.os.Bundle | |
import android.util.Log | |
import com.google.android.things.pio.PeripheralManagerService | |
class MainActivity : Activity() { | |
private val TAG = "MainActivity" | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
Log.d(TAG, "BOARD:" + Build.BOARD) | |
Log.d(TAG, "DEVICE:" + Build.DEVICE) | |
Log.d(TAG, "BRAND:" + Build.BRAND) | |
Log.d(TAG, "PRODUCT:" + Build.PRODUCT) | |
Log.d(TAG, "CPU_ABI2:" + Build.CPU_ABI2) | |
Log.d(TAG, "HARDWARE:" + Build.HARDWARE) | |
val manager = PeripheralManagerService() | |
Log.d(TAG, "GPIO:" + manager.gpioList.toString()) | |
Log.d(TAG, "PWM:" + manager.pwmList.toString()) | |
Log.d(TAG, "I2C:" + manager.i2cBusList.toString()) | |
Log.d(TAG, "SPI:" + manager.spiBusList.toString()) | |
Log.d(TAG, "UART:" + manager.uartDeviceList.toString()) | |
Log.d(TAG, "I2S:" + manager.i2sDeviceList.toString()) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
D/MainActivity: BOARD:imx7d | |
D/MainActivity: DEVICE:imx7d_pico | |
D/MainActivity: BRAND:Things | |
D/MainActivity: PRODUCT:iot_imx7d_pico | |
D/MainActivity: CPU_ABI2:armeabi | |
D/MainActivity: HARDWARE:imx7d | |
D/MainActivity: GPIO:[GPIO_10, GPIO_128, GPIO_172, GPIO_173, GPIO_174, GPIO_175, GPIO_32, GPIO_33, GPIO_34, GPIO_35, GPIO_37, GPIO_39] | |
D/MainActivity: PWM:[PWM1, PWM2] | |
D/MainActivity: I2C:[I2C1] | |
D/MainActivity: SPI:[SPI3.0, SPI3.1] | |
D/MainActivity: UART:[UART6] | |
D/MainActivity: I2S:[I2S3] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity | |
import android.os.Bundle | |
import android.os.Handler | |
import android.util.Log | |
import com.google.android.things.pio.PeripheralManagerService | |
import com.google.android.things.pio.Gpio | |
import java.io.IOException | |
import com.google.android.things.pio.GpioCallback | |
class MainActivity : Activity() { | |
private val TAG = "MainActivity" | |
private val LED_PIN_NAME = "GPIO_34" | |
private val BUTTON_PIN_NAME = "GPIO_174" | |
private lateinit var mLedGpio: Gpio | |
private lateinit var mButtonGpio: Gpio | |
// Callback for button | |
private val mCallback = object : GpioCallback() { | |
override fun onGpioEdge(gpio: Gpio?): Boolean { | |
Log.i(TAG, "GPIO changed, button pressed") | |
val mLedGpio = mLedGpio ?: return false | |
try { | |
mLedGpio.value = !mLedGpio.value | |
} catch (e: IOException) { | |
Log.e(TAG, "Error on PeripheralIO API", e) | |
} | |
return true | |
} | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
val service = PeripheralManagerService() | |
Log.d(TAG, "Available GPIO: " + service.gpioList) | |
try { | |
// Setup LED | |
mLedGpio = service.openGpio(LED_PIN_NAME) | |
mLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW) | |
mLedGpio.value = false | |
Log.i(TAG, "Setup LED pin") | |
// Setup Button | |
mButtonGpio = service.openGpio(BUTTON_PIN_NAME) | |
mButtonGpio.setDirection(Gpio.DIRECTION_IN) | |
mButtonGpio.setEdgeTriggerType(Gpio.EDGE_FALLING) | |
mButtonGpio.registerGpioCallback(mCallback) | |
Log.i(TAG, "Setup Button pin") | |
} catch (e: IOException) { | |
Log.e(TAG, "Error on PeripheralIO API", e) | |
} | |
} | |
override fun onDestroy() { | |
super.onDestroy() | |
mButtonGpio.unregisterGpioCallback(mCallback) | |
try { | |
mLedGpio.close() | |
mButtonGpio.close() | |
} catch (e: IOException) { | |
Log.e(TAG, "Error on PeripheralIO API", e) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment