First Commit

Dependencies:   mbed BLE_API nRF51822

Revision:
4:7e5d4a18cd22
Parent:
3:f530ca03e014
Child:
5:95238da08cee
--- a/main.cpp	Thu Jan 07 02:43:53 2016 +0000
+++ b/main.cpp	Sat May 20 11:54:14 2017 +0000
@@ -17,67 +17,72 @@
 
 */
 
+/*
+ *    The application works with the BlueJelly.js 
+ */
+ 
+//======================================================================
+//Grobal
+//====================================================================== 
+//------------------------------------------------------------
+//Include Header Files
+//------------------------------------------------------------ 
 #include "mbed.h"
 #include "ble/BLE.h"
-#include "Servo.h"
 
 
-#define BLE_UUID_TXRX_SERVICE            0x0000 /**< The UUID of the Nordic UART Service. */
-#define BLE_UUID_TX_CHARACTERISTIC       0x0002 /**< The UUID of the TX Characteristic. */
-#define BLE_UUIDS_RX_CHARACTERISTIC      0x0003 /**< The UUID of the RX Characteristic. */
-
-#define TXRX_BUF_LEN                     20
-
-#define DIGITAL_OUT_PIN                  P0_9       //TXD
-#define DIGITAL_IN_PIN                   P0_10      //CTS
-#define PWM_PIN                          P0_11      //RXD
-#define SERVO_PIN                        P0_8       //RTS
-#define ANALOG_IN_PIN                    P0_4       //P04
-
-BLE             ble;
-
-DigitalOut      LED_SET(DIGITAL_OUT_PIN);
-DigitalIn       BUTTON(DIGITAL_IN_PIN);
-PwmOut          PWM(PWM_PIN);
-AnalogIn        ANALOG(ANALOG_IN_PIN);
-Servo           MYSERVO(SERVO_PIN);
-
-//Serial pc(USBTX, USBRX);
-
-static uint8_t analog_enabled = 0;
-static uint8_t old_state = 0;
-
-// The Nordic UART Service
-static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
-static const uint8_t uart_tx_uuid[]   = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
-static const uint8_t uart_rx_uuid[]   = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
-static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
+//------------------------------------------------------------
+//Definition
+//------------------------------------------------------------ 
+#define TXRX_BUF_LEN 20                     //max 20
+#define DEVICE_LOCAL_NAME "BlueJelly!!"     
+#define ADVERTISING_INTERVAL 160            //160 * 0.625[ms] = 100[ms]
+#define TICKER_TIME 200000                  //[us]
+#define ANALOG_IN_PIN P0_4
 
 
+//------------------------------------------------------------
+//Object generation
+//------------------------------------------------------------ 
+BLE ble;
+AnalogIn ANALOG(ANALOG_IN_PIN);
+
+
+//------------------------------------------------------------
+//Service & Characteristic Setting
+//------------------------------------------------------------ 
+//Service UUID
+static const uint8_t base_uuid[] = { 0x71, 0x3D, 0x00, 0x00, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E } ;
+
+//Characteristic UUID
+static const uint8_t tx_uuid[]   = { 0x71, 0x3D, 0x00, 0x03, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E } ;
+static const uint8_t rx_uuid[]   = { 0x71, 0x3D, 0x00, 0x02, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E } ;
+
+//Characteristic Value
 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
 
-//static uint8_t rx_buf[TXRX_BUF_LEN];
-//static uint8_t rx_len=0;
+//Characteristic Property Setting etc
+GattCharacteristic  txCharacteristic (tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
+GattCharacteristic  rxCharacteristic (rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY| GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
+GattCharacteristic *myChars[] = {&txCharacteristic, &rxCharacteristic};
+
+//Service Setting
+GattService         myService(base_uuid, myChars, sizeof(myChars) / sizeof(GattCharacteristic *));
 
 
-GattCharacteristic  txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
-                                      
-GattCharacteristic  rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
-                                      
-GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
-
-GattService         uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
+//======================================================================
+//onDisconnection
+//======================================================================
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
+{
+    ble.startAdvertising();
+}
 
 
-
-void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
-{
-    //pc.printf("Disconnected \r\n");
-    //pc.printf("Restart advertising \r\n");
-    ble.gap().startAdvertising();
-}
-
+//======================================================================
+//onDataWritten
+//======================================================================
 void WrittenHandler(const GattWriteCallbackParams *Handler)
 {   
     uint8_t buf[TXRX_BUF_LEN];
@@ -86,165 +91,92 @@
     if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) 
     {
         ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
-        memset(txPayload, 0, TXRX_BUF_LEN);
-        memcpy(txPayload, buf, TXRX_BUF_LEN);       
-        
-        //for(index=0; index<bytesRead; index++)
-            //pc.putc(buf[index]);
-            
-        if(buf[0] == 0x01)
-        {
-            if(buf[1] == 0x01)
-                LED_SET = 1;
-            else
-                LED_SET = 0;    
-        }
-        else if(buf[0] == 0xA0)
-        {
-            if(buf[1] == 0x01)
-                analog_enabled = 1;
-            else
-                analog_enabled = 0;
-        }
-        else if(buf[0] == 0x02)
-        {
-            float value = (float)buf[1]/255;
-            PWM = value;
-        }
-        else if(buf[0] == 0x03)
-        {
-            MYSERVO.write(buf[1]);
-        }
-        else if(buf[0] == 0x04)
-        {
-            analog_enabled = 0;
-            PWM = 0;
-            MYSERVO.write(0);
-            LED_SET = 0;
-            old_state = 0;    
-        }
-
-    }
-}
-/*
-void uartCB(void)
-{   
-    while(pc.readable())    
-    {
-        rx_buf[rx_len++] = pc.getc();    
-        if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
-        {
-            ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len); 
-            pc.printf("RecHandler \r\n");
-            pc.printf("Length: ");
-            pc.putc(rx_len);
-            pc.printf("\r\n");
-            rx_len = 0;
-            break;
-        }
-    }
-}
-*/
-void m_status_check_handle(void)
-{   
-    uint8_t buf[3];
-    if (analog_enabled)  // if analog reading enabled
-    {
-        // Read and send out
-        float s = ANALOG;
-        uint16_t value = s*1024; 
-        buf[0] = (0x0B);
-        buf[1] = (value >> 8);
-        buf[2] = (value);
-        ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 
-    }
-    
-    // If digital in changes, report the state
-    if (BUTTON != old_state)
-    {
-        old_state = BUTTON;
-        
-        if (BUTTON == 1)
-        {
-            buf[0] = (0x0A);
-            buf[1] = (0x01);
-            buf[2] = (0x00);    
-            ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 
-        }
-        else
-        {
-            buf[0] = (0x0A);
-            buf[1] = (0x00);
-            buf[2] = (0x00);
-           ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 
-        }
+        memset(txPayload, 0, TXRX_BUF_LEN); //todo
+        memcpy(txPayload, buf, TXRX_BUF_LEN);     //todo  
     }
 }
 
 
+//======================================================================
+//onTimeout
+//======================================================================
+void m_status_check_handle(void)
+{   
+    uint8_t buf[2];
+
+    //Read Analog port
+    float s = ANALOG;
+    uint16_t value = s*1024; 
+    buf[0] = (value >> 8);
+    buf[1] = (value);
+    
+    //Send out
+    ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 2); 
+}
+
+
+//======================================================================
+//convert reverse UUID
+//======================================================================
+void reverseUUID(const uint8_t* src, uint8_t* dst)
+{
+    int i;
+    
+    for(i=0;i<16;i++)
+        dst[i] = src[15 - i];
+}
+
+
+//======================================================================
+//main
+//======================================================================
 int main(void)
-{   
+{
+    uint8_t base_uuid_rev[16];
+
+    //Timer Setting [us]
     Ticker ticker;
-    ticker.attach_us(m_status_check_handle, 200000);
+    ticker.attach_us(m_status_check_handle, TICKER_TIME);
     
+    //BLE init
     ble.init();
+    
+    //EventListener
     ble.onDisconnection(disconnectionCallback);
     ble.onDataWritten(WrittenHandler);  
+
+    //------------------------------------------------------------
+    //setup advertising 
+    //------------------------------------------------------------
+    //Classic BT not support
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
     
-    //pc.baud(9600);
-    //pc.printf("SimpleChat Init \r\n");
-
-    //pc.attach( uartCB , pc.RxIrq);
-    
-    // setup advertising 
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
+    //Connectable to Central
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
-    ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
-                                    (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1);
+    
+    //Local Name
+    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME,
+                                    (const uint8_t *)DEVICE_LOCAL_NAME, sizeof(DEVICE_LOCAL_NAME) - 1);
+    
+    //GAP AdvertisingData                                
+    reverseUUID(base_uuid, base_uuid_rev);  
     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
-                                    (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
-    // 100ms; in multiples of 0.625ms. 
-    ble.setAdvertisingInterval(160);
+                                    (uint8_t *)base_uuid_rev, sizeof(base_uuid));
+                                    
+    //Advertising Interval 
+    ble.setAdvertisingInterval(ADVERTISING_INTERVAL);
 
-    ble.addService(uartService);
+    //Add Service
+    ble.addService(myService);
     
+    //Start Advertising
     ble.startAdvertising(); 
     
-    //pc.printf("Advertising Start \r\n");
-    
+    //------------------------------------------------------------
+    //Loop
+    //------------------------------------------------------------
     while(1)
     {
         ble.waitForEvent(); 
     }
-}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+}
\ No newline at end of file