Alexander Lea / Mbed 2 deprecated BLE_GATT_test1

Dependencies:   BLE_API mbed nRF51822

Committer:
alexanderlea
Date:
Sun Jan 18 15:50:17 2015 +0000
Revision:
2:f538ff758828
Parent:
1:ebdf445c4bcc
Child:
3:f3d20b36b7ea
Tidied up loads of commented code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
alexanderlea 0:af868ad47854 1 /*
alexanderlea 0:af868ad47854 2 * TODO:
alexanderlea 0:af868ad47854 3 * - Make it work!
alexanderlea 0:af868ad47854 4 */
alexanderlea 0:af868ad47854 5
alexanderlea 0:af868ad47854 6 #include "mbed.h"
alexanderlea 0:af868ad47854 7 #include "BLEDevice.h"
alexanderlea 0:af868ad47854 8 #include "CarCommsService.h"
alexanderlea 0:af868ad47854 9 #include "TestGattService.h"
alexanderlea 0:af868ad47854 10 #include <string>
alexanderlea 0:af868ad47854 11
alexanderlea 0:af868ad47854 12 using namespace std;
alexanderlea 0:af868ad47854 13
alexanderlea 0:af868ad47854 14 BLEDevice ble;
alexanderlea 0:af868ad47854 15 DigitalOut led1(LED1);
alexanderlea 0:af868ad47854 16 Serial pc(USBTX, USBRX);
alexanderlea 0:af868ad47854 17
alexanderlea 0:af868ad47854 18 /*Variable Declarations*/
alexanderlea 1:ebdf445c4bcc 19 const static char DEVICE_NAME[] = "BLE_NODE";
alexanderlea 0:af868ad47854 20 static volatile bool connected;
alexanderlea 0:af868ad47854 21
alexanderlea 0:af868ad47854 22 void blink(void)
alexanderlea 0:af868ad47854 23 {
alexanderlea 0:af868ad47854 24 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
alexanderlea 0:af868ad47854 25 }
alexanderlea 0:af868ad47854 26
alexanderlea 0:af868ad47854 27 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
alexanderlea 0:af868ad47854 28 {
alexanderlea 0:af868ad47854 29 pc.printf("Disconnected! - start advertising. Handle:%d, Reason:0x%02x\r\n", handle, reason);
alexanderlea 0:af868ad47854 30 connected=false;
alexanderlea 0:af868ad47854 31 ble.startAdvertising();
alexanderlea 0:af868ad47854 32 }
alexanderlea 0:af868ad47854 33
alexanderlea 0:af868ad47854 34
alexanderlea 0:af868ad47854 35 //Handle_t, addr_type_t peerAddrType, const address_t peerAddr, const ConnectionParams_t *
alexanderlea 0:af868ad47854 36 void connectionCallback(Gap::Handle_t handle, Gap::addr_type_t peerAddrType, const Gap::address_t peerAddr, const Gap::ConnectionParams_t *parms)
alexanderlea 0:af868ad47854 37 {
alexanderlea 0:af868ad47854 38 pc.printf("Connected! - stop advertising. Handle:%d, eType:%d, Add:%u.\r\n", handle, peerAddrType, peerAddr);
alexanderlea 0:af868ad47854 39
alexanderlea 0:af868ad47854 40 connected = true;
alexanderlea 0:af868ad47854 41 // ble.stopAdvertising();
alexanderlea 0:af868ad47854 42 }
alexanderlea 0:af868ad47854 43
alexanderlea 0:af868ad47854 44 int main(void)
alexanderlea 0:af868ad47854 45 {
alexanderlea 2:f538ff758828 46 uint8_t testCommand = 12;
alexanderlea 0:af868ad47854 47
alexanderlea 0:af868ad47854 48 //blinky
alexanderlea 0:af868ad47854 49 led1 = 1;
alexanderlea 0:af868ad47854 50 Ticker t;
alexanderlea 0:af868ad47854 51 t.attach(blink, 1.0f);
alexanderlea 0:af868ad47854 52
alexanderlea 0:af868ad47854 53 //Create BLE stuff
alexanderlea 0:af868ad47854 54 ble.init();
alexanderlea 0:af868ad47854 55 ble.onDisconnection(disconnectionCallback);
alexanderlea 0:af868ad47854 56 ble.onConnection(connectionCallback);
alexanderlea 0:af868ad47854 57
alexanderlea 0:af868ad47854 58 //CarCommsService commsService(ble, cmd);
alexanderlea 2:f538ff758828 59 TestGattService testService(ble, 0);
alexanderlea 2:f538ff758828 60
alexanderlea 0:af868ad47854 61 /*
alexanderlea 0:af868ad47854 62 **BREDR_NOT_SUPPORTED = BLE only
alexanderlea 0:af868ad47854 63 **LE_GENERAL_DISCOVERABLE = Device is discoverable at any moment (no time out)
alexanderlea 0:af868ad47854 64 **ADV_CONNECTABLE_UNDIRECTED = Any central device can connect
alexanderlea 0:af868ad47854 65 */
alexanderlea 0:af868ad47854 66 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
alexanderlea 0:af868ad47854 67 // ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
alexanderlea 0:af868ad47854 68 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
alexanderlea 0:af868ad47854 69 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
alexanderlea 0:af868ad47854 70
alexanderlea 0:af868ad47854 71 //Advertise
alexanderlea 0:af868ad47854 72 ble.setAdvertisingInterval(160); /* 1s; in multiples of 0.625ms. */ //was 1600
alexanderlea 0:af868ad47854 73 ble.startAdvertising();
alexanderlea 0:af868ad47854 74
alexanderlea 0:af868ad47854 75 pc.printf("Advertising node %s\n\r", DEVICE_NAME);
alexanderlea 0:af868ad47854 76
alexanderlea 0:af868ad47854 77 while(true) {
alexanderlea 0:af868ad47854 78
alexanderlea 2:f538ff758828 79 ble.waitForEvent(); // this will return upon any system event (such as an interrupt or a ticker wakeup)
alexanderlea 2:f538ff758828 80
alexanderlea 2:f538ff758828 81 testCommand++;
alexanderlea 0:af868ad47854 82
alexanderlea 2:f538ff758828 83 if (testCommand < 100) {
alexanderlea 2:f538ff758828 84 testCommand = 0;
alexanderlea 0:af868ad47854 85 }
alexanderlea 0:af868ad47854 86
alexanderlea 2:f538ff758828 87 pc.printf("Level = %u\r\n", testCommand);
alexanderlea 2:f538ff758828 88 testService.sendCommand(testCommand);
alexanderlea 0:af868ad47854 89 }
alexanderlea 0:af868ad47854 90 }