Skip to content

Commit

Permalink
tuning of sensor sensitivity UI and preference setting
Browse files Browse the repository at this point in the history
  • Loading branch information
n8fr8 committed Jan 20, 2018
1 parent 51186eb commit 581ae37
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 88 deletions.
19 changes: 15 additions & 4 deletions src/main/java/org/havenapp/main/sensors/AccelerometerMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.FloatMath;
import android.util.Log;

import org.havenapp.main.PreferenceManager;
Expand Down Expand Up @@ -57,13 +58,16 @@ public class AccelerometerMonitor implements SensorEventListener {
*/
private int shakeThreshold = -1;

private float mAccelCurrent = SensorManager.GRAVITY_EARTH;
private float mAccelLast = SensorManager.GRAVITY_EARTH;
private float mAccel = 0.00f;
/**
* Text showing accelerometer values
*/
private int maxAlertPeriod = 30;
private int remainingAlertPeriod = 0;
private boolean alert = false;
private final static int CHECK_INTERVAL = 1000;
private final static int CHECK_INTERVAL = 100;

public AccelerometerMonitor(Context context) {
prefs = new PreferenceManager(context);
Expand Down Expand Up @@ -116,12 +120,19 @@ public void onSensorChanged(SensorEvent event) {

if (last_accel_values != null) {

/**
float speed = Math.abs(
accel_values[0] + accel_values[1] + accel_values[2] -
last_accel_values[0] + last_accel_values[1] + last_accel_values[2])
/ diffTime * 1000;

if (speed > shakeThreshold) {
**/
mAccelLast = mAccelCurrent;
mAccelCurrent =(float)Math.sqrt(accel_values[0]* accel_values[0] + accel_values[1]*accel_values[1]
+ accel_values[2]*accel_values[2]);
float delta = mAccelCurrent - mAccelLast;
mAccel = mAccel * 0.9f + delta;

if (mAccel > shakeThreshold) {
/*
* Send Alert
*/
Expand All @@ -131,7 +142,7 @@ public void onSensorChanged(SensorEvent event) {

Message message = new Message();
message.what = EventTrigger.ACCELEROMETER;
message.getData().putString("path",speed+"");
message.getData().putString("path",mAccel+"");

try {
if (serviceMessenger != null) {
Expand Down
76 changes: 35 additions & 41 deletions src/main/java/org/havenapp/main/ui/AccelConfigureActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Message;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
Expand All @@ -21,6 +23,7 @@

import org.havenapp.main.PreferenceManager;
import org.havenapp.main.R;
import org.havenapp.main.model.EventTrigger;

import java.util.LinkedList;

Expand Down Expand Up @@ -55,24 +58,25 @@ public class AccelConfigureActivity extends AppCompatActivity implements SensorE
private float last_accel_values[];


/**
* Shake threshold
*/
private int shakeThreshold = -1;
private float mAccelCurrent = SensorManager.GRAVITY_EARTH;
private float mAccelLast = SensorManager.GRAVITY_EARTH;
private float mAccel = 0.00f;


/**
* Text showing accelerometer values
*/
private int maxAlertPeriod = 30;
private int remainingAlertPeriod = 0;
private boolean alert = false;
private final static int CHECK_INTERVAL = 1000;
private final static int CHECK_INTERVAL = 100;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_accel_configure);
mPrefManager = new PreferenceManager(this.getApplicationContext());

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Expand All @@ -87,16 +91,17 @@ protected void onCreate(Bundle savedInstanceState) {

mNumberTrigger.setMinValue(0);
mNumberTrigger.setMaxValue(MAX_SLIDER_VALUE);
mNumberTrigger.setListener(new OnValueChangeListener() {
@Override
public void onValueChanged(int oldValue, int newValue) {
mWaveform.setThreshold(newValue);
mPrefManager.setAccelerometerSensitivity(newValue+"");

}
if (!mPrefManager.getAccelerometerSensitivity().equals(PreferenceManager.HIGH))
mNumberTrigger.setValue(Integer.parseInt(mPrefManager.getAccelerometerSensitivity()));
else
mNumberTrigger.setValue(50);

mNumberTrigger.setListener((oldValue, newValue) -> {
mWaveform.setThreshold(newValue);
mPrefManager.setAccelerometerSensitivity(newValue+"");
});

mPrefManager = new PreferenceManager(this.getApplicationContext());



Expand Down Expand Up @@ -216,42 +221,31 @@ public void onSensorChanged(SensorEvent event) {

if (last_accel_values != null) {

int speed = (int)(Math.abs(
accel_values[0] + accel_values[1] + accel_values[2] -
last_accel_values[0] + last_accel_values[1] + last_accel_values[2])
/ diffTime * 1000);
mAccelLast = mAccelCurrent;
mAccelCurrent =(float)Math.sqrt(accel_values[0]* accel_values[0] + accel_values[1]*accel_values[1]
+ accel_values[2]*accel_values[2]);
float delta = mAccelCurrent - mAccelLast;
mAccel = (mAccel * 0.9f + delta);

if (speed > shakeThreshold) {
/*
* Send Alert
*/

alert = true;
remainingAlertPeriod = maxAlertPeriod;

double averageDB = 0.0;
if (speed != 0) {
averageDB = 20 * Math.log10(Math.abs(speed));
}

if (averageDB > maxAmp) {
maxAmp = averageDB + 5d; //add 5db buffer
mNumberTrigger.setValue((int) maxAmp);
mNumberTrigger.invalidate();
}
double averageDB = 0.0;
if (mAccel != 0) {
averageDB = 20 * Math.log10(Math.abs(mAccel));
}

mWaveAmpList.addFirst(speed);
if (averageDB > maxAmp) {
maxAmp = averageDB + 5d; //add 5db buffer
}

if (mWaveAmpList.size() > mWaveform.width / mWaveform.barGap + 2) {
mWaveAmpList.removeLast();
}
mWaveAmpList.addFirst((int)mAccel);

mWaveform.refresh();
mTextLevel.setText(getString(R.string.current_accel_base) + ' ' + speed);
if (mWaveAmpList.size() > mWaveform.width / mWaveform.barGap + 2) {
mWaveAmpList.removeLast();
}

mWaveform.refresh();
mTextLevel.setText(getString(R.string.current_accel_base) + " " + (int)mAccel);


}
}
last_accel_values = accel_values.clone();
}
Expand Down
37 changes: 16 additions & 21 deletions src/main/java/org/havenapp/main/ui/CameraConfigureActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,20 @@
import org.havenapp.main.PreferenceManager;
import org.havenapp.main.R;

import me.angrybyte.numberpicker.listener.OnValueChangeListener;
import me.angrybyte.numberpicker.view.ActualNumberPicker;


public class CameraConfigureActivity extends AppCompatActivity {

private PreferenceManager preferences = null;
private PreferenceManager mPrefManager = null;

private boolean mIsMonitoring = false;
private boolean mIsInitializedLayout = false;

private CameraFragment mFragment;
private ActualNumberPicker mNumberTrigger;


@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -51,7 +56,7 @@ protected void onCreate(Bundle savedInstanceState) {
}

private void initLayout() {
preferences = new PreferenceManager(getApplicationContext());
mPrefManager = new PreferenceManager(getApplicationContext());
setContentView(R.layout.activity_camera_configure);

Toolbar toolbar = findViewById(R.id.toolbar);
Expand All @@ -69,36 +74,26 @@ public void onClick(View v) {
}
});

SeekBar sBar = ((SeekBar) findViewById(R.id.seekCameraSensitivity));
sBar.setProgress(preferences.getCameraSensitivity());
sBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
mFragment.setMotionSensitivity(i);
preferences.setCameraSensitivity(i);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}
mNumberTrigger = findViewById(R.id.number_trigger_level);
mNumberTrigger.setValue(mPrefManager.getCameraSensitivity());

mNumberTrigger.setListener(new OnValueChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {

public void onValueChanged(int oldValue, int newValue) {
mFragment.setMotionSensitivity(newValue);
mPrefManager.setCameraSensitivity(newValue);
}
});

mIsInitializedLayout = true;
}

private void switchCamera() {

String camera = preferences.getCamera();
String camera = mPrefManager.getCamera();
if (camera.equals(PreferenceManager.FRONT))
preferences.setCamera(PreferenceManager.BACK);
mPrefManager.setCamera(PreferenceManager.BACK);
else if (camera.equals(PreferenceManager.BACK))
preferences.setCamera(PreferenceManager.FRONT);
mPrefManager.setCamera(PreferenceManager.FRONT);

((CameraFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_camera)).resetCamera();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class MicrophoneConfigureActivity extends AppCompatActivity implements Mi
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_microphone_configure);
mPrefManager = new PreferenceManager(this.getApplicationContext());

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Expand All @@ -52,19 +53,20 @@ protected void onCreate(Bundle savedInstanceState) {
mTextLevel = findViewById(R.id.text_display_level);
mNumberTrigger = findViewById(R.id.number_trigger_level);
mWaveform = findViewById(R.id.simplewaveform);
mWaveform.setMaxVal(MAX_SLIDER_VALUE);
mWaveform.setMaxVal(100);

mNumberTrigger.setMinValue(0);
mNumberTrigger.setMaxValue(MAX_SLIDER_VALUE);
mNumberTrigger.setListener(new OnValueChangeListener() {
@Override
public void onValueChanged(int oldValue, int newValue) {
mWaveform.setThreshold(newValue);
mPrefManager.setMicrophoneSensitivity(newValue+"");
}
if (!mPrefManager.getMicrophoneSensitivity().equals(PreferenceManager.MEDIUM))
mNumberTrigger.setValue(Integer.parseInt(mPrefManager.getMicrophoneSensitivity()));
else
mNumberTrigger.setValue(60);

mNumberTrigger.setListener((oldValue, newValue) -> {
mWaveform.setThreshold(newValue);
mPrefManager.setMicrophoneSensitivity(newValue+"");
});

mPrefManager = new PreferenceManager(this.getApplicationContext());



Expand Down Expand Up @@ -95,6 +97,8 @@ private void initWave ()
//if show bars?
mWaveform.showBar = true;

mWaveform.setMaxVal(100);

//define how to show peaks outline
mWaveform.modePeak = SimpleWaveform.MODE_PEAK_ORIGIN;
//if show peaks outline?
Expand Down Expand Up @@ -225,7 +229,7 @@ public void onSignalReceived(short[] signal) {
mNumberTrigger.invalidate();
}

int perc = (int)((averageDB/160d)*100d);
int perc = (int)((averageDB/120d)*100d)-10;
mWaveAmpList.addFirst(perc);

if (mWaveAmpList.size() > mWaveform.width / mWaveform.barGap + 2) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/res/layout/activity_accel_configure.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:layout_marginTop="3dp"
android:layout_margin="6dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:background="#FFFFFF"
app:bar_color="@android:color/darker_gray"
app:bar_width="1dp"
Expand Down
32 changes: 22 additions & 10 deletions src/main/res/layout/activity_camera_configure.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,37 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
android:padding="0dp">

<ImageView
android:id="@+id/btnCameraSwitch"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_margin="20dp"
android:layout_margin="0dp"
android:foreground="?attr/selectableItemBackground"
app:srcCompat="@drawable/ic_camera_front_white" />

<SeekBar
android:layout_width="wrap_content"
<me.angrybyte.numberpicker.view.ActualNumberPicker
android:id="@+id/number_trigger_level"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_height="36dp"
android:layout_marginBottom="24dp"
android:id="@+id/seekCameraSensitivity"
android:max="100000"
android:min="2000"
/>
android:layout_gravity="center_horizontal"
android:layout_margin="6dp"
android:background="#FFFFFF"
app:bar_color="@android:color/darker_gray"
app:bar_width="1dp"
app:draw_over_controls="true"
app:max_value="60000"
app:value="10000"
app:min_value="0"
app:show_bars="true"
app:show_controls="false"
app:show_fast_controls="false"
app:show_text="true"
app:text_color="@android:color/darker_gray"
app:text_size="16sp" />


</LinearLayout>
</LinearLayout>
Expand Down
Loading

0 comments on commit 581ae37

Please sign in to comment.