|
16 | 16 | * Created by thomas on 07.11.14. |
17 | 17 | */ |
18 | 18 | public class OscDispatcher implements DataDispatcher { |
19 | | - private List<SensorConfiguration> sensorConfigurations = new ArrayList<SensorConfiguration>(); |
20 | | - private OscCommunication communication; |
21 | | - private float[] gravity; |
22 | | - private float[] geomagnetic; |
23 | | - private SensorManager sensorManager; |
| 19 | + private List<SensorConfiguration> sensorConfigurations = new ArrayList<SensorConfiguration>(); |
| 20 | + private OscCommunication communication; |
| 21 | + private float[] gravity; |
| 22 | + private float[] geomagnetic; |
| 23 | + private SensorManager sensorManager; |
24 | 24 |
|
25 | | - public OscDispatcher() { |
26 | | - communication = new OscCommunication("OSC dispatcher thread", Thread.MIN_PRIORITY); |
27 | | - communication.start(); |
28 | | - } |
| 25 | + public OscDispatcher() { |
| 26 | + communication = new OscCommunication("OSC dispatcher thread", Thread.MIN_PRIORITY); |
| 27 | + communication.start(); |
| 28 | + } |
29 | 29 |
|
30 | | - public void addSensorConfiguration(SensorConfiguration sensorConfiguration) { |
31 | | - this.sensorConfigurations.add(sensorConfiguration); |
32 | | - } |
| 30 | + public void addSensorConfiguration(SensorConfiguration sensorConfiguration) { |
| 31 | + this.sensorConfigurations.add(sensorConfiguration); |
| 32 | + } |
33 | 33 |
|
34 | | - @Override |
35 | | - public void dispatch(Measurement sensorData) { |
36 | | - for (SensorConfiguration sensorConfiguration : this.sensorConfigurations) { |
37 | | - if (sensorConfiguration.getSensorType() == sensorData.getSensorType()) { |
38 | | - trySend(sensorConfiguration, sensorData.getValues()); |
39 | | - } |
40 | | - if (sensorConfiguration.getSensorType() == Parameters.FAKE_ORIENTATION){ |
41 | | - // Fake orientation |
42 | | - if (sensorData.getSensorType() != Sensor.TYPE_ACCELEROMETER && sensorData.getSensorType() != Sensor.TYPE_MAGNETIC_FIELD){ |
43 | | - continue; |
44 | | - } |
45 | | - if (sensorData.getSensorType() == Sensor.TYPE_ACCELEROMETER){ |
46 | | - this.gravity = sensorData.getValues(); |
47 | | - } |
| 34 | + @Override |
| 35 | + public void dispatch(Measurement sensorData) { |
| 36 | + for (SensorConfiguration sensorConfiguration : this.sensorConfigurations) { |
| 37 | + if (sensorConfiguration.getSensorType() == sensorData.getSensorType()) { |
| 38 | + if (sensorData.getValues() != null) { |
| 39 | + trySend(sensorConfiguration, sensorData.getValues()); |
| 40 | + } else { |
| 41 | + trySend(sensorConfiguration, sensorData.getStringValue()); |
| 42 | + } |
| 43 | + } |
| 44 | + if (sensorConfiguration.getSensorType() == Parameters.FAKE_ORIENTATION || sensorConfiguration.getSensorType() == Parameters.INCLINATION) { |
| 45 | + // Fake orientation |
| 46 | + if (sensorData.getSensorType() != Sensor.TYPE_ACCELEROMETER && sensorData.getSensorType() != Sensor.TYPE_MAGNETIC_FIELD) { |
| 47 | + continue; |
| 48 | + } |
| 49 | + if (sensorData.getSensorType() == Sensor.TYPE_ACCELEROMETER) { |
| 50 | + this.gravity = sensorData.getValues(); |
| 51 | + } |
48 | 52 |
|
49 | | - if (sensorData.getSensorType() == Sensor.TYPE_MAGNETIC_FIELD){ |
50 | | - this.geomagnetic = sensorData.getValues(); |
51 | | - } |
52 | | - if (this.gravity != null && this.geomagnetic != null){ |
53 | | - float rotation[] = new float[9]; |
54 | | - float inclination[] = new float[9]; |
| 53 | + if (sensorData.getSensorType() == Sensor.TYPE_MAGNETIC_FIELD) { |
| 54 | + this.geomagnetic = sensorData.getValues(); |
| 55 | + } |
| 56 | + if (this.gravity != null && this.geomagnetic != null) { |
| 57 | + float rotationMatrix[] = new float[9]; |
| 58 | + float inclinationMatrix[] = new float[9]; |
55 | 59 |
|
56 | | - boolean success = this.sensorManager.getRotationMatrix(rotation, inclination, this.gravity, this.geomagnetic); |
57 | | - if (success) { |
58 | | - float orientation[] = new float[3]; |
59 | | - this.sensorManager.getOrientation(rotation, orientation); |
60 | | - this.trySend(sensorConfiguration, orientation); |
61 | | - } |
62 | | - } |
63 | | - } |
64 | | - } |
65 | | - } |
| 60 | + boolean success = this.sensorManager.getRotationMatrix(rotationMatrix, inclinationMatrix, this.gravity, this.geomagnetic); |
| 61 | + if (success) { |
| 62 | + if (sensorConfiguration.getSensorType() == Parameters.FAKE_ORIENTATION) { |
| 63 | + float orientation[] = new float[3]; |
| 64 | + this.sensorManager.getOrientation(rotationMatrix, orientation); |
| 65 | + this.trySend(sensorConfiguration, orientation); |
| 66 | + } |
| 67 | + if (sensorConfiguration.getSensorType() == Parameters.INCLINATION) { |
| 68 | + float inclination[] = new float[1]; |
| 69 | + inclination[0] = this.sensorManager.getInclination(inclinationMatrix); |
| 70 | + this.trySend(sensorConfiguration, inclination); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
66 | 77 |
|
67 | | - private void trySend(SensorConfiguration sensorConfiguration, float[] values) { |
68 | | - if (!sensorConfiguration.sendingNeeded(values)) { |
69 | | - return; |
70 | | - } |
71 | | - Message message = new Message(); |
72 | | - Bundle data = new Bundle(); |
73 | | - data.putFloatArray(Bundling.VALUE, values); |
74 | | - data.putString(Bundling.OSC_PARAMETER, sensorConfiguration.getOscParam()); |
75 | | - message.setData(data); |
76 | | - OscHandler handler = communication.getOscHandler(); |
77 | | - handler.sendMessage(message); |
78 | | - } |
| 78 | + private void trySend(SensorConfiguration sensorConfiguration, float[] values) { |
| 79 | + if (!sensorConfiguration.sendingNeeded(values)) { |
| 80 | + return; |
| 81 | + } |
| 82 | + Message message = new Message(); |
| 83 | + Bundle data = new Bundle(); |
| 84 | + data.putFloatArray(Bundling.VALUES, values); |
| 85 | + data.putString(Bundling.OSC_PARAMETER, sensorConfiguration.getOscParam()); |
| 86 | + message.setData(data); |
| 87 | + OscHandler handler = communication.getOscHandler(); |
| 88 | + handler.sendMessage(message); |
| 89 | + } |
79 | 90 |
|
80 | | - public void setSensorManager(SensorManager sensorManager) { |
81 | | - this.sensorManager = sensorManager; |
82 | | - } |
| 91 | + private void trySend(SensorConfiguration sensorConfiguration, String value) { |
| 92 | + if (!sensorConfiguration.sendingNeeded(new float[0])) { |
| 93 | + return; |
| 94 | + } |
| 95 | + Message message = new Message(); |
| 96 | + Bundle data = new Bundle(); |
| 97 | + data.putString(Bundling.STRING_VALUE, value); |
| 98 | + data.putString(Bundling.OSC_PARAMETER, sensorConfiguration.getOscParam()); |
| 99 | + message.setData(data); |
| 100 | + OscHandler handler = communication.getOscHandler(); |
| 101 | + handler.sendMessage(message); |
| 102 | + } |
| 103 | + |
| 104 | + public void setSensorManager(SensorManager sensorManager) { |
| 105 | + this.sensorManager = sensorManager; |
| 106 | + } |
83 | 107 | } |
0 commit comments