From a080d8317fb895987ed498267517f92b5ae1ef73 Mon Sep 17 00:00:00 2001
From: Dominik Grzelak <dominik.grzelak@tu-dresden.de>
Date: Tue, 24 Jul 2018 10:58:43 +0200
Subject: [PATCH] orientation with quaternions can now be transmitted too

---
 .../inf/st/sensorsharing/MainActivity.java    | 22 +++++++++++++++++++
 mobile/src/main/res/layout/activity_main.xml  | 12 +++++++++-
 mobile/src/main/res/values/strings.xml        |  1 +
 3 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/mobile/src/main/java/de/tudresden/inf/st/sensorsharing/MainActivity.java b/mobile/src/main/java/de/tudresden/inf/st/sensorsharing/MainActivity.java
index 7cb74e1..6e03261 100644
--- a/mobile/src/main/java/de/tudresden/inf/st/sensorsharing/MainActivity.java
+++ b/mobile/src/main/java/de/tudresden/inf/st/sensorsharing/MainActivity.java
@@ -226,9 +226,11 @@ public class MainActivity extends AppCompatActivity implements
     private void setupSendUpdatesCheckbox() {
         final CheckBox sendUpdates = findViewById(R.id.checkBox_send_brightness);
         final CheckBox sendUpdatesRotation = findViewById(R.id.checkBox_send_rotation_ahrs);
+        final CheckBox sendUpdatesRotationQuat = findViewById(R.id.checkBox_send_rotation_quaternion);
 
         sendMqttUpdates.put(phoneTopics.mqtt_topic_brightness, sendUpdates.isChecked());
         sendMqttUpdates.put(phoneTopics.mqtt_topic_rotation_ahrs, sendUpdatesRotation.isChecked());
+        sendMqttUpdates.put(phoneTopics.mqtt_topic_rotation, sendUpdatesRotationQuat.isChecked());
 
         CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
             @Override
@@ -240,11 +242,15 @@ public class MainActivity extends AppCompatActivity implements
                     case R.id.checkBox_send_rotation_ahrs:
                         sendMqttUpdates.put(phoneTopics.mqtt_topic_rotation_ahrs, sendUpdatesRotation.isChecked());
                         break;
+                    case R.id.checkBox_send_rotation_quaternion:
+                        sendMqttUpdates.put(phoneTopics.mqtt_topic_rotation, sendUpdatesRotationQuat.isChecked());
+                        break;
                 }
             }
         };
         sendUpdates.setOnCheckedChangeListener(onCheckedChangeListener);
         sendUpdatesRotation.setOnCheckedChangeListener(onCheckedChangeListener);
+        sendUpdatesRotationQuat.setOnCheckedChangeListener(onCheckedChangeListener);
     }
 
     private void setName() {
@@ -485,8 +491,10 @@ public class MainActivity extends AppCompatActivity implements
      * Whether the data is really published depends on the checkboxes.
      */
     private void sendUpdateOfSmartphone() {
+
         ObjectMapper mapper = new ObjectMapper();
 
+        // Brightness data
         HashMap<String, Float> dataLight = new HashMap<>();
         dataLight.put("brightness", mLightValue);
         try {
@@ -496,6 +504,7 @@ public class MainActivity extends AppCompatActivity implements
             e.printStackTrace();
         }
 
+        // AHRS data
         filter.Update(gyroVectorValues[0], gyroVectorValues[1], gyroVectorValues[2],
                 accelVectorValues[0], accelVectorValues[1], accelVectorValues[2],
                 magVectorValues[0], magVectorValues[1], magVectorValues[2]);
@@ -514,6 +523,19 @@ public class MainActivity extends AppCompatActivity implements
         } catch (JsonProcessingException e) {
             e.printStackTrace();
         }
+
+        // Quaternion data
+        HashMap<String, Float> dataQuat = new HashMap<>();
+        dataQuat.put("x", mRotationVectorValues[0]);
+        dataQuat.put("y", mRotationVectorValues[1]);
+        dataQuat.put("z", mRotationVectorValues[2]);
+        //dataQuat.put("w", mRotationVectorValues[3]);
+        try {
+            String s = mapper.writeValueAsString(dataQuat);
+            sendUpdate(phoneTopics.mqtt_topic_rotation, s);
+        } catch (JsonProcessingException e) {
+            e.printStackTrace();
+        }
     }
 
     private void sendUpdate(String topic, String newValue) {
diff --git a/mobile/src/main/res/layout/activity_main.xml b/mobile/src/main/res/layout/activity_main.xml
index c1bdc74..e68e0a0 100644
--- a/mobile/src/main/res/layout/activity_main.xml
+++ b/mobile/src/main/res/layout/activity_main.xml
@@ -257,6 +257,16 @@
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toBottomOf="@+id/checkBox_send_brightness" />
 
+    <CheckBox
+        android:id="@+id/checkBox_send_rotation_quaternion"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginStart="8dp"
+        android:layout_marginTop="8dp"
+        android:text="@string/send_updates_rotation_quaternion"
+        app:layout_constraintStart_toStartOf="parent"
+        app:layout_constraintTop_toBottomOf="@+id/checkBox_send_rotation_ahrs" />
+
     <TextView
         android:id="@+id/label_wear_status"
         android:layout_width="wrap_content"
@@ -277,7 +287,7 @@
         android:textOff="@string/switch_light_on"
         android:textOn="@string/switch_light_off"
         app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintTop_toBottomOf="@+id/checkBox_send_rotation_ahrs" />
+        app:layout_constraintTop_toBottomOf="@+id/checkBox_send_rotation_quaternion" />
 
 </android.support.constraint.ConstraintLayout>
 </ScrollView>
diff --git a/mobile/src/main/res/values/strings.xml b/mobile/src/main/res/values/strings.xml
index c7474e2..14a7d53 100644
--- a/mobile/src/main/res/values/strings.xml
+++ b/mobile/src/main/res/values/strings.xml
@@ -16,4 +16,5 @@
     <string name="switch_on">Switch On</string>
     <string name="switch_light_on">Switch Light ON</string>
     <string name="switch_light_off">Switch Light OFF</string>
+    <string name="send_updates_rotation_quaternion">Send Updates Rotation (Quaternion)</string>
 </resources>
-- 
GitLab