noboru koshinaka / Mbed 2 deprecated BLE_Remote_IO_UART

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "ble/BLE.h"
00019 #include "ble/services/UARTService.h"
00020 //
00021 ////////////////////////////////////////
00022 //
00023 #define LED_ON          1
00024 #define LED_OFF         0
00025 //
00026 ////////////////////////////////////////
00027 // I/O pin define
00028 #define LED_PIN1        P0_25
00029 #define LED_PIN2        P0_28
00030 #define LED_PIN3        P0_29
00031 #define LED_PIN4        P0_30
00032 
00033 #define SWITCH_PIN1     P0_4
00034 #define SWITCH_PIN2     P0_5
00035 #define SWITCH_PIN3     P0_6
00036 #define SWITCH_PIN4     P0_7
00037 
00038 #define OUT_PORT_PIN1   P0_18
00039 #define OUT_PORT_PIN2   P0_17
00040 #define OUT_PORT_PIN3   P0_16
00041 #define OUT_PORT_PIN4   P0_20
00042 #define OUT_PORT_PIN5   P0_21
00043 #define OUT_PORT_PIN6   P0_22
00044 #define OUT_PORT_PIN7   P0_23
00045 #define OUT_PORT_PIN8   P0_24
00046 
00047 #define IN_PORT_PIN1    P0_0
00048 #define IN_PORT_PIN2    P0_1
00049 #define IN_PORT_PIN3    P0_10
00050 #define IN_PORT_PIN4    P0_12
00051 #define IN_PORT_PIN5    P0_13
00052 #define IN_PORT_PIN6    P0_14
00053 #define IN_PORT_PIN7    P0_15
00054 #define IN_PORT_PIN8    P0_19
00055 
00056 #define ANALOG_IN_PIN1  P0_2
00057 #define ANALOG_IN_PIN2  P0_3
00058 
00059 //#define LED_PIN0        P0_8  //med bug can't use
00060 
00061 #define DEBUG_TX_PIN    P0_9
00062 #define DEBUG_RX_PIN    P0_11
00063 //
00064 ////////////////////////////////////////
00065 //
00066 #define cmd_LED 'L' // "Lxx"
00067 #define cmd_OUT 'O' // "Oxx"
00068 //
00069 ////////////////////////////////////////
00070 //
00071 DigitalOut      LED_1(LED_PIN1);
00072 DigitalOut      LED_2(LED_PIN2);
00073 DigitalOut      LED_3(LED_PIN3);
00074 DigitalOut      LED_4(LED_PIN4);
00075 
00076 DigitalIn       SW_1(SWITCH_PIN1);
00077 DigitalIn       SW_2(SWITCH_PIN2);
00078 DigitalIn       SW_3(SWITCH_PIN3);
00079 DigitalIn       SW_4(SWITCH_PIN4);
00080 
00081 DigitalOut      DO_1(OUT_PORT_PIN1);
00082 DigitalOut      DO_2(OUT_PORT_PIN2);
00083 DigitalOut      DO_3(OUT_PORT_PIN3);
00084 DigitalOut      DO_4(OUT_PORT_PIN4);
00085 DigitalOut      DO_5(OUT_PORT_PIN5);
00086 DigitalOut      DO_6(OUT_PORT_PIN6);
00087 DigitalOut      DO_7(OUT_PORT_PIN7);
00088 DigitalOut      DO_8(OUT_PORT_PIN8);
00089 
00090 DigitalIn       DI_1(IN_PORT_PIN1);
00091 DigitalIn       DI_2(IN_PORT_PIN2);
00092 DigitalIn       DI_3(IN_PORT_PIN3);
00093 DigitalIn       DI_4(IN_PORT_PIN4);
00094 DigitalIn       DI_5(IN_PORT_PIN5);
00095 DigitalIn       DI_6(IN_PORT_PIN6);
00096 DigitalIn       DI_7(IN_PORT_PIN7);
00097 DigitalIn       DI_8(IN_PORT_PIN8);
00098 
00099 //
00100 Serial          DEBUG(DEBUG_TX_PIN, DEBUG_RX_PIN);
00101 AnalogIn        AIN_1(ANALOG_IN_PIN1);
00102 AnalogIn        AIN_2(ANALOG_IN_PIN2);
00103 //DigitalOut      LED_0(LED_PIN0);
00104 //
00105 ////////////////////////////////////////
00106 //
00107 BLEDevice  ble;
00108 UARTService *uartServicePtr;
00109 Ticker ticker;
00110 //
00111 ////////////////////////////////////////
00112 uint16_t input_now;
00113 uint16_t input_old;
00114 uint16_t ain1_now;
00115 uint16_t ain2_now;
00116 uint16_t ain1_old;
00117 uint16_t ain2_old;
00118 uint16_t output_buf;
00119 uint16_t save_buf;
00120 
00121 ////////////////////////////////////////
00122 void periodicCallback_led_blink( void );
00123 void periodicCallback_sw_read( void );
00124 void set_LED( int num, int val );
00125 void set_OUT( int num, int val );
00126 void read_IN( void );
00127 void port_clear( void );
00128 void cmd_check( uint8_t* s );
00129 void board_test( void );
00130 //
00131 ////////////////////////////////////////
00132 uint16_t board_test_fg = 0;
00133 uint16_t uart_cmd_check( void );
00134 uint16_t cmd_c_wp;
00135 uint8_t cmd_str[8];
00136 bool update_characteristic_fg;
00137 bool change_input_fg;
00138 bool port_restor_fg;
00139 uint16_t adc_update_cnt;
00140 uint16_t first_update_cnt;
00141 //
00142 ////////////////////////////////////////
00143 // BLEイベント接続時
00144 void connectionCallback(const Gap::ConnectionCallbackParams_t * params)
00145 {
00146     ticker.detach();
00147     DEBUG.printf("Connection handle:%u \r\n",params->handle);
00148     // stop blinking when we connect
00149     LED_1 = save_buf & 0x0001;
00150     read_IN();
00151     ticker.attach(periodicCallback_sw_read, 0.05);
00152     update_characteristic_fg = true;
00153     first_update_cnt = 0;
00154     port_restor_fg = true;
00155     adc_update_cnt = 0;
00156 }
00157 
00158 //
00159 ////////////////////////////////////////
00160 // BLEイベント切断時
00161 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00162 {
00163 //    int i;
00164     update_characteristic_fg = false;
00165     first_update_cnt = 0;
00166     ticker.detach();
00167     DEBUG.printf("Disconnected!\n\r");
00168     DEBUG.printf("Restarting the advertising process\n\r");
00169     ble.startAdvertising();
00170     ticker.attach(periodicCallback_led_blink, 1);
00171 //    for( i=1; i < 4+1 ; i++ ){ set_LED(i,0); }
00172 //    for( i=1; i < 8+1 ; i++ ){ set_OUT(i,0); }
00173 }
00174 //
00175 ////////////////////////////////////////
00176 // BLE-UART受信時
00177 void onDataWritten(const GattWriteCallbackParams *params)
00178 {
00179     uint8_t* ps;
00180     uint16_t bytesRead;
00181 //    char send_str[20];
00182 //    uint16_t slen;
00183 
00184     if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
00185         bytesRead = params->len;
00186         DEBUG.printf("received %u bytes\n\r", bytesRead);
00187         ps = (uint8_t*)(&(params->data[0]));
00188         cmd_check(ps);
00189 //        sprintf(send_str,"o%04X00000000 \r",save_buf );    //出力ポートデータ
00190 //        slen = strlen(send_str);
00191 //        // BLE-UART送信
00192 //        ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(),(uint8_t*)(&send_str[0]),slen);
00193     }
00194 }
00195 //
00196 ////////////////////////////////////////
00197 // 未接続時周期割り込み
00198 void periodicCallback_led_blink(void)
00199 {
00200 //    LED_0 = !LED_0;
00201     LED_1 = !LED_1;
00202 }
00203 //
00204 ////////////////////////////////////////
00205 // 接続時周期割り込み
00206 void periodicCallback_sw_read( void  )
00207 {
00208     char send_str[20];
00209     uint16_t slen;
00210 
00211     read_IN();
00212     sprintf(send_str,"i%04X%04X%04X \r",input_now,ain1_now,ain2_now );
00213     slen = strlen(send_str);
00214 
00215     if( first_update_cnt < 10 )
00216     {
00217         first_update_cnt++;
00218         change_input_fg = true;
00219         sprintf(send_str,"o%04X00000000 \r",save_buf );    //出力ポートデータ
00220         slen = strlen(send_str);
00221     }
00222     else
00223     {
00224         change_input_fg = false;
00225     }
00226 
00227     if( input_now != input_old )
00228     {
00229         change_input_fg = true;
00230     }
00231     adc_update_cnt++;
00232     if( adc_update_cnt > 5  )
00233     {
00234         adc_update_cnt = 0;
00235         change_input_fg = true;
00236     }
00237     if(( update_characteristic_fg == true )&&( change_input_fg == true ))
00238     {
00239         DEBUG.printf("len=%d,data=%s",slen,send_str);
00240         ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(),(uint8_t*)(&send_str[0]),slen);
00241 //        LED_0 = !LED_0;
00242     }
00243 }
00244 //
00245 ////////////////////////////////////////
00246 // メイン
00247 int main(void)
00248 {
00249     DEBUG.baud(9600);
00250     port_clear();
00251     read_IN();
00252     read_IN();
00253     DEBUG.printf("\r\n");
00254     DEBUG.printf("BLE remote I/O \r\n");
00255     DEBUG.printf(" nRFUART version\r\n");
00256     DEBUG.printf("   2019/12/24\r\n");
00257     read_IN();
00258     DEBUG.printf("\r\n");
00259     if( ( input_now & 0x0003 ) == 0x0003 ){ board_test(); } //SW1,2 ON?
00260 //
00261     DEBUG.printf("Start program\r\n");
00262     ticker.attach(periodicCallback_led_blink, 1);
00263 
00264     ble.init();
00265     ble.onConnection(connectionCallback);
00266     ble.onDisconnection(disconnectionCallback);
00267     ble.onDataWritten(onDataWritten);
00268 
00269     /* setup advertising */
00270     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00271     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00272     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
00273                                      (const uint8_t *)"BLE IO", sizeof("BLE IO") - 1);
00274 //                                     (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
00275     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
00276                                      (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
00277 
00278     ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
00279     ble.startAdvertising();
00280 
00281     UARTService uartService(ble);
00282     uartServicePtr = &uartService;
00283 
00284     while (true) {
00285         ble.waitForEvent();
00286     }
00287 }
00288 
00289 ////////////////////////////////////////
00290 void board_test( void )
00291 {
00292 //  I/O port debug
00293     cmd_c_wp = 0;
00294 
00295     DEBUG.printf("board I/O debug \r\n");
00296     output_buf = 0;
00297     read_IN();
00298     read_IN();
00299     while( 1 )
00300     {
00301         wait_ms(200);
00302 //        LED_0 = !LED_0;
00303         read_IN();
00304         DEBUG.printf("%04X,%04X,%04X \r",input_now,ain1_now,ain2_now);
00305         //
00306         if( input_now & 0x0001 ){ set_LED(1,1); } else { set_LED(1,0); }
00307         if( input_now & 0x0002 ){ set_LED(2,1); } else { set_LED(2,0); }
00308         if( input_now & 0x0004 ){ set_LED(3,1); } else { set_LED(3,0); }
00309         if( input_now & 0x0008 ){ set_LED(4,1); } else { set_LED(4,0); }
00310         //
00311         if( output_buf & 0x0001 ){ set_OUT(1,1); } else { set_OUT(1,0); }
00312         if( output_buf & 0x0002 ){ set_OUT(2,1); } else { set_OUT(2,0); }
00313         if( output_buf & 0x0004 ){ set_OUT(3,1); } else { set_OUT(3,0); }
00314         if( output_buf & 0x0008 ){ set_OUT(4,1); } else { set_OUT(4,0); }
00315         if( output_buf & 0x0010 ){ set_OUT(5,1); } else { set_OUT(5,0); }
00316         if( output_buf & 0x0020 ){ set_OUT(6,1); } else { set_OUT(6,0); }
00317         if( output_buf & 0x0040 ){ set_OUT(7,1); } else { set_OUT(7,0); }
00318         if( output_buf & 0x0080 ){ set_OUT(8,1); } else { set_OUT(8,0); }
00319         output_buf++;
00320         if( uart_cmd_check() != 0 )
00321         {
00322             cmd_check( cmd_str );
00323         }
00324     }
00325 }
00326 
00327 ////////////////////////////////////////
00328 void set_LED( int num, int val )
00329 {
00330     switch( num )
00331     {
00332         case 1:
00333             LED_1 = val;
00334             if( val ){ save_buf |= 0x0001; }else{ save_buf &= 0xfffe; } 
00335             break;
00336         case 2:
00337             LED_2 = val;
00338             if( val ){ save_buf |= 0x0002; }else{ save_buf &= 0xfffd; } 
00339             break;
00340         case 3:
00341             LED_3 = val;
00342             if( val ){ save_buf |= 0x0004; }else{ save_buf &= 0xfffb; } 
00343             break;
00344         case 4:
00345             LED_4 = val;
00346             if( val ){ save_buf |= 0x0008; }else{ save_buf &= 0xfff7; } 
00347             break;
00348     }
00349 }
00350 
00351 void set_OUT( int num, int val )
00352 {
00353     switch( num )
00354     {
00355         case 1:
00356             DO_1 = val;
00357             if( val ){ save_buf |= 0x0100; }else{  save_buf &= 0xfeff; } 
00358             break;
00359         case 2:
00360             DO_2 = val;
00361             if( val ){ save_buf |= 0x0200; }else{  save_buf &= 0xfdff; } 
00362             break;
00363         case 3:
00364             DO_3 = val;
00365             if( val ){ save_buf |= 0x0400; }else{  save_buf &= 0xfbff; } 
00366             break;
00367         case 4:
00368             DO_4 = val;
00369             if( val ){ save_buf |= 0x0800; }else{  save_buf &= 0xf7ff; } 
00370             break;
00371         case 5:
00372             DO_5 = val;
00373             if( val ){ save_buf |= 0x1000; }else{  save_buf &= 0xefff; } 
00374             break;
00375         case 6:
00376             DO_6 = val;
00377             if( val ){ save_buf |= 0x2000; }else{  save_buf &= 0xdfff; } 
00378             break;
00379         case 7:
00380             DO_7 = val;
00381             if( val ){ save_buf |= 0x4000; }else{  save_buf &= 0xbfff; } 
00382             break;
00383         case 8:
00384             DO_8 = val;
00385             if( val ){ save_buf |= 0x8000; }else{  save_buf &= 0x7fff; } 
00386             break;
00387     }
00388 }
00389 
00390 void read_IN( void )
00391 {
00392     input_old = input_now;
00393     input_now = 0;
00394     if( SW_1 == 0 ){ input_now |= 0x0001; }
00395     if( SW_2 == 0 ){ input_now |= 0x0002; }
00396     if( SW_3 == 0 ){ input_now |= 0x0004; }
00397     if( SW_4 == 0 ){ input_now |= 0x0008; }
00398     if( DI_1 == 0 ){ input_now |= 0x0100; }
00399     if( DI_2 == 0 ){ input_now |= 0x0200; }
00400     if( DI_3 == 0 ){ input_now |= 0x0400; }
00401     if( DI_4 == 0 ){ input_now |= 0x0800; }
00402     if( DI_5 == 0 ){ input_now |= 0x1000; }
00403     if( DI_6 == 0 ){ input_now |= 0x2000; }
00404     if( DI_7 == 0 ){ input_now |= 0x4000; }
00405     if( DI_8 == 0 ){ input_now |= 0x8000; }
00406     ain1_old = ain1_now;
00407     ain1_now = AIN_1.read_u16();
00408     ain2_old = ain2_now;
00409     ain2_now = AIN_2.read_u16();
00410 }
00411 
00412 void port_clear( void )
00413 {
00414     int i;
00415     for( i=1; i < 4+1 ; i++ ){ set_LED(i,0); }
00416     for( i=1; i < 8+1 ; i++ ){ set_OUT(i,0); }
00417 //
00418     LED_1 = 1;
00419     wait_ms(100);
00420     LED_2 = 1;
00421     wait_ms(100);
00422     LED_3 = 1;
00423     wait_ms(100);
00424     LED_4 = 1;
00425     wait_ms(100);
00426     LED_1 = 0;
00427     wait_ms(100);
00428     LED_2 = 0;
00429     wait_ms(100);
00430     LED_3 = 0;
00431     wait_ms(100);
00432     LED_4 = 0;
00433     wait_ms(400);
00434     LED_1 = 1;
00435     LED_2 = 1;
00436     LED_3 = 1;
00437     LED_4 = 1;
00438     wait_ms(400);
00439     LED_1 = 0;
00440     LED_2 = 0;
00441     LED_3 = 0;
00442     LED_4 = 0;
00443     save_buf = 0;
00444 //
00445 }
00446 
00447 uint16_t uart_cmd_check( void )
00448 {
00449     uint16_t res;
00450     uint8_t c;
00451     res = 0;
00452     if( DEBUG.readable() )
00453     {
00454         c = DEBUG.getc();
00455         cmd_str[cmd_c_wp] = c;
00456         cmd_c_wp++;
00457         if( cmd_c_wp >= 4 )
00458         {
00459             cmd_str[cmd_c_wp] = 0;
00460             cmd_c_wp = 0;
00461             res = 1;
00462         }
00463     }
00464     return res;
00465 }
00466 
00467 void cmd_check( uint8_t* s )
00468 {
00469     int val;
00470 
00471     DEBUG.printf("\r\ncmd=%s\r\n",s);
00472     if( s[2] == '0' )
00473     {
00474         val = 0;
00475     }
00476     else if( s[2] == '1' )
00477     {
00478         val = 1;
00479     }
00480     if( s[0] == 'L' )
00481     {
00482         switch( s[1] )
00483         {
00484             case '1':
00485                 set_LED( 1, val );
00486                 break;
00487             case '2':
00488                 set_LED( 2, val );
00489                 break;
00490             case '3':
00491                 set_LED( 3, val );
00492                 break;
00493             case '4':
00494                 set_LED( 4, val );
00495                 break;
00496         }
00497         return;
00498     }
00499     if( s[0] == 'O' )
00500     {
00501         switch( s[1] )
00502         {
00503             case '1':
00504                 set_OUT( 1, val );
00505                 break;
00506             case '2':
00507                 set_OUT( 2, val );
00508                 break;
00509             case '3':
00510                 set_OUT( 3, val );
00511                 break;
00512             case '4':
00513                 set_OUT( 4, val );
00514                 break;
00515             case '5':
00516                 set_OUT( 5, val );
00517                 break;
00518             case '6':
00519                 set_OUT( 6, val );
00520                 break;
00521             case '7':
00522                 set_OUT( 7, val );
00523                 break;
00524             case '8':
00525                 set_OUT( 8, val );
00526                 break;
00527         }
00528         return;
00529     }
00530 }
00531 
00532