PirLibに対応したMSC

Dependencies:   BLE_API mbed nRF51822

Fork of nRF51822_SimpleChat by RedBearLab

Revision:
6:00fd0600ce81
Parent:
5:29b82cc045b6
--- a/main.cpp	Thu Dec 08 13:53:36 2016 +0000
+++ b/main.cpp	Sun Feb 12 10:36:33 2017 +0000
@@ -30,7 +30,21 @@
 
 // TX POWER用
 #include "ble_gap.h"
-#define TX_POWER                         -40
+#define TX_POWER                         -4
+//機器設定
+//#define TX_ZERO                         
+//#define TX_POWER_CALI                    -10
+//const static char     DEVICE_NAME[]        = "nRF51DK"; // change this
+
+//#define TX_POWER_CALI                      -7
+//const static char     DEVICE_NAME[]        = "51822"; // change this
+
+//#define TX_POWER_CALI                    -15
+//const static char     DEVICE_NAME[]        = "Nano"; // change this
+
+#define TX_POWER_CALI                    -16
+const static char     DEVICE_NAME[]        = "MN7"; // change this
+
 
 #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. */
@@ -43,7 +57,13 @@
 Serial pc(USBTX, USBRX);
 Ticker steper;
 DigitalOut ledori1(LED1);
+DigitalOut ledori2(LED2);
+//DigitalIn pirIn(P0_21); //BK
 
+//DigitalIn pirIn(P0_13); //共通Digital
+DigitalIn pirIn(P0_10); //Nano Digital
+
+//AnalogIn pirInAna(P0_1);
 
 // The Nordic UART Service
 //static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
@@ -52,10 +72,12 @@
 //static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
 
 // Send One CLK Service
-static const uint8_t uart_base_uuid[] = {0x53, 0xF3, 0xF5, 0x38, 0x8D, 0x11, 0x48, 0x02, 0xB6, 0xFC, 0xFB, 0x66, 0x16, 0xD4, 0xCD, 0x7C}; //Service
+//static const uint8_t uart_base_uuid[] = {0x53, 0xF3, 0xF5, 0x38, 0x8D, 0x11, 0x48, 0x02, 0xB6, 0xFC, 0xFB, 0x66, 0x16, 0xD4, 0xCD, 0x7C}; //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 uart_base_uuid_rev[] = {0x7C, 0xCD, 0xD4, 0x16, 0x66, 0xFB, 0xFC, 0xB6, 0x02, 0x48, 0x11, 0x8D, 0x38, 0xF5, 0xF3, 0x53};
+//static const uint8_t uart_base_uuid_rev[] = {0x7C, 0xCD, 0xD4, 0x16, 0x66, 0xFB, 0xFC, 0xB6, 0x02, 0x48, 0x11, 0x8D, 0x38, 0xF5, 0xF3, 0x53};
+static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
+static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
 
 
 // TX POWER用
@@ -63,9 +85,26 @@
 uint8_t powerPayload[TXRX_BUF_LEN] = {0,};
 static uint8_t power_buf[TXRX_BUF_LEN];
 static uint8_t power_len=0;
-GattCharacteristic  txPowerCharacteristic (power_level_uuid, powerPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); //tx power characteristic
+GattCharacteristic  txPowerCharacteristic (power_level_uuid, powerPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); //tx power characteristic
 Ticker txSteper;
 
+
+// MSC UUID
+static const uint8_t msc_uuid[]   = {0x71, 0x3D, 0, 6, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; //central_TxPower受信用
+uint8_t mscPayload[TXRX_BUF_LEN] = {0,};
+static uint8_t msc_buf[TXRX_BUF_LEN];
+static uint8_t msc_len=0;
+GattCharacteristic  mscCharacteristic (msc_uuid, mscPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); //tx power characteristic
+
+
+//Feature UUID
+static const uint8_t feature_uuid[]   = {0x71, 0x3D, 0, 5, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; //central_TxPower受信用
+uint8_t featurePayload[TXRX_BUF_LEN] = {0,};
+static uint8_t feature_buf[TXRX_BUF_LEN];
+static uint8_t feature_len=0;
+GattCharacteristic  featureCharacteristic (feature_uuid, featurePayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); //tx power characteristic
+
+
 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
 
@@ -77,14 +116,49 @@
 
 static int st = 0;
 
+int pirCount = -1;
 
 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  rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
+
+GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic, &txPowerCharacteristic, &mscCharacteristic, &featureCharacteristic};
+GattService         uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
+
 
-GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic, &txPowerCharacteristic};
+// MSC UUID
+void initMsc() {
+    //ここから-------------
+    char bufbuf_c[20];
+    uint8_t bufbuf[40];
+    //MSCのUUIDをセット
+    sprintf(bufbuf_c, "%s", "00000005");
+    //sprintf(bufbuf_c, "%d", "02f3f538-8d11-4802-b6fc-fb6616d4cd70");
+    int a;
+    for(a=0; bufbuf_c[a] != '\0'; a++) {
+        bufbuf[a] = bufbuf_c[a];
+    }
+    ble.updateCharacteristicValue(mscCharacteristic.getValueAttribute().getHandle(), bufbuf, a);
+    //ここまで-------------
+    //をこぴればデータを送信可能
+}
 
-GattService         uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
+
+// Feature
+void initFeature() {
+    //ここから-------------
+    char bufbuf_c[20];
+    uint8_t bufbuf[40];
+    sprintf(bufbuf_c, "%s", "In.Sensor.PIR");
+    int a;
+    for(a=0; bufbuf_c[a] != '\0'; a++) {
+        bufbuf[a] = bufbuf_c[a];
+    }
+    ble.updateCharacteristicValue(featureCharacteristic.getValueAttribute().getHandle(), bufbuf, a);
+    //ここまで-------------
+    //をこぴればデータを送信可能
+}
+
 
 // TX POWER用
 void txPowerUpdate(int newTxPower) {
@@ -92,7 +166,7 @@
     //ここから-------------
     char bufbuf_c[20];
     uint8_t bufbuf[40];
-    sprintf(bufbuf_c, "%d", newTxPower);
+    sprintf(bufbuf_c, "%d",newTxPower + TX_POWER_CALI);
     int a;
     for(a=0; bufbuf_c[a] != '\0'; a++) {
         bufbuf[a] = bufbuf_c[a];
@@ -106,13 +180,20 @@
     //ここから-------------
     char bufbuf_c[20];
     uint8_t bufbuf[40];
-    sprintf(bufbuf_c, "%d", TX_POWER);
+    sprintf(bufbuf_c, "%d",  TX_POWER + TX_POWER_CALI);
     int a;
     for(a=0; bufbuf_c[a] != '\0'; a++) {
         bufbuf[a] = bufbuf_c[a];
     }
-    ble.updateCharacteristicValue(txPowerCharacteristic.getValueAttribute().getHandle(), bufbuf, a); 
+    ble.updateCharacteristicValue(txPowerCharacteristic.getValueAttribute().getHandle(), bufbuf, a);
 }
+
+void initChara() {
+    initMsc();
+    initFeature();
+    txStep();
+}
+
 //-------------------
 
 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
@@ -172,7 +253,7 @@
         pc.printf("Data: ");
         for(index=0; index<bytesRead; index++)
         {
-            pc.putc(txPayload[index]);        
+            pc.putc(txPayload[index]);
         }
         pc.printf("\r\n");
     }
@@ -199,11 +280,40 @@
 
 // 1++していく
 void step() {
-    ++st;
+    //++st;
+    //float pir = pirInAna;
+    //pc.printf("%f", pir);
+
+    int pir = pirIn;
+    
+        pc.printf("%d", pir);
+      pc.printf("step!pre \r\n");
+
+      pc.printf("%d", pirCount);
+      pc.printf("Count!pre \r\n");
+    
+    //ここから-------------
+    if(pir == 1) {
+        pirCount = 8;
+    } else if(pirCount < -1000) {
+        pirCount = -1;
+    }
+    
+    pirCount = pirCount - 1;
+    
+    
+    if(pirCount < 0) {
+        pir = 0;
+    } else {
+        pir = 1;
+    }
+        
+    
+    
     //ここから-------------
     char bufbuf_c[20];
     uint8_t bufbuf[40];
-    sprintf(bufbuf_c, "%d", st);
+    sprintf(bufbuf_c, "%d", pir);
     
     int a;
     for(a=0; bufbuf_c[a] != '\0'; a++) {
@@ -215,29 +325,31 @@
     //ここまで-------------
     //をこぴればデータを送信可能
     
-    pc.printf("%d", st);
+    pc.printf("%d", pir);
     pc.printf("step! \r\n");
 }
 
 
-
-
 int main(void)
 {
+    pirIn.mode(PullDown);
     ledori1 = 0;
     ble.init();
     ble.onDisconnection(disconnectionCallback);
     ble.onDataWritten(WrittenHandler);  
    
+   
     pc.baud(9600);
+    pc.attach( uartCB , pc.RxIrq);
+    
     pc.printf("SimpleChat Init \r\n");
     
-    pc.attach( uartCB , pc.RxIrq);
+
    // setup advertising 
     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
-                                    (const uint8_t *)"KEI_SEND", sizeof("KEI_SEND") - 1);
+                                    (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. 
@@ -258,7 +370,8 @@
 
     //TX POWER用
     txPowerUpdate(TX_POWER);
-    txSteper.attach(&txStep, 2.0);
+//    txSteper.attach(&initChara, 1.0);
+    initChara();
     
     while(1)
     {