this is a new program based on BLE_Uart_Peripheral. Change program name due to relation Client&Peripheral to Client&Server

Dependencies:   RingBuffer nRF51_Vdd nRF51_WakeUp

This is a BLE Server (Device) program for nRF51.
You can communicate with mbed BLE using "BLE_Uart_Client" program as follows.
/users/kenjiArai/code/BLE_Uart_Client/
Please refer following my notebook.
/users/kenjiArai/notebook/ble-client-and-peripheral-using-switch-sience-ty51/#

Files at this revision

API Documentation at this revision

Comitter:
kenjiArai
Date:
Thu Apr 21 11:23:26 2016 +0000
Parent:
2:2322afea6ecf
Child:
4:a1b642566003
Commit message:
change program name and Client&Peripheral to Client&Server

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sat Apr 16 22:01:45 2016 +0000
+++ b/main.cpp	Thu Apr 21 11:23:26 2016 +0000
@@ -15,14 +15,14 @@
  */
 
 /*
- *  ------- BLE Peripheral UART function --------------------------------
+ *  ------- BLE Server UART function --------------------------------
  *      --- Tested on Switch Science mbed TY51822r3 ---
  *
  *      http://www.page.sannet.ne.jp/kenjia/index.html
  *      http://mbed.org/users/kenjiArai/
  *
  *      Started:  March     7th, 2016
- *      Revised:  April    17th, 2016
+ *      Revised:  April    21st, 2016
  *
  *  Original program:
  *      BLE_LoopbackUART
@@ -51,7 +51,7 @@
 #endif
 
 //  Object ----------------------------------------------------------------------------------------
-BLEDevice       ble;
+BLE             ble;
 DigitalOut      connectedLED(LED2);
 InterruptIn     wake_up_sw(P0_1);
 nRF51_WakeUp    wakeup(LED1, P0_0);
@@ -62,24 +62,25 @@
 
 //  ROM / Constant data ---------------------------------------------------------------------------
 #warning "You need to confirm your device name."
-const char      *deviceName = "UART_P";
+const static char       DEVICE_NAME[] = "UART_S";
+
 
 //  RAM -------------------------------------------------------------------------------------------
-Gap::Address_t      my_mac;
-uint8_t             tx_buf[24];
-uint8_t             tx_len          = 0;
-uint8_t             rx_buf[24];
-uint8_t             rx_len          = 0;
+Gap::Address_t   my_mac;
+uint8_t          tx_buf[24];
+uint8_t          tx_len          = 0;
+uint8_t          rx_buf[24];
+uint8_t          rx_len          = 0;
 volatile bool    trigger_transmit   = false;
 volatile bool    trigger_receive    = false;
 volatile uint8_t command_continue   = 0;
-uint16_t            time_out_cntr   = 3600;
-volatile bool       time_out        = false;
-bool                line_input      = false;
-uint8_t             linebuf_irq[24];
-int                 linebf_irq_len  = 0;
-uint8_t             linebuf[24];
-int                 linebf_len      = 0;
+uint16_t         time_out_cntr   = 3600;
+volatile bool    time_out        = false;
+bool             line_input      = false;
+uint8_t          linebuf_irq[24];
+int              linebf_irq_len  = 0;
+uint8_t          linebuf[24];
+int              linebf_len      = 0;
 
 //  Function prototypes ---------------------------------------------------------------------------
 //      BLE
@@ -108,8 +109,8 @@
     pc.attach(&serialRxCallback, Serial::RxIrq);
     ticker.attach(periodicCallback, 1);
     for (int k = 0; k < 20; k++) { pc.printf("\r\n");}          // clear terminal output
-    pc.printf("UART Communication / Peripheral side\r\n");      // opening message
-    pc.printf("  Client(Central) and Peripheral(device)\r\n");  // opening message
+    pc.printf("UART Communication / Server side\r\n");      // opening message
+    pc.printf("  Client(Central) and Server(Peripheral)\r\n");  // opening message
     // Interrupt by switch
     wake_up_sw.fall(&interrupt_by_sw);
     ble.init();
@@ -125,20 +126,21 @@
         my_mac[0], my_mac[1], my_mac[2], my_mac[3], my_mac[4], my_mac[5]
     );
     pc.printf("  Please write above mac_board_?(?=1,2,...) data into Client main.cpp\r\n");
-    ble.setDeviceName((const uint8_t *)deviceName);
     ble.onDisconnection(disconnectionCallback);
     ble.onDataWritten(onDataWritten);
     /* setup advertising */
     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
-                                    (const uint8_t *)deviceName,
-                                     strlen(deviceName)
-                                    );
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
-                                    (const uint8_t *)UARTServiceUUID_reversed,
-                                     sizeof(UARTServiceUUID_reversed)
-                                    );
+    ble.accumulateAdvertisingPayload(
+        GapAdvertisingData::COMPLETE_LOCAL_NAME,
+        (const uint8_t *)DEVICE_NAME,
+        sizeof(DEVICE_NAME)
+    );
+    ble.accumulateAdvertisingPayload(
+        GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
+        (const uint8_t *)UARTServiceUUID_reversed,
+         sizeof(UARTServiceUUID_reversed)
+    );
     // Advertize Interval
     ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
     // Start
@@ -166,7 +168,7 @@
         if (trigger_transmit){
             trigger_transmit = false;
             pc.printf("%s\r\n", rx_buf);
-            if (rx_buf[0] == '*'){          
+            if ((rx_buf[0] == '*') || (rx_buf[0] == '&')){          
                 switch (rx_buf[1]){
                     case 'v':
                         action_tx_vdd();
@@ -245,22 +247,22 @@
 
 void action_tx_help(){
                 //          12345678901234567890
-    sprintf((char *)tx_buf,"?:help by JH1PJL");
+    sprintf((char *)tx_buf,"*? or &?:help");
     tx_len = strlen((const char *)tx_buf); 
     Update_Values();
     wait(0.2);
                 //          12345678901234567890
-    sprintf((char *)tx_buf,"v:vdd");
+    sprintf((char *)tx_buf,"*v:vdd");
     tx_len = strlen((const char *)tx_buf); 
     Update_Values();
     wait(0.2);
                 //          12345678901234567890
-    sprintf((char *)tx_buf,"t:temperature");
+    sprintf((char *)tx_buf,"*t:temperature");
     tx_len = strlen((const char *)tx_buf); 
     Update_Values();
     wait(0.2);
                 //          12345678901234567890
-    sprintf((char *)tx_buf,"q:quit/sleep");
+    sprintf((char *)tx_buf,"*q:quit/sleep");
     tx_len = strlen((const char *)tx_buf); 
     Update_Values();
     wait(0.2);