Application running on nRF51822 PCA10001

Dependencies:   BLE_API MMA8652 nRF51822 mbed-src

Committer:
rosterloh84
Date:
Tue Jul 22 14:54:44 2014 +0000
Revision:
0:90c13be263a0
Child:
1:1c52fb502f6b
First working version with DevInfo and Battery simulated

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rosterloh84 0:90c13be263a0 1 /*******************************************************************************
rosterloh84 0:90c13be263a0 2 * Title : System Initialization
rosterloh84 0:90c13be263a0 3 * Filename : main.cpp
rosterloh84 0:90c13be263a0 4 * Author : Richard Osterloh
rosterloh84 0:90c13be263a0 5 * Origin Date : 22/07/2014
rosterloh84 0:90c13be263a0 6 * Version : 1.0.0
rosterloh84 0:90c13be263a0 7 * Compiler : mbed compiler
rosterloh84 0:90c13be263a0 8 * Target : Nordic nRF51822
rosterloh84 0:90c13be263a0 9 * Notes : None
rosterloh84 0:90c13be263a0 10 *******************************************************************************/
rosterloh84 0:90c13be263a0 11 /*************** MODULE REVISION LOG ******************************************
rosterloh84 0:90c13be263a0 12 *
rosterloh84 0:90c13be263a0 13 * Date Software Version Initials Description
rosterloh84 0:90c13be263a0 14 * 22/07/2014 1.0.0 RO Module Created.
rosterloh84 0:90c13be263a0 15 *
rosterloh84 0:90c13be263a0 16 *******************************************************************************/
rosterloh84 0:90c13be263a0 17 /** \file main.cpp
rosterloh84 0:90c13be263a0 18 * \brief This module contains the
rosterloh84 0:90c13be263a0 19 */
rosterloh84 0:90c13be263a0 20 /******************************************************************************
rosterloh84 0:90c13be263a0 21 * Includes
rosterloh84 0:90c13be263a0 22 *******************************************************************************/
rosterloh84 0:90c13be263a0 23 #include "mbed.h"
rosterloh84 0:90c13be263a0 24 #include "BLEDevice.h"
rosterloh84 0:90c13be263a0 25 #include "nRF51822n.h"
rosterloh84 0:90c13be263a0 26 //#include "MMA8652.h"
rosterloh84 0:90c13be263a0 27
rosterloh84 0:90c13be263a0 28 /******************************************************************************
rosterloh84 0:90c13be263a0 29 * Module Preprocessor Constants
rosterloh84 0:90c13be263a0 30 *******************************************************************************/
rosterloh84 0:90c13be263a0 31 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
rosterloh84 0:90c13be263a0 32 * it will have an impact on code-size and power consumption. */
rosterloh84 0:90c13be263a0 33 #if NEED_CONSOLE_OUTPUT
rosterloh84 0:90c13be263a0 34 Serial pc(USBTX, USBRX);
rosterloh84 0:90c13be263a0 35 #define DEBUG(...) { pc.printf(__VA_ARGS__); }
rosterloh84 0:90c13be263a0 36 #else
rosterloh84 0:90c13be263a0 37 #define DEBUG(...) /* nothing */
rosterloh84 0:90c13be263a0 38 #endif /* #if NEED_CONSOLE_OUTPUT */
rosterloh84 0:90c13be263a0 39
rosterloh84 0:90c13be263a0 40 /******************************************************************************
rosterloh84 0:90c13be263a0 41 * Module Preprocessor Macros
rosterloh84 0:90c13be263a0 42 *******************************************************************************/
rosterloh84 0:90c13be263a0 43
rosterloh84 0:90c13be263a0 44 /******************************************************************************
rosterloh84 0:90c13be263a0 45 * Module Typedefs
rosterloh84 0:90c13be263a0 46 *******************************************************************************/
rosterloh84 0:90c13be263a0 47
rosterloh84 0:90c13be263a0 48 /******************************************************************************
rosterloh84 0:90c13be263a0 49 * Module Variable Definitions
rosterloh84 0:90c13be263a0 50 *******************************************************************************/
rosterloh84 0:90c13be263a0 51 nRF51822n nrf;
rosterloh84 0:90c13be263a0 52 BLEDevice ble;
rosterloh84 0:90c13be263a0 53 DigitalOut oneSecondLed(LED1); /* LED1 is toggled every second. */
rosterloh84 0:90c13be263a0 54 DigitalOut advertisingStateLed(LED2); /* LED2 is on when we are advertising, otherwise off. */
rosterloh84 0:90c13be263a0 55 //MMA8652 acc1(I2C_SDA0, I2C_SCL0);
rosterloh84 0:90c13be263a0 56
rosterloh84 0:90c13be263a0 57 const static char DEVICE_NAME[] = "Buddi Blueband";
rosterloh84 0:90c13be263a0 58
rosterloh84 0:90c13be263a0 59 // https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.device_information.xml
rosterloh84 0:90c13be263a0 60 // Manufacturer Name String
rosterloh84 0:90c13be263a0 61 static char vendor[] = "Buddi Ltd.";
rosterloh84 0:90c13be263a0 62 GattCharacteristic vendorChar(GattCharacteristic::UUID_MANUFACTURER_NAME_STRING_CHAR, (uint8_t *)vendor, sizeof(vendor), sizeof(vendor),
rosterloh84 0:90c13be263a0 63 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
rosterloh84 0:90c13be263a0 64 // Model Number String
rosterloh84 0:90c13be263a0 65 static char model[] = "BlueBand";
rosterloh84 0:90c13be263a0 66 GattCharacteristic modelChar(GattCharacteristic::UUID_MODEL_NUMBER_STRING_CHAR, (uint8_t *)model, sizeof(model), sizeof(model),
rosterloh84 0:90c13be263a0 67 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
rosterloh84 0:90c13be263a0 68 // Serial Number String
rosterloh84 0:90c13be263a0 69 static char serial[] = "1234567890";
rosterloh84 0:90c13be263a0 70 GattCharacteristic serialChar(GattCharacteristic::UUID_SERIAL_NUMBER_STRING_CHAR, (uint8_t *)serial, sizeof(serial), sizeof(serial),
rosterloh84 0:90c13be263a0 71 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
rosterloh84 0:90c13be263a0 72 // Hardware Revision String
rosterloh84 0:90c13be263a0 73 static char hwversion[] = "Hardware: 0";
rosterloh84 0:90c13be263a0 74 GattCharacteristic hwChar(GattCharacteristic::UUID_HARDWARE_REVISION_STRING_CHAR, (uint8_t *)hwversion, sizeof(hwversion), sizeof(hwversion),
rosterloh84 0:90c13be263a0 75 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
rosterloh84 0:90c13be263a0 76 // Firmware Revision String
rosterloh84 0:90c13be263a0 77 static char fwversion[] = "Firmware: 0001";
rosterloh84 0:90c13be263a0 78 GattCharacteristic fwChar(GattCharacteristic::UUID_FIRMWARE_REVISION_STRING_CHAR, (uint8_t *)fwversion, sizeof(fwversion), sizeof(fwversion),
rosterloh84 0:90c13be263a0 79 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
rosterloh84 0:90c13be263a0 80 // Software Revision String
rosterloh84 0:90c13be263a0 81 static char swversion[] = "Build: 0001";
rosterloh84 0:90c13be263a0 82 GattCharacteristic swChar(GattCharacteristic::UUID_SOFTWARE_REVISION_STRING_CHAR, (uint8_t *)swversion, sizeof(swversion), sizeof(swversion),
rosterloh84 0:90c13be263a0 83 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
rosterloh84 0:90c13be263a0 84 // System ID
rosterloh84 0:90c13be263a0 85 static char systemId = 'A';
rosterloh84 0:90c13be263a0 86 GattCharacteristic systemID(GattCharacteristic::UUID_SYSTEM_ID_CHAR, (uint8_t *)systemId, sizeof(systemId), sizeof(systemId),
rosterloh84 0:90c13be263a0 87 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
rosterloh84 0:90c13be263a0 88 GattCharacteristic *devInfoChars[] = {&vendorChar, &modelChar, &serialChar, &hwChar, &fwChar, &swChar, &systemID };
rosterloh84 0:90c13be263a0 89 GattService devInfoService(GattService::UUID_DEVICE_INFORMATION_SERVICE, devInfoChars, sizeof(devInfoChars) / sizeof(GattCharacteristic *));
rosterloh84 0:90c13be263a0 90
rosterloh84 0:90c13be263a0 91 // https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml
rosterloh84 0:90c13be263a0 92 static uint8_t batteryLevel = 100;
rosterloh84 0:90c13be263a0 93 GattCharacteristic batteryPercentage(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR, (uint8_t *)batteryLevel, sizeof(batteryLevel), sizeof(batteryLevel),
rosterloh84 0:90c13be263a0 94 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
rosterloh84 0:90c13be263a0 95 GattCharacteristic *batteryChars[] = {&batteryPercentage };
rosterloh84 0:90c13be263a0 96 GattService batteryService(GattService::UUID_BATTERY_SERVICE, batteryChars, sizeof(batteryChars) / sizeof(GattCharacteristic *));
rosterloh84 0:90c13be263a0 97
rosterloh84 0:90c13be263a0 98 static const uint16_t uuid16_list[] = {GattService::UUID_DEVICE_INFORMATION_SERVICE,
rosterloh84 0:90c13be263a0 99 GattService::UUID_BATTERY_SERVICE};
rosterloh84 0:90c13be263a0 100
rosterloh84 0:90c13be263a0 101 /******************************************************************************
rosterloh84 0:90c13be263a0 102 * Function Prototypes
rosterloh84 0:90c13be263a0 103 *******************************************************************************/
rosterloh84 0:90c13be263a0 104
rosterloh84 0:90c13be263a0 105 /******************************************************************************
rosterloh84 0:90c13be263a0 106 * Function Definitions
rosterloh84 0:90c13be263a0 107 *******************************************************************************/
rosterloh84 0:90c13be263a0 108 void timeoutCallback(void)
rosterloh84 0:90c13be263a0 109 {
rosterloh84 0:90c13be263a0 110 DEBUG("Timeout!\r\n");
rosterloh84 0:90c13be263a0 111 }
rosterloh84 0:90c13be263a0 112
rosterloh84 0:90c13be263a0 113 void connectionCallback(void)
rosterloh84 0:90c13be263a0 114 {
rosterloh84 0:90c13be263a0 115 DEBUG("Connection\n\r");
rosterloh84 0:90c13be263a0 116 advertisingStateLed = 0;
rosterloh84 0:90c13be263a0 117 }
rosterloh84 0:90c13be263a0 118
rosterloh84 0:90c13be263a0 119 void disconnectionCallback(void)
rosterloh84 0:90c13be263a0 120 {
rosterloh84 0:90c13be263a0 121 DEBUG("Disconnected!\r\n");
rosterloh84 0:90c13be263a0 122 DEBUG("Restarting the advertising process\r\n");
rosterloh84 0:90c13be263a0 123 ble.startAdvertising();
rosterloh84 0:90c13be263a0 124 advertisingStateLed = 1;
rosterloh84 0:90c13be263a0 125 }
rosterloh84 0:90c13be263a0 126
rosterloh84 0:90c13be263a0 127 /**
rosterloh84 0:90c13be263a0 128 * Triggered periodically by the 'ticker' interrupt
rosterloh84 0:90c13be263a0 129 */
rosterloh84 0:90c13be263a0 130 void periodicCallback(void)
rosterloh84 0:90c13be263a0 131 {
rosterloh84 0:90c13be263a0 132 oneSecondLed = !oneSecondLed; /* Do blinky on LED1 while we're waiting for BLE events */
rosterloh84 0:90c13be263a0 133
rosterloh84 0:90c13be263a0 134 if (ble.getGapState().connected) {
rosterloh84 0:90c13be263a0 135 /* Update the battery measurement */
rosterloh84 0:90c13be263a0 136 batteryLevel--;
rosterloh84 0:90c13be263a0 137 if (batteryLevel == 1) {
rosterloh84 0:90c13be263a0 138 batteryLevel = 100;
rosterloh84 0:90c13be263a0 139 }
rosterloh84 0:90c13be263a0 140 ble.updateCharacteristicValue(batteryPercentage.getHandle(), &batteryLevel, sizeof(batteryLevel));
rosterloh84 0:90c13be263a0 141 }
rosterloh84 0:90c13be263a0 142 }
rosterloh84 0:90c13be263a0 143
rosterloh84 0:90c13be263a0 144 int main(void)
rosterloh84 0:90c13be263a0 145 {
rosterloh84 0:90c13be263a0 146 //float acc_data[3];
rosterloh84 0:90c13be263a0 147 //int16_t acc_raw[3];
rosterloh84 0:90c13be263a0 148
rosterloh84 0:90c13be263a0 149 oneSecondLed = 1;
rosterloh84 0:90c13be263a0 150 Ticker ticker;
rosterloh84 0:90c13be263a0 151 ticker.attach(periodicCallback, 1);
rosterloh84 0:90c13be263a0 152 /*
rosterloh84 0:90c13be263a0 153 DEBUG("MMA8652 Acc = %X\r\n", acc1.getWhoAmI());
rosterloh84 0:90c13be263a0 154 acc1.ReadXYZ(acc_data);
rosterloh84 0:90c13be263a0 155 acc1.ReadXYZraw(acc_raw);
rosterloh84 0:90c13be263a0 156 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]);
rosterloh84 0:90c13be263a0 157 */
rosterloh84 0:90c13be263a0 158 DEBUG("Initialising the nRF51822\r\n");
rosterloh84 0:90c13be263a0 159 ble.init(); /* Initialise the nRF51822 */
rosterloh84 0:90c13be263a0 160 //ble.onTimeout(timeoutCallback);
rosterloh84 0:90c13be263a0 161 ble.onConnection(connectionCallback);
rosterloh84 0:90c13be263a0 162 ble.onDisconnection(disconnectionCallback);
rosterloh84 0:90c13be263a0 163 //ble.onDataSent(GattServer::ServerEventCallback_t callback);
rosterloh84 0:90c13be263a0 164 //ble.onDataWritten(GattServer::EventCallback_t callback);
rosterloh84 0:90c13be263a0 165 //ble.onUpdatesEnabled(GattServer::EventCallback_t callback);
rosterloh84 0:90c13be263a0 166 //ble.onUpdatesDisabled(GattServer::EventCallback_t callback);
rosterloh84 0:90c13be263a0 167 //ble.onConfirmationReceived(GattServer::EventCallback_t callback);
rosterloh84 0:90c13be263a0 168
rosterloh84 0:90c13be263a0 169 /* setup advertising */
rosterloh84 0:90c13be263a0 170 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
rosterloh84 0:90c13be263a0 171 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t*)uuid16_list, sizeof(uuid16_list));
rosterloh84 0:90c13be263a0 172 ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_WATCH);
rosterloh84 0:90c13be263a0 173 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
rosterloh84 0:90c13be263a0 174 //ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
rosterloh84 0:90c13be263a0 175 //ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
rosterloh84 0:90c13be263a0 176 // (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
rosterloh84 0:90c13be263a0 177
rosterloh84 0:90c13be263a0 178 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
rosterloh84 0:90c13be263a0 179 ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
rosterloh84 0:90c13be263a0 180 ble.startAdvertising();
rosterloh84 0:90c13be263a0 181 advertisingStateLed = 1;
rosterloh84 0:90c13be263a0 182
rosterloh84 0:90c13be263a0 183 ble.addService(devInfoService);
rosterloh84 0:90c13be263a0 184 ble.addService(batteryService);
rosterloh84 0:90c13be263a0 185
rosterloh84 0:90c13be263a0 186 while (true) {
rosterloh84 0:90c13be263a0 187 ble.waitForEvent();
rosterloh84 0:90c13be263a0 188 }
rosterloh84 0:90c13be263a0 189 }