Browse Source

Added Accelerometer as Runnable Class and ThreadDemo as example

lm
Leon Market 1 year ago
parent
commit
9e5b669986

+ 124
- 0
app/src/main/java/com/example/ueberwachungssystem/Accelerometer.java View File

package com.example.ueberwachungssystem;



import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;


public class Accelerometer implements Runnable, SensorEventListener {

Logger logger;
private volatile boolean running = false;
public SensorManager sensorManager;
private int sensorType = Sensor.TYPE_ACCELEROMETER;
private Sensor accelerometer;
private Context context;
private Thread thread;
private String threadName = "Accelerometer Thread";
double[] gravity = new double[3];
double[] linear_acceleration = new double[3];
private TextView textViewWorkerThread;

// In constructor pass Context from MainActivity in Accelerometer class
public Accelerometer(Context context, TextView textViewWorkerThread){
this.context = context;
this.textViewWorkerThread = textViewWorkerThread;
}
// Passing Activity's instance as argument on worker thread
AppCompatActivity activity;
public Accelerometer(AppCompatActivity activity){
this.activity = activity;
}

private void print (final String s){
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
textViewWorkerThread.setText(s);
}
});
}

@Override
public void run() {
while (running) {
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
if (sensorManager.getSensorList(sensorType).size() == 0) {
logger.log("Es gibt den gewünschten Sensor nicht");
accelerometer = null;
} else {
accelerometer = sensorManager.getSensorList(sensorType).get(0);
}
}
}
void start(){
logger.log("Starting" + threadName);
running = true;
thread = new Thread(this);
thread.setName(threadName);
thread.start();
logger.log("alright, " + threadName + " started!");

// register the sensor before using //
sensorManager.registerListener((SensorEventListener) context, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
logger.log("Accelerometer, du bist dran!");
}

void stop() {
// unregister from the sensor to stop using it //
sensorManager.unregisterListener((SensorEventListener) context, accelerometer);
logger.log("Das reicht Accelerometer, komm zurück!");
if (!running){
logger.log(threadName + " not running.");
} else {
logger.log("Stopping " + threadName);
running = false;
while (true) {
try {
thread.join();
logger.log("..." + threadName + " stopped");
break;
} catch (InterruptedException e){
e.printStackTrace();
}
}
}
}

@Override
public void onSensorChanged(SensorEvent event) {
// Highpass filter to filter out gravity influence
final double alpha = 0.8;

gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];
linear_acceleration[0] = event.values[0] - gravity[0];

linear_acceleration[1] = event.values[1] - gravity[1];
linear_acceleration[2] = event.values[2] - gravity[2];

String sb = "t=" + event.timestamp +
"\nx=" + event.values[0] + // Wert liegend: x = 0.04
"\ny=" + event.values[1] + // Wert liegend: y = 0.44
"\nz=" + event.values[2] + // Wert liegend: z = 9.90 = Erdbeschleunigung --> Wenn Ausrichtung unbekannt ist, müsste kalibrierung bei Start der Bewegungsüberwachung vorgenommen werden
"\nlin_x=" + linear_acceleration[0] +
"\nlin_x=" + linear_acceleration[1] +
"\nlin_x=" + linear_acceleration[2];

logger.clearLog();
logger.log(sb);
}

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

}
}

+ 70
- 0
app/src/main/java/com/example/ueberwachungssystem/ThreadDemo.java View File

package com.example.ueberwachungssystem;

import android.content.Context;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class ThreadDemo implements Runnable {
private volatile boolean running = false;
private Thread thread;
private String threadname = "testThread";
Logger logger;

// Passing Activity's instance as argument on worker thread
AppCompatActivity activity;
public ThreadDemo(AppCompatActivity activity){
this.activity = activity;
}

//Method print which delegates access on MainActivity to runOnUiThread
private void print(final String s) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
//textViewWorkerThread.setText(s);
}
});
}

@Override
public void run() {
int i = 0;
while (running) {
i++;
print(String.valueOf(i));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//print(activity.getString(R.string.workerThread) + " endet mit " + i);
}

void start() {
logger.log("Starting " + threadname + "...");
running = true;
thread = new Thread(this);
thread.setName(threadname);
thread.start();
logger.log("..." + threadname + " started");
}
void stop() {
if (!running) {
logger.log(threadname + " not running");
} else {
logger.log("Stopping " + threadname + "...");
running = false;
while(true){
try {
thread.join();
logger.log("... " + threadname + " stopped");
break;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}

Loading…
Cancel
Save