Ble for smart sOlutions

Dependencies:   Adafruit_WS2801

Revision:
3:f594022fe519
Parent:
1:9fc54848a198
Child:
6:ee9c86f06eae
--- a/source/ColorService.h	Mon Mar 25 19:02:54 2019 +0000
+++ b/source/ColorService.h	Sat Apr 20 11:02:35 2019 +0000
@@ -3,9 +3,11 @@
 
 #include "ble/BLE.h"
 #include "CustomUUIDs.h"
+Serial pc(USBTX, USBRX);
+
 class ColorService {
-public:                                
-    typedef int16_t  ColorType_t;
+public:             
+    typedef int ColorType_t[STRIP_LENGTH * 4];
     /**
      * @brief   ColorService constructor.
      * @param   ble Reference to BLE device.
@@ -21,12 +23,13 @@
 
         GattCharacteristic *charTable[] = { &colorCharacteristic };
 
-        GattService tempHumService(
+        GattService colorService(
         CustomUUIDs::UUID_COLOR_SERVICE,
         charTable, 
         sizeof(charTable) / sizeof(GattCharacteristic *));
+        ble.gattServer().addService(colorService);
 
-        ble.gattServer().addService(tempHumService);
+        ble.onDataWritten(this, &ColorService::onDataWritten);
         serviceAdded = true;
     }
 
@@ -37,15 +40,38 @@
      */
     void updateColor(ColorType_t newColorVal)
     {
-        color = newColorVal;
+        for (int i = 0; i < STRIP_LENGTH; i++) {
+          pc.printf("Index: %d, val %d \r\n", i, *((int*)newColorVal + i));
+          
+          color[i] = ( *((int*)newColorVal + i) );
+        }
+
         ble.gattServer().write(colorCharacteristic.getValueHandle(), (uint8_t *) &color, sizeof(ColorType_t));
     }
-
+    
+    virtual void onDataWritten(const GattWriteCallbackParams *writeParams) {
+        uint16_t handle = writeParams->handle;
+        pc.printf("You wrote some data... But for which char?");
+        pc.printf("%d == %d", handle, colorCharacteristic.getValueHandle());//colorCharacteristic.getValueHandle());
+        if(handle == colorCharacteristic.getValueHandle()){
+            pc.printf("You wrote a color!");
+            pc.printf("The value should be an array of integers.");
+//            color = writeParams->data;
+            int ledIndex = 0;
+            for (int i=0; i < mystrip.numPixels()*4;  i+=4) {
+              color[i] = writeParams->data[i] << 24 | (writeParams->data[i+1] & 0xff) << 16 | (writeParams->data[i+2] & 0xff) << 8| (writeParams->data[i+3] & 0xff);  
+//              color[i] = writeParams->data[i];
+              mystrip.setPixelColor(ledIndex, color[i]);
+              ledIndex++;
+              pc.printf("Index: %d, val %08X \r\n", ledIndex, color[i]);
+              
+            }
+        }
+        mystrip.show();
+    }
 private:
     BLE& ble;
-
     ColorType_t color;
-    
     ReadWriteGattCharacteristic<ColorType_t>    colorCharacteristic;
 };
 #endif /* #ifndef __BLE_COLOR_SERVICE_H__*/
\ No newline at end of file