Application running on nRF51822 PCA10001

Dependencies:   BLE_API MMA8652 nRF51822 mbed-src

main.cpp

Committer:
rosterloh84
Date:
2014-07-22
Revision:
0:90c13be263a0
Child:
1:1c52fb502f6b

File content as of revision 0:90c13be263a0:

/*******************************************************************************
* Title                 :   System Initialization
* Filename              :   main.cpp
* Author                :   Richard Osterloh
* Origin Date           :   22/07/2014
* Version               :   1.0.0
* Compiler              :   mbed compiler
* Target                :   Nordic nRF51822
* Notes                 :   None
*******************************************************************************/
/*************** MODULE REVISION LOG ******************************************
*
*    Date    Software Version    Initials   Description 
* 22/07/2014      1.0.0             RO      Module Created.
*
*******************************************************************************/
/** \file main.cpp
 * \brief This module contains the 
 */
/******************************************************************************
* Includes
*******************************************************************************/
#include "mbed.h"
#include "BLEDevice.h"
#include "nRF51822n.h"
//#include "MMA8652.h"

/******************************************************************************
* Module Preprocessor Constants
*******************************************************************************/
#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 */

/******************************************************************************
* Module Preprocessor Macros
*******************************************************************************/
 
/******************************************************************************
* Module Typedefs
*******************************************************************************/
 
/******************************************************************************
* Module Variable Definitions
*******************************************************************************/
nRF51822n  nrf;
BLEDevice  ble;
DigitalOut oneSecondLed(LED1);              /* LED1 is toggled every second. */
DigitalOut advertisingStateLed(LED2);       /* LED2 is on when we are advertising, otherwise off. */
//MMA8652    acc1(I2C_SDA0, I2C_SCL0);

const static char  DEVICE_NAME[] = "Buddi Blueband";

// https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.device_information.xml
// Manufacturer Name String
static char         vendor[] = "Buddi Ltd.";
GattCharacteristic  vendorChar(GattCharacteristic::UUID_MANUFACTURER_NAME_STRING_CHAR, (uint8_t *)vendor, sizeof(vendor), sizeof(vendor),
                           GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
// Model Number String
static char         model[] = "BlueBand";
GattCharacteristic  modelChar(GattCharacteristic::UUID_MODEL_NUMBER_STRING_CHAR, (uint8_t *)model, sizeof(model), sizeof(model),
                           GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
// Serial Number String
static char         serial[] = "1234567890";
GattCharacteristic  serialChar(GattCharacteristic::UUID_SERIAL_NUMBER_STRING_CHAR, (uint8_t *)serial, sizeof(serial), sizeof(serial),
                           GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
// Hardware Revision String
static char         hwversion[] = "Hardware: 0";
GattCharacteristic  hwChar(GattCharacteristic::UUID_HARDWARE_REVISION_STRING_CHAR, (uint8_t *)hwversion, sizeof(hwversion), sizeof(hwversion),
                           GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
// Firmware Revision String
static char         fwversion[] = "Firmware: 0001";
GattCharacteristic  fwChar(GattCharacteristic::UUID_FIRMWARE_REVISION_STRING_CHAR, (uint8_t *)fwversion, sizeof(fwversion), sizeof(fwversion),
                           GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
// Software Revision String
static char         swversion[] = "Build: 0001";
GattCharacteristic  swChar(GattCharacteristic::UUID_SOFTWARE_REVISION_STRING_CHAR, (uint8_t *)swversion, sizeof(swversion), sizeof(swversion),
                           GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
// System ID
static char         systemId = 'A';
GattCharacteristic  systemID(GattCharacteristic::UUID_SYSTEM_ID_CHAR, (uint8_t *)systemId, sizeof(systemId), sizeof(systemId),
                           GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
GattCharacteristic *devInfoChars[] = {&vendorChar, &modelChar, &serialChar, &hwChar, &fwChar, &swChar, &systemID };
GattService         devInfoService(GattService::UUID_DEVICE_INFORMATION_SERVICE, devInfoChars, sizeof(devInfoChars) / sizeof(GattCharacteristic *));

// https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml
static uint8_t      batteryLevel = 100;
GattCharacteristic  batteryPercentage(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, (uint8_t *)batteryLevel, sizeof(batteryLevel), sizeof(batteryLevel),
                           GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
GattCharacteristic *batteryChars[] = {&batteryPercentage };
GattService         batteryService(GattService::UUID_BATTERY_SERVICE, batteryChars, sizeof(batteryChars) / sizeof(GattCharacteristic *));

static const uint16_t uuid16_list[] = {GattService::UUID_DEVICE_INFORMATION_SERVICE,
                                       GattService::UUID_BATTERY_SERVICE};

/******************************************************************************
* Function Prototypes
*******************************************************************************/
 
/******************************************************************************
* Function Definitions
*******************************************************************************/
void timeoutCallback(void)
{
    DEBUG("Timeout!\r\n");
}

void connectionCallback(void)
{
    DEBUG("Connection\n\r");
    advertisingStateLed = 0;
}

void disconnectionCallback(void)
{
    DEBUG("Disconnected!\r\n");
    DEBUG("Restarting the advertising process\r\n");
    ble.startAdvertising();
    advertisingStateLed = 1;
}

/**
 * Triggered periodically by the 'ticker' interrupt
 */
void periodicCallback(void)
{
    oneSecondLed = !oneSecondLed; /* Do blinky on LED1 while we're waiting for BLE events */

    if (ble.getGapState().connected) {
        /* Update the battery measurement */
        batteryLevel--;
        if (batteryLevel == 1) {
            batteryLevel = 100;
        }
        ble.updateCharacteristicValue(batteryPercentage.getHandle(), &batteryLevel, sizeof(batteryLevel));
    }
}

int main(void)
{
    //float acc_data[3];
    //int16_t acc_raw[3];
    
    oneSecondLed = 1;
    Ticker ticker;
    ticker.attach(periodicCallback, 1);
    /*
    DEBUG("MMA8652 Acc    = %X\r\n", acc1.getWhoAmI());
    acc1.ReadXYZ(acc_data);
    acc1.ReadXYZraw(acc_raw);
    DEBUG("MMA8652 Acc:  X:%1.3f Y:%1.3f Z:%1.3f (Raw X:%3d Y:%3d Z:%3d)\r\n", acc_data[0], acc_data[1], acc_data[2], acc_raw[0], acc_raw[1], acc_raw[2]);
    */
    DEBUG("Initialising the nRF51822\r\n");
    ble.init();                                     /* Initialise the nRF51822 */
    //ble.onTimeout(timeoutCallback);
    ble.onConnection(connectionCallback);
    ble.onDisconnection(disconnectionCallback);
    //ble.onDataSent(GattServer::ServerEventCallback_t callback);
    //ble.onDataWritten(GattServer::EventCallback_t callback);
    //ble.onUpdatesEnabled(GattServer::EventCallback_t callback);
    //ble.onUpdatesDisabled(GattServer::EventCallback_t callback);
    //ble.onConfirmationReceived(GattServer::EventCallback_t callback);

    /* setup advertising */
    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t*)uuid16_list, sizeof(uuid16_list));
    ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_WATCH);
    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
    //ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
    //ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
    //                                (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
    
    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
    ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
    ble.startAdvertising();
    advertisingStateLed = 1;
    
    ble.addService(devInfoService);
    ble.addService(batteryService);

    while (true) {
        ble.waitForEvent();
    }
}