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 */
Committer:
rgrover1
Date:
Wed Jul 30 07:57:10 2014 +0000
Revision:
0:6b62bf1cc2ba
Child:
2:20990b390374
Initial checkin for a demo based on BLE_UART and SharpLCD.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 0:6b62bf1cc2ba 1 /* mbed Microcontroller Library
rgrover1 0:6b62bf1cc2ba 2 * Copyright (c) 2006-2013 ARM Limited
rgrover1 0:6b62bf1cc2ba 3 *
rgrover1 0:6b62bf1cc2ba 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 0:6b62bf1cc2ba 5 * you may not use this file except in compliance with the License.
rgrover1 0:6b62bf1cc2ba 6 * You may obtain a copy of the License at
rgrover1 0:6b62bf1cc2ba 7 *
rgrover1 0:6b62bf1cc2ba 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 0:6b62bf1cc2ba 9 *
rgrover1 0:6b62bf1cc2ba 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 0:6b62bf1cc2ba 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 0:6b62bf1cc2ba 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 0:6b62bf1cc2ba 13 * See the License for the specific language governing permissions and
rgrover1 0:6b62bf1cc2ba 14 * limitations under the License.
rgrover1 0:6b62bf1cc2ba 15 */
rgrover1 0:6b62bf1cc2ba 16
rgrover1 0:6b62bf1cc2ba 17 #include "mbed.h"
rgrover1 0:6b62bf1cc2ba 18 #include "BLEDevice.h"
rgrover1 0:6b62bf1cc2ba 19 #include "SharpLCD.hpp"
rgrover1 0:6b62bf1cc2ba 20 #include "font.h"
rgrover1 0:6b62bf1cc2ba 21
rgrover1 0:6b62bf1cc2ba 22 #define BLE_UUID_NUS_SERVICE 0x0001 /**< The UUID of the Nordic UART Service. */
rgrover1 0:6b62bf1cc2ba 23 #define BLE_UUID_NUS_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
rgrover1 0:6b62bf1cc2ba 24 #define BLE_UUID_NUS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
rgrover1 0:6b62bf1cc2ba 25
rgrover1 0:6b62bf1cc2ba 26 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
rgrover1 0:6b62bf1cc2ba 27 * it will have an impact on code-size and power consumption. */
rgrover1 0:6b62bf1cc2ba 28
rgrover1 0:6b62bf1cc2ba 29 #if NEED_CONSOLE_OUTPUT
rgrover1 0:6b62bf1cc2ba 30 Serial pc(USBTX, USBRX);
rgrover1 0:6b62bf1cc2ba 31 #define DEBUG(...) { pc.printf(__VA_ARGS__); }
rgrover1 0:6b62bf1cc2ba 32 #else
rgrover1 0:6b62bf1cc2ba 33 #define DEBUG(...) /* nothing */
rgrover1 0:6b62bf1cc2ba 34 #endif /* #if NEED_CONSOLE_OUTPUT */
rgrover1 0:6b62bf1cc2ba 35
rgrover1 0:6b62bf1cc2ba 36 BLEDevice ble;
rgrover1 0:6b62bf1cc2ba 37 DigitalOut led1(LED1);
rgrover1 0:6b62bf1cc2ba 38
rgrover1 0:6b62bf1cc2ba 39 // The Nordic UART Service
rgrover1 0:6b62bf1cc2ba 40 static const uint8_t uart_base_uuid[] = {0x6e, 0x40, 0x00, 0x01, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5,0x0e, 0x24, 0xdc, 0xca, 0x9e};
rgrover1 0:6b62bf1cc2ba 41 static const uint8_t uart_tx_uuid[] = {0x6e, 0x40, 0x00, 0x02, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5,0x0e, 0x24, 0xdc, 0xca, 0x9e};
rgrover1 0:6b62bf1cc2ba 42 static const uint8_t uart_rx_uuid[] = {0x6e, 0x40, 0x00, 0x03, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5,0x0e, 0x24, 0xdc, 0xca, 0x9e};
rgrover1 0:6b62bf1cc2ba 43 static const uint8_t uart_base_uuid_rev[] = {0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x01, 0x00, 0x40, 0x6e};
rgrover1 0:6b62bf1cc2ba 44
rgrover1 0:6b62bf1cc2ba 45 bool rxPayloadUpdated = false;
rgrover1 0:6b62bf1cc2ba 46 uint8_t rxPayload[20] = {0,};
rgrover1 0:6b62bf1cc2ba 47 uint8_t txPayload[20] = {0,};
rgrover1 0:6b62bf1cc2ba 48 GattCharacteristic rxCharacteristic (uart_tx_uuid, rxPayload, 1, sizeof(rxPayload),
rgrover1 0:6b62bf1cc2ba 49 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
rgrover1 0:6b62bf1cc2ba 50 GattCharacteristic txCharacteristic (uart_rx_uuid, txPayload, 1, sizeof(txPayload),
rgrover1 0:6b62bf1cc2ba 51 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
rgrover1 0:6b62bf1cc2ba 52 GattCharacteristic *uartChars[] = {&rxCharacteristic, &txCharacteristic};
rgrover1 0:6b62bf1cc2ba 53 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
rgrover1 0:6b62bf1cc2ba 54
rgrover1 0:6b62bf1cc2ba 55
rgrover1 0:6b62bf1cc2ba 56 uint8_t framebuffer[SharpLCD::SIZEOF_FRAMEBUFFER_FOR_ALLOC];
rgrover1 0:6b62bf1cc2ba 57
rgrover1 0:6b62bf1cc2ba 58 void disconnectionCallback(uint16_t handle)
rgrover1 0:6b62bf1cc2ba 59 {
rgrover1 0:6b62bf1cc2ba 60 DEBUG("Disconnected!\n\r");
rgrover1 0:6b62bf1cc2ba 61 DEBUG("Restarting the advertising process\n\r");
rgrover1 0:6b62bf1cc2ba 62 ble.startAdvertising();
rgrover1 0:6b62bf1cc2ba 63 }
rgrover1 0:6b62bf1cc2ba 64
rgrover1 0:6b62bf1cc2ba 65 void onDataWritten(uint16_t charHandle)
rgrover1 0:6b62bf1cc2ba 66 {
rgrover1 0:6b62bf1cc2ba 67 if (charHandle == rxCharacteristic.getHandle()) {
rgrover1 0:6b62bf1cc2ba 68 DEBUG("onDataWritten()\n\r");
rgrover1 0:6b62bf1cc2ba 69 uint16_t bytesRead;
rgrover1 0:6b62bf1cc2ba 70 ble.readCharacteristicValue(rxCharacteristic.getHandle(), rxPayload, &bytesRead);
rgrover1 0:6b62bf1cc2ba 71 DEBUG("ECHO: %s\n\r", (char *)rxPayload);
rgrover1 0:6b62bf1cc2ba 72 ble.updateCharacteristicValue(txCharacteristic.getHandle(), rxPayload, bytesRead);
rgrover1 0:6b62bf1cc2ba 73 rxPayloadUpdated = true;
rgrover1 0:6b62bf1cc2ba 74 }
rgrover1 0:6b62bf1cc2ba 75 }
rgrover1 0:6b62bf1cc2ba 76
rgrover1 0:6b62bf1cc2ba 77 void periodicCallback(void)
rgrover1 0:6b62bf1cc2ba 78 {
rgrover1 0:6b62bf1cc2ba 79 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
rgrover1 0:6b62bf1cc2ba 80 }
rgrover1 0:6b62bf1cc2ba 81
rgrover1 0:6b62bf1cc2ba 82 int main(void)
rgrover1 0:6b62bf1cc2ba 83 {
rgrover1 0:6b62bf1cc2ba 84 led1 = 1;
rgrover1 0:6b62bf1cc2ba 85 Ticker ticker;
rgrover1 0:6b62bf1cc2ba 86 ticker.attach(periodicCallback, 1);
rgrover1 0:6b62bf1cc2ba 87
rgrover1 0:6b62bf1cc2ba 88 DEBUG("Initialising the nRF51822\n\r");
rgrover1 0:6b62bf1cc2ba 89 ble.init();
rgrover1 0:6b62bf1cc2ba 90 ble.onDisconnection(disconnectionCallback);
rgrover1 0:6b62bf1cc2ba 91 ble.onDataWritten(onDataWritten);
rgrover1 0:6b62bf1cc2ba 92
rgrover1 0:6b62bf1cc2ba 93 /* setup advertising */
rgrover1 0:6b62bf1cc2ba 94 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
rgrover1 0:6b62bf1cc2ba 95 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
rgrover1 0:6b62bf1cc2ba 96 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
rgrover1 0:6b62bf1cc2ba 97 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
rgrover1 0:6b62bf1cc2ba 98 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
rgrover1 0:6b62bf1cc2ba 99 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
rgrover1 0:6b62bf1cc2ba 100
rgrover1 0:6b62bf1cc2ba 101 ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
rgrover1 0:6b62bf1cc2ba 102 ble.startAdvertising();
rgrover1 0:6b62bf1cc2ba 103
rgrover1 0:6b62bf1cc2ba 104 ble.addService(uartService);
rgrover1 0:6b62bf1cc2ba 105
rgrover1 0:6b62bf1cc2ba 106 SharpLCD lcd(p21 /* display enable */, SPI_PSELSS0, SPI_PSELMOSI0, SPI_PSELMISO0, SPI_PSELSCK0, NC);
rgrover1 0:6b62bf1cc2ba 107 SharpLCD::FrameBuffer fb(framebuffer);
rgrover1 0:6b62bf1cc2ba 108
rgrover1 0:6b62bf1cc2ba 109 lcd.enableDisplay();
rgrover1 0:6b62bf1cc2ba 110 lcd.clear();
rgrover1 0:6b62bf1cc2ba 111 fb.printString(lookupFontFace("DejaVu Serif", 8),
rgrover1 0:6b62bf1cc2ba 112 20,
rgrover1 0:6b62bf1cc2ba 113 40,
rgrover1 0:6b62bf1cc2ba 114 BLACK,
rgrover1 0:6b62bf1cc2ba 115 "Init");
rgrover1 0:6b62bf1cc2ba 116 lcd.drawFrameBuffer(fb);
rgrover1 0:6b62bf1cc2ba 117
rgrover1 0:6b62bf1cc2ba 118 while (true) {
rgrover1 0:6b62bf1cc2ba 119 if (rxPayloadUpdated) {
rgrover1 0:6b62bf1cc2ba 120 fb.clear();
rgrover1 0:6b62bf1cc2ba 121 lcd.drawFrameBuffer(fb);
rgrover1 0:6b62bf1cc2ba 122 fb.printString(lookupFontFace("DejaVu Serif", 8),
rgrover1 0:6b62bf1cc2ba 123 20,
rgrover1 0:6b62bf1cc2ba 124 40,
rgrover1 0:6b62bf1cc2ba 125 BLACK,
rgrover1 0:6b62bf1cc2ba 126 (const char *)rxPayload);
rgrover1 0:6b62bf1cc2ba 127 lcd.drawFrameBuffer(fb);
rgrover1 0:6b62bf1cc2ba 128
rgrover1 0:6b62bf1cc2ba 129 rxPayloadUpdated = false;
rgrover1 0:6b62bf1cc2ba 130 }
rgrover1 0:6b62bf1cc2ba 131
rgrover1 0:6b62bf1cc2ba 132 ble.waitForEvent();
rgrover1 0:6b62bf1cc2ba 133 }
rgrover1 0:6b62bf1cc2ba 134 }