Remote I/O Switchx4 LEDx4 AINx2(Battery,Potentiometer) OUTx8 INx8 nRFUARTの改造版 ライブラリアップデートに注意 BLE_API,nRF51822,mbed

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

Revision:
17:663243430dc2
Parent:
16:5df99e9291af
--- a/main.cpp	Wed Dec 28 18:10:12 2016 +0000
+++ b/main.cpp	Fri Jan 13 13:46:10 2017 +0000
@@ -56,7 +56,7 @@
 #define ANALOG_IN_PIN1  P0_2
 #define ANALOG_IN_PIN2  P0_3
 
-//#define PWM_PIN         P0_8
+//#define LED_PIN0        P0_8  //med bug can't use
 
 #define DEBUG_TX_PIN    P0_9
 #define DEBUG_RX_PIN    P0_11
@@ -100,6 +100,7 @@
 Serial          DEBUG(DEBUG_TX_PIN, DEBUG_RX_PIN);
 AnalogIn        AIN_1(ANALOG_IN_PIN1);
 AnalogIn        AIN_2(ANALOG_IN_PIN2);
+//DigitalOut      LED_0(LED_PIN0);
 //
 ////////////////////////////////////////
 //
@@ -115,6 +116,7 @@
 uint16_t ain1_old;
 uint16_t ain2_old;
 uint16_t output_buf;
+uint16_t save_buf;
 
 ////////////////////////////////////////
 void periodicCallback_led_blink( void );
@@ -133,8 +135,9 @@
 uint8_t cmd_str[8];
 bool update_characteristic_fg;
 bool change_input_fg;
-bool first_update_fg;
+bool port_restor_fg;
 uint16_t adc_update_cnt;
+uint16_t first_update_cnt;
 //
 ////////////////////////////////////////
 // BLEイベント接続時
@@ -143,11 +146,12 @@
     ticker.detach();
     DEBUG.printf("Connection handle:%u \r\n",params->handle);
     // stop blinking when we connect
-    LED_1 = LED_OFF;
+    LED_1 = save_buf & 0x0001;
     read_IN();
     ticker.attach(periodicCallback_sw_read, 0.05);
     update_characteristic_fg = true;
-    first_update_fg = true;
+    first_update_cnt = 0;
+    port_restor_fg = true;
     adc_update_cnt = 0;
 }
 
@@ -156,29 +160,36 @@
 // BLEイベント切断時
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
-    int i;
+//    int i;
     update_characteristic_fg = false;
+    first_update_cnt = 0;
     ticker.detach();
     DEBUG.printf("Disconnected!\n\r");
     DEBUG.printf("Restarting the advertising process\n\r");
     ble.startAdvertising();
     ticker.attach(periodicCallback_led_blink, 1);
-    for( i=1; i < 4+1 ; i++ ){ set_LED(i,0); }
-    for( i=1; i < 8+1 ; i++ ){ set_OUT(i,0); }
+//    for( i=1; i < 4+1 ; i++ ){ set_LED(i,0); }
+//    for( i=1; i < 8+1 ; i++ ){ set_OUT(i,0); }
 }
 //
 ////////////////////////////////////////
 // BLE-UART受信時
 void onDataWritten(const GattWriteCallbackParams *params)
 {
-    uint8_t* s;
+    uint8_t* ps;
+    uint16_t bytesRead;
+//    char send_str[20];
+//    uint16_t slen;
+
     if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
-        uint16_t bytesRead = params->len;
+        bytesRead = params->len;
         DEBUG.printf("received %u bytes\n\r", bytesRead);
-        s = (uint8_t*)(&(params->data[0]));
-        cmd_check(s);
-        // BLE-UART送信
-//        ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
+        ps = (uint8_t*)(&(params->data[0]));
+        cmd_check(ps);
+//        sprintf(send_str,"o%04X00000000 \r",save_buf );    //出力ポートデータ
+//        slen = strlen(send_str);
+//        // BLE-UART送信
+//        ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(),(uint8_t*)(&send_str[0]),slen);
     }
 }
 //
@@ -186,6 +197,7 @@
 // 未接続時周期割り込み
 void periodicCallback_led_blink(void)
 {
+//    LED_0 = !LED_0;
     LED_1 = !LED_1;
 }
 //
@@ -197,13 +209,15 @@
     uint16_t slen;
 
     read_IN();
-    sprintf(send_str,"%04X,%04X,%04X \r",input_now,ain1_now,ain2_now);
+    sprintf(send_str,"i%04X%04X%04X \r",input_now,ain1_now,ain2_now );
     slen = strlen(send_str);
 
-    if( first_update_fg == true )
+    if( first_update_cnt < 10 )
     {
-        first_update_fg = false;
+        first_update_cnt++;
         change_input_fg = true;
+        sprintf(send_str,"o%04X00000000 \r",save_buf );    //出力ポートデータ
+        slen = strlen(send_str);
     }
     else
     {
@@ -224,6 +238,7 @@
     {
         DEBUG.printf("len=%d,data=%s",slen,send_str);
         ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(),(uint8_t*)(&send_str[0]),slen);
+//        LED_0 = !LED_0;
     }
 }
 //
@@ -284,6 +299,7 @@
     while( 1 )
     {
         wait_ms(200);
+//        LED_0 = !LED_0;
         read_IN();
         DEBUG.printf("%04X,%04X,%04X \r",input_now,ain1_now,ain2_now);
         //
@@ -315,15 +331,19 @@
     {
         case 1:
             LED_1 = val;
+            if( val ){ save_buf |= 0x0001; }else{ save_buf &= 0xfffe; } 
             break;
         case 2:
             LED_2 = val;
+            if( val ){ save_buf |= 0x0002; }else{ save_buf &= 0xfffd; } 
             break;
         case 3:
             LED_3 = val;
+            if( val ){ save_buf |= 0x0004; }else{ save_buf &= 0xfffb; } 
             break;
         case 4:
             LED_4 = val;
+            if( val ){ save_buf |= 0x0008; }else{ save_buf &= 0xfff7; } 
             break;
     }
 }
@@ -334,27 +354,35 @@
     {
         case 1:
             DO_1 = val;
+            if( val ){ save_buf |= 0x0100; }else{  save_buf &= 0xfeff; } 
             break;
         case 2:
             DO_2 = val;
+            if( val ){ save_buf |= 0x0200; }else{  save_buf &= 0xfdff; } 
             break;
         case 3:
             DO_3 = val;
+            if( val ){ save_buf |= 0x0400; }else{  save_buf &= 0xfbff; } 
             break;
         case 4:
             DO_4 = val;
+            if( val ){ save_buf |= 0x0800; }else{  save_buf &= 0xf7ff; } 
             break;
         case 5:
             DO_5 = val;
+            if( val ){ save_buf |= 0x1000; }else{  save_buf &= 0xefff; } 
             break;
         case 6:
             DO_6 = val;
+            if( val ){ save_buf |= 0x2000; }else{  save_buf &= 0xdfff; } 
             break;
         case 7:
             DO_7 = val;
+            if( val ){ save_buf |= 0x4000; }else{  save_buf &= 0xbfff; } 
             break;
         case 8:
             DO_8 = val;
+            if( val ){ save_buf |= 0x8000; }else{  save_buf &= 0x7fff; } 
             break;
     }
 }
@@ -412,6 +440,7 @@
     LED_2 = 0;
     LED_3 = 0;
     LED_4 = 0;
+    save_buf = 0;
 //
 }