Kris Scholte Lubberink
/
SSS_Ble
Ble for smart sOlutions
Diff: source/ColorService.h
- Revision:
- 6:ee9c86f06eae
- Parent:
- 3:f594022fe519
- Child:
- 7:9cda1b0f25ae
diff -r 94adaf1f8d51 -r ee9c86f06eae source/ColorService.h --- a/source/ColorService.h Sat Apr 20 11:04:45 2019 +0000 +++ b/source/ColorService.h Mon May 20 09:55:38 2019 +0200 @@ -1,35 +1,40 @@ -#ifndef __BLE_TEMPHUM_SERVICE_H__ +#ifndef __BLE_COLOR_SERVICE_H__ #define __BLE_COLOR_SERVICE_H__ #include "ble/BLE.h" #include "CustomUUIDs.h" -Serial pc(USBTX, USBRX); +#include "Adafruit_WS2801/Adafruit_WS2801.h" +#define STRIP_LENGTH 20 class ColorService { -public: - typedef int ColorType_t[STRIP_LENGTH * 4]; +public: /** * @brief ColorService constructor. * @param ble Reference to BLE device. */ + + typedef int ColorType_t[STRIP_LENGTH * 4]; ColorService(BLE& _ble) : - ble(_ble), - colorCharacteristic(CustomUUIDs::UUID_COLOR_CHAR, &color) + ble(_ble), + mystrip(Adafruit_WS2801(STRIP_LENGTH, p26,p27, WS2801_RGB)), + colorCharacteristic((UUID)CustomUUIDs::UUID_COLOR_CHAR, &color) { +// this->mystrip(); + 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, + charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); ble.gattServer().addService(colorService); - ble.onDataWritten(this, &ColorService::onDataWritten); + ble.gattServer().onDataWritten(this, &ColorService::onDataWritten); serviceAdded = true; } @@ -38,40 +43,44 @@ * @brief Update temperature characteristic. * @param newTemperatureVal New temperature measurement. */ - void updateColor(ColorType_t newColorVal) + void updateColor(const ColorType_t newColorVal) { for (int i = 0; i < STRIP_LENGTH; i++) { - pc.printf("Index: %d, val %d \r\n", i, *((int*)newColorVal + 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; - 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."); + 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 < 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); + 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]; - mystrip.setPixelColor(ledIndex, color[i]); + this->mystrip.setPixelColor(ledIndex, color[i]); ledIndex++; - pc.printf("Index: %d, val %08X \r\n", ledIndex, color[i]); + printf("Index: %d, val %08X \r\n", ledIndex, color[i]); } } - mystrip.show(); + this->mystrip.show(); } 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