Ble for smart sOlutions

Dependencies:   Adafruit_WS2801

Committer:
krissl
Date:
Sat Apr 20 11:02:35 2019 +0000
Revision:
3:f594022fe519
Parent:
1:9fc54848a198
Child:
6:ee9c86f06eae
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
krissl 1:9fc54848a198 1 #ifndef __BLE_TEMPHUM_SERVICE_H__
krissl 1:9fc54848a198 2 #define __BLE_COLOR_SERVICE_H__
krissl 1:9fc54848a198 3
krissl 1:9fc54848a198 4 #include "ble/BLE.h"
krissl 1:9fc54848a198 5 #include "CustomUUIDs.h"
krissl 3:f594022fe519 6 Serial pc(USBTX, USBRX);
krissl 3:f594022fe519 7
krissl 1:9fc54848a198 8 class ColorService {
krissl 3:f594022fe519 9 public:
krissl 3:f594022fe519 10 typedef int ColorType_t[STRIP_LENGTH * 4];
krissl 1:9fc54848a198 11 /**
krissl 1:9fc54848a198 12 * @brief ColorService constructor.
krissl 1:9fc54848a198 13 * @param ble Reference to BLE device.
krissl 1:9fc54848a198 14 */
krissl 1:9fc54848a198 15 ColorService(BLE& _ble) :
krissl 1:9fc54848a198 16 ble(_ble),
krissl 1:9fc54848a198 17 colorCharacteristic(CustomUUIDs::UUID_COLOR_CHAR, &color)
krissl 1:9fc54848a198 18 {
krissl 1:9fc54848a198 19 static bool serviceAdded = false; /* We should only ever need to add the information service once. */
krissl 1:9fc54848a198 20 if (serviceAdded) {
krissl 1:9fc54848a198 21 return;
krissl 1:9fc54848a198 22 }
krissl 1:9fc54848a198 23
krissl 1:9fc54848a198 24 GattCharacteristic *charTable[] = { &colorCharacteristic };
krissl 1:9fc54848a198 25
krissl 3:f594022fe519 26 GattService colorService(
krissl 1:9fc54848a198 27 CustomUUIDs::UUID_COLOR_SERVICE,
krissl 1:9fc54848a198 28 charTable,
krissl 1:9fc54848a198 29 sizeof(charTable) / sizeof(GattCharacteristic *));
krissl 3:f594022fe519 30 ble.gattServer().addService(colorService);
krissl 1:9fc54848a198 31
krissl 3:f594022fe519 32 ble.onDataWritten(this, &ColorService::onDataWritten);
krissl 1:9fc54848a198 33 serviceAdded = true;
krissl 1:9fc54848a198 34 }
krissl 1:9fc54848a198 35
krissl 1:9fc54848a198 36
krissl 1:9fc54848a198 37 /**
krissl 1:9fc54848a198 38 * @brief Update temperature characteristic.
krissl 1:9fc54848a198 39 * @param newTemperatureVal New temperature measurement.
krissl 1:9fc54848a198 40 */
krissl 1:9fc54848a198 41 void updateColor(ColorType_t newColorVal)
krissl 1:9fc54848a198 42 {
krissl 3:f594022fe519 43 for (int i = 0; i < STRIP_LENGTH; i++) {
krissl 3:f594022fe519 44 pc.printf("Index: %d, val %d \r\n", i, *((int*)newColorVal + i));
krissl 3:f594022fe519 45
krissl 3:f594022fe519 46 color[i] = ( *((int*)newColorVal + i) );
krissl 3:f594022fe519 47 }
krissl 3:f594022fe519 48
krissl 1:9fc54848a198 49 ble.gattServer().write(colorCharacteristic.getValueHandle(), (uint8_t *) &color, sizeof(ColorType_t));
krissl 1:9fc54848a198 50 }
krissl 3:f594022fe519 51
krissl 3:f594022fe519 52 virtual void onDataWritten(const GattWriteCallbackParams *writeParams) {
krissl 3:f594022fe519 53 uint16_t handle = writeParams->handle;
krissl 3:f594022fe519 54 pc.printf("You wrote some data... But for which char?");
krissl 3:f594022fe519 55 pc.printf("%d == %d", handle, colorCharacteristic.getValueHandle());//colorCharacteristic.getValueHandle());
krissl 3:f594022fe519 56 if(handle == colorCharacteristic.getValueHandle()){
krissl 3:f594022fe519 57 pc.printf("You wrote a color!");
krissl 3:f594022fe519 58 pc.printf("The value should be an array of integers.");
krissl 3:f594022fe519 59 // color = writeParams->data;
krissl 3:f594022fe519 60 int ledIndex = 0;
krissl 3:f594022fe519 61 for (int i=0; i < mystrip.numPixels()*4; i+=4) {
krissl 3:f594022fe519 62 color[i] = writeParams->data[i] << 24 | (writeParams->data[i+1] & 0xff) << 16 | (writeParams->data[i+2] & 0xff) << 8| (writeParams->data[i+3] & 0xff);
krissl 3:f594022fe519 63 // color[i] = writeParams->data[i];
krissl 3:f594022fe519 64 mystrip.setPixelColor(ledIndex, color[i]);
krissl 3:f594022fe519 65 ledIndex++;
krissl 3:f594022fe519 66 pc.printf("Index: %d, val %08X \r\n", ledIndex, color[i]);
krissl 3:f594022fe519 67
krissl 3:f594022fe519 68 }
krissl 3:f594022fe519 69 }
krissl 3:f594022fe519 70 mystrip.show();
krissl 3:f594022fe519 71 }
krissl 1:9fc54848a198 72 private:
krissl 1:9fc54848a198 73 BLE& ble;
krissl 1:9fc54848a198 74 ColorType_t color;
krissl 1:9fc54848a198 75 ReadWriteGattCharacteristic<ColorType_t> colorCharacteristic;
krissl 1:9fc54848a198 76 };
krissl 1:9fc54848a198 77 #endif /* #ifndef __BLE_COLOR_SERVICE_H__*/