Template for the workshop 'Ontwerp je eigen Bluetooth LE device' for the NIOC Congress 2018

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "ble/BLE.h"
00004 
00005 // Define the interface to the leds, they are digital outputs when can be
00006 // enabled or disabled
00007 enum { total_leds = 4 };
00008 DigitalOut led[total_leds] = { D6, D7, D8, D9 };
00009 
00010 // Function to enable led with index 0..3
00011 void EnableLed(uint8_t index)
00012 {
00013     if(index < total_leds)
00014         led[index] = 0;
00015 }
00016 
00017 // Function to disable led with index 0..3
00018 void DisableLed(uint8_t index)
00019 {
00020     if(index < total_leds)
00021         led[index] = 1;
00022 }
00023 
00024 // Define the interface to the buttons, they are configured to 
00025 // generate an interrupt and call the ButtonPress function
00026 enum { total_buttons = 4 };
00027 InterruptIn buttons[total_buttons] = { D2, D3, D4, D5 };
00028 
00029 // The name of this device
00030 // TODO .. Change the name of this device
00031 const static char DEVICE_NAME[] = "Nioc_BLE";
00032 
00033 // The UUID (Unique Universal Identifier) of our Alert service and Alert characteristic
00034 enum { ALERT_SERVICE_UUID = 0x1811 };
00035 enum { ALERT_VALUE_UUID = 0x2A06 };
00036 
00037 // Definition our alert characteristic
00038 ReadWriteGattCharacteristic<uint8_t> alert_characteristic(ALERT_VALUE_UUID, NULL, 
00039     GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
00040 
00041 // This function is called when data is written to a characteristic
00042 void onDataWrittenCallback(const GattWriteCallbackParams *params)
00043 {
00044     // Check if it really was the alert characteristic that was written by checking the handle
00045     if(params->handle == alert_characteristic.getValueHandle() && params->len == sizeof(uint8_t))
00046     {
00047         // We got a new value for the alert characteristic, do something useful with this
00048         uint8_t value = *(uint8_t const *)(params->data);
00049         // TODO: .. do something useful with this value
00050     }
00051 }
00052 
00053 // This function is called when the gatt connection is disconnected
00054 // we have to manually re-enable the advertisements again when this occurs
00055 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
00056 {
00057     BLE::Instance().gap().startAdvertising();
00058 }
00059 
00060 // This function is called when the BLE stack is initialized
00061 // Setup the GATT Service and Advertisement here for the example
00062 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00063 {
00064     if(params->error != BLE_ERROR_NONE)
00065         return;
00066     
00067     BLE &ble = BLE::Instance();
00068     
00069     // Initialize the default of our alert characteristic
00070     uint8_t init_alert_value = 0;
00071     BLE::Instance().gattServer().write(alert_characteristic.getValueHandle(), &init_alert_value, sizeof(uint8_t)); 
00072     
00073     // First create our GATT Service (basicly a list of characteristics )
00074     enum { TOTAL_SERVICE_CHARACTERTERISTICS = 1 };
00075     GattCharacteristic *characteristics_table[TOTAL_SERVICE_CHARACTERTERISTICS] = { &alert_characteristic };
00076     GattService alert_service(ALERT_SERVICE_UUID, characteristics_table, TOTAL_SERVICE_CHARACTERTERISTICS);
00077     ble.gattServer().addService(alert_service);
00078     
00079     // Setup the BLE callbacks    
00080     ble.gap().onDisconnection(disconnectionCallback);
00081     ble.gattServer().onDataWritten(onDataWrittenCallback);
00082     
00083     // Configure the advertisement, we add the name of the device to the advertisement and some service data
00084     ble.gap().setDeviceName((uint8_t const *)DEVICE_NAME);
00085     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00086     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (uint8_t const *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1);
00087     
00088     uint8_t service_data[] = { 0x11, 0x18, 0x00 };
00089     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, service_data, sizeof(service_data));
00090 
00091     // Start advertising our device information now
00092     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00093     ble.gap().setAdvertisingInterval(250);
00094     ble.gap().startAdvertising();
00095 }
00096 
00097 // This function is called when a button is pressed
00098 void ButtonPressed(uint8_t index)
00099 {
00100     // TODO: ... do something useful here when a button is pressed
00101 }
00102 
00103 // Eventqueue to handle incoming events
00104 EventQueue eventQueue(16 * EVENTS_EVENT_SIZE);
00105 
00106 // Function required to process events from the BLE Stack
00107 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext *context)
00108 {
00109     eventQueue.call(Callback<void()>(&BLE::Instance(), &BLE::processEvents));
00110 }
00111 
00112 // Let's setup the BLE stack
00113 int main(void)
00114 {    
00115     // Configure the led to the default state
00116     EnableLed(0);
00117     DisableLed(1);
00118     DisableLed(2);
00119     DisableLed(3);
00120     
00121     // Configure the button callbacks
00122     buttons[0].mode(PullUp);
00123     buttons[0].fall(eventQueue.event(ButtonPressed, 0));
00124     buttons[1].mode(PullUp);
00125     buttons[1].fall(eventQueue.event(ButtonPressed, 1));
00126     buttons[2].mode(PullUp);
00127     buttons[2].fall(eventQueue.event(ButtonPressed, 2));
00128     buttons[3].mode(PullUp);
00129     buttons[3].fall(eventQueue.event(ButtonPressed, 3));
00130     
00131     // Start the BLE Stack 
00132     BLE::Instance().onEventsToProcess(scheduleBleEventsProcessing);
00133     BLE::Instance().init(bleInitComplete);
00134     eventQueue.dispatch_forever();
00135     return 0;
00136 }