サイコン用プログラム BLE通信送信確認

Dependencies:   mbed BLE_API nRF51822

Revision:
4:3a25f1bdffc7
Parent:
3:ca2cdabf5977
Child:
5:807096fdd895
--- a/main.cpp	Thu Jan 15 12:23:50 2015 +0000
+++ b/main.cpp	Mon Jan 26 11:21:56 2015 +0000
@@ -23,13 +23,13 @@
 static volatile bool  triggerSensorPolling = false;
 
 //9FDF3283-9049-CF8D-5C4D-98E7E2002731
-const uint8_t MPU6050_adv_service_uuid[] = {
-    0x9F,0xDF,0x32,0x83,
-    0x90,0x49,
-    0xCF,0x8D,
-    0x5C,0x4D,    
-    0x98,0xE7,0xE2,0x00,0x27,0x31
-};
+//const uint8_t MPU6050_adv_service_uuid[] = {
+//    0x9F,0xDF,0x32,0x83,
+//    0x90,0x49,
+//    0xCF,0x8D,
+//    0x5C,0x4D,    
+//    0x98,0xE7,0xE2,0x00,0x27,0x31
+//};
 
 const uint8_t MPU6050_service_uuid[] = {
     0x31,0x27,0x00,0xE2,0xE7,0x98,0x4D,0x5C,0x8D,0xCF,0x49,0x90,0x83,0x32,0xDF,0x9F
@@ -42,52 +42,30 @@
     0x1C,0x06,0x0C,0xAC,0x97,0x67
 };
 
-const uint8_t MPU6050_Gyro_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,
+//    0x95, 0x75,
+//    0xC6, 0xDD, 0xB5, 0xAE, 0x7F, 0xA5
+//};
 
-uint8_t accelPayload[sizeof(float)*3] = {0,0,0};
-uint8_t gyroPayload[sizeof(float)*3] = {0,0,0};
-uint8_t tempPayload[sizeof(float)*1] = {0};
-uint8_t batt = 100;     /* Battery level */
-uint8_t read_batt = 0;  /* Variable to hold battery level reads */
+uint8_t accelPayload[sizeof(float)*7] = {0,};
+//uint8_t gyroPayload[sizeof(float)*3] = {0,0,0};
+//uint8_t tempPayload[sizeof(float)*1] = {0};
+//uint8_t batt = 100;     /* Battery level */
+//uint8_t read_batt = 0;  /* Variable to hold battery level reads */
 
 GattCharacteristic  accelChar (MPU6050_Accel_Characteristic_uuid,
-                                        accelPayload, (sizeof(float) * 3), (sizeof(float) * 3),
-                                        GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
-
-GattCharacteristic  gyroChar (MPU6050_Gyro_Characteristic_uuid,
-                                        gyroPayload, (sizeof(float) * 3), (sizeof(float) * 3),
+                                        accelPayload, (sizeof(float) * 7), (sizeof(float) * 7),
                                         GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
 
-GattCharacteristic *ControllerChars[] = { &accelChar, &gyroChar, };
-GattService         MPU6050Service(MPU6050_service_uuid, ControllerChars, sizeof(ControllerChars) / sizeof(GattCharacteristic *));
-
-
-GattCharacteristic  tempChar (GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR,
-                                        tempPayload, (sizeof(float) * 1), (sizeof(float) * 1),
-                                        GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
-
-GattCharacteristic *htmChars[] = { &tempChar, };
-GattService        htmService(GattService::UUID_HEALTH_THERMOMETER_SERVICE, htmChars, 
-                                sizeof(htmChars) / sizeof(GattCharacteristic *));
+//GattCharacteristic  gyroChar (MPU6050_Gyro_Characteristic_uuid,
+//                                        gyroPayload, (sizeof(float) * 3), (sizeof(float) * 3),
+//                                        GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
 
-/* Battery Level Service */
-
-GattCharacteristic battLevel ( GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, 
-                                 (uint8_t *)batt, 1, 1,
-                                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
-
-GattCharacteristic *battChars[] = {&battLevel, };
-GattService        battService(GattService::UUID_BATTERY_SERVICE, battChars,
-                                sizeof(battChars) / sizeof(GattCharacteristic *));
-
-uint16_t             uuid16_list[] = {GattService::UUID_HEALTH_THERMOMETER_SERVICE,
-                                      GattService::UUID_BATTERY_SERVICE};
-
+//GattCharacteristic *ControllerChars[] = { &accelChar, &gyroChar, };
+GattCharacteristic *ControllerChars[] = { &accelChar, };
+GattService         MPU6050Service(MPU6050_service_uuid, ControllerChars, sizeof(ControllerChars) / sizeof(GattCharacteristic *));
 
 void updateValue();
 
@@ -113,7 +91,7 @@
     /******************************************************************************/
     
     #define MIN_CONN_INTERVAL 250  /**< Minimum connection interval (250 ms) */
-    #define MAX_CONN_INTERVAL 250  /**< Maximum connection interval (350 ms). */
+    #define MAX_CONN_INTERVAL 350  /**< Maximum connection interval (350 ms). */
     #define CONN_SUP_TIMEOUT  6000 /**< Connection supervisory timeout (6 seconds). */
     #define SLAVE_LATENCY     4
 
@@ -166,7 +144,7 @@
     }
     
     Ticker ticker;
-    ticker.attach(periodicCallback, 1); //1sec
+    ticker.attach(periodicCallback, 0.25f); //1sec
     
     ble.init();
     ble.onDisconnection(disconnectionCallback);
@@ -179,18 +157,15 @@
     /* setup advertising */
     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED  | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t*)uuid16_list, sizeof(uuid16_list));
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_THERMOMETER);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (const uint8_t *)DEVICENAME, sizeof(DEVICENAME));
     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
-                                    (const uint8_t *)MPU6050_adv_service_uuid, sizeof(MPU6050_adv_service_uuid));
+                                    (const uint8_t *)MPU6050_service_uuid, sizeof(MPU6050_service_uuid));
+                                    //(const uint8_t *)MPU6050_adv_service_uuid, sizeof(MPU6050_adv_service_uuid));
 
     ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
     ble.startAdvertising();
 
     ble.addService(MPU6050Service);
-    ble.addService(htmService);
-    ble.addService(battService);
     
     while(true) {
         if (triggerSensorPolling && ble.getGapState().connected) {
@@ -218,14 +193,14 @@
     //ジャイロを取得
     mpu.getGyro(gyData);
 
-    memcpy(gyroPayload+sizeof(float)*0, &gyData[0], sizeof(gyData[0]));
-    memcpy(gyroPayload+sizeof(float)*1, &gyData[1], sizeof(gyData[1]));
-    memcpy(gyroPayload+sizeof(float)*2, &gyData[2], sizeof(gyData[2]));
+    memcpy(accelPayload+sizeof(float)*3, &gyData[0], sizeof(gyData[0]));
+    memcpy(accelPayload+sizeof(float)*4, &gyData[1], sizeof(gyData[1]));
+    memcpy(accelPayload+sizeof(float)*5, &gyData[2], sizeof(gyData[2]));
 
     //温度を取得
     tempData = mpu.getTemp();
 
-    memcpy(tempPayload+sizeof(float)*0, &tempData, sizeof(tempData));
+    memcpy(accelPayload+sizeof(float)*6, &tempData, sizeof(tempData));
     /*
     pc.printf("Accel: %.3lf,%.3lf,%.3lf\r\n",
                         *(float*)&accelPayload[sizeof(float)*0],
@@ -242,7 +217,4 @@
     */
     
     ble.updateCharacteristicValue(accelChar.getValueAttribute().getHandle(), accelPayload, sizeof(accelPayload));    //Mod
-    ble.updateCharacteristicValue(gyroChar.getValueAttribute().getHandle(), gyroPayload, sizeof(gyroPayload));    //Mod
-    ble.updateCharacteristicValue(tempChar.getValueAttribute().getHandle(), tempPayload, sizeof(tempPayload));    //Mod
-    ble.updateCharacteristicValue(battLevel.getValueAttribute().getHandle(), &batt, sizeof(batt));    //Mod
 }