Ble for smart sOlutions

Dependencies:   Adafruit_WS2801

Revision:
10:d845189d146e
Parent:
9:92d861703f96
Child:
11:d6ed1437c2ee
--- a/source/BleDevice.h	Tue Jun 11 11:16:55 2019 +0200
+++ b/source/BleDevice.h	Fri Jun 14 10:24:57 2019 +0200
@@ -15,6 +15,7 @@
 #include "ble/DiscoveredService.h"
 #include "ble/gap/Gap.h"
 #include "ble/gap/AdvertisingDataParser.h"
+#include "MyStripSingleton.h"
 #include <events/mbed_events.h>
 
 
@@ -368,15 +369,67 @@
         }
         printf(".\r\n");
 
-//        Properties_t properties = _>getProperties();
+//        Characteristic value at 6 equal to: 0xFF 0x00 0xFF 0x04 0xFF 0x55 0xFF 0x01 0x05 0x05 0x05 0x07 0xFF 0xFF 0xFF 0x06 0xFF 0xEE 0xFF 0x09 .
+        //TODO: Nummer op pos 3, 7, 11, 15, 19 vergelijken met mijn interesses. Als er eentje overeen komt, licht dan op.
+        for(int i = 3; i < 20; i += 4){
+            int targetInterest = read_event->data[i];
+            printf("Let's see if there's a match between you guys..\r\n");
+            for(int j = 0; j < 5; j++){
+                    if(MyStripSingleton::getInstance()->ints.interest1 >> 24 == targetInterest){
+                        printf("Match on your interest 1!\r\n");
+                        uint8_t byte1 =  MyStripSingleton::getInstance()->ints.interest1 & 0x000000ff;
+                        uint8_t byte2 = (MyStripSingleton::getInstance()->ints.interest1 & 0x0000ff00) >> 8;
+                        uint8_t byte3 = (MyStripSingleton::getInstance()->ints.interest1 & 0x00ff0000) >> 16;
+                        uint32_t color = byte3 << 16 | byte2 << 8 | byte3;
+                        MyStripSingleton::getInstance()->ambientColor = color;
+                        MyStripSingleton::getInstance()->solidColor(color);
+                        break;
+                    } else if(MyStripSingleton::getInstance()->ints.interest2 >> 24 == targetInterest){
+                        printf("Match on your interest 2!\r\n");
+                        uint8_t byte1 =  MyStripSingleton::getInstance()->ints.interest2 & 0x000000ff;
+                        uint8_t byte2 = (MyStripSingleton::getInstance()->ints.interest2 & 0x0000ff00) >> 8;
+                        uint8_t byte3 = (MyStripSingleton::getInstance()->ints.interest2 & 0x00ff0000) >> 16;
+                        uint32_t color = byte3 << 16 | byte2 << 8 | byte3;
+                        MyStripSingleton::getInstance()->ambientColor = color;
+                        MyStripSingleton::getInstance()->solidColor(((unsigned char *)MyStripSingleton::getInstance()->ints.interest2)[3] << 16 | ((unsigned char *)MyStripSingleton::getInstance()->ints.interest2)[2] << 8 | ((unsigned char *)MyStripSingleton::getInstance()->ints.interest2)[1]);
+                        break;
+                    } else if(MyStripSingleton::getInstance()->ints.interest3 >> 24 == targetInterest){
+                        printf("Match on your interest 3!\r\n");
+                        uint8_t byte1 =  MyStripSingleton::getInstance()->ints.interest3 & 0x000000ff;
+                        uint8_t byte2 = (MyStripSingleton::getInstance()->ints.interest3 & 0x0000ff00) >> 8;
+                        uint8_t byte3 = (MyStripSingleton::getInstance()->ints.interest3 & 0x00ff0000) >> 16;
+                        uint32_t color = byte3 << 16 | byte2 << 8 | byte3;
+                        MyStripSingleton::getInstance()->ambientColor = color;
+                        MyStripSingleton::getInstance()->solidColor(((unsigned char *)MyStripSingleton::getInstance()->ints.interest3)[3] << 16 | ((unsigned char *)MyStripSingleton::getInstance()->ints.interest3)[2] << 8 | ((unsigned char *)MyStripSingleton::getInstance()->ints.interest3)[1]);
+                        break;
+                    } else if(MyStripSingleton::getInstance()->ints.interest4 >> 24 == targetInterest){
+                        printf("Match on your interest 4!\r\n");
+                        uint8_t byte1 =  MyStripSingleton::getInstance()->ints.interest4 & 0x000000ff;
+                        uint8_t byte2 = (MyStripSingleton::getInstance()->ints.interest4 & 0x0000ff00) >> 8;
+                        uint8_t byte3 = (MyStripSingleton::getInstance()->ints.interest4 & 0x00ff0000) >> 16;
+                        uint32_t color = byte3 << 16 | byte2 << 8 | byte3;
+                        MyStripSingleton::getInstance()->ambientColor = color;
+                        MyStripSingleton::getInstance()->solidColor(((unsigned char *)MyStripSingleton::getInstance()->ints.interest4)[3] << 16 | ((unsigned char *)MyStripSingleton::getInstance()->ints.interest4)[2] << 8 | ((unsigned char *)MyStripSingleton::getInstance()->ints.interest4)[1]);
+                        break;
+                    } else if(MyStripSingleton::getInstance()->ints.interest5 >> 24 == targetInterest){
+                        printf("Match on your interest 5!\r\n");
+                        uint8_t byte1 =  MyStripSingleton::getInstance()->ints.interest5 & 0x000000ff;
+                        uint8_t byte2 = (MyStripSingleton::getInstance()->ints.interest5 & 0x0000ff00) >> 8;
+                        uint8_t byte3 = (MyStripSingleton::getInstance()->ints.interest5 & 0x00ff0000) >> 16;
+                        uint32_t color = byte3 << 16 | byte2 << 8 | byte3;
+                        MyStripSingleton::getInstance()->ambientColor = color;
+                        MyStripSingleton::getInstance()->solidColor(((unsigned char *)MyStripSingleton::getInstance()->ints.interest5)[3] << 16 | ((unsigned char *)MyStripSingleton::getInstance()->ints.interest5)[2] << 8 | ((unsigned char *)MyStripSingleton::getInstance()->ints.interest5)[1]);
+                        break;
+                    } else {
+                        //No matches, too bad.
+                    }
+            }
+        }
+        ble::local_disconnection_reason_t res = ble::local_disconnection_reason_t::LOW_RESOURCES;
+        printf("[CENTRAL]\t Disconnect from client, we got what we need \r\n");
 
-//        if(properties.notify() || properties.indicate()) {
-//            discover_descriptors(_it->value);
-//        } else {
-//            process_next_characteristic();
-//        }
+        _ble.gap().disconnect((ble::connection_handle_t )read_event->connHandle, (ble::local_disconnection_reason_t)res);
     }
-
     /* Event handler */
 
     /** This is called by Gap to notify the application we disconnected,
@@ -386,8 +439,10 @@
         printf("Disconnected\r\n");
         _event_queue.break_dispatch();
         //And/or resume advertising?
+    };
+    virtual void compareInterests(const GattReadCallbackParams *read_event){
 
-    };
+    }
 
     virtual void onAdvertisingEnd(const ble::AdvertisingEndEvent &)
     {
@@ -409,28 +464,12 @@
         if (event.getStatus() == BLE_ERROR_NONE) {
             /* store the handle for future Security Manager requests */
             _handle = event.getConnectionHandle();
-
             printf("Connected\r\n");
 
-            /* in this example the local device is the master so we request pairing */
-//            ble_error_t error = _ble.securityManager().requestPairing(_handle);
-
-//            if (error) {
-//                printf("Error during SM::requestPairing %d\r\n", error);
-//                return;
-//            }
             _ble.gattClient().onServiceDiscoveryTermination(makeFunctionPointer(this, &Self::when_service_discovery_ends));
             _ble.gattClient().launchServiceDiscovery(_handle, makeFunctionPointer(this, &Self::when_service_discovered), makeFunctionPointer(this, &Self::when_characteristic_discovered));
             /* upon pairing success the application will disconnect */
         }
-
-        /* failed to connect - restart scan */
-//        ble_error_t error = _ble.gap().startScan();
-
-//        if (error) {
-//            print_error(error, "Error in Gap::startScan %d\r\n");
-//            return;
-//        }
     }
 
 private: