最終更新:
moonlight_aska 2017年10月28日(土) 09:05:04履歴
- I2cActivity.java
- PeripheralManagerServiceクラスのインスタンスを生成する.
- PeripheralManagerService#openI2cDeviceメソッドで, デバイスアドレスを指定してI2cDeviceクラスのオブジェクトを取得する.
- I2cDevice#readRegByteメソッドで, 指定したアドレスのレジスタ値を読む.
メソッド | 説明 |
readRegByte(int reg) | レジスタから1バイト読み |
readRegWord(int reg) | レジスタから2バイト読み |
readRegBuffer(int reg, byte[] buffer, int length) | レジスタから指定されたバイト数読む |
- I2cDevice#closeメソッドで, デバイスを解放する.
package com.moonlight_aska.androidthings.i2c;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.google.android.things.pio.I2cDevice;
import com.google.android.things.pio.PeripheralManagerService;
import java.io.IOException;
public class I2cActivity extends Activity {
private static final String TAG = "I2cActivity";
private static final String I2C_NAME = "I2C1";
private static final int I2C_ADDRESS = 0x10;
private I2cDevice mI2c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PeripheralManagerService manager = new PeripheralManagerService();
try {
// I2Cデバイスオープン
mI2c = manager.openI2cDevice(I2C_NAME, I2C_ADDRESS);
for (int i=0; i<4; i++) {
// レジスタから1バイトリード
byte value = mI2c.readRegByte(i);
Log.d(TAG, "REG[" + i + "] = " + Integer.toHexString(value));
}
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mI2c != null) {
try {
// I2Cデバイスクローズ
mI2c.close();
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
} finally {
mI2c = null;
}
}
}
}
- 動作例

タグ
コメントをかく