Ble for smart sOlutions

Dependencies:   Adafruit_WS2801

Revision:
10:d845189d146e
Parent:
9:92d861703f96
Child:
11:d6ed1437c2ee
--- a/source/ColorService.h	Tue Jun 11 11:16:55 2019 +0200
+++ b/source/ColorService.h	Fri Jun 14 10:24:57 2019 +0200
@@ -3,9 +3,8 @@
 
 #include "ble/BLE.h"
 #include "CustomUUIDs.h"
-#include "Adafruit_WS2801/Adafruit_WS2801.h"
+#include "MyStripSingleton.h"
 
-#define STRIP_LENGTH 37
 
 class ColorService {
 public:
@@ -13,26 +12,11 @@
      * @brief   ColorService constructor.
      * @param   ble Reference to BLE device.
      */
-
-    typedef int ColorType_t[STRIP_LENGTH * 4];
+    typedef uint32_t ColorType_t;
     ColorService(BLE& _ble) :
-    ble(_ble),
-    mystrip(Adafruit_WS2801(STRIP_LENGTH, p26,p27, WS2801_RGB)),
-    colorCharacteristic((UUID)CustomUUIDs::UUID_COLOR_CHAR, &color)
+            ble(_ble),
+            colorCharacteristic((UUID)CustomUUIDs::UUID_COLOR_CHAR, &color)
     {
-        mystrip.begin();
-
-        for (int z=STRIP_LENGTH; z >= 0 ; z--) {
-            mystrip.setPixelColor(z, 0xFFFFFF);
-            mystrip.show();
-        }
-
-        wait(0.5);
-
-        for (int z=STRIP_LENGTH; z >= 0 ; z--) {
-            mystrip.setPixelColor(z, 0);
-        }
-        mystrip.show();
         static bool serviceAdded = false; /* We should only ever need to add the information service once. */
         if (serviceAdded) {
             return;
@@ -41,9 +25,9 @@
         GattCharacteristic *charTable[] = { &colorCharacteristic };
 
         GattService colorService(
-        CustomUUIDs::UUID_COLOR_SERVICE,
-        charTable,
-        sizeof(charTable) / sizeof(GattCharacteristic *));
+                CustomUUIDs::UUID_COLOR_SERVICE,
+                charTable,
+                sizeof(charTable) / sizeof(GattCharacteristic *));
         ble.gattServer().addService(colorService);
 
         ble.gattServer().onDataWritten(this, &ColorService::onDataWritten);
@@ -55,15 +39,11 @@
      * @brief   Update temperature characteristic.
      * @param   newTemperatureVal New temperature measurement.
      */
-    void updateColor(const ColorType_t newColorVal)
-    {
-        for (int i = 0; i < STRIP_LENGTH; i++) {
-          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));
-    }
+//    void updateColor(const ColorType_t newColorVal)
+//    {
+//        color = ( *((int*)newColorVal ) );
+//        ble.gattServer().write(colorCharacteristic.getValueHandle(), (uint8_t *) &color, sizeof(ColorType_t));
+//    }
 
     virtual ~ColorService() {
         printf("[PERIPHERAL]\t Destructing ColorService");
@@ -73,28 +53,20 @@
         uint16_t handle = writeParams->handle;
         printf("You wrote some data... But for which char? \r\n");
         printf("%d == %d", handle, colorCharacteristic.getValueHandle());//colorCharacteristic.getValueHandle());
-       if(handle == colorCharacteristic.getValueHandle()){
+        if(handle == colorCharacteristic.getValueHandle()){
             printf("[PERIPHERAL]\t You wrote a color! \r\n");
-            printf("[PERIPHERAL]\t The value should be an array of integers.\r\n");
-//            color = writeParams->data;
-            int ledIndex = 0;
-         /*   for (int i=0; i < this->mystrip.getNpixels()*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];
-              this->mystrip.setColor(ledIndex, color[i]);
-              ledIndex++;
-              printf("[PERIPHERAL]\t Index: %d, val %08X \r\n", ledIndex, color[i]);
-              
-            }*/
+            printf("[PERIPHERAL]\t The value should be an integer.\r\n");
+            color = (unsigned  char)writeParams->data[0] << 16 | writeParams->data[1] << 8 | writeParams->data[2];
+            printf("[PERIPHERAL]\t Setting color %06X\n\r", color);
+
+            MyStripSingleton::getInstance()->ambientColor = color;
+            MyStripSingleton::getInstance()->solidColor(color);
         }
-       printf("[PERIPHERAL]\t Tryna set color\n\r");
-//       this->mystrip.show();
         printf("[PERIPHERAL]\t Did that work?\n\r");
     }
 private:
     BLE& ble;
     ColorType_t color;
-    Adafruit_WS2801 mystrip;
     ReadWriteGattCharacteristic<ColorType_t>    colorCharacteristic;
 };
 #endif /* #ifndef __BLE_COLOR_SERVICE_H__*/
\ No newline at end of file