summaryrefslogtreecommitdiffstats
path: root/tests/Camera2Tests/SmartCamera/SimpleCamera/src/androidx/media/filterfw/samples/simplecamera/MotionSensorWTime.java
blob: 64f3ef37758692fd8fa3354c383fa5fff0d9a6a7 (plain)
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
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// Make values from a motion sensor (e.g., accelerometer) available as filter outputs.

package androidx.media.filterfw.samples.simplecamera;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;

import androidx.media.filterfw.Filter;
import androidx.media.filterfw.FrameType;
import androidx.media.filterfw.FrameValue;
import androidx.media.filterfw.FrameValues;
import androidx.media.filterfw.MffContext;
import androidx.media.filterfw.OutputPort;
import androidx.media.filterfw.Signature;

public final class MotionSensorWTime extends Filter implements SensorEventListener {

    private SensorManager mSensorManager = null;
    private Sensor mSensor = null;

    private float[] mValues = new float[3];
    private float[][] mTemp = new float[3][3];
    private float[] mAvgValues = new float[3];
    private int mCounter = 0;

    public MotionSensorWTime(MffContext context, String name) {
        super(context, name);
    }

    @Override
    public Signature getSignature() {
        return new Signature()
            .addOutputPort("values", Signature.PORT_REQUIRED, FrameType.array(float.class))
            .addOutputPort("timestamp", Signature.PORT_OPTIONAL, FrameType.single(long.class))
            .disallowOtherPorts();
    }

    @Override
    protected void onPrepare() {
        mSensorManager = (SensorManager)getContext().getApplicationContext()
                            .getSystemService(Context.SENSOR_SERVICE);
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
        // TODO: currently, the type of sensor is hardcoded. Should be able to set the sensor
        //  type as filter input!
        mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_UI);
    }

    @Override
    protected void onTearDown() {
        mSensorManager.unregisterListener(this);
    }

    @Override
    public final void onAccuracyChanged(Sensor sensor, int accuracy) {
        // (Do we need to do something when sensor accuracy changes?)
    }

    @Override
    public final void onSensorChanged(SensorEvent event) {
        synchronized(mValues) {
            mValues[0] = event.values[0];
            mValues[1] = event.values[1];
            mValues[2] = event.values[2];
        }
    }

    @Override
    protected void onProcess() {
        OutputPort outPort = getConnectedOutputPort("values");
        FrameValues outFrame = outPort.fetchAvailableFrame(null).asFrameValues();
        synchronized(mValues) {
            if (mCounter < 3 && mCounter >= 0) {
                mTemp[0][mCounter] = mValues[0];
                mTemp[1][mCounter] = mValues[1];
                mTemp[2][mCounter] = mValues[2];
            }

            mCounter = (mCounter + 1) % 3;

            mAvgValues[0] = (mTemp[0][0] + mTemp[0][1] + mTemp[0][2]) / 3;
            mAvgValues[1] = (mTemp[1][0] + mTemp[1][1] + mTemp[1][2]) / 3;
            mAvgValues[2] = (mTemp[2][0] + mTemp[2][1] + mTemp[2][2]) / 3;
            outFrame.setValues(mAvgValues);
        }
        outFrame.setTimestamp(System.currentTimeMillis() * 1000000L);
        outPort.pushFrame(outFrame);

        OutputPort timeOutPort = getConnectedOutputPort("timestamp");
        if (timeOutPort != null) {
            long timestamp = System.nanoTime();
            Log.v("MotionSensor", "Timestamp is: " + timestamp);
            FrameValue timeStampFrame = timeOutPort.fetchAvailableFrame(null).asFrameValue();
            timeStampFrame.setValue(timestamp);
            timeOutPort.pushFrame(timeStampFrame);
        }
    }
}