10
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Raspberry Pi 3に、Rainbow HATを装着して、AndroidThingsのアプリを書いてみた

Last updated at Posted at 2017-01-13

Rainbow HAT

AndroidThingsのサンプルアプリでも使われている、Rainbow HATが届いたので、動作チェックも兼ねてプログラムを書いてみた。

th_IMG_2182.jpgth_IMG_2184.jpg

#環境
Android Studio 2.3.3
Raspberry Pi 3 (image androidthings_rpi3_devpreview_1.zip)
Rainbow HAT

#プロジェクトの作成
こちらに関しては、以前投稿したAndroid Things 用の空のプロジェクトの作り方を参照のこと

#ライブラリーの追加
build.grade(app)に追記下記を追記します。

build.grade
compile 'com.google.android.things.contrib:driver-rainbowhat:0.1'

import文を追加する

MainActivity.java
import com.google.android.things.contrib.driver.rainbowhat.RainbowHat;

#搭載されてい赤、緑、青の単色LEDをコントロールする例

import文を追加する

MainActivity.java
import com.google.android.things.pio.Gpio;
import java.io.IOException;

グローバル変数の定義

MainActivity.java
private Gpio led1 = null;
private Gpio led2 = null;
private Gpio led3 = null;
MainActivity.java
    @Override
    protected void onResume() {
        Log.d(TAG,"onResume()");
        super.onResume();

        try {
            led1 = RainbowHat.openLed(RainbowHat.LED_RED);
            led1.setValue(true);

            led2 = RainbowHat.openLed(RainbowHat.LED_GREEN);
            led2.setValue(true);

            led3 = RainbowHat.openLed(RainbowHat.LED_BLUE);
            led3.setValue(true);
        } catch (IOException e) {
            Log.d(TAG,e.toString());
        }
    }

    @Override
    protected void onPause() {
        Log.d(TAG,"onPause()");
        super.onPause();

        try {
            if(led1 != null) {
                led1.setValue(false);
                led1.close();
            }

            if(led2 != null) {
                led2.setValue(false);
                led2.close();
            }

            if(led3 != null) {
                led3.setValue(false);
                led3.close();
            }

        } catch (IOException e) {
            Log.d(TAG,e.toString());
        }

    }

#ブザーをコントールする例

import文を追加する

MainActivity.java
import com.google.android.things.contrib.driver.pwmspeaker.Speaker;
import java.io.IOException;

グローバル変数の定義

MainActivity.java
private Speaker buzzer = null;
MainActivity.java
    @Override
    protected void onResume() {
        Log.d(TAG,"onResume()");
        super.onResume();

        try {
            buzzer = RainbowHat.openPiezo();
            buzzer.play(261);
        } catch (IOException e) {
            Log.d(TAG,e.toString());
        }        } catch (IOException e) {
            Log.d(TAG,e.toString());
        }
    }

    @Override
    protected void onPause() {
        Log.d(TAG,"onPause()");
        super.onPause();

        try {
            if(buzzer != null) {
                buzzer.stop();
                buzzer.close();
            }
        } catch (IOException e) {
            Log.d(TAG,e.toString());
        }
    }

#4桁のディスプレイをコントロールする例

import文を追加する

MainActivity.java
import com.google.android.things.contrib.driver.ht16k33.AlphanumericDisplay;
import com.google.android.things.contrib.driver.ht16k33.Ht16k33;
import java.io.IOException;

グローバル変数の定義

MainActivity.java
private AlphanumericDisplay segment = null;
MainActivity.java
    @Override
    protected void onResume() {
        Log.d(TAG,"onResume()");
        super.onResume();

        try {
            segment = RainbowHat.openDisplay();
            segment.setBrightness(Ht16k33.HT16K33_BRIGHTNESS_MAX);
            segment.setEnabled(true);
            segment.display("1234");
        } catch (IOException e) {
            Log.d(TAG,e.toString());
        }
    }

    @Override
    protected void onPause() {
        Log.d(TAG,"onPause()");
        super.onPause();

        try {
            if(segment != null)
            {
                segment.close();
            }
        } catch (IOException e) {
            Log.d(TAG,e.toString());
        }

    }

#7つのフルカラーLEDをコントロールする例

import文を追加する

MainActivity.java
import android.graphics.Color;
import com.google.android.things.contrib.driver.apa102.Apa102;
import java.io.IOException;

グローバル変数の定義

MainActivity.java
private Apa102 ledstrip = null;
MainActivity.java
    @Override
    protected void onResume() {
        Log.d(TAG,"onResume()");
        super.onResume();

        try {
            int[] rainbow = new int[7];

            ledstrip = RainbowHat.openLedStrip();
            ledstrip.setBrightness(1);

            for (int i = 0; i < rainbow.length; i++) {
                rainbow[i] = Color.HSVToColor(255, new float[]{i * 360.f / rainbow.length, 1.0f, 1.0f});
            }
            ledstrip.write(rainbow);
        } catch (IOException e) {
            Log.d(TAG,e.toString());
        }
    }

    @Override
    protected void onPause() {
        Log.d(TAG,"onPause()");
        super.onPause();

        try {
            if(ledstrip != null)
            {
                ledstrip.close();
            }
        } catch (IOException e) {
            Log.d(TAG,e.toString());
        }

    }

#タッチボタンのイベントを拾う

import文を追加する

MainActivity.java
import com.google.android.things.contrib.driver.button.Button;
import java.io.IOException;

グローバル変数の定義

MainActivity.java
private Button buttonA = null;
private Button buttonB = null;
private Button buttonC = null;
MainActivity.java
    @Override
    protected void onResume() {
        Log.d(TAG,"onResume()");
        super.onResume();

        try {
            buttonA = RainbowHat.openButton(RainbowHat.BUTTON_A);
            buttonA.setOnButtonEventListener(onEvent);

            buttonB = RainbowHat.openButton(RainbowHat.BUTTON_B);
            buttonB.setOnButtonEventListener(onEvent);

            buttonC = RainbowHat.openButton(RainbowHat.BUTTON_C);
            buttonC.setOnButtonEventListener(onEvent);


        } catch (IOException e) {
            Log.d(TAG,e.toString());
        }
    }

    @Override
    protected void onPause() {
        Log.d(TAG,"onPause()");
        super.onPause();

        try {
            if(buttonA != null) {
                buttonA.close();
            }

            if(buttonB != null) {
                buttonB.close();
            }

            if(buttonC != null) {
                buttonC.close();
            }
        } catch (IOException e) {
            Log.d(TAG,e.toString());
        }
    }

    Button.OnButtonEventListener onEvent = new Button.OnButtonEventListener() {
        @Override
        public void onButtonEvent(Button button, boolean pressed) {
            if(button == buttonA)
            {
                Log.d(TAG,"Button A");
            }

            if(button == buttonB)
            {
                Log.d(TAG,"Button B");
            }

            if(button == buttonC)
            {
                Log.d(TAG,"Button C");
            }
        }
    };

#温度、気圧の値をイベントで取得する

import文を追加する

MainActivity.java
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

import com.google.android.things.contrib.driver.bmx280.Bmx280SensorDriver;
import java.io.IOException;

グローバル変数の定義

MainActivity.java
private Bmx280SensorDriver mSensorDriver = null;
private SensorManager sensorManager = null;
MainActivity.java
    @Override protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG,"onCreate()");
        super.onCreate(savedInstanceState);

        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

        try {
            mSensorDriver = new Bmx280SensorDriver(RainbowHat.BUS_SENSOR);
            mSensorDriver.registerTemperatureSensor();
            mSensorDriver.registerPressureSensor();
        } catch (IOException e) {
            Log.d(TAG,e.toString());
        }
    }

    @Override
    protected void onResume() {
        Log.d(TAG,"onResume()");
        super.onResume();

        List<Sensor> lists = sensorManager.getDynamicSensorList(Sensor.TYPE_ALL);

        for(int i = 0 ; i< lists.size() ; i++)
        {
            Log.d(TAG,"" + lists.get(i).toString());
            sensorManager.registerListener(onEvent2, lists.get(i), SensorManager.SENSOR_DELAY_NORMAL);
        }
    }

    @Override
    protected void onPause() {
        Log.d(TAG,"onPause()");
        super.onPause();

        try {
            if(mSensorDriver != null) {
                mSensorDriver.unregisterTemperatureSensor();
                mSensorDriver.unregisterPressureSensor();
                mSensorDriver.close();
            }
            sensorManager.unregisterListener(onEvent2);
        } catch (IOException e) {
            Log.d(TAG,e.toString());
        }
    }

    SensorEventListener onEvent2 = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent sensorEvent) {
            if(sensorEvent.sensor.getType() == Sensor.TYPE_AMBIENT_TEMPERATURE) {
                Log.d(TAG, "TEMPERATURE=" + sensorEvent.values[0]);
            }
            else if(sensorEvent.sensor.getType() == Sensor.TYPE_PRESSURE)
            {
                Log.d(TAG,"PRESSURE="+ sensorEvent.values[0]);
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int i) {

        }
    };

#動かしてみて
温度を取得してみるとわかるのですが、室温となんか違うなあと思うと思います。
Rainbow HATのBMP280が設置されている場所をみると、Raspberry Pi3のCPUの上にあることがわかりますってことは・・・
温度はご参考までにって感じです。

搭載されているセンサーはBMP280なので、本来は、湿度も取れますが、ライブラリに実装されていないようです。

以上、ご参考までに

10
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?