Application running on nRF51822 PCA10001

Dependencies:   BLE_API MMA8652 nRF51822 mbed-src

main.cpp

Committer:
rosterloh84
Date:
2014-07-25
Revision:
1:1c52fb502f6b
Parent:
0:90c13be263a0
Child:
2:2ddac99c3bde

File content as of revision 1:1c52fb502f6b:

/*******************************************************************************
* Title                 :   System Initialisation
* 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. */
//AnalogIn   adc1(p0_0);
//MMA8652    acc1(I2C_SDA0, I2C_SCL0);
PwmOut      ledr(p21);
PwmOut      ledg(p22);
PwmOut      ledb(p23);

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 *));

// Custom Buddi Service 0x92d7dc20-ba55-4df8-84b1-ad8af6e1ea4a
static const uint8_t buddi_service_uuid[] = {0x92, 0xd7, 0xdc, 0x20, 0xba, 0x55, 0x4d, 0xf8, 0x84, 0xb1, 0xad, 0x8a, 0xf6, 0xe1, 0xea, 0x4a};
// Command Characteristic
static const uint8_t command_uuid[] = {0x92, 0xd7, 0xdc, 0x20, 0xbb, 0x01, 0x4d, 0xf8, 0x84, 0xb1, 0xad, 0x8a, 0xf6, 0xe1, 0xea, 0x4a};
uint8_t commandPayload[8] = {0,};
GattCharacteristic  commandCharacteristic (command_uuid, commandPayload, 1, 8, 
                            GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
// Notification Characteristic
static const uint8_t notification_uuid[] = {0x92, 0xd7, 0xdc, 0x20, 0xbb, 0x02, 0x4d, 0xf8, 0x84, 0xb1, 0xad, 0x8a, 0xf6, 0xe1, 0xea, 0x4a};
uint8_t notificationPayload[8] = {0,};
GattCharacteristic  notificationCharacteristic (notification_uuid, notificationPayload, 1, 8, 
                            GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
GattCharacteristic *buddiChars[] = {&commandCharacteristic, &notificationCharacteristic};
GattService         buddiService(buddi_service_uuid, buddiChars, sizeof(buddiChars) / sizeof(GattCharacteristic *));

//static const uint8_t dev_info_uuid_rev[]      = {0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x0a, 0x18, 0x00, 0x00}; // 0000180a-0000-1000-8000-00805f9b34fb
//static const uint8_t battery_uuid_rev[]       = {0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x0f, 0x18, 0x00, 0x00}; // 0000180f-0000-1000-8000-00805f9b34fb
//static const uint8_t buddi_service_uuid_rev[] = {0x4a, 0xea, 0xe1, 0xf6, 0x8a, 0xad, 0xb1, 0x84, 0xf8, 0x4d, 0x55, 0xba, 0x20, 0xdc, 0xd7, 0x92}; // 92d7dc20-ba55-4df8-84b1-ad8af6e1ea4a

static const uint16_t uuid16_list[] = {GattService::UUID_DEVICE_INFORMATION_SERVICE,
                                       GattService::UUID_BATTERY_SERVICE};
//static const unit8_t uuid128_list[] = {(const uint8_t *)buddi_service_uuid_rev, 
//                                       (const uint8_t *)buddi_service_uuid_rev, 
//                                       (const uint8_t *)buddi_service_uuid_rev};

float r = 0.0f;
float g = 0.0f;
float b = 0.0f;
                                         
/******************************************************************************
* 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;
}

void onDataWritten(uint16_t charHandle)
{
    if (charHandle == commandCharacteristic.getHandle()) {
        DEBUG("onDataWritten()\n\r");
        uint16_t bytesRead;
        ble.readCharacteristicValue(commandCharacteristic.getHandle(), commandPayload, &bytesRead);
        DEBUG("ECHO: %s\n\r", (char *)commandPayload);
        ble.updateCharacteristicValue(notificationCharacteristic.getHandle(), commandPayload, bytesRead);
    }
}

/**
 * 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 = adc1.read_u16()&0x0FFF) * 3.3/4096;
        batteryLevel--;
        if (batteryLevel == 1) {
            batteryLevel = 100;
        }
        //ble.updateCharacteristicValue(batteryPercentage.getHandle(), &batteryLevel, sizeof(batteryLevel));
    }
    
    if(r <= 1.0f) {
        r += 0.1f;
    } else {
        r = 0.0f;
    }
    ledr = r;
    
    if(g <= 1.0f) {
        g += 0.2f;
    } else {
        g = 0.0f;
    }
    ledg = g;
    
    if(b <= 1.0f) {
        b += 0.3f;
    } else {
        b = 0.0f;
    }
    ledb = b;
}

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(onDataSent);
    ble.onDataWritten(onDataWritten);
    //ble.onUpdatesEnabled(onUpdatesEnabled);
    //ble.onUpdatesDisabled(onUpdatesDisabled);
    //ble.onConfirmationReceived(onConfirmationReceived);

    /* setup advertising */
    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
    ble.accumulateAdvertisingPayload(GapAdvertisingData::INCOMPLETE_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, (uint8_t*)uuid128_list, sizeof(uuid128_list));
    
    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);
    ble.addService(buddiService);

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