Application running on nRF51822 PCA10001
Dependencies: BLE_API MMA8652 nRF51822 mbed-src
Diff: main.cpp
- Revision:
- 3:596283411a00
- Parent:
- 2:2ddac99c3bde
- Child:
- 4:630f1560a0f3
--- a/main.cpp Mon Jul 28 07:45:00 2014 +0000 +++ b/main.cpp Fri Aug 29 12:36:01 2014 +0000 @@ -21,21 +21,17 @@ * Includes *******************************************************************************/ #include "mbed.h" +#include "Logger.h" +#include "Configuration.h" #include "BLEDevice.h" -#include "nRF51822n.h" +//#include "nRF51822n.h" //#include "MMA8652.h" +#include "DevInfoService.h" +#include "BuddiService.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 @@ -48,49 +44,18 @@ /****************************************************************************** * Module Variable Definitions *******************************************************************************/ -nRF51822n nrf; +//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); /* Red LED on PCA10000 */ -PwmOut ledg(p22); /* Green LED on PCA10000 */ -PwmOut ledb(p23); /* Blue LED on PCA10000 */ - -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 *)); +static const uint8_t *uuidlist = Nudge::getServiceUUIDp(); +static uint8_t uuidlistrev[16]; +static const char *DEVICE_NAME = "Nudge"; + +void bluetoothInit(); // https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml static uint8_t batteryLevel = 100; @@ -99,27 +64,8 @@ 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, ¬ificationCharacteristic}; -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 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}; @@ -127,10 +73,6 @@ static volatile bool triggerSensorPolling = false; /* set to high periodically to indicate to the main thread that * polling is necessary. */ static Gap::ConnectionParams_t connectionParams; - -float r = 0.0f; -float g = 0.0f; -float b = 0.0f; /****************************************************************************** * Function Prototypes @@ -146,13 +88,16 @@ void connectionCallback(Gap::Handle_t handle) { - DEBUG("connected. Got handle %u\r\n", handle); + DEBUG("Connected. Got handle %u\r\n", handle); - connectionParams.slaveLatency = 1; + connectionParams.minConnectionInterval = Config::minConnectionInterval; + connectionParams.maxConnectionInterval = Config::maxConnectionInterval; + connectionParams.slaveLatency = Config::slaveLatency; + connectionParams.connectionSupervisionTimeout = Config::supervisionTimeout; if (ble.updateConnectionParams(handle, &connectionParams) != BLE_ERROR_NONE) { DEBUG("failed to update connection paramter\r\n"); } - + advertisingStateLed = 0; } @@ -164,15 +109,15 @@ advertisingStateLed = 1; } +void onUpdatesEnabled(Gap::Handle_t handle) +{ + DEBUG("Notifications enabled for %d\r\n", handle); +} + 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); - } + // bubble up to services, they will emit callbacks if handle matches + Nudge::handleDataWritten(charHandle); } /** @@ -187,50 +132,73 @@ * thread.*/ } +void bluetoothInit() +{ + DEBUG("Bluetooth initialising...\r\n"); + ble.init(); /* Initialise the nRF51822 */ + ble.setDeviceName(Config::deviceName); + //ble.onTimeout(timeoutCallback); + ble.onConnection(connectionCallback); + ble.onDisconnection(disconnectionCallback); + //ble.onDataSent(onDataSent); + ble.onDataWritten(onDataWritten); + ble.onUpdatesEnabled(onUpdatesEnabled); + //ble.onUpdatesDisabled(onUpdatesDisabled); + //ble.onConfirmationReceived(onConfirmationReceived); + + // Make sure we use our preferred conn. parameters + connectionParams.minConnectionInterval = Config::minConnectionInterval; + connectionParams.maxConnectionInterval = Config::maxConnectionInterval; + connectionParams.slaveLatency = Config::slaveLatency; + connectionParams.connectionSupervisionTimeout = Config::supervisionTimeout; + ble.setPreferredConnectionParams(&connectionParams); + ble.getPreferredConnectionParams(&connectionParams); + DEBUG("Conn. params => min=%d, max=%d, slave=%d, supervision=%d\r\n", + connectionParams.minConnectionInterval, + connectionParams.maxConnectionInterval, + connectionParams.slaveLatency, + connectionParams.connectionSupervisionTimeout); + + // Initialise services + //ble.addService(devInfoService); + DevInfo::init(ble); + ble.addService(batteryService); + //ble.addService(buddiService); + Nudge::init(ble); + + /* setup advertising */ + ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); + ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, uuidlistrev, 16); + ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); + ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); + ble.setAdvertisingInterval(Config::advertisingInterval); + ble.startAdvertising(); + advertisingStateLed = 1; + DEBUG("Ready. Advertising.\r\n"); +} + int main(void) { - //float acc_data[3]; - //int16_t acc_raw[3]; - + DEBUG("Initialising Buddi Nudge | Built %s %s\n\r", __DATE__, __TIME__); + + for (int i = 0; i < 16; i++) { + uuidlistrev[15 - i] = uuidlist[i]; + } + + // TODO: Add battery and dev info to advertising list + bluetoothInit(); + oneSecondLed = 1; Ticker ticker; ticker.attach(periodicCallback, 1); + //float acc_data[3]; + //int16_t acc_raw[3]; /* 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); - - ble.getPreferredConnectionParams(&connectionParams); - - /* 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) { if (triggerSensorPolling) { triggerSensorPolling = false; @@ -245,27 +213,6 @@ } //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; } else { ble.waitForEvent();