コンテンツにスキップ

Color

203で色を取得

ソース

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView android:text="R"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textViewR"
        />

    <TextView android:text="G"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textViewG"
        android:layout_below="@+id/textViewR"
        />

    <TextView android:text="B"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textViewB"
        android:layout_below="@+id/textViewG"
        />

    <TextView android:text="I"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textViewI"
        android:layout_below="@+id/textViewB"
        />

</RelativeLayout>
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.gclue.myapplication;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.google.android.things.pio.Gpio;
import com.google.android.things.pio.I2cDevice;
import com.google.android.things.pio.PeripheralManagerService;
import java.io.IOException;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    // I2C Device Name
    private static final String I2C_DEVICE_NAME = "I2C1";
    // I2C Slave Address
    private static final int I2C_ADDRESS = 0x2A;
    private static final String TAG = "THINGS";
    private Handler mHandler = new Handler();
    private static final int INTERVAL_BETWEEN_AXIS_MS = 100;

    private I2cDevice mDevice;
    /** Who_am_i register */
    private byte S11059_CONTROL = 0x00;
    /** Data Format Control */
    private byte S11059_DATA_RED_H = 0x03;
    private byte S11059_CTRL_GAIN  = 0x08;
    private byte S11059_CTRL_MODE  = 0x04;
    private byte S11059_CTRL_TIME_224M = 0x2;
    /** TextView */
    TextView mTextViewR;
    TextView mTextViewG;
    TextView mTextViewB;
    TextView mTextViewI;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        mTextViewR = (TextView) findViewById(R.id.textViewR);
        mTextViewG = (TextView) findViewById(R.id.textViewG);
        mTextViewB = (TextView) findViewById(R.id.textViewB);
        mTextViewI = (TextView) findViewById(R.id.textViewI);

        // Attempt to access the I2C device
        try {
            PeripheralManagerService manager = new PeripheralManagerService();
            mDevice = manager.openI2cDevice(I2C_DEVICE_NAME, I2C_ADDRESS);
        } catch (IOException e) {
            Log.w(TAG, "Unable to access I2C device", e);
        }

        try {
            // Setting
            byte value = mDevice.readRegByte(S11059_CONTROL);
            value |= S11059_CTRL_GAIN;
            value &= ~(S11059_CTRL_MODE);
            value &= 0xFC;
            value |= S11059_CTRL_TIME_224M;
            value &= 0x3F; // RESET off,SLEEP off
            mDevice.writeRegByte(S11059_CONTROL, value);

            // Handler.
            mHandler.post(mAxisRunnable);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private Runnable mAxisRunnable = new Runnable() {
        @Override
        public void run() {

            if (mDevice == null) {
                return;
            }

            try {
                int length = 8;
                byte color_buff[] = new byte[length];
                mDevice.readRegBuffer(S11059_DATA_RED_H, color_buff, color_buff.length);
                mHandler.postDelayed(mAxisRunnable, INTERVAL_BETWEEN_AXIS_MS);
                int r = (((int)color_buff[0]) << 8) | color_buff[1];
                int g = (((int)color_buff[2]) << 8) | color_buff[3];
                int b = (((int)color_buff[4]) << 8) | color_buff[5];
                int i = (((int)color_buff[6]) << 8) | color_buff[7];

                Log.i(TAG, "r=" + r + " g=" + g + " b=" + b + " i=" + i);
                mTextViewR.setText("G: " + r);
                mTextViewG.setText("R: " + g);
                mTextViewB.setText("B: " + b);
                mTextViewI.setText("I: " + i);

            } catch (IOException e) {
                Log.e(TAG, "Error on I2C Device API", e);
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mDevice != null) {
            try {
                mDevice.close();
                mDevice = null;
            } catch (IOException e) {
                Log.w(TAG, "Unable to close I2C device", e);
            }
        }
    }



}