jpms_box / Mbed OS SB-2018-X_MPU_12

Files at this revision

API Documentation at this revision

Comitter:
harada_jpms
Date:
Tue Sep 29 23:59:54 2020 +0000
Parent:
6:adf67f0b57e5
Commit message:
test

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Jun 14 07:06:42 2018 +0000
+++ b/main.cpp	Tue Sep 29 23:59:54 2020 +0000
@@ -1,9 +1,12 @@
 //  Include --------------------------------------------------------------------
 #include "mbed.h"
 #include "BLE.h"
+#include "nrf_ble_gap.h"
 #include "UARTService.h"
 
 //  Definition -----------------------------------------------------------------
+#define VER  12  // 20190314
+
 #define     NUM_ONCE            20
 #define     BFSIZE              (NUM_ONCE+4)
 
@@ -16,10 +19,21 @@
 
 #define OFF             0
 #define ON              1
+#define NON             2
 
 #define CONNECT_ON      1
 #define CONNECT_OFF     0
 
+//#define RFID_SAMPLE     20      // ms
+#define RFID_SAMPLE     50     // ms  Ver12
+
+#define STATE_WAIT_OFF  0
+#define STATE_WAIT_ON   1
+#define STATE_WAIT_END  2
+
+//#define UNCONNECT_SLEEP   0
+//#define SLEEP_MODE      0
+
 //  Object ---------------------------------------------------------------------
 BLE&            ble_uart = BLE::Instance();
 //Serial          pc(USBTX, USBRX, 115200);   // DEBUG BOARD
@@ -27,6 +41,8 @@
 UARTService     *uartServicePtr;
 Ticker          main_timer;      // メインタイマー
 
+Mutex           bletx_mutex;
+
 SPI rfid( P0_13, P0_14, P0_15 );
 DigitalOut rfid_cs( P0_12 );
 DigitalOut rfid_en1( P0_6 );
@@ -50,7 +66,7 @@
 unsigned int ble_connect_flag = false;
 
 // Serial
-#define SIO_BUF_SIZE    256     /* SIO リングバッファーサイズ (1K byte) */
+#define SIO_BUF_SIZE    64     /* SIO リングバッファーサイズ (1K byte) */
 
 typedef struct {
     unsigned short      data_cnt;               /* リング・バッファ データカウント */
@@ -63,29 +79,35 @@
 ring_cnt_t cmd_buf;         // 通信用コマンドバッファ
 
 // RFID
-unsigned int interval_cnt;
 unsigned int rfid_cnt;
 unsigned int connect_stat;
 unsigned int no_alive_cnt;
 
 unsigned int start_flag;
 unsigned int idget_flag;
+unsigned int pause_flag;
 
-unsigned int id_LH;
-unsigned int id_LL;
-unsigned int id_RH;
-unsigned int id_RL;
+unsigned int id_LH[4];
+unsigned int id_LL[4];
+unsigned int id_RH[4];
+unsigned int id_RL[4];
 
-unsigned int interval;
+char id_L_BYTE[4][8]; // Ver12
+char id_R_BYTE[4][8]; // Ver12
+char id_G_BYTE[8];    // Ver12
+
 unsigned int not_touch_cnt;
 unsigned int touch_cnt;
+unsigned int point_res;
+unsigned int touch_start;
 
 typedef struct {
-    unsigned int Result_pulse;
+    unsigned int Result_state;
     unsigned int Result;
     unsigned int Result_d1;
     unsigned int Result_ON_cnt;
     unsigned int Result_OFF_cnt;
+    unsigned int Result_NON_cnt;
     unsigned int Point;
 } result_t;
 
@@ -100,8 +122,22 @@
 
 unsigned int uid_scnt;
 
-unsigned int resp_cnt;
+unsigned int sleep_flag;
+
+char level_s;
+unsigned int uid_H_s;
+unsigned int uid_L_s;
 
+unsigned int lpsave;
+unsigned int rpsave;
+
+#define MEM_ID_NUM      5000
+unsigned char mem_id[MEM_ID_NUM];
+
+char trf7970a_reg09_dat;        // Ver12
+char trf7970a_reg0A_dat;        // Ver12
+char trf7970a_reg0B_dat;        // Ver12
+unsigned int Iccard_mem[10];    // Ver12
 
 //  Function prototypes --------------------------------------------------------
 //      BLE
@@ -115,6 +151,9 @@
 char trf7970a_get(char dat);
 void trf7970a_init();
 char trf7970a_ID_get(char *dat, char *level);
+void trf7970a_ID_check(char *level);                    // Ver12
+void trf7970a_ID_write( char adr, unsigned int dat );   // Ver12
+char trf7970a_ID_read( char adr, unsigned int *dat );   // Ver12
 void main_timer_proc();
 
 // COMMAND
@@ -123,11 +162,7 @@
 void ble_cmd();
 void uart_init();
 
-unsigned int point_result_proc( result_t *result, unsigned int ret,
-            unsigned int uid_H, unsigned int uid_L, unsigned int id_H, unsigned int id_L );
-            
-
-
+unsigned int point_result_proc( result_t *result, char punch, char gardL, char gardR ); // Ver12
 
 int main()
 { 
@@ -135,39 +170,71 @@
            
     start_flag = OFF;
     idget_flag = OFF;
+    pause_flag = OFF;
 
-    id_LH = 0xE0070000;
-    id_LL = 0x24F3EED5;
-    id_RH = 0xE0070000;
-    id_RL = 0x1F9A3634;    
+    // Ver12 ///////////////////////////
+    id_LH[0] = 0x00000000;
+    id_LL[0] = 0x00000000;
+    id_RH[0] = 0x00000000;
+    id_RL[0] = 0x00000000;    
+
+    for( int j=0; j<4; j++ )
+    {
+        for( int i=0; i<8; i++ )
+        {
+            id_L_BYTE[j][i] = 0x00;
+            id_R_BYTE[j][i] = 0x00;
+            id_G_BYTE[i] = 0x00;            
+        }
+    }
+    
+    for( int i=0; i<10; i++ )
+    {
+        Iccard_mem[i] = 0x00000000;
+    }
+    //////////////////////////////////////
     
     rfid_cnt = 0;
 
     connect_stat = OFF;
     no_alive_cnt = 0;
 
-    Result_L.Result_pulse = OFF;
+    Result_L.Result_state = STATE_WAIT_OFF;
     Result_L.Result = OFF;
     Result_L.Result_d1 = OFF;
     Result_L.Result_ON_cnt = 0;
     Result_L.Result_OFF_cnt = 0;
+    Result_L.Result_NON_cnt = 0;
     Result_L.Point = 0;
 
-    Result_R.Result_pulse = OFF;
+    Result_R.Result_state = STATE_WAIT_OFF;
     Result_R.Result = OFF;
     Result_R.Result_d1 = OFF;
     Result_R.Result_ON_cnt = 0;
     Result_R.Result_OFF_cnt = 0;
+    Result_R.Result_NON_cnt = 0;
     Result_R.Point = 0;
 
-    resp_cnt = 0;
+    lpsave = 0;
+    rpsave = 0;
 
-    interval = 1;
-    interval_cnt = 0;    
+    not_touch_cnt = 200 / RFID_SAMPLE;
+    touch_cnt = 800 / RFID_SAMPLE;
+    touch_start = 50 / RFID_SAMPLE;     // Ver12
     
-    interval = 1;
-    not_touch_cnt = 200 / 20;
-    touch_cnt = 800 / 20;
+    point_res = OFF;
+ 
+    // Ver12 で追加
+    trf7970a_reg09_dat = 0x00;
+    trf7970a_reg0A_dat = 0x40;
+    trf7970a_reg0B_dat = 0x07;
+           
+    for( int i=0; i<MEM_ID_NUM; i++ )
+    {
+        mem_id[i] = 0;
+    }
+    
+    led = 0;
     
 // opening message
     pc.printf("UART Communication / Server(Peripheral) side\r\n");
@@ -211,7 +278,12 @@
         sizeof(UARTServiceUUID_reversed)
     );
     // Advertize Interval
-    ble_uart.setAdvertisingInterval(1000); /* 1000ms;in multiples of 0.625ms.*/
+    //ble_uart.setAdvertisingInterval(1000); /* 1000ms;in multiples of 0.625ms.*/
+    ble_uart.setAdvertisingInterval(100);
+    
+    uint32_t res = sd_ble_gap_tx_power_set( 4 );
+    
+    pc.printf( "tx_power_set_res = %d\r", res );
 
 // Avertisingをスタート
     // Start
@@ -220,18 +292,36 @@
     
     UARTService uartService(ble_uart);
     uartServicePtr = &uartService;
+    
+#ifdef SLEEP_MODE
 
+    sleep_flag = ON;
+    rfid_en1 = 0;
 
-    rfid_en1 = 1;
-    rfid_en2 = 1;
+#else
     
-    trf7970a_init();    // RFID 初期化処理
+    #ifdef UNCONNECT_SLEEP
+        sleep_flag = ON;
+        rfid_en1 = 0;
+    #else
+        sleep_flag = OFF;
+                                   
+        rfid_en1 = 1;
+                                
+        trf7970a_init();    // RFID 初期化処理
+    #endif
+    
+#endif
+
+    int pcnt = 0;
 
     timer_cnt = 0;
     timer_cnt_d = timer_cnt;
 
-    // メインタイマー設定(20ms)
-    main_timer.attach(&main_timer_proc, 0.020); 
+    // メインタイマー設定
+    main_timer.attach(&main_timer_proc, RFID_SAMPLE / 1000.0);  // Ver12
+    
+    int cnt = 0;
     
     while( true )
     {
@@ -248,35 +338,27 @@
         if( timer_cnt != timer_cnt_d )
         {
             timer_cnt_d = timer_cnt;
-
+            
+            cnt++;
+            
             if( ble_connect_flag == CONNECT_ON )
             {
-                if( resp_cnt >= 50 * 5 )    // 5秒
-                {
-                    char buf[BFSIZE];
-                                       
-                    sprintf( buf, "RESP %d\r", rfid_cnt );
-
-                    ble_write( buf );
+                //led = 0;
+            }
+            else
+            {
+                //led = ( cnt >> 5 ) & 0x0001;
+            }          
 
-                    resp_cnt = 0;
-                }
-                else
-                {
-                    resp_cnt++;
-                }
-            }
+            // Ver12 にて処理を大きく変更した
+            if( start_flag == ON )
+            {                                   
+                char level[4];
+led = 1;       
+                // RFID チェック
+                trf7970a_ID_check( &level[0] );
 
-            if( ( interval_cnt == 0 ) && ( ( start_flag == ON ) || ( idget_flag == ON ) ) )
-            {                    
-                char ret;
-                char uid[10];
-                char level;
-                unsigned int uid_H;
-                unsigned int uid_L;
-        
-                // RFID取得
-                ret = trf7970a_ID_get( &uid[0], &level );
+                pc.printf( "[%d %d %d %d] ", level[0], level[1], level[2], level[3] );
 
                 // RFID取得カウントを設定
                 if( rfid_cnt < 9999 )
@@ -285,7 +367,151 @@
                 }
                 else
                 {
-                    rfid_cnt = 0;
+                    rfid_cnt = 1;   // Ver12 修正 0->1                  
+                }
+
+                //level_s = level;
+                //uid_H_s = uid_H;
+                //uid_L_s = uid_L; 
+
+                //pc.printf("%d %08X %08X ",level, uid_H, uid_L);
+
+                // 解析
+
+                unsigned int pret_L;
+                unsigned int pret_R;
+
+                pret_L = point_result_proc( &Result_L, level[0], level[2], level[3] );
+                pret_R = point_result_proc( &Result_R, level[1], level[2], level[3] );
+
+                if( point_res == ON )
+                {
+                    if( ( Result_L.Point > lpsave ) || ( Result_R.Point > rpsave ) )
+                    {                          
+                        char buf[BFSIZE];
+                                                        
+                        sprintf( buf, "P %d %d %d\r", rfid_cnt, Result_L.Point, Result_R.Point );
+                         
+                        point_res = OFF;
+                            
+                        pcnt = 0;
+                         
+                        ble_write( buf );
+                    }
+                    else
+                    {
+                        pcnt++;
+ 
+                        if( pcnt >= 1000 / RFID_SAMPLE )   // 1000ms
+                        {           
+                            char buf[BFSIZE];
+                                                                
+                            sprintf( buf, "P %d %d %d\r", rfid_cnt, Result_L.Point, Result_R.Point );
+                                 
+                            point_res = OFF;
+                                    
+                            pcnt = 0;
+                                 
+                            ble_write( buf );     
+                        }
+                    }
+                        
+                    lpsave = Result_L.Point;
+                    rpsave = Result_R.Point;
+                }
+
+#if 0                          
+                pc.printf( "%d %d %d %d %d %d %d\r\n", Result_L.Result_state, 
+                                                       Result_L.Result,
+                                                       Result_L.Result_d1,
+                                                       Result_L.Result_ON_cnt,
+                                                       Result_L.Result_OFF_cnt,
+                                                       Result_L.Result_NON_cnt,
+                                                       Result_L.Point );
+#else                                                       
+                pc.printf( "%d %d %d %d %d %d %d\r\n", Result_R.Result_state, 
+                                                       Result_R.Result,
+                                                       Result_R.Result_d1,
+                                                       Result_R.Result_ON_cnt,
+                                                       Result_R.Result_OFF_cnt,
+                                                       Result_R.Result_NON_cnt,
+                                                       Result_R.Point );                                                            
+#endif                 
+                /////////////////////////////////////////////////////////////////////////////
+ #if 0                 
+                int mem_no = 0x0F;
+                                                   
+                if( ret == UID_OK )
+                {
+                    if( ( uid_H == id_LH[0] ) && ( uid_L == id_LL[0] ) )
+                    {
+                        mem_no = 0x00;
+                    }
+                    else if( ( uid_H == id_LH[1] ) && ( uid_L == id_LL[1] ) )
+                    {
+                        mem_no = 0x01;
+                    }
+                    else if( ( uid_H == id_LH[2] ) && ( uid_L == id_LL[2] ) )
+                    {
+                        mem_no = 0x02;
+                    }
+                    else if( ( uid_H == id_LH[3] ) && ( uid_L == id_LL[3] ) )
+                    {
+                        mem_no = 0x03;
+                    }
+                    else if( ( uid_H == id_RH[0] ) && ( uid_L == id_RL[0] ) )
+                    {
+                        mem_no = 0x04;
+                    }
+                    else if( ( uid_H == id_RH[1] ) && ( uid_L == id_RL[1] ) )
+                    {
+                        mem_no = 0x05;
+                    }
+                    else if( ( uid_H == id_RH[2] ) && ( uid_L == id_RL[2] ) )
+                    {
+                        mem_no = 0x06;
+                    }
+                    else if( ( uid_H == id_RH[3] ) && ( uid_L == id_RL[3] ) )
+                    {
+                        mem_no = 0x07;
+                    }                        
+                    else
+                    {
+                        mem_no = 0x08;
+                    }                                               
+                }
+                else
+                {
+                    mem_no = 0x0F;
+                }
+                    
+                if( rfid_cnt <= MEM_ID_NUM )
+                {
+                    mem_id[rfid_cnt - 1] = mem_no;
+                }
+#endif
+led = 0;
+            }
+            // IDゲットの場合
+            else if( idget_flag == ON )
+            {
+                char ret;
+                char uid[10] = {0};
+                char level;
+                unsigned int uid_H;
+                unsigned int uid_L;
+led = 1;       
+                // RFID取得
+                ret = trf7970a_ID_get( &uid[0], &level );
+led = 0;
+                // RFID取得カウントを設定
+                if( rfid_cnt < 9999 )
+                {
+                    rfid_cnt++;
+                }
+                else
+                {
+                    rfid_cnt = 1;   // Ver12 修正 0->1                  
                 }
 
                 // 解析処理
@@ -299,82 +525,193 @@
                 uid_L |= ( uid[6] <<  8 );
                 uid_L |= ( uid[7] <<  0 );
 
+                level_s = level;
+                uid_H_s = uid_H;
+                uid_L_s = uid_L; 
+
+                pc.printf( "%d %08X %08X %d\r", level, uid_H, uid_L, uid_scnt );
+
                 // 解析
 
-                // IDリターンモード
-                if( idget_flag == ON )
+                if( ret == UID_OK )
                 {
-                    if( ret == UID_OK )
+                    if( ( uid_H == uid_H_d ) && ( uid_L == uid_L_d ) )
                     {
-                        if( ( uid_H == uid_H_d ) && ( uid_L == uid_L_d ) )
+                        uid_scnt++;
+                            
+                        if( uid_scnt >= 250 / RFID_SAMPLE )  // 250ms
                         {
-                            uid_scnt++;
+                            id_G_BYTE[0] = ( char )( ( uid_L >>  0 ) & 0x000000FF );
+                            id_G_BYTE[1] = ( char )( ( uid_L >>  8 ) & 0x000000FF );
+                            id_G_BYTE[2] = ( char )( ( uid_L >> 16 ) & 0x000000FF );
+                            id_G_BYTE[3] = ( char )( ( uid_L >> 24 ) & 0x000000FF );
+
+                            id_G_BYTE[4] = ( char )( ( uid_H >>  0 ) & 0x000000FF );
+                            id_G_BYTE[5] = ( char )( ( uid_H >>  8 ) & 0x000000FF );
+                            id_G_BYTE[6] = ( char )( ( uid_H >> 16 ) & 0x000000FF );
+                            id_G_BYTE[7] = ( char )( ( uid_H >> 24 ) & 0x000000FF );
+
+#if 0  // 赤 左  パンチ
+    trf7970a_ID_write( 0x00, 0x53554745 );  // SUGE
+    trf7970a_ID_write( 0x01, 0x52424F58 );  // RBOX cv
+    trf7970a_ID_write( 0x02, 0x20190311 );  // 製造年月日
+    trf7970a_ID_write( 0x03, 0x01234567 );  // シリアル
+    trf7970a_ID_write( 0x04, 0x00000000 );  // bit16 ( TYPE  0:Punch  1:Gard ) / bit8 ( LR 0:Left 1:Right ) / bit0 ( CORNER 0:Red 1:Blue )
+    trf7970a_ID_write( 0x05, 0x00000000 );  // リザーブ
+    trf7970a_ID_write( 0x06, 0xE0070000 );  // Punch のUID(上位32bit) 
+    trf7970a_ID_write( 0x07, 0x168513BD );  // Punch のUID(下位32bit)
+    trf7970a_ID_write( 0x08, 0xE0070000 );  // Gard  のUID(上位32bit) 
+    trf7970a_ID_write( 0x09, 0x16851161 );  // Gard  のUID(下位32bit)
+#endif
+
+#if 0  // 赤 左  ガード
+    trf7970a_ID_write( 0x00, 0x53554745 );  // SUGE
+    trf7970a_ID_write( 0x01, 0x52424F58 );  // RBOX cv
+    trf7970a_ID_write( 0x02, 0x20190311 );  // 製造年月日
+    trf7970a_ID_write( 0x03, 0x01234567 );  // シリアル
+    trf7970a_ID_write( 0x04, 0x00010000 );  // bit16 ( TYPE  0:Punch  1:Gard ) / bit8 ( LR 0:Left 1:Right ) / bit0 ( CORNER 0:Red 1:Blue )
+    trf7970a_ID_write( 0x05, 0x00000000 );  // リザーブ
+    trf7970a_ID_write( 0x06, 0xE0070000 );  // Punch のUID(上位32bit) 
+    trf7970a_ID_write( 0x07, 0x168513BD );  // Punch のUID(下位32bit)
+    trf7970a_ID_write( 0x08, 0xE0070000 );  // Gard  のUID(上位32bit) 
+    trf7970a_ID_write( 0x09, 0x16851161 );  // Gard  のUID(下位32bit)
+#endif
+
+#if 0  // 赤 右  パンチ
+    trf7970a_ID_write( 0x00, 0x53554745 );  // SUGE
+    trf7970a_ID_write( 0x01, 0x52424F58 );  // RBOX cv
+    trf7970a_ID_write( 0x02, 0x20190311 );  // 製造年月日
+    trf7970a_ID_write( 0x03, 0x01234567 );  // シリアル
+    trf7970a_ID_write( 0x04, 0x00000100 );  // bit16 ( TYPE  0:Punch  1:Gard ) / bit8 ( LR 0:Left 1:Right ) / bit0 ( CORNER 0:Red 1:Blue )
+    trf7970a_ID_write( 0x05, 0x00000000 );  // リザーブ
+    trf7970a_ID_write( 0x06, 0xE0070000 );  // Punch のUID(上位32bit) 
+    trf7970a_ID_write( 0x07, 0x16851169 );  // Punch のUID(下位32bit)
+    trf7970a_ID_write( 0x08, 0xE0070000 );  // Gard  のUID(上位32bit) 
+    trf7970a_ID_write( 0x09, 0x168513CD );  // Gard  のUID(下位32bit)
+#endif
+
+#if 0  // 赤 右  ガード
+    trf7970a_ID_write( 0x00, 0x53554745 );  // SUGE
+    trf7970a_ID_write( 0x01, 0x52424F58 );  // RBOX cv
+    trf7970a_ID_write( 0x02, 0x20190311 );  // 製造年月日
+    trf7970a_ID_write( 0x03, 0x01234567 );  // シリアル
+    trf7970a_ID_write( 0x04, 0x00010100 );  // bit16 ( TYPE  0:Punch  1:Gard ) / bit8 ( LR 0:Left 1:Right ) / bit0 ( CORNER 0:Red 1:Blue )
+    trf7970a_ID_write( 0x05, 0x00000000 );  // リザーブ
+    trf7970a_ID_write( 0x06, 0xE0070000 );  // Punch のUID(上位32bit) 
+    trf7970a_ID_write( 0x07, 0x16851169 );  // Punch のUID(下位32bit)
+    trf7970a_ID_write( 0x08, 0xE0070000 );  // Gard  のUID(上位32bit) 
+    trf7970a_ID_write( 0x09, 0x168513CD );  // Gard  のUID(下位32bit)
+#endif
+
+
+
+#if 0  // 青 左  パンチ
+    trf7970a_ID_write( 0x00, 0x53554745 );  // SUGE
+    trf7970a_ID_write( 0x01, 0x52424F58 );  // RBOX cv
+    trf7970a_ID_write( 0x02, 0x20190311 );  // 製造年月日
+    trf7970a_ID_write( 0x03, 0x01234567 );  // シリアル
+    trf7970a_ID_write( 0x04, 0x00000001 );  // bit16 ( TYPE  0:Punch  1:Gard ) / bit8 ( LR 0:Left 1:Right ) / bit0 ( CORNER 0:Red 1:Blue )
+    trf7970a_ID_write( 0x05, 0x00000000 );  // リザーブ
+    trf7970a_ID_write( 0x06, 0xE0070000 );  // Punch のUID(上位32bit) 
+    trf7970a_ID_write( 0x07, 0x16851176 );  // Punch のUID(下位32bit)
+    trf7970a_ID_write( 0x08, 0xE0070000 );  // Gard  のUID(上位32bit) 
+    trf7970a_ID_write( 0x09, 0x16850FF2 );  // Gard  のUID(下位32bit)
+#endif
+
+#if 0  // 青 左  ガード
+    trf7970a_ID_write( 0x00, 0x53554745 );  // SUGE
+    trf7970a_ID_write( 0x01, 0x52424F58 );  // RBOX cv
+    trf7970a_ID_write( 0x02, 0x20190311 );  // 製造年月日
+    trf7970a_ID_write( 0x03, 0x01234567 );  // シリアル
+    trf7970a_ID_write( 0x04, 0x00010001 );  // bit16 ( TYPE  0:Punch  1:Gard ) / bit8 ( LR 0:Left 1:Right ) / bit0 ( CORNER 0:Red 1:Blue )
+    trf7970a_ID_write( 0x05, 0x00000000 );  // リザーブ
+    trf7970a_ID_write( 0x06, 0xE0070000 );  // Punch のUID(上位32bit) 
+    trf7970a_ID_write( 0x07, 0x16851176 );  // Punch のUID(下位32bit)
+    trf7970a_ID_write( 0x08, 0xE0070000 );  // Gard  のUID(上位32bit) 
+    trf7970a_ID_write( 0x09, 0x16850FF2 );  // Gard  のUID(下位32bit)
+#endif
+
+#if 0  // 青 右  パンチ
+    trf7970a_ID_write( 0x00, 0x53554745 );  // SUGE
+    trf7970a_ID_write( 0x01, 0x52424F58 );  // RBOX cv
+    trf7970a_ID_write( 0x02, 0x20190311 );  // 製造年月日
+    trf7970a_ID_write( 0x03, 0x01234567 );  // シリアル
+    trf7970a_ID_write( 0x04, 0x00000101 );  // bit16 ( TYPE  0:Punch  1:Gard ) / bit8 ( LR 0:Left 1:Right ) / bit0 ( CORNER 0:Red 1:Blue )
+    trf7970a_ID_write( 0x05, 0x00000000 );  // リザーブ
+    trf7970a_ID_write( 0x06, 0xE0070000 );  // Punch のUID(上位32bit) 
+    trf7970a_ID_write( 0x07, 0x16851206 );  // Punch のUID(下位32bit)
+    trf7970a_ID_write( 0x08, 0xE0070000 );  // Gard  のUID(上位32bit) 
+    trf7970a_ID_write( 0x09, 0x1685113B );  // Gard  のUID(下位32bit)
+#endif
+
+#if 0  // 青 右  ガード
+    trf7970a_ID_write( 0x00, 0x53554745 );  // SUGE
+    trf7970a_ID_write( 0x01, 0x52424F58 );  // RBOX cv
+    trf7970a_ID_write( 0x02, 0x20190311 );  // 製造年月日
+    trf7970a_ID_write( 0x03, 0x01234567 );  // シリアル
+    trf7970a_ID_write( 0x04, 0x00010101 );  // bit16 ( TYPE  0:Punch  1:Gard ) / bit8 ( LR 0:Left 1:Right ) / bit0 ( CORNER 0:Red 1:Blue )
+    trf7970a_ID_write( 0x05, 0x00000000 );  // リザーブ
+    trf7970a_ID_write( 0x06, 0xE0070000 );  // Punch のUID(上位32bit) 
+    trf7970a_ID_write( 0x07, 0x16851206 );  // Punch のUID(下位32bit)
+    trf7970a_ID_write( 0x08, 0xE0070000 );  // Gard  のUID(上位32bit) 
+    trf7970a_ID_write( 0x09, 0x1685113B );  // Gard  のUID(下位32bit)
+#endif
+
+                            ret = UID_OK;
                             
-                            if( uid_scnt >= 20 )  // 1秒
+                            for( int i=0; i<10; i++ )
+                            {
+                                ret = trf7970a_ID_read( i, &Iccard_mem[i] );
+                                
+                                pc.printf( "%08X \r", Iccard_mem[i] );
+                                
+                                if( ret == UID_NG )
+                                {
+                                    break;
+                                }
+                            }
+                              
+                            if( ret == UID_OK )
                             {
                                 char buf[BFSIZE];
-                                
+                                    
                                 sprintf( buf, "U %08X %08X\r", uid_H, uid_L );
- 
+     
                                 ble_write( buf );
- 
+     
                                 pc.printf( "U %08X %08X\r", uid_H, uid_L );
- 
+     
+                                trf7970a_reg0A_dat = 0x40;  // 感度を戻す。
+                                trf7970a_init();
+
                                 idget_flag = OFF;
                             }
+                            else
+                            {
+                                uid_scnt = 0;
+                                
+                                pc.printf( "IC CARD READ NG\r" );
+                            }
                         }
-                        else
-                        {
-                            uid_scnt = 0;
-                        }
-
-                        uid_H_d = uid_H;
-                        uid_L_d = uid_L;
                     }
                     else
                     {
-                        uid_H = 0x00000000;
-                        uid_L = 0x00000000;
-                        uid_H_d = 0xFFFFFFFF;
-                        uid_L_d = 0xFFFFFFFF;
+                        uid_scnt = 0;
+                    }
 
-                        uid_scnt = 0;
-                     }
+                    uid_H_d = uid_H;
+                    uid_L_d = uid_L;
                 }
-                // 通常モード
                 else
                 {
-                    unsigned int pret_L;
-                    unsigned int pret_R;
-
-                    pret_L = point_result_proc( &Result_L, ret, uid_H, uid_L, id_LH, id_LL );
-                    pret_R = point_result_proc( &Result_R, ret, uid_H, uid_L, id_RH, id_RL );
-
-                    if( ( pret_L == ON ) || ( pret_R == ON ) )
-                    {
-test = 1;
-                        char buf[BFSIZE];
-                                
-                        sprintf( buf, "P %d %d %d\r", rfid_cnt, Result_L.Point, Result_R.Point );
- 
-                        ble_write( buf );
-                                
-                        pc.printf( "P %d %d %d\r", rfid_cnt, Result_L.Point, Result_R.Point );
-                        
-                        resp_cnt = 0;
-test = 0;
-                    }
+                    uid_H = 0x00000000;
+                    uid_L = 0x00000000;
+                    uid_H_d = 0xFFFFFFFF;
+                    uid_L_d = 0xFFFFFFFF;
+                    
+                    uid_scnt = 0;
                 }
             }
-
-            if( interval_cnt >= ( interval - 1 ) )
-            {
-                interval_cnt = 0;
-            }
-            else
-            {
-                interval_cnt++;
-            } 
         }
         
         ble_uart.waitForEvent();
@@ -385,7 +722,7 @@
 {
     uint8_t linebf_irq[BFSIZE];
     
-    printf( (char *)linebf_irq, "%s", dat);
+    sprintf( (char *)linebf_irq, "%s", dat);
     
     int len = 0;
     
@@ -408,26 +745,51 @@
     
     if( ble_connect_flag == true )
     {
+        //NVIC_DisableIRQ(RADIO_IRQn);   // 割り込み禁止
+        
+        bletx_mutex.lock();
+        
         ble_uart.updateCharacteristicValue(
         uartServicePtr->getRXCharacteristicHandle(),
         linebf_irq,
         len);
+        
+        bletx_mutex.unlock();
+        
+        //NVIC_EnableIRQ(RADIO_IRQn);    // 割り込み許可
     }   
 }
 
 void onDataWritten_action(const GattWriteCallbackParams *params)
 {
+    NVIC_DisableIRQ(RADIO_IRQn);   // 割り込み禁止
+    
     if ((uartServicePtr != NULL) &&
             (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
         strcpy((char *)rx_buf, (const char *)params->data);
         trigger_transmit = true;
     }
+    
+    NVIC_EnableIRQ(RADIO_IRQn);    // 割り込み許可
 }
 
 void ConnectionCallback(const Gap::ConnectionCallbackParams_t *params)
 {
     pc.printf("Connected!\n\r");
     ble_connect_flag = true; // BLEコネクトフラグ
+    //led = 0;
+    
+#ifdef UNCONNECT_SLEEP  
+    // WAKE
+    if( sleep_flag == ON )
+    {
+        sleep_flag = OFF;
+                            
+        rfid_en1 = 1;
+                            
+        trf7970a_init();    // RFID 初期化処理
+    }
+#endif       
 }
 
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
@@ -436,6 +798,19 @@
     pc.printf("Restarting the advertising process\r\n");
     ble_connect_flag = false; // BLEコネクトフラグ
     ble_uart.startAdvertising();
+    //led = 1;
+    
+#ifdef UNCONNECT_SLEEP     
+    sleep_flag = ON;
+                            
+    rfid_en1 = 0;
+#endif
+
+    // Ver12 追加
+     start_flag = OFF;
+     idget_flag = OFF;
+     pause_flag = OFF;                       
+     point_res = OFF;
 }
 
 /****************************************************************************/
@@ -491,7 +866,7 @@
 
     // SPI フォーマット設定
     rfid.format( 8, 1 );
-    rfid.frequency( 4000000 );
+    rfid.frequency( 10000000 ); // Ver12
 
     rfid_cs = CHIP_SEL_OFF; // チップセレクト OFF
 
@@ -539,7 +914,13 @@
 
     wait(0.001);
 
+// Ver12
+#if 0
     trf7970a_set(  0x09, 0x00, 2);  // [Address-W]  Modulator and SYS_CLK control
+#else
+    trf7970a_set(  0x09, trf7970a_reg09_dat, 2);  // [Address-W]  Modulator and SYS_CLK control
+#endif
+
     trf7970a_set(  0x99, NOND, 1);  // [Command]    Test external RF (RSSI at RX input with TX on)
 
     wait(0.001);
@@ -548,11 +929,21 @@
     //pc.printf( "0x4F = %02X\n", rdat[2] );
     //pc.printf( "\n" );
 
-    trf7970a_set(  0x00, 0x20, 2);  // [Address-W]  Chip status control
-
+    // 3.3V 
+    //trf7970a_set(  0x00, 0x20, 2);  // [Address-W]  Chip status control
+    // 5V
+    trf7970a_set(  0x00, 0x21, 2);  // [Address-W]  Chip status control
+    
     trf7970a_set(  0x01, 0x02, 2);  // [Address-W]  ISO control
+
+// Ver12    
+#if 0    
     trf7970a_set(  0x0A, 0x40, 2);  // [Address-W]  RX special setting
-    trf7970a_set(  0x0B, 0x00, 2);  // [Address-W]  Regulator and I/O control
+    trf7970a_set(  0x0B, 0x07, 2);  // [Address-W]  Regulator and I/O control
+#else
+    trf7970a_set(  0x0A, trf7970a_reg0A_dat, 2);  // [Address-W]  RX special setting
+    trf7970a_set(  0x0B, trf7970a_reg0B_dat, 2);  // [Address-W]  Regulator and I/O control    
+#endif
     trf7970a_set(  0x14, 0x00, 2);  // [Address-W]  Adjustable FIFO IRQ levels
     trf7970a_set(  0x18, 0x0F, 2);  // [Address-W]  NFC target detection level
 
@@ -605,32 +996,397 @@
     *level = ( trf7970a_get( 0x4F ) & 0x07 ) + 1;   // [Address-R]  RSSI levels and oscillator status
     char uid_length = trf7970a_get( 0x5C );     // [Address-R]  FIFO status
 
-    //pc.printf( "%02X ", irq_status );
-    //pc.printf( "%02X \r", uid_length );
+    pc.printf( "%02X ", irq_status );
+    pc.printf( "%02X ", uid_length );
 
     if( uid_length == UID_LEN )
     {
         char uid[10];
-
+    
         rfid_cs = CHIP_SEL_ON;
-
+    
         rfid.write( 0x7F );     // [Address-R]  FIFO Read Continuous
-
+    
         for( k=0; k<UID_LEN; k++ )
         {
             uid[UID_LEN-k-1] = rfid.write( 0x00 );
         }
-
+    
         rfid_cs = CHIP_SEL_OFF;
-
+    
         trf7970a_set(  0x8F, NOND, 1 ); // [Command]    Reset FIFO
-
+    
         for( k=0; k<UID_LEN; k++ )
         {
             *dat++ = uid[k];
         }
 
-        return( UID_OK );
+        // Ver12    
+        if( irq_status == 0xC0 )
+        {
+            return( UID_OK );
+        }
+        else
+        {
+            return( UID_NG );
+        }
+    }
+    else
+    {
+        *level = 0;
+        return( UID_NG );
+    }
+}
+
+/****************************************************************************/
+/*  関数名 : trf7970a_ID_check                                              */
+/*  概要   : RFID ID チェック処理                                            */
+/*  作成者 : JPMS H.Harada                                                  */
+/*  作成日 : 2019.03.08                                                     */
+/****************************************************************************/
+void trf7970a_ID_check(char *level)
+{
+// 1 相手の左パンチ ////////////////////////////////////////////////////
+    rfid_cs = CHIP_SEL_ON;
+
+    rfid.write( 0x8F ); // [Command]    Reset FIFO
+    rfid.write( 0x91 ); // [Command]    Transmission with CRC
+    rfid.write( 0x3D );
+    rfid.write( 0x00 );
+    rfid.write( 0xB0 );
+    rfid.write( 0x22 );
+    rfid.write( 0x20 );
+    rfid.write( id_L_BYTE[0][0] );
+    rfid.write( id_L_BYTE[0][1] );
+    rfid.write( id_L_BYTE[0][2] );
+    rfid.write( id_L_BYTE[0][3] );
+    rfid.write( id_L_BYTE[0][4] );
+    rfid.write( id_L_BYTE[0][5] );
+    rfid.write( id_L_BYTE[0][6] );
+    rfid.write( id_L_BYTE[0][7] );
+    rfid.write( 0x00 );
+                           
+    rfid_cs = CHIP_SEL_OFF;
+
+    wait(0.007);
+
+    char irq_status1 = trf7970a_get( 0x4C );         // [Address-R]  IRQ status
+    char level1 = ( trf7970a_get( 0x4F ) & 0x07 ) + 1;   // [Address-R]  RSSI levels and oscillator status
+    char uid_length1 = trf7970a_get( 0x5C );     // [Address-R]  FIFO status
+
+    //pc.printf( "%02X ", irq_status1 );
+    //pc.printf( "%02X ", uid_length1 );
+    
+    if( irq_status1 == 0xE0 )
+    {
+        if( uid_length1 == 5 )
+        {
+            //pc.printf( "%d ", level1 );
+            
+            *level++ = level1;
+        }
+        else
+        {
+            //pc.printf( "0 " );
+            
+            *level++ = 0;      
+        }
+    }
+    else
+    {
+        //pc.printf( "- " );
+        
+        *level++ = 9;
+    }
+    
+// 2 相手の右パンチ /////////////////////////////////////////////////////////////////////////
+    rfid_cs = CHIP_SEL_ON;
+
+    rfid.write( 0x8F ); // [Command]    Reset FIFO
+    rfid.write( 0x91 ); // [Command]    Transmission with CRC
+    rfid.write( 0x3D );
+    rfid.write( 0x00 );
+    rfid.write( 0xB0 );
+    rfid.write( 0x22 );
+    rfid.write( 0x20 );
+    rfid.write( id_R_BYTE[0][0] );
+    rfid.write( id_R_BYTE[0][1] );
+    rfid.write( id_R_BYTE[0][2] );
+    rfid.write( id_R_BYTE[0][3] );
+    rfid.write( id_R_BYTE[0][4] );
+    rfid.write( id_R_BYTE[0][5] );
+    rfid.write( id_R_BYTE[0][6] );
+    rfid.write( id_R_BYTE[0][7] );
+    rfid.write( 0x00 );
+                           
+    rfid_cs = CHIP_SEL_OFF;
+
+    wait(0.007);
+
+    char irq_status2 = trf7970a_get( 0x4C );         // [Address-R]  IRQ status
+    char level2 = ( trf7970a_get( 0x4F ) & 0x07 ) + 1;   // [Address-R]  RSSI levels and oscillator status
+    char uid_length2 = trf7970a_get( 0x5C );     // [Address-R]  FIFO status
+
+    //pc.printf( "%02X ", irq_status2 );
+    //pc.printf( "%02X ", uid_length2 );
+    
+    if( irq_status2 == 0xE0 )
+    {
+        if( uid_length2 == 5 )
+        {
+            //pc.printf( "%d ", level2 );
+            
+            *level++ = level2;
+        }
+        else
+        {
+            //pc.printf( "0 " );
+            
+            *level++ = 0;      
+        }
+    }
+    else
+    {
+        //pc.printf( "- " );
+        
+        *level++ = 9;
+    }
+    
+// 3 自分の左ガード /////////////////////////////////////////////////////////////////////////
+    rfid_cs = CHIP_SEL_ON;
+
+    rfid.write( 0x8F ); // [Command]    Reset FIFO
+    rfid.write( 0x91 ); // [Command]    Transmission with CRC
+    rfid.write( 0x3D );
+    rfid.write( 0x00 );
+    rfid.write( 0xB0 );
+    rfid.write( 0x22 );
+    rfid.write( 0x20 );
+    rfid.write( id_L_BYTE[3][0] );
+    rfid.write( id_L_BYTE[3][1] );
+    rfid.write( id_L_BYTE[3][2] );
+    rfid.write( id_L_BYTE[3][3] );
+    rfid.write( id_L_BYTE[3][4] );
+    rfid.write( id_L_BYTE[3][5] );
+    rfid.write( id_L_BYTE[3][6] );
+    rfid.write( id_L_BYTE[3][7] );
+    rfid.write( 0x00 );
+                           
+    rfid_cs = CHIP_SEL_OFF;
+
+    wait(0.007);
+
+    char irq_status3 = trf7970a_get( 0x4C );         // [Address-R]  IRQ status
+    char level3 = ( trf7970a_get( 0x4F ) & 0x07 ) + 1;   // [Address-R]  RSSI levels and oscillator status
+    char uid_length3 = trf7970a_get( 0x5C );     // [Address-R]  FIFO status
+
+    //pc.printf( "%02X ", irq_status3 );
+    //pc.printf( "%02X ", uid_length3 );
+    
+    if( irq_status3 == 0xE0 )
+    {
+        if( uid_length3 == 5 )
+        {
+            //pc.printf( "%d ", level3 );
+            
+            *level++ = level3;
+        }
+        else
+        {
+            //pc.printf( "0 " );
+            
+            *level++ = 0;      
+        }
+    }
+    else
+    {
+        //pc.printf( "- " );
+        
+        *level++ = 9;
+    }
+
+// 4 自分の右ガード /////////////////////////////////////////////////////////////////////////
+    rfid_cs = CHIP_SEL_ON;
+
+    rfid.write( 0x8F ); // [Command]    Reset FIFO
+    rfid.write( 0x91 ); // [Command]    Transmission with CRC
+    rfid.write( 0x3D );
+    rfid.write( 0x00 );
+    rfid.write( 0xB0 );
+    rfid.write( 0x22 );
+    rfid.write( 0x20 );
+    rfid.write( id_R_BYTE[3][0] );
+    rfid.write( id_R_BYTE[3][1] );
+    rfid.write( id_R_BYTE[3][2] );
+    rfid.write( id_R_BYTE[3][3] );
+    rfid.write( id_R_BYTE[3][4] );
+    rfid.write( id_R_BYTE[3][5] );
+    rfid.write( id_R_BYTE[3][6] );
+    rfid.write( id_R_BYTE[3][7] );
+    rfid.write( 0x00 );
+                           
+    rfid_cs = CHIP_SEL_OFF;
+
+    wait(0.007);
+
+    char irq_status4 = trf7970a_get( 0x4C );         // [Address-R]  IRQ status
+    char level4 = ( trf7970a_get( 0x4F ) & 0x07 ) + 1;   // [Address-R]  RSSI levels and oscillator status
+    char uid_length4 = trf7970a_get( 0x5C );     // [Address-R]  FIFO status 
+    
+    //pc.printf( "%02X ", irq_status4 );
+    //pc.printf( "%02X ", uid_length4 );
+    
+    if( irq_status4 == 0xE0 )
+    {
+        if( uid_length4 == 5 )
+        {
+            //pc.printf( "%d     ", level4 );
+            
+            *level++ = level4;
+        }
+        else
+        {
+            //pc.printf( "0     " );
+            
+            *level++ = 0;      
+        }
+    }
+    else
+    {
+        //pc.printf( "-     " );
+        
+        *level++ = 9;
+    }
+    
+/////////////////////////////////////////////////////////
+}
+
+/****************************************************************************/
+/*  関数名 : trf7970a_ID_write                                              */
+/*  概要   : RFID ID ライト処理                                              */
+/*  作成者 : JPMS H.Harada                                                  */
+/*  作成日 : 2019.03.12                                                     */
+/****************************************************************************/
+void trf7970a_ID_write( char adr, unsigned int dat )
+{
+    char wdat[4];
+    
+    wdat[0] = ( char )( ( dat >>  0 ) & 0x000000FF );
+    wdat[1] = ( char )( ( dat >>  8 ) & 0x000000FF );
+    wdat[2] = ( char )( ( dat >> 16 ) & 0x000000FF );
+    wdat[3] = ( char )( ( dat >> 24 ) & 0x000000FF );
+    
+    pc.printf( "%02X %02X %02X %02X \r", wdat[3], wdat[2], wdat[1], wdat[0] );
+    
+    rfid_cs = CHIP_SEL_ON;
+
+    rfid.write( 0x8F ); // [Command]    Reset FIFO
+    rfid.write( 0x91 ); // [Command]    Transmission with CRC
+    rfid.write( 0x3D );
+    rfid.write( 0x00 );
+    rfid.write( 0xF0 );
+    rfid.write( 0x62 );
+    rfid.write( 0x21 );
+    rfid.write( id_G_BYTE[0] );
+    rfid.write( id_G_BYTE[1] );
+    rfid.write( id_G_BYTE[2] );
+    rfid.write( id_G_BYTE[3] );
+    rfid.write( id_G_BYTE[4] );
+    rfid.write( id_G_BYTE[5] );
+    rfid.write( id_G_BYTE[6] );
+    rfid.write( id_G_BYTE[7] );
+    rfid.write( adr );
+    rfid.write( wdat[3] );
+    rfid.write( wdat[2] );
+    rfid.write( wdat[1] );
+    rfid.write( wdat[0] );
+                           
+    rfid_cs = CHIP_SEL_OFF;
+
+    wait(0.007);
+
+    char irq_status = trf7970a_get( 0x4C );         // [Address-R]  IRQ status
+    char uid_length = trf7970a_get( 0x5C );     // [Address-R]  FIFO status
+
+    pc.printf( "W %02X ", irq_status );
+    pc.printf( "%02X \r", uid_length );
+}
+
+/****************************************************************************/
+/*  関数名 : trf7970a_ID_read                                               */
+/*  概要   : RFID ID リード処理                                              */
+/*  作成者 : JPMS H.Harada                                                  */
+/*  作成日 : 2019.03.12                                                     */
+/****************************************************************************/
+char trf7970a_ID_read( char adr, unsigned int *dat )
+{
+    unsigned int k;
+
+    rfid_cs = CHIP_SEL_ON;
+
+    rfid.write( 0x8F ); // [Command]    Reset FIFO
+    rfid.write( 0x91 ); // [Command]    Transmission with CRC
+    rfid.write( 0x3D );
+    rfid.write( 0x00 );
+    rfid.write( 0xB0 );
+    rfid.write( 0x22 );
+    rfid.write( 0x20 );
+    rfid.write( id_G_BYTE[0] );
+    rfid.write( id_G_BYTE[1] );
+    rfid.write( id_G_BYTE[2] );
+    rfid.write( id_G_BYTE[3] );
+    rfid.write( id_G_BYTE[4] );
+    rfid.write( id_G_BYTE[5] );
+    rfid.write( id_G_BYTE[6] );
+    rfid.write( id_G_BYTE[7] );
+    rfid.write( adr );
+                           
+    rfid_cs = CHIP_SEL_OFF;
+
+    wait(0.007);
+
+    char irq_status = trf7970a_get( 0x4C );         // [Address-R]  IRQ status
+    char level = ( trf7970a_get( 0x4F ) & 0x07 ) + 1;   // [Address-R]  RSSI levels and oscillator status
+    char uid_length = trf7970a_get( 0x5C );     // [Address-R]  FIFO status
+
+    pc.printf( "R %d %02X ", adr, irq_status );
+    pc.printf( "%02X ", level );
+    pc.printf( "%02X ", uid_length );
+
+    if( uid_length == 5 )
+    {
+        char uid[10];  
+        
+        rfid_cs = CHIP_SEL_ON;
+    
+        rfid.write( 0x7F );     // [Address-R]  FIFO Read Continuous
+    
+        for( k=0; k<5; k++ )
+        {
+            uid[k] = rfid.write( 0x00 );
+        }
+    
+        rfid_cs = CHIP_SEL_OFF;
+    
+        trf7970a_set(  0x8F, NOND, 1 ); // [Command]    Reset FIFO
+    
+        unsigned int rdat = 0x00000000;
+        
+        rdat = uid[4];
+        rdat |= ( uid[3] <<  8 );
+        rdat |= ( uid[2] << 16 );
+        rdat |= ( uid[1] << 24 );
+        
+        *dat = rdat;
+    
+        if( irq_status == 0xE0 )
+        {
+            return( UID_OK );
+        }
+        else
+        {
+            return( UID_NG );
+        }
     }
     else
     {
@@ -786,8 +1542,35 @@
     return(ret_data);
 }
 
+#define CMD_NUM     25
+
 char str[16][10];
-char str2[10][8] = { "START  ", "IDGET  ", "STOP   ", "ISETL  ", "ISETR  ", "INTVL  ", "NTC    ", "OTC    ", "       ", "       " };
+char str2[CMD_NUM][8] = {   "START  ",  // 0
+                            "IDGET  ",  // 1
+                            "STOP   ",  // 2
+                            "L      ",  // 3
+                            "R      ",  // 4
+                            "TS     ",  // 5
+                            "TC     ",  // 6
+                            "MMR    ",  // 7
+                            "MES    ",  // 8
+                            "RSSI   ",  // 9
+                            "READ   ",  // 10
+                            "PRES   ",  // 11
+                            "A      ",  // 12
+                            "B      ",  // 13
+                            "C      ",  // 14
+                            "D      ",  // 15
+                            "E      ",  // 16
+                            "F      ",  // 17
+                            "G      ",  // 18
+                            "H      ",  // 19
+                            "PAUSE  ",  // 20
+                            "REG09  ",  // 21 Ver12
+                            "REG0A  ",  // 22 Ver12
+                            "REG0B  ",  // 23 Ver12
+                            "ICMR   "   // 24 Ver12                          
+                    };
 
 /****************************************************************************/
 /*  関数名 : ble_cmd                                                        */
@@ -804,20 +1587,27 @@
     char buf[BFSIZE];
     int cc = 0;
     int len = 0;
+    
+//pc.printf( "%s", (const char*)rx_buf );
           
     sprintf( buf, "%s", (const char*)rx_buf );
-                   
+                  
     for( i=0; i<NUM_ONCE; i++ )
     {
-        if( rx_buf[i] >= ' ' || rx_buf[i] == '\r' || rx_buf[i] == '\n' )
+        if( ( rx_buf[i] >= 0x20 ) && ( rx_buf[i] < 0x7F ) )
         {
             buf[cc++] = (char)rx_buf[i];
         }
+        else if( rx_buf[i] == '\r' )
+        {
+            buf[cc++] = (char)rx_buf[i];
+            break;       
+        }
     }
       
     len = cc;
             
-    pc.printf( "%d %s\r", len, buf );
+//pc.printf( "%d %s  ", len, buf );
 
     for( i=0; i<len; i++ )
     {
@@ -892,7 +1682,7 @@
                     int ret;
                     int cmd_no = 0xFF;
 
-                    for( j=0; j<10; j++ )
+                    for( j=0; j<CMD_NUM; j++ )
                     {
                         ret = 1;
 
@@ -914,30 +1704,57 @@
                         }
                     }
                     
-pc.printf( "CMD_NO = %d\r", cmd_no );
+//pc.printf( "CMD_NO = %d  ", cmd_no );
                     
                     // コマンド START
                     if( cmd_no == 0 )
                     {
-                        rfid_cnt = 0;
+                        // ポーズで停止場合は、カウントをクリアしない。 ただし、得点はクリアする。
+                        if( pause_flag == OFF )
+                        {
+                            rfid_cnt = 0;
+                            
+                            // ログの初期化
+                            for( int i=0; i<MEM_ID_NUM; i++ )
+                            {
+                                mem_id[i] = 0;
+                            }
+                        }
+                        
                         start_flag = ON;
                         idget_flag = OFF;
+                        pause_flag = OFF;
+                        
+                        point_res = ON;
 
-                        Result_L.Result_pulse = OFF;
+                        Result_L.Result_state = STATE_WAIT_OFF;
                         Result_L.Result = OFF;
                         Result_L.Result_d1 = OFF;
                         Result_L.Result_ON_cnt = 0;
                         Result_L.Result_OFF_cnt = 0;
+                        Result_L.Result_NON_cnt = 0;
                         Result_L.Point = 0;
 
-                        Result_R.Result_pulse = OFF;
+                        Result_R.Result_state = STATE_WAIT_OFF;
                         Result_R.Result = OFF;
                         Result_R.Result_d1 = OFF;
                         Result_R.Result_ON_cnt = 0;
                         Result_R.Result_OFF_cnt = 0;
+                        Result_R.Result_NON_cnt = 0;
                         Result_R.Point = 0;
-                        
-                        ble_write( "START OK\r" );
+                     
+                    #ifdef SLEEP_MODE   
+                        // WAKE
+                        if( sleep_flag == ON )
+                        {
+                            sleep_flag = OFF;
+                            
+                            rfid_en1 = 1;
+                            
+                            trf7970a_init();    // RFID 初期化処理
+                        }                      
+                    #endif  
+                        ble_write( "START\r" );
                         pc.printf( "START OK\r" );
                     }
                     // コマンド IDGET
@@ -953,8 +1770,24 @@
                         uid_L_d = 0xFFFFFFFF;
 
                         uid_scnt = 0;
-                        
-                        ble_write( "IDGET OK\r" );
+
+                        // Ver12
+                        trf7970a_reg0A_dat = 0x4C;  // 電波が混線しなように感度を下げる。
+                        trf7970a_init();
+
+                    #ifdef SLEEP_MODE
+                        // WAKE
+                        if( sleep_flag == ON )
+                        {
+                            sleep_flag = OFF;
+                            
+                            rfid_en1 = 1;
+                            
+                            trf7970a_init();    // RFID 初期化処理
+                        }
+                    #endif                   
+                                                
+                        ble_write( "IDGET\r" );
                         pc.printf( "IDGET OK\r" );
                     }
                     // コマンド  STOP
@@ -962,69 +1795,414 @@
                     {
                         start_flag = OFF;
                         idget_flag = OFF;
+                        pause_flag = OFF;
                         
-                        ble_write( "STOP OK\r" );
+                        point_res = OFF;
+                    
+                    #if SLEEP_MODE    
+                        // SLEEP
+                        sleep_flag = ON;                  
+                        rfid_en1 = 0;
+                    #endif
+                          
+                        ble_write( "STOP\r" );
                         pc.printf( "STOP OK\r" );
                     }
-                    // コマンド  ISETA
+                    // コマンド  L (ISETL)
                     else if( cmd_no == 3 )
                     {
-                        id_LH = atoh(str[1], strlen(str[1]));
-                        id_LL = atoh(str[2], strlen(str[2]));
-
-                        pc.printf( "ISETL = %08X %08X\r\n", id_LH, id_LL );
+                        int no = atow(str[1], strlen(str[1]));
                         
-                        ble_write( "ISETL OK\r" );
+                        if( no == 0 )
+                        {                         
+                            id_LH[0] = atoh(str[2], strlen(str[2]));
+                            id_LL[0] = atoh(str[3], strlen(str[3]));
+                            
+                            pc.printf( "ISETL[%d] = %08X %08X\r\n", no, id_LH[0], id_LL[0] );
+                        }
+                        else if( no == 1 )
+                        {
+                            id_LH[1] = atoh(str[2], strlen(str[2]));
+                            id_LL[1] = atoh(str[3], strlen(str[3]));
+                            
+                            pc.printf( "ISETL[%d] = %08X %08X\r\n", no, id_LH[1], id_LL[1] );
+                        }
+                        else if( no == 2 )
+                        {
+                            id_LH[2] = atoh(str[2], strlen(str[2]));
+                            id_LL[2] = atoh(str[3], strlen(str[3]));
+                            
+                            pc.printf( "ISETL[%d] = %08X %08X\r\n", no, id_LH[2], id_LL[2] );
+                        }
+                        else if( no == 3 )
+                        {
+                            id_LH[3] = atoh(str[2], strlen(str[2]));
+                            id_LL[3] = atoh(str[3], strlen(str[3]));
+                            
+                            pc.printf( "ISETL[%d] = %08X %08X\r\n", no, id_LH[3], id_LL[3] );
+                        }
+                        
+                        ble_write( "ISETL\r" );
                     }
-                    // コマンド  ISETB
+                    // コマンド  R (ISETR)
                     else if( cmd_no == 4 )
                     {
-                        id_RH = atoh(str[1], strlen(str[1]));
-                        id_RL = atoh(str[2], strlen(str[2]));
-
-                        pc.printf( "ISETR = %08X %08X\r\n", id_RH, id_RL );
+                        int no = atow(str[1], strlen(str[1]));
                         
-                        ble_write( "ISETR OK\r" );                      
+                        if( no == 0 )
+                        {
+                            id_RH[0] = atoh(str[2], strlen(str[2]));
+                            id_RL[0] = atoh(str[3], strlen(str[3]));
+                            
+                            pc.printf( "ISETR[%d] = %08X %08X\r\n", no, id_RH[0], id_RL[0] );
+                        }
+                        else if( no == 1 )
+                        {
+                            id_RH[1] = atoh(str[2], strlen(str[2]));
+                            id_RL[1] = atoh(str[3], strlen(str[3]));
+                            
+                            pc.printf( "ISETR[%d] = %08X %08X\r\n", no, id_RH[1], id_RL[1] );
+                        } 
+                        else if( no == 2 )
+                        {
+                            id_RH[2] = atoh(str[2], strlen(str[2]));
+                            id_RL[2] = atoh(str[3], strlen(str[3]));
+                            
+                            pc.printf( "ISETR[%d] = %08X %08X\r\n", no, id_RH[2], id_RL[2] );
+                        } 
+                        else if( no == 3 )
+                        {
+                            id_RH[3] = atoh(str[2], strlen(str[2]));
+                            id_RL[3] = atoh(str[3], strlen(str[3]));
+                            
+                            pc.printf( "ISETR[%d] = %08X %08X\r\n", no, id_RH[3], id_RL[3] );
+                        } 
+                          
+                        ble_write( "ISETR\r" );                      
                     }
 
-                    // コマンド INTVL
+                    // コマンド TS
                     else if( cmd_no == 5 )
                     {
-                        // インターバル
-                        interval = atow(str[1], strlen(str[1]));
+                        touch_start = atow(str[1], strlen(str[1])) / RFID_SAMPLE;
                         
-                        ble_write( "INTVL OK\r" );
-                        pc.printf( "INTVL OK\r" );
+                        ble_write( "TS\r" );
+                        pc.printf( "TS OK\r" );                        
                     }
-                    // コマンド  NTC
+                    // コマンド  TC
                     else if( cmd_no == 6 )
                     {
                         // 非接触カウント
-                        not_touch_cnt = atow(str[1], strlen(str[1]));
+                        not_touch_cnt = atow(str[1], strlen(str[1])) / RFID_SAMPLE;
+                        touch_cnt = atow(str[2], strlen(str[2])) / RFID_SAMPLE;
                         
-                        ble_write( "NTC OK\r" );
-                        pc.printf( "NTC OK\r" );
+                        ble_write( "TC\r" );
+                        pc.printf( "TC OK\r" );
                     }
 
-                    // コマンド  OTC
+                    // コマンド MMR
                     else if( cmd_no == 7 )
                     {
-                        // 接触カウント
-                        touch_cnt = atow(str[1], strlen(str[1]));
+                        int no = atow(str[1], strlen(str[1]));
+                        
+                        unsigned int rmd1 = 0x00000000;
+                        unsigned int rmd2 = 0x00000000;
                         
-                        ble_write( "OTC OK\r" );
-                        pc.printf( "OTC OK\r" );
+                        if( no <= rfid_cnt - 16 )
+                        {
+                            rmd1 |= ( mem_id[ no + 0 - 1 ] & 0x0F ) << 0;
+                            rmd1 |= ( mem_id[ no + 1 - 1 ] & 0x0F ) << 4;
+                            rmd1 |= ( mem_id[ no + 2 - 1 ] & 0x0F ) << 8;
+                            rmd1 |= ( mem_id[ no + 3 - 1 ] & 0x0F ) << 12;
+                            rmd1 |= ( mem_id[ no + 4 - 1 ] & 0x0F ) << 16;
+                            rmd1 |= ( mem_id[ no + 5 - 1 ] & 0x0F ) << 20;
+                            rmd1 |= ( mem_id[ no + 6 - 1 ] & 0x0F ) << 24;
+                            rmd1 |= ( mem_id[ no + 7 - 1 ] & 0x0F ) << 28;
+                            
+                            rmd2 |= ( mem_id[ no + 8 - 1 ] & 0x0F ) << 0;
+                            rmd2 |= ( mem_id[ no + 9 - 1 ] & 0x0F ) << 4;
+                            rmd2 |= ( mem_id[ no + 10 - 1 ] & 0x0F ) << 8;
+                            rmd2 |= ( mem_id[ no + 11 - 1 ] & 0x0F ) << 12;
+                            rmd2 |= ( mem_id[ no + 12 - 1 ] & 0x0F ) << 16;
+                            rmd2 |= ( mem_id[ no + 13 - 1 ] & 0x0F ) << 20;
+                            rmd2 |= ( mem_id[ no + 14 - 1 ] & 0x0F ) << 24;
+                            rmd2 |= ( mem_id[ no + 15 - 1 ] & 0x0F ) << 28;
+                        }
+                        else
+                        {
+                            rmd1 = 0xEEEEEEEE;
+                            rmd2 = 0xEEEEEEEE;
+                        }
+                        
+                        char buf[BFSIZE];
+                        sprintf( buf, "X %08X %08X\r", rmd2, rmd1 );
+                        
+                        ble_write( buf );
                     }
 
-                    // コマンド
+                    // コマンド MES
                     else if( cmd_no == 8 )
                     {
-
+                        char buf[BFSIZE];
+                                
+                        //sprintf( buf, "M %d%08X%08X\r", level_s, uid_H_s, uid_L_s );
+                        sprintf( buf, "%d %08X %08X\r", level_s, uid_H_s, uid_L_s );
+ 
+                        ble_write( buf );                       
                     }
-                    // コマンド
+                    // コマンド RSSI
                     else if( cmd_no == 9 )
                     {
+                        ble_write( "RSSI\r" );
+                    }
+                    // コマンド READ
+                    else if( cmd_no == 10 )
+                    {
+                        char buf[BFSIZE];
+                                
+                        sprintf( buf, "P %d %d %d\r", rfid_cnt, Result_L.Point, Result_R.Point );
+ 
+                        ble_write( buf );
+                        
+                        //pc.printf("%d \r", rfid_cnt);
+                    }
+                    // コマンド PRES
+                    else if( cmd_no == 11 )
+                    {
+                        point_res = ON;
+                    }
+                    
+                    
+                    // コマンド  A  ISETL 0
+                    else if( cmd_no == 12 )
+                    {
+                        id_LH[0] = atoh(str[1], strlen(str[1]));
+                        id_LL[0] = atoh(str[2], strlen(str[2]));
+                        
+                        // Ver12 //////////////////////////////////////////////////////
+                        id_L_BYTE[0][4] = ( char )( ( id_LH[0] >>  0 ) & 0x000000FF );
+                        id_L_BYTE[0][5] = ( char )( ( id_LH[0] >>  8 ) & 0x000000FF );
+                        id_L_BYTE[0][6] = ( char )( ( id_LH[0] >> 16 ) & 0x000000FF );
+                        id_L_BYTE[0][7] = ( char )( ( id_LH[0] >> 24 ) & 0x000000FF );
+                        
+                        id_L_BYTE[0][0] = ( char )( ( id_LL[0] >>  0 ) & 0x000000FF );
+                        id_L_BYTE[0][1] = ( char )( ( id_LL[0] >>  8 ) & 0x000000FF );
+                        id_L_BYTE[0][2] = ( char )( ( id_LL[0] >> 16 ) & 0x000000FF );
+                        id_L_BYTE[0][3] = ( char )( ( id_LL[0] >> 24 ) & 0x000000FF );
+                        ///////////////////////////////////////////////////////////////  
+                                                    
+                        pc.printf( "ISETL[0] = %08X %08X\r\n", id_LH[0], id_LL[0] );
+                       
+                        ble_write( "ISETL\r" );
+                    }
+                    // コマンド  B  ISETL 1
+                    else if( cmd_no == 13 )
+                    {
+                        id_LH[1] = atoh(str[1], strlen(str[1]));
+                        id_LL[1] = atoh(str[2], strlen(str[2]));
 
+                        // Ver12 //////////////////////////////////////////////////////
+                        id_L_BYTE[1][4] = ( char )( ( id_LH[1] >>  0 ) & 0x000000FF );
+                        id_L_BYTE[1][5] = ( char )( ( id_LH[1] >>  8 ) & 0x000000FF );
+                        id_L_BYTE[1][6] = ( char )( ( id_LH[1] >> 16 ) & 0x000000FF );
+                        id_L_BYTE[1][7] = ( char )( ( id_LH[1] >> 24 ) & 0x000000FF );
+                        
+                        id_L_BYTE[1][0] = ( char )( ( id_LL[1] >>  0 ) & 0x000000FF );
+                        id_L_BYTE[1][1] = ( char )( ( id_LL[1] >>  8 ) & 0x000000FF );
+                        id_L_BYTE[1][2] = ( char )( ( id_LL[1] >> 16 ) & 0x000000FF );
+                        id_L_BYTE[1][3] = ( char )( ( id_LL[1] >> 24 ) & 0x000000FF );
+                        ///////////////////////////////////////////////////////////////
+                                                    
+                        pc.printf( "ISETL[1] = %08X %08X\r\n", id_LH[1], id_LL[1] );
+                       
+                        ble_write( "ISETL\r" );
+                    }
+                    // コマンド  C  ISETL 2
+                    else if( cmd_no == 14 )
+                    {
+                        id_LH[2] = atoh(str[1], strlen(str[1]));
+                        id_LL[2] = atoh(str[2], strlen(str[2]));
+
+                        // Ver12 //////////////////////////////////////////////////////
+                        id_L_BYTE[2][4] = ( char )( ( id_LH[2] >>  0 ) & 0x000000FF );
+                        id_L_BYTE[2][5] = ( char )( ( id_LH[2] >>  8 ) & 0x000000FF );
+                        id_L_BYTE[2][6] = ( char )( ( id_LH[2] >> 16 ) & 0x000000FF );
+                        id_L_BYTE[2][7] = ( char )( ( id_LH[2] >> 24 ) & 0x000000FF );
+                        
+                        id_L_BYTE[2][0] = ( char )( ( id_LL[2] >>  0 ) & 0x000000FF );
+                        id_L_BYTE[2][1] = ( char )( ( id_LL[2] >>  8 ) & 0x000000FF );
+                        id_L_BYTE[2][2] = ( char )( ( id_LL[2] >> 16 ) & 0x000000FF );
+                        id_L_BYTE[2][3] = ( char )( ( id_LL[2] >> 24 ) & 0x000000FF );
+                        ///////////////////////////////////////////////////////////////
+                                                    
+                        pc.printf( "ISETL[2] = %08X %08X\r\n", id_LH[2], id_LL[2] );
+                       
+                        ble_write( "ISETL\r" );
+                    }
+                    // コマンド  D  ISETL 3
+                    else if( cmd_no == 15 )
+                    {
+                        id_LH[3] = atoh(str[1], strlen(str[1]));
+                        id_LL[3] = atoh(str[2], strlen(str[2]));
+
+                        // Ver12 //////////////////////////////////////////////////////
+                        id_L_BYTE[3][4] = ( char )( ( id_LH[3] >>  0 ) & 0x000000FF );
+                        id_L_BYTE[3][5] = ( char )( ( id_LH[3] >>  8 ) & 0x000000FF );
+                        id_L_BYTE[3][6] = ( char )( ( id_LH[3] >> 16 ) & 0x000000FF );
+                        id_L_BYTE[3][7] = ( char )( ( id_LH[3] >> 24 ) & 0x000000FF );
+                        
+                        id_L_BYTE[3][0] = ( char )( ( id_LL[3] >>  0 ) & 0x000000FF );
+                        id_L_BYTE[3][1] = ( char )( ( id_LL[3] >>  8 ) & 0x000000FF );
+                        id_L_BYTE[3][2] = ( char )( ( id_LL[3] >> 16 ) & 0x000000FF );
+                        id_L_BYTE[3][3] = ( char )( ( id_LL[3] >> 24 ) & 0x000000FF );
+                        ///////////////////////////////////////////////////////////////
+                                                    
+                        pc.printf( "ISETL[3] = %08X %08X\r\n", id_LH[3], id_LL[3] );
+                        
+                        ble_write( "ISETL\r" );                        
+                    }
+                    // コマンド  E  ISETR 0
+                    else if( cmd_no == 16 )
+                    {
+                        id_RH[0] = atoh(str[1], strlen(str[1]));
+                        id_RL[0] = atoh(str[2], strlen(str[2]));
+
+                        // Ver12 //////////////////////////////////////////////////////
+                        id_R_BYTE[0][4] = ( char )( ( id_RH[0] >>  0 ) & 0x000000FF );
+                        id_R_BYTE[0][5] = ( char )( ( id_RH[0] >>  8 ) & 0x000000FF );
+                        id_R_BYTE[0][6] = ( char )( ( id_RH[0] >> 16 ) & 0x000000FF );
+                        id_R_BYTE[0][7] = ( char )( ( id_RH[0] >> 24 ) & 0x000000FF );
+                        
+                        id_R_BYTE[0][0] = ( char )( ( id_RL[0] >>  0 ) & 0x000000FF );
+                        id_R_BYTE[0][1] = ( char )( ( id_RL[0] >>  8 ) & 0x000000FF );
+                        id_R_BYTE[0][2] = ( char )( ( id_RL[0] >> 16 ) & 0x000000FF );
+                        id_R_BYTE[0][3] = ( char )( ( id_RL[0] >> 24 ) & 0x000000FF );
+                        ///////////////////////////////////////////////////////////////
+                                                    
+                        pc.printf( "ISETR[0] = %08X %08X\r\n", id_RH[0], id_RL[0] );
+                       
+                        ble_write( "ISETR\r" );
+                    }
+                    // コマンド  F  ISETR 1
+                    else if( cmd_no == 17 )
+                    {
+                        id_RH[1] = atoh(str[1], strlen(str[1]));
+                        id_RL[1] = atoh(str[2], strlen(str[2]));
+
+                        // Ver12 //////////////////////////////////////////////////////
+                        id_R_BYTE[1][4] = ( char )( ( id_RH[1] >>  0 ) & 0x000000FF );
+                        id_R_BYTE[1][5] = ( char )( ( id_RH[1] >>  8 ) & 0x000000FF );
+                        id_R_BYTE[1][6] = ( char )( ( id_RH[1] >> 16 ) & 0x000000FF );
+                        id_R_BYTE[1][7] = ( char )( ( id_RH[1] >> 24 ) & 0x000000FF );
+                        
+                        id_R_BYTE[1][0] = ( char )( ( id_RL[1] >>  0 ) & 0x000000FF );
+                        id_R_BYTE[1][1] = ( char )( ( id_RL[1] >>  8 ) & 0x000000FF );
+                        id_R_BYTE[1][2] = ( char )( ( id_RL[1] >> 16 ) & 0x000000FF );
+                        id_R_BYTE[1][3] = ( char )( ( id_RL[1] >> 24 ) & 0x000000FF );
+                        ///////////////////////////////////////////////////////////////
+                                                    
+                        pc.printf( "ISETR[1] = %08X %08X\r\n", id_RH[1], id_RL[1] );
+                       
+                        ble_write( "ISETR\r" );                        
+                    }
+                    // コマンド  G  ISETR 2
+                    else if( cmd_no == 18 )
+                    {
+                        id_RH[2] = atoh(str[1], strlen(str[1]));
+                        id_RL[2] = atoh(str[2], strlen(str[2]));
+
+                        // Ver12 //////////////////////////////////////////////////////
+                        id_R_BYTE[2][4] = ( char )( ( id_RH[2] >>  0 ) & 0x000000FF );
+                        id_R_BYTE[2][5] = ( char )( ( id_RH[2] >>  8 ) & 0x000000FF );
+                        id_R_BYTE[2][6] = ( char )( ( id_RH[2] >> 16 ) & 0x000000FF );
+                        id_R_BYTE[2][7] = ( char )( ( id_RH[2] >> 24 ) & 0x000000FF );
+                        
+                        id_R_BYTE[2][0] = ( char )( ( id_RL[2] >>  0 ) & 0x000000FF );
+                        id_R_BYTE[2][1] = ( char )( ( id_RL[2] >>  8 ) & 0x000000FF );
+                        id_R_BYTE[2][2] = ( char )( ( id_RL[2] >> 16 ) & 0x000000FF );
+                        id_R_BYTE[2][3] = ( char )( ( id_RL[2] >> 24 ) & 0x000000FF );
+                        ///////////////////////////////////////////////////////////////
+                                                    
+                        pc.printf( "ISETR[2] = %08X %08X\r\n", id_RH[2], id_RL[2] );
+                       
+                        ble_write( "ISETR\r" );
+                    }
+                    // コマンド  H  ISETL 3
+                    else if( cmd_no == 19 )
+                    {
+                        id_RH[3] = atoh(str[1], strlen(str[1]));
+                        id_RL[3] = atoh(str[2], strlen(str[2]));
+
+                        // Ver12 //////////////////////////////////////////////////////
+                        id_R_BYTE[3][4] = ( char )( ( id_RH[3] >>  0 ) & 0x000000FF );
+                        id_R_BYTE[3][5] = ( char )( ( id_RH[3] >>  8 ) & 0x000000FF );
+                        id_R_BYTE[3][6] = ( char )( ( id_RH[3] >> 16 ) & 0x000000FF );
+                        id_R_BYTE[3][7] = ( char )( ( id_RH[3] >> 24 ) & 0x000000FF );
+                        
+                        id_R_BYTE[3][0] = ( char )( ( id_RL[3] >>  0 ) & 0x000000FF );
+                        id_R_BYTE[3][1] = ( char )( ( id_RL[3] >>  8 ) & 0x000000FF );
+                        id_R_BYTE[3][2] = ( char )( ( id_RL[3] >> 16 ) & 0x000000FF );
+                        id_R_BYTE[3][3] = ( char )( ( id_RL[3] >> 24 ) & 0x000000FF );
+                        ///////////////////////////////////////////////////////////////
+                                                    
+                        pc.printf( "ISETR[3] = %08X %08X\r\n", id_RH[3], id_RL[3] );
+                       
+                        ble_write( "ISETR\r" );                        
+                    }
+                    // コマンド  PAUSE
+                    else if( cmd_no == 20 )
+                    {
+                        start_flag = OFF;
+                        idget_flag = OFF;
+                        pause_flag = ON;
+                        
+                        point_res = OFF;
+                    
+                    #if SLEEP_MODE    
+                        // SLEEP
+                        sleep_flag = ON;                  
+                        rfid_en1 = 0;
+                    #endif
+                          
+                        ble_write( "PAUSE\r" );
+                        pc.printf( "PAUSE OK\r" );
+                    }
+                    // コマンド  REG09   Ver12
+                    else if( cmd_no == 21 )
+                    {
+                        trf7970a_reg09_dat = char( atoh(str[1], strlen(str[1])) );
+                        trf7970a_init();
+                        ble_write( "REG09\r" );
+                        pc.printf( "REG09 %02X\r", trf7970a_reg09_dat );
+                    }
+                    // コマンド  REG0A   Ver12
+                    else if( cmd_no == 22 )
+                    {
+                        trf7970a_reg0A_dat = char( atoh(str[1], strlen(str[1])) );
+                        trf7970a_init();
+                        ble_write( "REG0A\r" );
+                        pc.printf( "REG0A %02X\r", trf7970a_reg0A_dat );
+                    }
+                    // コマンド  REG0B   Ver12
+                    else if( cmd_no == 23 )
+                    {
+                        trf7970a_reg0B_dat = char( atoh(str[1], strlen(str[1])) );
+                        trf7970a_init();    
+                        ble_write( "REG0B\r" );
+                        pc.printf( "REG0B %02X\r", trf7970a_reg0B_dat );
+                    }
+                    // コマンド  ICMR   Ver12
+                    else if( cmd_no == 24 )
+                    {
+                        int adr = atow(str[1], strlen(str[1]));
+                        
+                        char buf[BFSIZE];
+                                
+                        sprintf( buf, "ICMR %d %08X\r", adr, Iccard_mem[adr] );
+ 
+                        ble_write( buf );
+                        
+                        pc.printf( "ICMR %d %08X\r", adr, Iccard_mem[adr] );
                     }
                     else
                     {
@@ -1036,69 +2214,155 @@
     }
 }
 
+// Ver12 にてポイント判定処理を大きく変更した。
 /****************************************************************************/
 /*  関数名 : point_result_proc                                              */
 /*  概要   : ポイント処理                                                    */
 /*  作成者 : JPMS H.Harada                                                  */
-/*  作成日 : 2018.03.20                                                     */
+/*  作成日 : 2019.03.8                                                     */
 /****************************************************************************/
-unsigned int point_result_proc( result_t *result, unsigned int ret,
-            unsigned int uid_H, unsigned int uid_L, unsigned int id_H, unsigned int id_L )
+unsigned int point_result_proc( result_t *result, char punch, char gardL, char gardR )
 {
     unsigned int pret;
 
     pret = OFF;
 
-    if( ( ret == UID_OK ) && ( ( id_H == uid_H ) && ( id_L == uid_L ) ) )
+    if( ( punch == 0 ) || ( punch == 1 ) )
     {
-        result->Result = ON;
+        result->Result = NON;
     }
-    else
+    else if( punch == 9 )
     {
         result->Result = OFF;
     }
+    else
+    {
+        if( ( gardL >= 3 && gardL < 9 ) || ( gardR >= 3 && gardR < 9 ) )
+        {
+            result->Result = NON;
+        }
+        else
+        {
+            result->Result = ON;
+        }
+    }
 
-    if( result->Result_pulse == OFF )
+    if( result->Result_state == STATE_WAIT_OFF )
     {
-        if( ( result->Result == ON ) && ( result->Result_d1 == ON ) )
+        if( result->Result == OFF )
         {
-            result->Result_pulse = ON;
-            result->Result_ON_cnt = 1;
+            result->Result_OFF_cnt++;
+                        
+            if( result->Result_OFF_cnt >= not_touch_cnt )
+            {       
+                result->Result_ON_cnt = 0;
+                result->Result_OFF_cnt = 0;
+                result->Result_NON_cnt = 0;
+                
+                result->Result_state = STATE_WAIT_ON;  
+            }
+        }
+        else
+        {
             result->Result_OFF_cnt = 0;
         }
     }
-    else
+    else if( result->Result_state == STATE_WAIT_ON )
     {
-        if( ( result->Result == OFF ) && ( result->Result_d1 == OFF ) )
+        if( result->Result == ON )
         {
-            if( result->Result_OFF_cnt < 999 )
+            result->Result_ON_cnt++;
+            
+            if( ( result->Result_ON_cnt < touch_cnt ) && ( result->Result_ON_cnt >= touch_start ) )
+            {
+                result->Result_OFF_cnt = 0;
+                result->Result_NON_cnt = 0;
+                
+                result->Result_state = STATE_WAIT_END;
+            }
+        }
+        else if( result->Result == OFF )
+        {
+            if( result->Result_OFF_cnt < 9999 )
             {
                 result->Result_OFF_cnt++;
             }
             
-            if( result->Result_OFF_cnt >= not_touch_cnt )
+            if( result->Result_OFF_cnt >= 5 )
+            {
+                if( result->Result_ON_cnt >= 5 )
+                {
+                    result->Result_ON_cnt = 0;
+                    result->Result_OFF_cnt = 0;
+                    result->Result_NON_cnt = 0;
+                    
+                    result->Result_state = STATE_WAIT_OFF;
+                }
+            }
+        }
+        else if( result->Result == NON )
+        {
+            result->Result_NON_cnt++;
+            
+            if( result->Result_NON_cnt >= 5 )
             {
-                if( result->Result_ON_cnt < touch_cnt )
+                result->Result_ON_cnt = 0;
+                result->Result_OFF_cnt = 0;
+                result->Result_NON_cnt = 0;
+                
+                result->Result_state = STATE_WAIT_OFF;
+            }         
+        }
+        else
+        {
+            
+        }                   
+    }
+    else if( result->Result_state == STATE_WAIT_END )
+    {
+        if( result->Result == OFF )
+        {
+            result->Result_OFF_cnt++;
+                      
+            if( result->Result_OFF_cnt >= not_touch_cnt )
+            {               
+                if( ( result->Result_ON_cnt < touch_cnt ) && ( result->Result_ON_cnt >= touch_start ) )
                 {
                     result->Point++;
 
                     pret = ON; 
                 }
-            
-                result->Result_pulse = OFF;
+  
                 result->Result_ON_cnt = 0;
                 result->Result_OFF_cnt = 0;
+                
+                result->Result_state = STATE_WAIT_ON;  
             }
         }
-        else if( ( result->Result == ON ) && ( result->Result_d1 == ON ) )
+        else if( result->Result == ON )
         {
-            result->Result_pulse = ON;
-
             if( result->Result_ON_cnt < 9999 )
             {
                 result->Result_ON_cnt++;
-            }            
+            }         
         }
+        else if( result->Result == NON )
+        {
+            result->Result_NON_cnt++;
+            
+            if( result->Result_NON_cnt >=  5 )
+            {
+                result->Result_ON_cnt = 0;
+                result->Result_OFF_cnt = 0;
+                result->Result_NON_cnt = 0;
+                
+                result->Result_state = STATE_WAIT_OFF;
+            }         
+        }     
+        else
+        {
+
+        }       
     }
 
     result->Result_d1 = result->Result;