Ble for smart sOlutions

Dependencies:   Adafruit_WS2801

source/ColorService.h

Committer:
kris@kris-X682X
Date:
2019-05-21
Revision:
8:369b80cef5ae
Parent:
7:9cda1b0f25ae
Child:
9:92d861703f96

File content as of revision 8:369b80cef5ae:

#ifndef __BLE_COLOR_SERVICE_H__
#define __BLE_COLOR_SERVICE_H__

#include "ble/BLE.h"
#include "CustomUUIDs.h"
#define STRIP_LENGTH 20

class ColorService {
public:
    /**
     * @brief   ColorService constructor.
     * @param   ble Reference to BLE device.
     */

    typedef int ColorType_t[STRIP_LENGTH * 4];
    ColorService(BLE& _ble) :
    ble(_ble),
    mystrip(Adafruit_WS2801(STRIP_LENGTH, p26,p27, WS2801_RGB)),
    colorCharacteristic((UUID)CustomUUIDs::UUID_COLOR_CHAR, &color)
    {
        static bool serviceAdded = false; /* We should only ever need to add the information service once. */
        if (serviceAdded) {
            return;
        }
        printf("[PERIPHERAL] Adding service.. \r\n");
        GattCharacteristic *charTable[] = { &colorCharacteristic };

        GattService colorService(
        CustomUUIDs::UUID_COLOR_SERVICE,
        charTable,
        sizeof(charTable) / sizeof(GattCharacteristic *));
        ble.gattServer().addService(colorService);

        ble.gattServer().onDataWritten(this, &ColorService::onDataWritten);
//        serviceAdded = true;
    }


    /**
     * @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));
    }

    virtual ~ColorService() {
        printf("[PERIPHERAL]\t Destructing ColorService");
    }

    virtual void onDataWritten(const GattWriteCallbackParams *writeParams) {
        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()){
            printf("You wrote a color!");
            printf("The value should be an array of integers.");
//            color = writeParams->data;
            int ledIndex = 0;
            for (int i=0; i < this->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];
              this->mystrip.setPixelColor(ledIndex, color[i]);
              ledIndex++;
              printf("Index: %d, val %08X \r\n", ledIndex, color[i]);
              
            }
        }
        this->mystrip.show();
    }
private:
    BLE& ble;
    ColorType_t color;
    Adafruit_WS2801 mystrip;
    ReadWriteGattCharacteristic<ColorType_t>    colorCharacteristic;
};
#endif /* #ifndef __BLE_COLOR_SERVICE_H__*/