package io.fabo.test;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import java.io.IOException;
import io.fabo.driver.Adx345AccelerometerDriver;
import io.fabo.driver.ISL29034AmbientDriver;
import io.fabo.driver.S11059ColorDriver;
public class MainActivity extends Activity implements SensorEventListener {
private S11059ColorDriver mS11059ColorDriver;
private SensorManager mSensorManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensorManager.registerDynamicSensorCallback(new SensorManager.DynamicSensorCallback() {
@Override
public void onDynamicSensorConnected(Sensor sensor) {
Log.i(TAG, "Connected");
Log.i(TAG, "sensor.getType():" + sensor.getType());
Log.i(TAG, "sensor.getName():" + sensor.getName());
if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
Log.i(TAG, "sensor.getName():" + sensor.getName());
if(sensor.getName().startsWith("FaBoAdx345")) {
Log.i(TAG, "3Axis sensor connected");
mSensorManager.registerListener(MainActivity.this, sensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
}
}
});
try {
mAdx345AccelerometerDriver = new Adx345AccelerometerDriver(BoardDefaults.getI2CPort());
mAdx345AccelerometerDriver.register();
Log.i(TAG, "Accelerometer driver registered");
} catch (IOException e) {
Log.e(TAG, "Error initializing accelerometer driver: ", e);
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (mS11059ColorDriver != null) {
mSensorManager.unregisterListener(this);
mS11059ColorDriver.unregister();
try {
mS11059ColorDriver.close();
} catch (IOException e) {
Log.e(TAG, "Error closing accelerometer driver: ", e);
} finally {
mS11059ColorDriver = null;
}
}
}
@Override
public void onSensorChanged(SensorEvent event) {
Log.i(TAG, "event.sensor.getName()=" + event.sensor.getName());
if(event.sensor.getName().startsWith("FaBoAdx345")) {
Log.i(TAG, "Accelerometer event: " +
event.values[0] + ", " + event.values[1] + ", " + event.values[2]);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}