Seeed / Mbed 2 deprecated BLE_Color_Pixels

Dependencies:   BLE_API color_pixels mbed nRF51822

Fork of BLE_LCDDemo by Bluetooth Low Energy

Color pixels library using WS2812B and nRF51822 (16MHz)

http://www.seeedstudio.com/depot/bmz_cache/4/4f346dc15724a7b5a5c1383253aeefc9.image.530x397.jpg

/media/uploads/yihui/color_pixels.png

You can get the colorful led strip from seeed:

Click this link to download the color pixels app for android. The source code of the Android app is at https://github.com/Seeed-Studio/ble_color_pixels

If the BLE device is disconnected frequently, we can improve the stability by changing the BLE parameters - Advertising Duration (main.cpp), Min Interval and Max Interval (nRF51822/projectconfig.h)

#define CFG_GAP_CONNECTION_MIN_INTERVAL_MS           20                     /**< Minimum acceptable connection interval */
#define CFG_GAP_CONNECTION_MAX_INTERVAL_MS          200                     /**< Maximum acceptable connection interval */
Revision:
4:3ce54cebbdc3
Parent:
3:fc93699018c9
Child:
7:0e54bd52bd2d
--- a/main.cpp	Thu Aug 07 14:29:13 2014 +0000
+++ b/main.cpp	Sat Aug 09 06:48:16 2014 +0000
@@ -1,27 +1,9 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2013 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+/* BLE Color Pixels
  */
 
 #include "mbed.h"
 #include "BLEDevice.h"
-#include "SharpLCD.hpp"
-#include "font.h"
-
-#define BLE_UUID_NUS_SERVICE            0x0001 /**< The UUID of the Nordic UART Service. */
-#define BLE_UUID_NUS_TX_CHARACTERISTIC  0x0002 /**< The UUID of the TX Characteristic. */
-#define BLE_UUID_NUS_RX_CHARACTERISTIC  0x0003 /**< The UUID of the RX Characteristic. */
+#include "color_pixels.h"
 
 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
                                * it will have an impact on code-size and power consumption. */
@@ -33,6 +15,8 @@
 #define DEBUG(...) /* nothing */
 #endif /* #if NEED_CONSOLE_OUTPUT */
 
+ColorPixels pixels(1, 32);
+
 BLEDevice  ble;
 DigitalOut led1(LED1);
 
@@ -53,7 +37,36 @@
 GattService         uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
 
 
-uint8_t framebuffer[SharpLCD::SIZEOF_FRAMEBUFFER_FOR_ALLOC];
+
+void processPacket(uint8_t *packet)
+{
+    uint8_t red = packet[0];
+    uint8_t green = packet[1];
+    uint8_t blue = packet[2];
+    
+    uint8_t mode = packet[3];
+    
+    uint8_t number = packet[4] - 1;
+    
+    mode = mode & 1;
+    
+    DEBUG("r: %d, g: %d, b: %d, mode: %d\n\r", red, green, blue, mode);
+    
+    if (mode == 0) {
+        pixels.set_color(number, red, green, blue);
+        pixels.update();
+    } else if (mode == 1) {
+        for (int i = 0; i < 32; i++) {
+            pixels.set_color(i, red, green, blue);
+        }
+        pixels.update();
+    } else if (mode == 2) {
+ 
+    } else {
+        
+    }
+    
+}
 
 void disconnectionCallback(uint16_t handle)
 {
@@ -71,9 +84,7 @@
         if (bytesRead < sizeof(rxPayload)) {
             rxPayload[bytesRead] = 0;
         }
-        DEBUG("ECHO: %s\n\r", (char *)rxPayload);
-        ble.updateCharacteristicValue(txCharacteristic.getHandle(), rxPayload, bytesRead);
-        rxPayloadUpdated = true;
+        processPacket(rxPayload);
     }
 }
 
@@ -87,6 +98,12 @@
     led1 = 1;
     Ticker ticker;
     ticker.attach(periodicCallback, 1);
+    
+    pixels.clear();
+    pixels.set_color(0, 255, 0, 0);
+    pixels.set_color(0, 0, 255, 0);
+    pixels.set_color(0, 0, 0, 255);
+    pixels.update();
 
     DEBUG("Initialising the nRF51822\n\r");
     ble.init();
@@ -97,7 +114,7 @@
     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
-                                    (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
+                                    (const uint8_t *)"Color Pixels", sizeof("Color Pixels") - 1);
     ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
                                     (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
 
@@ -106,24 +123,8 @@
 
     ble.addService(uartService);
 
-    SharpLCD lcd(p21 /* display enable */, SPI_PSELSS0, SPI_PSELMOSI0, SPI_PSELMISO0, SPI_PSELSCK0, NC);
-    SharpLCD::FrameBuffer fb(framebuffer);
-
-    lcd.enableDisplay();
-    lcd.clear();
-    fb.printString(lookupFontFace("DejaVu Serif", 8), 20, 40, BLACK, "Init");
-    lcd.drawFrameBuffer(fb);
-
     while (true) {
-        if (rxPayloadUpdated) {
-            fb.clear();
-            lcd.drawFrameBuffer(fb);
-            fb.printString(lookupFontFace("DejaVu Serif", 8), 20, 40, BLACK, (const char *)rxPayload);
-            lcd.drawFrameBuffer(fb);
-
-            rxPayloadUpdated = false;
-        }
-
         ble.waitForEvent();
     }
 }
+