InfraredLibに対応したMSC

Dependencies:   BLE_API RemoteIR mbed nRF51822

Fork of nRF51822_SimpleChat_IRRX_nRF51DK by Keiichi Matsuoka

Files at this revision

API Documentation at this revision

Comitter:
ke_ix1
Date:
Sun Feb 12 10:31:54 2017 +0000
Parent:
6:976503121a46
Commit message:
InfraredLib

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 976503121a46 -r 66243b10532c main.cpp
--- a/main.cpp	Sat Dec 03 10:37:12 2016 +0000
+++ b/main.cpp	Sun Feb 12 10:31:54 2017 +0000
@@ -44,26 +44,69 @@
 Serial pc(USBTX, USBRX);
 Ticker steper;
 DigitalOut led1(P0_17);
+DigitalOut ledori1(LED1);
+//DigitalOut ledori2(LED2);
 
-ReceiverIR ir_rx(P0_25);
+//ReceiverIR ir_rx(P0_25); //nRF51
+//ReceiverIR ir_rx(P0_4); //Nano できんかった
+ReceiverIR ir_rx(P0_2); //51822 nrf51
+
 TransmitterIR ir_tx(P0_18);
 
 //ReceiverIR ir_rx(P0_25);
 
 
-// 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};
-//static const uint8_t uart_tx_uuid[]   = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
-//static const uint8_t uart_rx_uuid[]   = {0x71, 0x3D, 0, 2, 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用
+#include "ble_gap.h"
+#define TX_POWER                         0
+//機器設定
+//#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                      -7
+const static char     DEVICE_NAME[]        = "MN6"; // change this
 
 // The Nordic UART Service
-static const uint8_t uart_base_uuid[] = {0x01, 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 uart_base_uuid_rev[] = {0x70, 0xCD, 0xD4, 0x16, 0x66, 0xFB, 0xFC, 0xB6, 0x02, 0x48, 0x11, 0x8D, 0x38, 0xF5, 0xF3, 0x01};
+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_tx_uuid[]   = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
+static const uint8_t uart_rx_uuid[]   = {0x71, 0x3D, 0, 2, 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};
+
+
+
+// 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
 
-//01f3f538-8d11-4802-b6fc-fb6616d4cd70
+// TX POWER用
+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受信用
+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 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); //tx power characteristic
+Ticker txSteper;
+
+//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,};
@@ -81,12 +124,91 @@
 
 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  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 *));
 
+
+
+
+
+// TX POWER用
+void txPowerUpdate(int newTxPower) {
+    sd_ble_gap_tx_power_set(newTxPower);
+    //ここから-------------
+    char bufbuf_c[20];
+    uint8_t bufbuf[40];
+    sprintf(bufbuf_c, "%d", newTxPower + 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);
+    //ここまで-------------
+    //をこぴればデータを送信可能
+}
+
+
+// MSC UUID
+void initMsc() {
+    //ここから-------------
+    char bufbuf_c[20];
+    uint8_t bufbuf[40];
+    sprintf(bufbuf_c, "%s", "00000003");
+    //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);
+    //ここまで-------------
+    //をこぴればデータを送信可能
+}
+
+
+// Feature
+void initFeature() {
+    //ここから-------------
+    char bufbuf_c[20];
+    uint8_t bufbuf[40];
+    sprintf(bufbuf_c, "%s", "In.Sensor.Infrared");
+    int a;
+    for(a=0; bufbuf_c[a] != '\0'; a++) {
+        bufbuf[a] = bufbuf_c[a];
+    }
+    ble.updateCharacteristicValue(featureCharacteristic.getValueAttribute().getHandle(), bufbuf, a);
+    //ここまで-------------
+    //をこぴればデータを送信可能
+}
+
+
+void txStep() {
+    //ここから-------------
+    //sd_ble_gap_tx_power_set(TX_POWER);
+    char bufbuf_c[20];
+    uint8_t bufbuf[40];
+    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);
+}
+
+
+void initChara() {
+    initMsc();
+    initFeature();
+    txStep();
+}
+
+//-------------------
+
+
+
+
 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
 {
     pc.printf("Disconnected \r\n");
@@ -197,6 +319,7 @@
     uint8_t bufbuf[40];
     sprintf(bufbuf_c, "%d", receivedFlag);
     led1 = receivedFlag;
+    //ledori2 = led1;
     int a;
     for(a=0; bufbuf_c[a] != '\0'; a++) {
         bufbuf[a] = bufbuf_c[a];
@@ -306,6 +429,8 @@
 
 int main(void)
 {
+    ledori1 = 0;
+    //ledori2 = 0;
     led1 = 0;
     ble.init();
     ble.onDisconnection(disconnectionCallback);
@@ -319,7 +444,7 @@
     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
-                                    (const uint8_t *)"KEI_IRRX", sizeof("KEI_IRRX") - 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. 
@@ -336,11 +461,16 @@
     ble.startAdvertising(); 
     pc.printf("Advertising Start \r\n");
     
-    steper.attach(&flagSet, 1.0);
+    steper.attach(&flagSet, 0.5);
+    
+        //TX POWER用
+    //txSteper.attach(&initChara, 3.0);
+    txPowerUpdate(TX_POWER);
    
+   initChara();
     while(1)
     {
-        pc.printf("-------- \r\n");
+        //pc.printf("-------- \r\n");
         ble.waitForEvent();
         
         uint8_t buf1[32];
@@ -363,12 +493,14 @@
                 }
                 continue;
             }
+            /*
             pc.printf("receive ok! \r\n");
             display_status("RECV", bitlength1);
             display_data(buf1, bitlength1);
             display_format(format);
+            */
             receivedFlag = 1;
-            flagMargine = 50;
+            flagMargine = 30000;
         }
     }
 }