Dependencies:   BLE_API mbed

Revision:
4:a43cbe8aed6a
Parent:
3:53cd39af0b12
Child:
5:a8c07a29df8a
diff -r 53cd39af0b12 -r a43cbe8aed6a main.cpp
--- a/main.cpp	Mon Feb 22 16:21:00 2016 +0000
+++ b/main.cpp	Wed Jun 01 15:17:19 2016 +0000
@@ -2,17 +2,17 @@
 Primera Linea por Gustavo Belduma
 Copyright (c) 2012-2014 RedBearLab
 
-Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
-and associated documentation files (the "Software"), to deal in the Software without restriction, 
-including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
-and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+and associated documentation files (the "Software"), to deal in the Software without restriction,
+including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
+and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
 subject to the following conditions:
 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
-INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
-PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
-FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
+INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
+PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
+FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
 */
@@ -62,9 +62,9 @@
 
 
 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 *));
@@ -77,83 +77,72 @@
     pc.printf("Restart advertising \r\n");
     ble.startAdvertising();
 }
-*/ 
+*/
 
 // La de arriba era la original, pero se cambio para que funcione, solucion sacada de la web
 // https://developer.mbed.org/forum/repo-61676-BLE_GAP_Example-community/topic/17193/
-void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) 
-{ 
-    BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); 
+void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
+{
+    BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
     pc.printf("Disconnected \r\n");
     pc.printf("Restart advertising \r\n");
-    ble.startAdvertising(); 
-    
+    ble.startAdvertising();
+
 }
 
 
 void WrittenHandler(const GattWriteCallbackParams *Handler)
-{   
+{
     uint8_t buf[TXRX_BUF_LEN];
     uint16_t bytesRead, index;
-    
-    if (Handler->handle == txCharacteristic.getValueAttribute().getHandle()) 
-    {
+
+    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);  
-        
+        memcpy(txPayload, buf, TXRX_BUF_LEN);
+
         //por dixys
-        pc.printf("Buffer recibido desde Android: \r\n");     
-        
+        pc.printf("Buffer recibido desde Android: \r\n");
+
         for(index=0; index<bytesRead; index++)
             pc.putc(buf[index]);
-            
-        if(buf[0] == 0x01)
-        {
-            if(buf[1] == 0x01){
+
+        if(buf[0] == 0x01) {
+            if(buf[1] == 0x01) {
                 LED_SET = 1;
                 //por dixys
                 pc.printf("LED SET 1 \r\n");
-            }else{
-                LED_SET = 0;    
+            } else {
+                LED_SET = 0;
                 //por dixys
                 pc.printf("LED SET 0 \r\n");
             }
-        }
-        else if(buf[0] == 0xA0)
-        {
-            if(buf[1] == 0x01){
+        } else if(buf[0] == 0xA0) {
+            if(buf[1] == 0x01) {
                 analog_enabled = 1;
                 //por dixys
                 pc.printf("ANALOG ENABLE \r\n");
-            }
-            else {
+            } else {
                 //analog_enabled = 0;
                 // lo vamos a poner fio enable para hacer pruebas luego lo quitamos
                 //por dixys
                 pc.printf("ANALOG DISAABLE \r\n");
             }
-        }
-        else if(buf[0] == 0x02)
-        {
+        } else if(buf[0] == 0x02) {
             float value = (float)buf[1]/255;
             PWM = value;
             //por dixys
             pc.printf("PWM = %f  \r\n", value);
-        }
-        else if(buf[0] == 0x03)
-        {
+        } else if(buf[0] == 0x03) {
             //MYSERVO.write(buf[1]);
             //por dixys
             pc.printf("SERVO buffer  \r\n");
-        }
-        else if(buf[0] == 0x04)
-        {
+        } else if(buf[0] == 0x04) {
             analog_enabled = 0;
             PWM = 0;
             //MYSERVO.write(0);
             LED_SET = 0;
-            old_state = 0;    
+            old_state = 0;
             //por dixys
             pc.printf("opcion 4 \r\n");
         }
@@ -162,13 +151,13 @@
 }
 /*
 void uartCB(void)
-{   
-    while(pc.readable())    
+{
+    while(pc.readable())
     {
-        rx_buf[rx_len++] = pc.getc();    
+        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); 
+            ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
             pc.printf("RecHandler \r\n");
             pc.printf("Length: ");
             pc.putc(rx_len);
@@ -180,113 +169,73 @@
 }
 */
 void m_status_check_handle(void)
-{   
+{
     uint8_t buf[3];
-    if (analog_enabled)  // if analog reading enabled
-    {
+    if (analog_enabled) { // if analog reading enabled
         // Read and send out
         float s = ANALOG;
-        uint16_t value = s*1024; 
+        uint16_t value = s*1024;
         buf[0] = (0x0B);
         buf[1] = (value >> 8);
         buf[2] = (value);
-        ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 
+        ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
     }
-    
+
     // If digital in changes, report the state
-    if (BUTTON != old_state)
-    {
+    if (BUTTON != old_state) {
         old_state = BUTTON;
-        
-        if (BUTTON == 1)
-        {
+
+        if (BUTTON == 1) {
             buf[0] = (0x0A);
             buf[1] = (0x01);
-            buf[2] = (0x00);    
-            ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3); 
-        }
-        else
-        {
+            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); 
+            ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), buf, 3);
         }
     }
 }
 
-
 int main(void)
-{   
+{
     Ticker ticker;
     ticker.attach_us(m_status_check_handle, 200000);
-    
+
     ble.init();
     ble.onDisconnection(disconnectionCallback);
-    
-    ble.onDataWritten(WrittenHandler);  
-    
+
+    ble.onDataWritten(WrittenHandler);
+
     pc.baud(9600);
     pc.printf("SimpleChat Init \r\n");
 
     //pc.attach( uartCB , pc.RxIrq);
-    
-    // setup advertising 
+
+    // setup advertising
     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
-                                    //(const uint8_t *)"RedBearLab_1", sizeof("RedBearLab_1") - 1);   // Original:  Biscuit
-                                    (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1);   // Original:  Biscuit
+                                     //(const uint8_t *)"RedBearLab_1", sizeof("RedBearLab_1") - 1);   // Original:  Biscuit
+                                     (const uint8_t *)"Biscuit", sizeof("Biscuit") - 1);   // Original:  Biscuit
     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. 
+                                     (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
+    // 100ms; in multiples of 0.625ms.
     ble.setAdvertisingInterval(160);
 
     ble.addService(uartService);
-    
-    ble.startAdvertising(); 
-    
+
+    ble.startAdvertising();
+
     pc.printf("Advertising Start \r\n");
-    
+
     //por dixys
     // para probar, luego quitar. Esto hace que cada ticker se envie un dato analogico via BLE
     analog_enabled = 1;
-                
-    
-    while(1)
-    {
-        ble.waitForEvent(); 
+
+    while(1) {
+        ble.waitForEvent();
     }
-}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+}
\ No newline at end of file