InfraredLibに対応したMSC

Dependencies:   BLE_API RemoteIR mbed nRF51822

Fork of nRF51822_SimpleChat_IRRX_nRF51DK by Keiichi Matsuoka

Revision:
4:f1923d9f536e
Parent:
3:175ba3a059cb
Child:
5:1bf7a84598a1
--- a/main.cpp	Mon Feb 29 07:05:55 2016 +0000
+++ b/main.cpp	Fri Dec 02 02:14:10 2016 +0000
@@ -27,6 +27,9 @@
 #include "mbed.h"
 #include "ble/BLE.h"
 #include <string.h>
+#include "ReceiverIR.h"
+#include "TransmitterIR.h"
+
 //#include <stdio.h>
 
 
@@ -42,13 +45,25 @@
 Ticker steper;
 
 
+ReceiverIR ir_rx(P0_25);
+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};
+//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};
 
+// 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};
+
+//01f3f538-8d11-4802-b6fc-fb6616d4cd70
 
 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
@@ -56,7 +71,12 @@
 static uint8_t rx_buf[TXRX_BUF_LEN];
 static uint8_t rx_len=0;
 static uint8_t rx_buf_st[20];
-//static int st = 0;
+
+int8_t buf_before[40];
+
+static int st = 0;
+
+int receivedFlag = 0;
 
 GattCharacteristic  txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
                                       
@@ -76,49 +96,13 @@
 
 void onScanCallback(const Gap::AdvertisementCallbackParams_t *params)
 {
-    
-
-//#if NEED_CONSOLE_OUTPUT
-//    DEBUG("onScan() ");
-//    DEBUG("RSSI:%d ",params->rssi);
-//    DEBUG("Addr:%d ",params->peerAddr);
-//    DEBUG("Len:%d ",params->advertisingDataLen);
-//    DEBUG("Data:");
-//    for (int i=0; i< params->advertisingDataLen; i++){
-//        DEBUG("%02x,",params->advertisingData[i]);
-//    }
-    //rx_buf_st[0] = params->rssi;
-    uint8_t bufbuf[40];
-    char bufbuf_c[20];
-    char bufbuf_addr[20];
-    sprintf(bufbuf_c, "%d", params->rssi);
+    //printf("onScanNau\n");
+  
+//    printf("adv peerAddr[%d %d %d %d %d %d] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
 
-    //strcat(bufbuf_c, '¥0');
-    int a;
-    for(a=0; bufbuf_c[a] != '\0'; a++) {
-        bufbuf[a] = bufbuf_c[a];
-    }
-    bufbuf[a] = ',';
-    a++;
-    int b;
-//    for(b=5; b >= 0; b--) {
-//        sprintf(bufbuf_addr,"%02x",params->peerAddr[b]);
-//        bufbuf[a] = bufbuf_addr[b];
-//        memset(bufbuf_addr,NULL,sizeof(bufbuf_addr));
-//        bufbuf[a++] = ',';
-//        a++;
-//    }
-    for(b=5; b >= 0; b--) {
-      //  sprintf(bufbuf_addr,"%02x",params->peerAddr[b]);
-        bufbuf[a] = params->peerAddr[b];//bufbuf_addr[b];
-      //  memset(bufbuf_addr,'\0',sizeof(bufbuf_addr));
-        a++;
-    }
-
-    ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), bufbuf, a);
- 
+//だいじ
+/* 
     printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
-//    printf("adv peerAddr[%d %d %d %d %d %d] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
            params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
            params->rssi, params->isScanResponse, params->type);
     printf("\n");
@@ -127,6 +111,8 @@
   for (int i=0; i< params->advertisingDataLen; i++){
     printf("%02x,",params->advertisingData[i]);
   }
+  */
+  
  // for (int i=0; i<params->advertisingDataLen; i++){
  //   printf("%02x,",params->advertisingData[i]);
  // }
@@ -135,8 +121,8 @@
     
 //   DEBUG(" \n\r");
 //#endif
-
-
+    
+    
 }
 
 
@@ -184,14 +170,139 @@
 
 // 1++していく
 void step() {
-    //++st;
-    //rx_buf_st[0] = st;
-    //ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); 
-    //ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf_st, sizeof(rx_buf_st)); 
+    ++st;
+    //ここから-------------
+    char bufbuf_c[20];
+    uint8_t bufbuf[40];
+    sprintf(bufbuf_c, "%d", st);
+    
+    int a;
+    for(a=0; bufbuf_c[a] != '\0'; a++) {
+        bufbuf[a] = bufbuf_c[a];
+    }
+    
+//    ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); 
+    ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), bufbuf, a); 
+    //ここまで-------------
+    //をこぴればデータを送信可能
+    
     //pc.printf("%d", st);
     //pc.printf("step! \r\n");
 }
 
+void flagSet() {
+        //ここから-------------
+    char bufbuf_c[20];
+    uint8_t bufbuf[40];
+    sprintf(bufbuf_c, "%d", receivedFlag);
+    
+    int a;
+    for(a=0; bufbuf_c[a] != '\0'; a++) {
+        bufbuf[a] = bufbuf_c[a];
+    }
+    
+//    ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); 
+    ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), bufbuf, a); 
+    //ここまで-------------
+    //をこぴればデータを送信可能
+}
+
+
+
+/**
+ * Receive.
+ *
+ * @param format Pointer to a format.
+ * @param buf Pointer to a buffer.
+ * @param bufsiz Size of the buffer.
+ *
+ * @return Bit length of the received data.
+ */
+int receive(RemoteIR::Format *format, uint8_t *buf, int bufsiz, int timeout = 100) {
+    int cnt = 0;
+    while (ir_rx.getState() != ReceiverIR::Received) {
+        //pc.printf("no receive \r\n");
+        cnt++;
+        if (timeout < cnt) {
+            return -1;
+        }
+    }
+    return ir_rx.getData(format, buf, bufsiz * 8);
+}
+
+/**
+ * Transmit.
+ *
+ * @param format Format.
+ * @param buf Pointer to a buffer.
+ * @param bitlength Bit length of the data.
+ *
+ * @return Bit length of the received data.
+ */
+int transmit(RemoteIR::Format format, uint8_t *buf, int bitlength, int timeout = 100) {
+    int cnt = 0;
+    while (ir_tx.getState() != TransmitterIR::Idle) {
+        cnt++;
+        if (timeout < cnt) {
+            return -1;
+        }
+    }
+    return ir_tx.setData(format, buf, bitlength);
+}
+
+/**
+ * Display a data.
+ *
+ * @param buf Pointer to a buffer.
+ * @param bitlength Bit length of a data.
+ */
+void display_data(uint8_t *buf, int bitlength) {
+    //lcd.locate(0, 1);
+    const int n = bitlength / 8 + (((bitlength % 8) != 0) ? 1 : 0);
+    for (int i = 0; i < n; i++) {
+      
+      pc.printf("%02X", buf[i]);
+    }
+    for (int i = 0; i < 8 - n; i++) {
+      pc.printf("--");
+    }
+}
+
+/**
+ * Display a current status.
+ */
+void display_status(char *status, int bitlength) {
+    //lcd.locate(8, 0);
+    pc.printf("%-5.5s:%02d", status, bitlength);
+}
+
+/**
+ * Display a format of a data.
+ */
+void display_format(RemoteIR::Format format) {
+    //lcd.locate(0, 0);
+    switch (format) {
+        case RemoteIR::UNKNOWN:
+            pc.printf("????????");
+            break;
+        case RemoteIR::NEC:
+            pc.printf("NEC     ");
+            break;
+        case RemoteIR::NEC_REPEAT:
+            pc.printf("NEC  (R)");
+            break;
+        case RemoteIR::AEHA:
+            pc.printf("AEHA    ");
+            break;
+        case RemoteIR::AEHA_REPEAT:
+            pc.printf("AEHA (R)");
+            break;
+        case RemoteIR::SONY:
+            pc.printf("SONY    ");
+            break;
+    }
+}
+
 
 
 int main(void)
@@ -208,7 +319,7 @@
     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
-                                    (const uint8_t *)"keiichi", sizeof("keiichi") - 1);
+                                    (const uint8_t *)"KEI_IRRX", sizeof("KEI_IRRX") - 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. 
@@ -225,11 +336,33 @@
     ble.startAdvertising(); 
     pc.printf("Advertising Start \r\n");
     
-    steper.attach(&step, 1.0);
+    steper.attach(&flagSet, 1.0);
    
     while(1)
     {
-        ble.waitForEvent(); 
+        pc.printf("-------- \r\n");
+        ble.waitForEvent();
+        
+        uint8_t buf1[32];
+        int bitlength1;
+        RemoteIR::Format format;
+
+        //memset(buf1, 0x00, sizeof(buf1));
+
+        {
+            bitlength1 = receive(&format, buf1, sizeof(buf1));
+            if (bitlength1 < 0) {
+                receivedFlag = 0;
+                continue;
+            }
+            pc.printf("receive ok! \r\n");
+            display_status("RECV", bitlength1);
+            display_data(buf1, bitlength1);
+            display_format(format);
+            receivedFlag = 1;
+            
+            
+        }
     }
 }