iot

Dependencies:   MPU9250 mbed-os

Files at this revision

API Documentation at this revision

Comitter:
alonsopg
Date:
Tue Oct 17 20:55:04 2017 +0000
Parent:
2:02175845b24c
Commit message:
iot

Changed in this revision

ButtonService.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 02175845b24c -r 3157e61f2bfd ButtonService.h
--- a/ButtonService.h	Sun Oct 15 18:24:03 2017 +0000
+++ b/ButtonService.h	Tue Oct 17 20:55:04 2017 +0000
@@ -25,15 +25,44 @@
 
     ButtonService(BLE &_ble, bool buttonPressedInitial) :
         ble(_ble), buttonState(BUTTON_STATE_CHARACTERISTIC_UUID,
-                               (uint8_t []) {0,0},
+                               (uint8_t []) {
+        0,0
+    },
     GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) {
         GattCharacteristic *charTable[] = {&buttonState};
         GattService         buttonService(ButtonService::BUTTON_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
         ble.gattServer().addService(buttonService);
     }
 
-    void updateButtonState(uint8_t v[12]) {        
-        ble.gattServer().write(buttonState.getValueHandle(), (uint8_t *)v, sizeof(v));
+    void updateButtonState(float roll, float pitch, float yaw, float gx, float gy, float gz) {
+        uint8_t sensorValues[12] = {0};
+
+        uint16_t rollValue = (uint16_t)roll;
+        uint16_t pitchValue = (uint16_t)pitch;
+        uint16_t yawValue = (uint16_t)yaw;
+
+        uint16_t gxValue = (uint16_t)gx;
+        uint16_t gyValue = (uint16_t)gy;
+        uint16_t gzValue = (uint16_t)gz;
+        sensorValues[0]= rollValue & 0xff;
+        sensorValues[1]= (rollValue >> 8);
+
+        sensorValues[2]= pitchValue & 0xff;
+        sensorValues[3]= (pitchValue >> 8);
+
+        sensorValues[4]= yawValue & 0xff;
+        sensorValues[5]= (yawValue >> 8);
+
+        sensorValues[6]= gxValue & 0xff;
+        sensorValues[7]= (gxValue >> 8);
+
+        sensorValues[8]= gyValue & 0xff;
+        sensorValues[9]= (gyValue >> 8);
+
+        sensorValues[10]= gzValue & 0xff;
+        sensorValues[11]= (gzValue >> 8);
+
+        ble.gattServer().write(buttonState.getValueHandle(), (uint8_t *)sensorValues, sizeof(sensorValues));
         //ble.updateCharacteristicValue(buttonState.getValueHandle(), (uint8_t *)v ,sizeof(v));
     }
 
diff -r 02175845b24c -r 3157e61f2bfd main.cpp
--- a/main.cpp	Sun Oct 15 18:24:03 2017 +0000
+++ b/main.cpp	Tue Oct 17 20:55:04 2017 +0000
@@ -10,10 +10,9 @@
 MPU9250 mpu = MPU9250(p26, p27);
 
 // Configuration
-bool testMPUConnection = true;
-bool doSensorInitialization = false;
-bool printAccelerometer = false;
-bool printGyroscope = false;
+bool doSensorInitialization = true;
+bool printAccelerometer = true;
+bool printGyroscope = true;
 
 DigitalOut  led1(LED1);
 InterruptIn button(BUTTON1);
@@ -27,24 +26,8 @@
     IDLE
 };
 
-static uint8_t buttonState = IDLE;
-
 static ButtonService *buttonServicePtr;
 
-void buttonPressedCallback(void)
-{
-    /* Note that the buttonPressedCallback() executes in interrupt context, so it is safer to access
-     * BLE device API from the main thread. */
-    buttonState = PRESSED;
-}
-
-void buttonReleasedCallback(void)
-{
-    /* Note that the buttonReleasedCallback() executes in interrupt context, so it is safer to access
-     * BLE device API from the main thread. */
-    buttonState = RELEASED;
-}
-
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
     BLE::Instance().gap().startAdvertising();
@@ -106,9 +89,7 @@
 
     //Attach a function to be called by the Ticker, specifiying the interval in seconds.
     Ticker ticker;
-    ticker.attach(periodicFunctionCallback, 1);
-    button.fall(buttonPressedCallback);
-    button.rise(buttonReleasedCallback);
+    ticker.attach(periodicFunctionCallback, 1);    
 
     BLE &ble = BLE::Instance();
     ble.init(bleInitComplete);
@@ -119,13 +100,6 @@
         /* spin loop */
     }
 
-    if (testMPUConnection) {
-        uint8_t whoami = mpu.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250);  // Read WHO_AM_I register for MPU-9250
-        pc.printf("I AM 0x%x\n\r", whoami);
-        pc.printf("I SHOULD BE 0x71\n\r");
-        wait(1);
-    }
-
     if (doSensorInitialization) {
         // Initialise sensor board
         pc.printf("Initializing sensor\n\r");
@@ -136,53 +110,26 @@
 
     while(1) {
 
-        //pc.printf("Starting to stream data");
         if(printAccelerometer && printGyroscope) {
             mpu.readAccelData(accelerometer);
             float ax = accelerometer[0] * 2.0 / 32768.0;
             float ay = accelerometer[1] * 2.0 / 32768.0;
             float az = accelerometer[2] * 2.0 / 32768.0;
 
-            //pc.printf("Acelerometer information, AX, AY, AZ \n");
-            //pc.printf("(%f, %f, %f)\n", ax,ay,az);
-
             float roll = float(atan2(ay, az) * 180/3.1416f);
             float pitch = float(atan2(-ax, sqrt(ay*ay + az*az)) * 180/3.1416f);
             float yaw = atan(ax/-ay);
 
-            //pc.printf("Roll/Pitch/Yaw: (%f, %f, %f)\n", roll, pitch, yaw);
-
             mpu.readGyroData(gyroscope);
             float gx = gyroscope[0] * 250.0 / 32768.0;
             float gy = gyroscope[1] * 250.0 / 32768.0;
             float gz = gyroscope[2] * 250.0 / 32768.0;
-
-
-            pc.printf("(%f, %f, %f, %f, %f, %f)\n", roll, pitch, yaw, gx, gy, gz);
-
+                                                            
+            buttonServicePtr->updateButtonState(roll, pitch, yaw, gx, gy, gz);       
 
         }
-        if (buttonState != IDLE) {
-            //buttonServicePtr->updateButtonState(buttonState);
-            //char myword[] = { 'H', 'e', 'l', 'l', 'o', '\0' };          
-            uint8_t sensorValues[12] = {0};
-            sensorValues[0] = 12;
-            sensorValues[1] = 12;
-            sensorValues[2] = 12;
-            sensorValues[3] = 12;
-            sensorValues[4] = 12;
-            sensorValues[5] = 12;
-            sensorValues[6] = 12;
-            sensorValues[7] = 12;
-            sensorValues[8] = 12;
-            sensorValues[9] = 12;
-            sensorValues[10] = 12;
-            sensorValues[11] = 12;
-            buttonServicePtr->updateButtonState(sensorValues);
-            buttonState = IDLE;
-        }
         ble.waitForEvent();
 
-        //wait(0.3);
+        wait(0.3);
     }
 }
\ No newline at end of file