L0_BlueNRG_Test_Client
Dependencies: BLE_API BLE_BlueNRG mbed
Fork of L0_BlueNRG_Test by
main.cpp@1:3b1c20952274, 2014-07-25 (annotated)
- Committer:
- mridup
- Date:
- Fri Jul 25 07:08:14 2014 +0000
- Revision:
- 1:3b1c20952274
- Parent:
- 0:e0204b0e5541
- Child:
- 2:cf54be3a9e6a
BlueNRG Test Code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mridup | 0:e0204b0e5541 | 1 | #include "mbed.h" |
mridup | 0:e0204b0e5541 | 2 | |
mridup | 0:e0204b0e5541 | 3 | DigitalOut myled(LED1); |
mridup | 0:e0204b0e5541 | 4 | |
mridup | 1:3b1c20952274 | 5 | #include "btle.h" |
mridup | 1:3b1c20952274 | 6 | //#include "BlueNRGDevice.h"//User does not use any platform specific header file |
mridup | 1:3b1c20952274 | 7 | #include "BLEDevice.h" |
mridup | 1:3b1c20952274 | 8 | #include "UUID.h" |
mridup | 1:3b1c20952274 | 9 | |
mridup | 1:3b1c20952274 | 10 | BLEDevice dev; |
mridup | 1:3b1c20952274 | 11 | |
mridup | 1:3b1c20952274 | 12 | #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; |
mridup | 1:3b1c20952274 | 13 | * it will have an impact on code-size and power consumption. */ |
mridup | 1:3b1c20952274 | 14 | |
mridup | 1:3b1c20952274 | 15 | #if NEED_CONSOLE_OUTPUT |
mridup | 1:3b1c20952274 | 16 | Serial pc(USBTX, USBRX); |
mridup | 1:3b1c20952274 | 17 | #define DEBUG(...) { pc.printf(__VA_ARGS__); } |
mridup | 1:3b1c20952274 | 18 | #else |
mridup | 1:3b1c20952274 | 19 | #define DEBUG(...) /* nothing */ |
mridup | 1:3b1c20952274 | 20 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
mridup | 1:3b1c20952274 | 21 | |
mridup | 1:3b1c20952274 | 22 | const static char DEVICE_NAME[] = "STBLUE_NRG"; |
mridup | 1:3b1c20952274 | 23 | const uint8_t device_address[6] = { 0x12, 0x34, 0x00, 0xe1, 0x80, 0x02 }; //Peripheral address |
mridup | 1:3b1c20952274 | 24 | |
mridup | 1:3b1c20952274 | 25 | void Append128bitUUID(uint8_t *uuid128_list, const LongUUID_t HRM_SERVICE_UUID_128); |
mridup | 1:3b1c20952274 | 26 | void print_array(uint8_t *array); |
mridup | 0:e0204b0e5541 | 27 | |
mridup | 1:3b1c20952274 | 28 | /*********128 bit UUIDs. Not defined in GattCharacteristic.h and GattService.h*************/ |
mridup | 1:3b1c20952274 | 29 | |
mridup | 1:3b1c20952274 | 30 | |
mridup | 1:3b1c20952274 | 31 | #define MAX_SERVICES_NOS 1 |
mridup | 1:3b1c20952274 | 32 | //typedef uint8_t UUID_128_BIT[16]; |
mridup | 1:3b1c20952274 | 33 | const LongUUID_t HEART_RATE_CHAR_UUID_128 = {0x42,0x82,0x1a,0x40, 0xe4,0x77, 0x11,0xe2, 0x82,0xd0, 0x00,0x02,0xa5,0xd5,0xc5,0x1a}; |
mridup | 1:3b1c20952274 | 34 | const LongUUID_t HRM_SERVICE_UUID_128 = {0x42,0x82,0x1a,0x40, 0xe4,0x77, 0x11,0xe2, 0x82,0xd0, 0x00,0x02,0xa5,0xd5,0xc5,0x1b}; |
mridup | 1:3b1c20952274 | 35 | |
mridup | 1:3b1c20952274 | 36 | uint8_t UUID_Count=0; |
mridup | 1:3b1c20952274 | 37 | |
mridup | 1:3b1c20952274 | 38 | |
mridup | 1:3b1c20952274 | 39 | void onConnectionCallback(void) { |
mridup | 1:3b1c20952274 | 40 | myled = 1; // LED is ON |
mridup | 1:3b1c20952274 | 41 | DEBUG("Connected BlueNRG!!"); |
mridup | 1:3b1c20952274 | 42 | } |
mridup | 1:3b1c20952274 | 43 | |
mridup | 1:3b1c20952274 | 44 | void Append128bitUUID(uint8_t *array, const LongUUID_t SERVICE_UUID_128) |
mridup | 1:3b1c20952274 | 45 | { |
mridup | 1:3b1c20952274 | 46 | for(int x=0;x<16; x++) |
mridup | 1:3b1c20952274 | 47 | { |
mridup | 1:3b1c20952274 | 48 | array[x+UUID_Count*16]=SERVICE_UUID_128[x]; |
mridup | 1:3b1c20952274 | 49 | } |
mridup | 1:3b1c20952274 | 50 | UUID_Count++; |
mridup | 1:3b1c20952274 | 51 | return; |
mridup | 1:3b1c20952274 | 52 | } |
mridup | 0:e0204b0e5541 | 53 | |
mridup | 1:3b1c20952274 | 54 | int main() { |
mridup | 1:3b1c20952274 | 55 | |
mridup | 1:3b1c20952274 | 56 | Ticker ticker; //For Tick interrupt if used later on (periodic data updates?) |
mridup | 1:3b1c20952274 | 57 | |
mridup | 1:3b1c20952274 | 58 | //LongUUID_t HEART_RATE_CHAR_UUID_128, HRM_SERVICE_UUID_128; |
mridup | 1:3b1c20952274 | 59 | //COPY_HRM_SERVICE_UUID(HRM_SERVICE_UUID_128); |
mridup | 1:3b1c20952274 | 60 | //COPY_HRM_CHAR_UUID(HEART_RATE_CHAR_UUID_128); |
mridup | 1:3b1c20952274 | 61 | UUID heart_rate_char_UUID = UUID(HEART_RATE_CHAR_UUID_128); |
mridup | 1:3b1c20952274 | 62 | UUID hrm_service_UUID = UUID(HRM_SERVICE_UUID_128); |
mridup | 1:3b1c20952274 | 63 | |
mridup | 1:3b1c20952274 | 64 | myled = 0;//Switch OFF LED1 |
mridup | 1:3b1c20952274 | 65 | |
mridup | 1:3b1c20952274 | 66 | DEBUG("Initializing BlueNRG..."); |
mridup | 1:3b1c20952274 | 67 | dev.init(); |
mridup | 1:3b1c20952274 | 68 | |
mridup | 1:3b1c20952274 | 69 | dev.onConnection(onConnectionCallback); |
mridup | 1:3b1c20952274 | 70 | //TODO. |
mridup | 1:3b1c20952274 | 71 | //dev.setAddress(Gap::ADDR_TYPE_PUBLIC, device_address);//Does not work after gap/gatt init. |
mridup | 1:3b1c20952274 | 72 | //TODO. |
mridup | 1:3b1c20952274 | 73 | |
mridup | 1:3b1c20952274 | 74 | static uint8_t hrmCounter = 100; |
mridup | 1:3b1c20952274 | 75 | static uint8_t bpm[2] = {0x00, hrmCounter}; |
mridup | 1:3b1c20952274 | 76 | GattCharacteristic hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, bpm, sizeof(bpm), sizeof(bpm), |
mridup | 1:3b1c20952274 | 77 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
mridup | 0:e0204b0e5541 | 78 | |
mridup | 1:3b1c20952274 | 79 | GattCharacteristic *hrmChars[] = {&hrmRate }; |
mridup | 1:3b1c20952274 | 80 | GattService hrmService(GattService::UUID_HEART_RATE_SERVICE, hrmChars, sizeof(hrmChars) / sizeof(GattCharacteristic *)); |
mridup | 0:e0204b0e5541 | 81 | |
mridup | 1:3b1c20952274 | 82 | uint8_t uuid128_list[16*MAX_SERVICES_NOS];// = {HRM_SERVICE_UUID_128[0], HRM_SERVICE_UUID_128[1]}; |
mridup | 1:3b1c20952274 | 83 | static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE}; |
mridup | 1:3b1c20952274 | 84 | |
mridup | 1:3b1c20952274 | 85 | //Append128bitUUID(uuid128_list, HRM_SERVICE_UUID_128); |
mridup | 1:3b1c20952274 | 86 | |
mridup | 1:3b1c20952274 | 87 | /* setup advertising */ |
mridup | 1:3b1c20952274 | 88 | dev.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
mridup | 0:e0204b0e5541 | 89 | |
mridup | 1:3b1c20952274 | 90 | //TODO:IMP STUFF: 128bit list is basically a uint8_t list. User should know how many services he supports and define the number in MAX_SERVICES_NOS |
mridup | 1:3b1c20952274 | 91 | dev.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t*)uuid16_list, sizeof(uuid16_list)); |
mridup | 1:3b1c20952274 | 92 | |
mridup | 1:3b1c20952274 | 93 | dev.accumulateAdvertisingPayload(GapAdvertisingData::HEART_RATE_SENSOR_HEART_RATE_BELT); |
mridup | 1:3b1c20952274 | 94 | |
mridup | 1:3b1c20952274 | 95 | dev.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
mridup | 1:3b1c20952274 | 96 | dev.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
mridup | 1:3b1c20952274 | 97 | dev.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
mridup | 1:3b1c20952274 | 98 | DEBUG("Starting Advertising..."); |
mridup | 1:3b1c20952274 | 99 | dev.startAdvertising(); |
mridup | 1:3b1c20952274 | 100 | |
mridup | 1:3b1c20952274 | 101 | dev.addService(hrmService); |
mridup | 0:e0204b0e5541 | 102 | |
mridup | 0:e0204b0e5541 | 103 | while(1) { |
mridup | 1:3b1c20952274 | 104 | //myled = 1; // LED is ON |
mridup | 1:3b1c20952274 | 105 | //wait(0.5); // 500 ms |
mridup | 1:3b1c20952274 | 106 | //myled = 0; // LED is OFF |
mridup | 1:3b1c20952274 | 107 | //wait(0.5); // 500 ms |
mridup | 1:3b1c20952274 | 108 | //DEBUG("Testing BlueNRG!!"); |
mridup | 1:3b1c20952274 | 109 | dev.waitForEvent(); |
mridup | 0:e0204b0e5541 | 110 | } |
mridup | 0:e0204b0e5541 | 111 | } |
mridup | 0:e0204b0e5541 | 112 |