LEDをON(文字列'a'を送る)OFF(文字列'b'を送る)できるMSC

Dependencies:   BLE_API mbed nRF51822

Fork of nRF51822_SimpleChat by RedBearLab

Revision:
5:35728098eeae
Parent:
4:41cf035db84a
Child:
6:b15d219371da
diff -r 41cf035db84a -r 35728098eeae main.cpp
--- a/main.cpp	Sat Dec 03 10:44:01 2016 +0000
+++ b/main.cpp	Thu Dec 08 10:57:15 2016 +0000
@@ -26,7 +26,8 @@
  
 #include "mbed.h"
 #include "ble/BLE.h"
-
+#include "TxPower.h"
+#include "ble_gap.h"
 
 #define BLE_UUID_TXRX_SERVICE            0x0000 /**< The UUID of the Nordic UART Service. */
 #define BLE_UUID_TX_CHARACTERISTIC       0x0002 /**< The UUID of the TX Characteristic. */
@@ -35,12 +36,12 @@
 #define TXRX_BUF_LEN                     20
 #define DIGITAL_OUT_PIN                  P0_17  //D7
 
-
+#define TX_POWER                         -40
 
 BLE  ble;
 
 Serial pc(USBTX, USBRX);
-
+Ticker steper;
 DigitalOut led1(DIGITAL_OUT_PIN);
 DigitalOut ledori1(LED1);
 
@@ -54,28 +55,42 @@
 static const uint8_t uart_base_uuid[] = {0x02, 0xF3, 0xF5, 0x38, 0x8D, 0x11, 0x48, 0x02, 0xB6, 0xFC, 0xFB, 0x66, 0x16, 0xD4, 0xCD, 0x70}; //Service
 static const uint8_t uart_tx_uuid[]   = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; //central_送信用
 static const uint8_t uart_rx_uuid[]   = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; //central_受信用
+static const uint8_t power_level_uuid[]   = {0x71, 0x3D, 0, 4, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; //central_TxPower受信用
 static const uint8_t uart_base_uuid_rev[] = {0x70, 0xCD, 0xD4, 0x16, 0x66, 0xFB, 0xFC, 0xB6, 0x02, 0x48, 0x11, 0x8D, 0x38, 0xF5, 0xF3, 0x02};
 
+// Tx Power Service
+//static const uint8_t power_base_uuid[] = {0x03, 0xF3, 0xF5, 0x38, 0x8D, 0x11, 0x48, 0x02, 0xB6, 0xFC, 0xFB, 0x66, 0x16, 0xD4, 0xCD, 0x70}; //Service
+//static const uint8_t power_base_uuid_rev[] = {0x70, 0xCD, 0xD4, 0x16, 0x66, 0xFB, 0xFC, 0xB6, 0x02, 0x48, 0x11, 0x8D, 0x38, 0xF5, 0xF3, 0x03};
+//static const uint8_t power_level_uuid[]   = {0x71, 0x3D, 0, 4, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; //central_TxPower受信用
+
+/*
+static const uint16_t txpower_base_uuid[] = {0x18, 0x04};
+static const uint16_t txpower_base_uuid_rev[] = {0x04, 0x18};
+*/
 //02f3f538-8d11-4802-b6fc-fb6616d4cd70
 
-const static char     DEVICE_NAME[]        = "KEI_LED"; // change this
+const static char     DEVICE_NAME[]        = "KEI_LED2"; // change this
 
 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
+uint8_t powerPayload[TXRX_BUF_LEN] = {0,};
 
 static uint8_t rx_buf[TXRX_BUF_LEN];
 static uint8_t rx_len=0;
 
+static uint8_t power_buf[TXRX_BUF_LEN];
+static uint8_t power_len=0;
 
+//Uart Service associated
 GattCharacteristic  txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
-                                      
 GattCharacteristic  rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
-                                      
-GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
-
+GattCharacteristic  txPowerCharacteristic (power_level_uuid, powerPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); //tx power characteristic
+GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic, &txPowerCharacteristic};
 GattService         uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
 
-
+//TxPowerService *txPowerService = NULL;
+//BatteryService *batteryService = NULL;
+//uint8_t batteryLevel = 50;
 
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
@@ -96,11 +111,11 @@
     uint8_t buf[TXRX_BUF_LEN];
     uint16_t bytesRead, index;
     
-    if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) 
+    if (Handler->handle == txCharacteristic.getValueAttribute().getHandle())
     {
         ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
         memset(txPayload, 0, TXRX_BUF_LEN);
-        memcpy(txPayload, buf, TXRX_BUF_LEN);       
+        memcpy(txPayload, buf, TXRX_BUF_LEN);
         pc.printf("WriteHandler \r\n");
         pc.printf("Length: ");
         pc.putc(bytesRead); //受け取った文字の先頭アドレス?
@@ -111,13 +126,13 @@
             pc.printf("yeahhhhhh!!!!!!!!");
 //            if(led1 == 0) {
                 led1 = 1;
-                ledori1 = 1;
+                ledori1 = 0;
 //            } else {
 //                led1 = 0;
 //            }
         } else if(txPayload[1]=='b') {
             led1 = 0;
-            ledori1 = 0;
+            ledori1 = 1;
         }
         pc.printf("\r\n");
         pc.printf("Data: ");
@@ -129,6 +144,22 @@
     }
 }
 
+
+void txPowerUpdate(int newTxPower) {
+    sd_ble_gap_tx_power_set(-40);
+    //ここから-------------
+    char bufbuf_c[20];
+    uint8_t bufbuf[40];
+    sprintf(bufbuf_c, "%d", newTxPower);
+    int a;
+    for(a=0; bufbuf_c[a] != '\0'; a++) {
+        bufbuf[a] = bufbuf_c[a];
+    }
+    ble.updateCharacteristicValue(txPowerCharacteristic.getValueAttribute().getHandle(), bufbuf, a);
+    //ここまで-------------
+    //をこぴればデータを送信可能
+}
+
 void uartCB(void)
 {   
     while(pc.readable())    
@@ -136,6 +167,8 @@
         rx_buf[rx_len++] = pc.getc();    
         if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
         {
+            txPowerUpdate(TX_POWER);
+            
             ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); 
             pc.printf("RecHandler \r\n");
             pc.printf("Length: ");
@@ -147,6 +180,20 @@
     }
 }
 
+
+// 1++していく
+void txStep() {
+    //ここから-------------
+    char bufbuf_c[20];
+    uint8_t bufbuf[40];
+    sprintf(bufbuf_c, "%d", TX_POWER);
+    int a;
+    for(a=0; bufbuf_c[a] != '\0'; a++) {
+        bufbuf[a] = bufbuf_c[a];
+    }
+    ble.updateCharacteristicValue(txPowerCharacteristic.getValueAttribute().getHandle(), bufbuf, a); 
+}
+
 int main(void)
 {
     led1 = 0;
@@ -160,21 +207,39 @@
     pc.printf("SimpleChat Init \r\n");
     
     pc.attach( uartCB , pc.RxIrq);
+    
+//   txPowerService = new TxPowerService(ble, -40);
+//    txPowerService->updateTxPower(TX_POWER);
+    
+        
+    //TxPowerService tx(ble, -40); // setting up the service and assigning an initial value
+    //tx.updateTxPower(TX_POWER);    //changing the initial value just for testing ....   
+    
    // setup advertising 
+//    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
+//    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)txpower_base_uuid_rev, sizeof(txpower_base_uuid_rev));
+    
     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
                                     (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
                                     (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
+
     // 100ms; in multiples of 0.625ms. 
     ble.setAdvertisingInterval(50);
 
     ble.addService(uartService);
     
+    ble.setScanParams(GapScanningParams::SCAN_INTERVAL_MIN,
+                      GapScanningParams::SCAN_WINDOW_MIN,
+                      0);
+                      
     ble.startAdvertising(); 
     pc.printf("Advertising Start \r\n");
     
+    steper.attach(&txStep, 2.0);
+
     while(1)
     {
         ble.waitForEvent();