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_LIGHT) {
Log.i(TAG, "sensor.getName():" + sensor.getName());
if(sensor.getName().startsWith("FaBoISL29034")) {
Log.i(TAG, "Light sensor connected");
mSensorManager.registerListener(MainActivity.this, sensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
}
}
});
try {
mISL29034AmbientDriver = new ISL29034AmbientDriver(BoardDefaults.getI2CPort());
mISL29034AmbientDriver.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("FaBoISL29034")) {
Log.i(TAG, "Ambient event: " + event.values[0]);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}