Alexander Lea / Mbed 2 deprecated BLE_GATT_test1

Dependencies:   BLE_API mbed nRF51822

Committer:
alexanderlea
Date:
Tue Feb 17 13:57:28 2015 +0000
Revision:
3:f3d20b36b7ea
Parent:
2:f538ff758828
Child:
4:ac0ee88ea0ed
Working as pretend battery status node

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 3:f3d20b36b7ea 19 const static char DEVICE_NAME[] = "BLE_Broadcaster";
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 void connectionCallback(Gap::Handle_t handle, Gap::addr_type_t peerAddrType, const Gap::address_t peerAddr, const Gap::ConnectionParams_t *parms)
alexanderlea 0:af868ad47854 35 {
alexanderlea 0:af868ad47854 36 pc.printf("Connected! - stop advertising. Handle:%d, eType:%d, Add:%u.\r\n", handle, peerAddrType, peerAddr);
alexanderlea 0:af868ad47854 37
alexanderlea 0:af868ad47854 38 connected = true;
alexanderlea 3:f3d20b36b7ea 39 ble.stopAdvertising();
alexanderlea 0:af868ad47854 40 }
alexanderlea 0:af868ad47854 41
alexanderlea 0:af868ad47854 42 int main(void)
alexanderlea 0:af868ad47854 43 {
alexanderlea 3:f3d20b36b7ea 44 //TODO: I want to be able to send something like:
alexanderlea 3:f3d20b36b7ea 45 uint8_t command[8] = { 0x4d,0x32,0x81,0xc0,0x4d,0x32,0x81,0xc0 };
alexanderlea 3:f3d20b36b7ea 46 //with first 4 bits being type code, and last 4 being command, or something
alexanderlea 3:f3d20b36b7ea 47
alexanderlea 3:f3d20b36b7ea 48 uint8_t testCommand = 0x12; //max value is 255
alexanderlea 0:af868ad47854 49
alexanderlea 0:af868ad47854 50 //blinky
alexanderlea 3:f3d20b36b7ea 51 led1 = 1;
alexanderlea 0:af868ad47854 52 Ticker t;
alexanderlea 3:f3d20b36b7ea 53 t.attach(blink, 5.0f);
alexanderlea 0:af868ad47854 54
alexanderlea 0:af868ad47854 55 //Create BLE stuff
alexanderlea 0:af868ad47854 56 ble.init();
alexanderlea 0:af868ad47854 57 ble.onDisconnection(disconnectionCallback);
alexanderlea 0:af868ad47854 58 ble.onConnection(connectionCallback);
alexanderlea 0:af868ad47854 59
alexanderlea 0:af868ad47854 60 //CarCommsService commsService(ble, cmd);
alexanderlea 3:f3d20b36b7ea 61 TestGattService testService(ble, testCommand);
alexanderlea 2:f538ff758828 62
alexanderlea 0:af868ad47854 63 /*
alexanderlea 0:af868ad47854 64 **BREDR_NOT_SUPPORTED = BLE only
alexanderlea 0:af868ad47854 65 **LE_GENERAL_DISCOVERABLE = Device is discoverable at any moment (no time out)
alexanderlea 0:af868ad47854 66 **ADV_CONNECTABLE_UNDIRECTED = Any central device can connect
alexanderlea 0:af868ad47854 67 */
alexanderlea 0:af868ad47854 68 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
alexanderlea 0:af868ad47854 69 // ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
alexanderlea 0:af868ad47854 70 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
alexanderlea 0:af868ad47854 71 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
alexanderlea 0:af868ad47854 72
alexanderlea 0:af868ad47854 73 //Advertise
alexanderlea 0:af868ad47854 74 ble.setAdvertisingInterval(160); /* 1s; in multiples of 0.625ms. */ //was 1600
alexanderlea 0:af868ad47854 75 ble.startAdvertising();
alexanderlea 0:af868ad47854 76
alexanderlea 0:af868ad47854 77 pc.printf("Advertising node %s\n\r", DEVICE_NAME);
alexanderlea 0:af868ad47854 78
alexanderlea 0:af868ad47854 79 while(true) {
alexanderlea 0:af868ad47854 80
alexanderlea 2:f538ff758828 81 ble.waitForEvent(); // this will return upon any system event (such as an interrupt or a ticker wakeup)
alexanderlea 2:f538ff758828 82
alexanderlea 2:f538ff758828 83 testCommand++;
alexanderlea 0:af868ad47854 84
alexanderlea 3:f3d20b36b7ea 85 if (testCommand > 100) {
alexanderlea 2:f538ff758828 86 testCommand = 0;
alexanderlea 0:af868ad47854 87 }
alexanderlea 0:af868ad47854 88
alexanderlea 2:f538ff758828 89 pc.printf("Level = %u\r\n", testCommand);
alexanderlea 2:f538ff758828 90 testService.sendCommand(testCommand);
alexanderlea 0:af868ad47854 91 }
alexanderlea 0:af868ad47854 92 }