USonicLibに対応したMSC

Dependencies:   BLE_API mbed nRF51822

Fork of nRF51822_SimpleChat by RedBearLab

Revision:
6:1331ef9ec5cc
Parent:
5:29b82cc045b6
--- a/main.cpp	Thu Dec 08 13:53:36 2016 +0000
+++ b/main.cpp	Sun Feb 12 10:24:35 2017 +0000
@@ -30,7 +30,21 @@
 
 // TX POWER用
 #include "ble_gap.h"
-#define TX_POWER                         -40
+#define TX_POWER                         -8
+//機器設定
+//#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                      -12
+const static char     DEVICE_NAME[]        = "MN8"; // 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. */
@@ -40,9 +54,60 @@
 
 BLE  ble;
 
+//ピン機能
 Serial pc(USBTX, USBRX);
 Ticker steper;
 DigitalOut ledori1(LED1);
+DigitalOut ledori2(LED2);
+//DigitalOut USSTrigger(P0_25); // nRF51: P0_25 :超音波センサ トリガ出力
+DigitalOut USSTrigger(P0_20); // 51822: P0_25 :超音波センサ トリガ出力
+Timer ActiveTime;
+
+//割り込み処理宣言
+Ticker TriggerTiming; //Triggerピン :インターバルタイマ
+//
+InterruptIn USSEcho(P0_28); //nRF51 P0_28 :超音波センサ エコー入力
+InterruptIn USSEcho(P0_22); //51822 P0_28 :超音波センサ エコー入力
+
+//関数宣言
+void initSonic(void);
+void Output_Monitor(unsigned short Value);
+
+//グローバル変数宣言
+unsigned short USSDistance; //USSDistance :超音波センサの測定距離
+
+// 60msごとの割り込みでUSSTriggerに10usのON出力
+void Trigger() {
+    USSTrigger = 1;
+    wait_us(10);
+    USSTrigger = 0;
+}
+
+//USSEcho立ち上がりでの割り込み
+//Hiの場合ActiveTimeタイマスタート
+void RiseEcho() {
+    ActiveTime.start();
+}
+
+//USSEcho立ち下がりでの割り込み
+//Lowの場合ActiveTime停止+値読み取り
+void FallEcho() {
+    unsigned long ActiveWidth;
+    ActiveTime.stop();
+    ActiveWidth = ActiveTime.read_us();
+    USSDistance = ActiveWidth * 0.0170;
+    ActiveTime.reset();
+}
+
+void initSonic(void) {
+    TriggerTiming.attach(Trigger, 0.060); //USSTrigger周期60ms
+    USSEcho.rise(RiseEcho); //USSEcho立ち上がり時割り込み
+    USSEcho.fall(FallEcho); //USSEcho立ち下がり時割り込み
+}
+
+void Output_Monitor(unsigned short Value){
+    pc.printf("%d[cm]\r\n",Value);
+}
 
 
 // The Nordic UART Service
@@ -52,10 +117,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 +130,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,};
 
@@ -80,11 +164,46 @@
 
 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", "00000008");
+    //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.USonic");
+    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 +211,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,12 +225,19 @@
     //ここから-------------
     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();
 }
 //-------------------
 
@@ -172,7 +298,7 @@
         pc.printf("Data: ");
         for(index=0; index<bytesRead; index++)
         {
-            pc.putc(txPayload[index]);        
+            pc.putc(txPayload[index]);
         }
         pc.printf("\r\n");
     }
@@ -199,11 +325,15 @@
 
 // 1++していく
 void step() {
-    ++st;
+    //++st;
+    //float pir = pirInAna;
+    //pc.printf("%f", pir);
+
+
     //ここから-------------
     char bufbuf_c[20];
     uint8_t bufbuf[40];
-    sprintf(bufbuf_c, "%d", st);
+    sprintf(bufbuf_c, "%d", USSDistance);
     
     int a;
     for(a=0; bufbuf_c[a] != '\0'; a++) {
@@ -211,7 +341,7 @@
     }
     
 //    ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); 
-    ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), bufbuf, a); 
+    ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), bufbuf, a);
     //ここまで-------------
     //をこぴればデータを送信可能
     
@@ -220,8 +350,6 @@
 }
 
 
-
-
 int main(void)
 {
     ledori1 = 0;
@@ -237,7 +365,7 @@
     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,11 +386,19 @@
 
     //TX POWER用
     txPowerUpdate(TX_POWER);
-    txSteper.attach(&txStep, 2.0);
+    //txSteper.attach(&initChara, 1.0);
+    
+    initChara();
+    char val;
+    initSonic();
     
     while(1)
     {
         ble.waitForEvent();
+        //if( pc.readable() ){
+          //  Output_Monitor( USSDistance );
+            //val = pc.getc();
+        //}
     }
 }