BLE Color Pixels based on nRF51822 and WS2812B

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 */

main.cpp

Committer:
yihui
Date:
2015-05-05
Revision:
7:0e54bd52bd2d
Parent:
4:3ce54cebbdc3

File content as of revision 7:0e54bd52bd2d:

/* BLE Color Pixels
 */

#include "mbed.h"
#include "BLEDevice.h"
#include "UARTService.h"
#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. */

#if NEED_CONSOLE_OUTPUT
Serial  pc(USBTX, USBRX);
#define DEBUG(...) { pc.printf(__VA_ARGS__); }
#else
#define DEBUG(...) /* nothing */
#endif /* #if NEED_CONSOLE_OUTPUT */

ColorPixels pixels(1, 32);

BLEDevice  ble;
DigitalOut led1(LED1);

UARTService *uartServicePtr;
 

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(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
{
    DEBUG("Disconnected!\n\r");
    
    DEBUG("Restarting the advertising process\n\r");
    ble.startAdvertising();
}

void onDataWritten(const GattCharacteristicWriteCBParams *params)
{
    if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) {
        uint16_t bytesRead = params->len;
        DEBUG("received %u bytes\n\r", bytesRead);
        ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
        processPacket((uint8_t *)params->data);
    }
}

void periodicCallback(void)
{
    led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
}

int main(void)
{
    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();
    ble.onDisconnection(disconnectionCallback);
    ble.onDataWritten(onDataWritten);
 
    /* setup advertising */
    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
    ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
                                     (const uint8_t *)"Color Pixels", sizeof("Color Pixels") - 1);
    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
                                     (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
 
    ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000));
    ble.startAdvertising();
 
    UARTService uartService(ble);
    uartServicePtr = &uartService;
 
    while (true) {
        ble.waitForEvent();
    }
}