Ble for smart sOlutions

Dependencies:   Adafruit_WS2801

source/ColorService.h

Committer:
krissl
Date:
2019-04-20
Revision:
3:f594022fe519
Parent:
1:9fc54848a198
Child:
6:ee9c86f06eae

File content as of revision 3:f594022fe519:

#ifndef __BLE_TEMPHUM_SERVICE_H__
#define __BLE_COLOR_SERVICE_H__

#include "ble/BLE.h"
#include "CustomUUIDs.h"
Serial pc(USBTX, USBRX);

class ColorService {
public:             
    typedef int ColorType_t[STRIP_LENGTH * 4];
    /**
     * @brief   ColorService constructor.
     * @param   ble Reference to BLE device.
     */
    ColorService(BLE& _ble) :
        ble(_ble),
        colorCharacteristic(CustomUUIDs::UUID_COLOR_CHAR, &color)
    {
        static bool serviceAdded = false; /* We should only ever need to add the information service once. */
        if (serviceAdded) {
            return;
        }

        GattCharacteristic *charTable[] = { &colorCharacteristic };

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

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


    /**
     * @brief   Update temperature characteristic.
     * @param   newTemperatureVal New temperature measurement.
     */
    void updateColor(ColorType_t 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__*/