test

Dependencies:   BufferedSerial WatchdogTimer

Revision:
3:455575531b33
Parent:
2:a88b0b4c4f37
Child:
4:1920e4a63bde
diff -r a88b0b4c4f37 -r 455575531b33 main.cpp
--- a/main.cpp	Tue Jun 05 03:00:08 2018 +0000
+++ b/main.cpp	Fri Jun 08 05:36:22 2018 +0000
@@ -13,11 +13,18 @@
 
 //  Object ---------------------------------------------------------------------
 BLE&        ble_uart = BLE::Instance();
-Serial      pc(P0_1, P0_3, 115200);     // for another board
-Ticker      main_timer;      // メインタイマー
+Serial      pc(P0_1, P0_3, 115200);     // DEBUG BOARD
+//Serial      pc(P0_9, P0_11, 115200);  // NEW BOARD
+Ticker      main_timer;      // メインタイ216
+
+DigitalOut led(P0_6);
+
+DigitalOut state(P0_24);    // DEBUG BOARD
+//DigitalOut state(P0_3);   // NEW BOARD
+
 
 //  ROM / Constant data --------------------------------------------------------
-const Gap::Address_t    mac_board_0   = {0xFF, 0x42, 0xD5, 0x9C, 0x9C, 0xE1};
+Gap::Address_t    mac_board_0   = {0xFF, 0x42, 0xD5, 0x9C, 0x9C, 0xE1};
 
 //  RAM ------------------------------------------------------------------------
 Gap::Handle_t   connectionHandle        = 0xFFFF;
@@ -39,6 +46,7 @@
 unsigned long mac_2;
 unsigned long mac_3;
 
+
 #define SIO_BUF_SIZE    256     /* SIO リングバッファーサイズ (1K byte) */
 
 typedef struct {
@@ -98,10 +106,10 @@
     // Client(Central) role ****************************************************
     ble_uart.gattClient().onHVX(onReceivedDataFromDeviceCallback);
     ble_uart.gap().setScanParams(500, 450);
-    ble_uart.gap().startScan(advertisementCallback);
-    
-    
-    while(true) {
+    //ble_uart.gap().startScan(advertisementCallback);
+       
+    while(true) 
+    {
         // allow notifications from Server(Peripheral)
         if (foundUartRXCharacteristic &&
                 !ble_uart.gattClient().isServiceDiscoveryActive()) {
@@ -122,7 +130,10 @@
         {
             timer_cnt_d = timer_cnt;
         
-            ble_rs232c_cmd();
+            state = connected2server;
+            led = connected2server;
+            
+            //ble_rs232c_cmd();
             pc_rs232c_cmd();
         }
 
@@ -137,25 +148,9 @@
                 == uartRXCharacteristic.getValueHandle()) && (params->len > 0)) {
             uart_bf_len = params->len;
             strcpy((char *)uart_buffer, (char *)params->data);
-            received_uart_dat = true;
+            received_uart_dat = true;         
             
-            for( int i=0; i<uart_bf_len; i++ )
-            {
-                //pc.putc(uart_buffer[i]);
-                ble_uart_buf.buf[ ble_uart_buf.wp ] = uart_buffer[i];
-                ble_uart_buf.data_cnt++;
-
-                if( ble_uart_buf.wp == SIO_BUF_SIZE-1 )
-                {
-                    ble_uart_buf.wp = 0;
-                }
-                else
-                {
-                    ble_uart_buf.wp++;
-                }
-            }
-
-            uart_bf_len = 0;
+            ble_rs232c_cmd();
         }
     }
 }
@@ -285,11 +280,14 @@
 //    connection_1st = false;
     connection_tx = false;
     connection_rx = false;
+    
+#if 0
     if (params->handle == SOFT_DEVICE_FATHER_HANDLE) {
         ble_uart.startAdvertising();                    // restart advertising
     } else {
         ble_uart.gap().startScan(advertisementCallback);// restart scan
     }
+#endif
 }
 
 /****************************************************************************/
@@ -457,7 +455,7 @@
 
 /****************************************************************************/
 /*  関数名 : rs232c_cmd                                                     */
-/*  概要      : 通信コマンド処理                                              */
+/*  概要   : 通信コマンド処理                                                 */
 /*  作成者 : JPMS H.Harada                                                  */
 /*  作成日 : 2017.08.30                                                     */
 /****************************************************************************/
@@ -465,10 +463,29 @@
 {
     unsigned int c, i, j;
     int rdat;
-   
+
+    for( int i=0; i<uart_bf_len; i++ )
+    {
+        //pc.putc(uart_buffer[i]);
+        ble_uart_buf.buf[ ble_uart_buf.wp ] = uart_buffer[i];
+        ble_uart_buf.data_cnt++;
+
+        if( ble_uart_buf.wp == SIO_BUF_SIZE-1 )
+        {
+            ble_uart_buf.wp = 0;
+        }
+        else
+        {
+            ble_uart_buf.wp++;
+        }
+    }
+
+    uart_bf_len = 0;   
+    
     if( ble_uart_buf.data_cnt > 0 )
     {
         unsigned int len = ble_uart_buf.data_cnt;
+        
         for( c=0; c<len; c++ )
         {
             rdat = ble_uart_buf.buf[ble_uart_buf.rp];
@@ -492,17 +509,20 @@
             {
                 int k = 0;
                 int l = 0;
+                int p = 0;
+                
                 for (i=0;i<ble_cmd_buf.data_cnt;i++)
                 {
-                    if( ble_cmd_buf.buf[i] == ' ' ||
-                        ble_cmd_buf.buf[i] == '\r' ||
-                        ble_cmd_buf.buf[i] == '\t' )
+                    if( ( ( ble_cmd_buf.buf[i] == ' ' ) && ( p > 0 ) ) || 
+                          ( ble_cmd_buf.buf[i] == '\r') )
                     {
                         ble_str[k++][l] = '\0';
                         l = 0;
+                        p = 0;
                     }
                     else
                     {
+                        p++;
                         ble_str[k][l++] = ble_cmd_buf.buf[i];
                     }
                 }
@@ -767,6 +787,21 @@
                         mac_2 = atoh(pc_str[2], strlen(pc_str[2]));
                         mac_3 = atoh(pc_str[3], strlen(pc_str[3]));
 
+                        mac_board_0[0] = ( mac_3 >> 0 ) & 0x00FF;
+                        mac_board_0[1] = ( mac_3 >> 8 ) & 0x00FF;
+                        mac_board_0[2] = ( mac_2 >> 0 ) & 0x00FF;
+                        mac_board_0[3] = ( mac_2 >> 8 ) & 0x00FF;
+                        mac_board_0[4] = ( mac_1 >> 0 ) & 0x00FF;
+                        mac_board_0[5] = ( mac_1 >> 8 ) & 0x00FF;
+
+                        pc.printf( "%02X %02X %02X %02X %02X %02X\r", 
+                            mac_board_0[5],
+                            mac_board_0[4],
+                            mac_board_0[3],
+                            mac_board_0[2],
+                            mac_board_0[1],
+                            mac_board_0[0] );
+
                         pc.printf( "MSET OK\r\n" );
                     }
                     // コマンド
@@ -825,15 +860,32 @@
                     // コマンド CONNECT
                     else if( cmd_no == 11 )
                     {
-                        // CONNECT処理追加
-                        
-                        pc.printf( "CONNECT OK\r\n" );
+                        if( connected2server == false )
+                        {
+                            // CONNECT処理 
+                            ble_uart.gap().connect( mac_board_0, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
+                            
+                            pc.printf( "CONNECT OK\r\n" );
+                        }
+                        else
+                        {
+                            // DISC処理
+                            Gap::DisconnectionReason_t reason; 
+                            ble_uart.gap().disconnect( reason );
+                             
+                            pc.printf( "CONNECT NG\r\n" );
+                        }
                     }
                     // コマンド DISC
                     else if( cmd_no == 12 )
                     {
-                        // DISC処理追加
-
+                        if( connected2server == true )
+                        {
+                            // DISC処理
+                            Gap::DisconnectionReason_t reason; 
+                            ble_uart.gap().disconnect( reason );
+                        }
+                        
                         pc.printf( "DISC OK\r\n" );
                     }
                     // コマンド  INTVL