Dung Nguyen / Mbed 2 deprecated TimeSync

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_GATT_Example by Bluetooth Low Energy

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "ble/BLE.h"
00003 
00004 DigitalOut led(P0_4, 0);
00005 uint16_t customServiceUUID  = 0xA000;
00006 uint16_t readCharUUID       = 0xA001;
00007 uint16_t writeCharUUID      = 0xA002;
00008 
00009 const static char     DEVICE_NAME[]        = "timesync2"; // change this
00010 static const uint16_t uuid16_list[]        = {0xFFFF}; //Custom UUID, FFFF is reserved for development
00011 
00012 
00013 static uint8_t readValue[10] = {0};
00014 ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(readCharUUID, readValue);
00015 
00016 static uint8_t writeValue[10] = {0};
00017 WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue);
00018 
00019 /* Set up custom service */
00020 GattCharacteristic *characteristics[] = {&readChar, &writeChar};
00021 GattService        customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
00022 
00023 /*
00024  *  Restart advertising when phone app disconnects
00025 */
00026 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
00027 {
00028     led = 0;    // just in case
00029     BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising();
00030 }
00031 
00032 /*
00033  *  Handle writes to writeCharacteristic
00034 */
00035 void writeCharCallback(const GattWriteCallbackParams *params)
00036 {
00037     /* Check to see what characteristic was written, by handle */
00038     if(params->handle == writeChar.getValueHandle()) {
00039         // since LED is pullup mode
00040         led = 1;
00041         wait(1.0);
00042         led = 0;
00043     }
00044 }
00045 
00046 
00047 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00048 {
00049     BLE &ble          = params->ble;
00050     ble_error_t error = params->error;
00051     
00052     if (error != BLE_ERROR_NONE) {
00053         return;
00054     }
00055 
00056     ble.gap().onDisconnection(disconnectionCallback);
00057     ble.gattServer().onDataWritten(writeCharCallback);
00058 
00059     /* Setup advertising */
00060     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT
00061     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type
00062     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name
00063     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet
00064     ble.gap().setAdvertisingInterval(400); // 500ms.
00065 
00066     /* Add our custom service */
00067     ble.addService(customService);
00068 
00069     /* Start advertising */
00070     ble.gap().startAdvertising();
00071 }
00072 
00073 /*
00074  *  Main loop
00075 */
00076 int main(void)
00077 {   
00078     NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos;
00079     NRF_POWER->TASKS_LOWPWR = 1;
00080     NRF_ADC->ENABLE =   (ADC_ENABLE_ENABLE_Disabled << ADC_ENABLE_ENABLE_Pos); 
00081     NRF_POWER->DCDCEN = 0x00000001;
00082     
00083     BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
00084     ble.init(bleInitComplete);
00085     ble.setTxPower(-4);
00086     
00087     /* SpinWait for initialization to complete. This is necessary because the
00088      * BLE object is used in the main loop below. */
00089     while (ble.hasInitialized()  == false) { /* spin loop */ }
00090 
00091     /* Infinite loop waiting for BLE interrupt events */
00092     while (true) {
00093         ble.waitForEvent(); /* Save power */
00094     }
00095 }