MPU6050センサ Wallbot_BLE用 サンプル

Dependencies:   BLE_API mbed nRF51822

Revision:
5:807096fdd895
Parent:
4:3a25f1bdffc7
Child:
6:e640afab8288
--- a/main.cpp	Mon Jan 26 11:21:56 2015 +0000
+++ b/main.cpp	Fri Jan 30 16:42:24 2015 +0000
@@ -42,6 +42,14 @@
     0x1C,0x06,0x0C,0xAC,0x97,0x67
 };
 
+static const uint8_t MPU6050_Write_Characteristic_uuid[] =
+{
+    0x65, 0x60, 0xBE, 0x95,0xD9, 0x31, 
+    0x4F, 0x64,
+    0x95, 0x75,
+    0xC6, 0xDD, 0xB5, 0xAE, 0x7F, 0xA5
+};
+
 //const uint8_t MPU6050_Gyro_Characteristic_uuid[] = {
 //    0x65, 0x60, 0xBE, 0x95,0xD9, 0x31, 
 //    0x4F, 0x64,
@@ -50,6 +58,8 @@
 //};
 
 uint8_t accelPayload[sizeof(float)*7] = {0,};
+uint8_t writePayload[3] = {0};
+
 //uint8_t gyroPayload[sizeof(float)*3] = {0,0,0};
 //uint8_t tempPayload[sizeof(float)*1] = {0};
 //uint8_t batt = 100;     /* Battery level */
@@ -59,12 +69,17 @@
                                         accelPayload, (sizeof(float) * 7), (sizeof(float) * 7),
                                         GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
 
+GattCharacteristic  writeChar (MPU6050_Write_Characteristic_uuid,
+                                        writePayload, 3, 3,
+                                        GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
+
+// | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
 //GattCharacteristic  gyroChar (MPU6050_Gyro_Characteristic_uuid,
 //                                        gyroPayload, (sizeof(float) * 3), (sizeof(float) * 3),
 //                                        GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
 
 //GattCharacteristic *ControllerChars[] = { &accelChar, &gyroChar, };
-GattCharacteristic *ControllerChars[] = { &accelChar, };
+GattCharacteristic *ControllerChars[] = { &accelChar, &writeChar, };
 GattService         MPU6050Service(MPU6050_service_uuid, ControllerChars, sizeof(ControllerChars) / sizeof(GattCharacteristic *));
 
 void updateValue();
@@ -104,8 +119,67 @@
     //if (ble.updateConnectionParams(handle, &gap_conn_params) != BLE_ERROR_NONE) {
     //    DEBUG("failed to update connection paramter\r\n");
     //}
-    
-    updateValue();
+}
+
+void onDataWritten(const GattCharacteristicWriteCBParams *params){
+
+    if (params->charHandle == writeChar.getValueAttribute().getHandle()) {
+        uint16_t len = params->len;
+        
+        if (len == 3) {
+            uint8_t controller[3] = {0};
+
+            ble.readCharacteristicValue(writeChar.getValueAttribute().getHandle(), writePayload, &len);
+            memcpy( &controller, writePayload, sizeof(controller));
+            
+            /* mpu reset */
+            if(controller[0] == 1){
+                mpu.initialize();
+            }
+            
+            /*
+                MPU6050_ACCELERO_RANGE_2G    0
+                MPU6050_ACCELERO_RANGE_4G    1
+                MPU6050_ACCELERO_RANGE_8G    2
+            */
+            switch(controller[1]){
+                case 0:
+                    mpu.setAcceleroRange(0);
+                    break;
+                case 1:
+                    mpu.setAcceleroRange(1);
+                    break;
+                case 2:
+                    mpu.setAcceleroRange(2);
+                    break;
+                default:
+                    break;  
+            }
+            
+            /*
+            MPU6050_GYRO_RANGE_250          0
+            MPU6050_GYRO_RANGE_500          1
+            MPU6050_GYRO_RANGE_1000         2
+            MPU6050_GYRO_RANGE_2000         3 
+            */
+            switch(controller[2]){
+                case 0:
+                    mpu.setGyroRange(0);
+                    break;
+                case 1:
+                    mpu.setGyroRange(1);
+                    break;
+                case 2:
+                    mpu.setGyroRange(2);
+                    break;
+                case 3:
+                    mpu.setGyroRange(3);
+                    break;
+                default:
+                    break;
+            }
+        }
+    }
 }
 
 void timeoutCallback(void)
@@ -149,6 +223,7 @@
     ble.init();
     ble.onDisconnection(disconnectionCallback);
     ble.onConnection(onConnectionCallback);
+    ble.onDataWritten(onDataWritten);
     ble.onTimeout(timeoutCallback);
 
     /* setup device name */