Hackaday Tiny BLE Sample

Dependencies:   BLE_API mbed nRF51822

Fork of Seeed_Tiny_BLE_Get_Started by Seeed

main.cpp

Committer:
wd5gnr
Date:
2016-03-27
Revision:
4:bb933be5a507
Parent:
3:24e365bd1b97

File content as of revision 4:bb933be5a507:


#include "mbed.h"
#include "nrf51.h"
#include "nrf51_bitfields.h"

#include "BLE.h"
#include "DFUService.h"
#include "UARTService.h"


#define LOG(...)    { pc.printf(__VA_ARGS__); }

#define LED_GREEN   p21
#define LED_RED     p22
#define LED_BLUE    p23
#define BUTTON_PIN  p17
#define BATTERY_PIN p1


#define UART_TX     p9
#define UART_RX     p11
#define UART_CTS    p8
#define UART_RTS    p10



DigitalOut blue(LED_BLUE);
DigitalOut green(LED_GREEN);
DigitalOut red(LED_RED);

InterruptIn button(BUTTON_PIN);
AnalogIn    battery(BATTERY_PIN);
Serial pc(UART_TX, UART_RX);


int read_none_count = 0;

BLEDevice  ble;
UARTService *uartServicePtr;

volatile bool bleIsConnected = false;
volatile uint8_t tick_event = 0;



void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
{
    LOG("Connected!\n");
    bleIsConnected = true;
}

void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *cbParams)
{
    LOG("Disconnected!\n");
    LOG("Restarting the advertising process\n");
    ble.startAdvertising();
    bleIsConnected = false;
}

void tick(void)
{
    static uint32_t count = 0;
    
    LOG("%d\r\n", count++);
//    green = !green;
}

void detect(void)
{
    LOG("Button pressed\n");  
    blue = !blue;
}



int main(void)
{
    blue  = 1;
    green = 1;
    red   = 1;

    pc.baud(115200);
    
    wait(1);
    
    LOG("---- Hackaday! ----\n");
    
    
    
    
    Ticker ticker;
    ticker.attach(tick, 3);

    button.fall(detect);

    LOG("Initialising the nRF51822\n");
    ble.init();
    ble.gap().onDisconnection(disconnectionCallback);
    ble.gap().onConnection(connectionCallback);


    /* setup advertising */
    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
    ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
                                     (const uint8_t *)"smurfs", sizeof("smurfs"));
    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
                                     (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
    DFUService dfu(ble);                                 
    UARTService uartService(ble);
    uartServicePtr = &uartService;
    //uartService.retargetStdout();

    ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
    ble.gap().startAdvertising();
    char r[4];
    r[1]='\r';
    r[2]='\n';
    r[3]='\0';
    
    while (true) {
        ble.waitForEvent();
        int c;      
        r[0]=c=uartService._getc();
        if (c<=0) continue;
        if (c=='R' || c=='r') {  red=0; green=1; blue=1; }
        else if (c=='G' || c=='g') {  red=1; green=0; blue=1; }
        else if (c=='B' || c=='b') {  red=1; green=1; blue=0; }
        else  r[0]='?';
        uartService.writeString(r);
        
        }
    }