Example program for mbed Cloud Bluetooth Devicelink

Dependencies:   ChainableLED

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include <events/mbed_events.h>
00017 
00018 #include <mbed.h>
00019 #include "ble/BLE.h"
00020 #include "ble/Gap.h"
00021 #include "ButtonService.h"
00022 #include "GroveLedService.h"
00023 #include "ChainableLED.h"
00024 
00025 DigitalOut  led1(LED1, 1);
00026 InterruptIn button(BLE_BUTTON_PIN_NAME);
00027 ChainableLED grove_led(D2, D3, 1);
00028 
00029 static EventQueue eventQueue(
00030     /* event count */ 10 * /* event size */ 32
00031 );
00032 
00033 const static char     DEVICE_NAME[] = "DeviceLinkTest";
00034 static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID, GroveLEDService::LED_SERVICE_UUID};
00035 
00036 ButtonService *buttonServicePtr;
00037 GroveLEDService *ledServicePtr;
00038 
00039 static uint8_t button_count = 0;
00040 
00041 void buttonPressedCallback(void)
00042 {
00043     eventQueue.call(Callback<void(uint8_t)>(buttonServicePtr, &ButtonService::updateButtonState), ++button_count);
00044 }
00045 
00046 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00047 {
00048     BLE::Instance().gap().startAdvertising(); // restart advertising
00049 }
00050 
00051 void blinkCallback(void)
00052 {
00053     led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */
00054 }
00055 
00056 void onBleInitError(BLE &ble, ble_error_t error)
00057 {
00058     /* Initialization error handling should go here */
00059 }
00060 
00061 /**
00062  * This callback allows the LEDService to receive updates to the ledState Characteristic.
00063  *
00064  * @param[in] params
00065  *     Information about the characteristic being updated.
00066  */
00067 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
00068     if ((params->handle == ledServicePtr->getValueHandle()) && (params->len >= 3)) {
00069         grove_led.setColorRGB(0, params->data[0], params->data[1], params->data[2]);
00070     }
00071 }
00072 
00073 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00074 {
00075     BLE&        ble   = params->ble;
00076     ble_error_t error = params->error;
00077 
00078     if (error != BLE_ERROR_NONE) {
00079         /* In case of error, forward the error handling to onBleInitError */
00080         onBleInitError(ble, error);
00081         return;
00082     }
00083 
00084     /* Ensure that it is the default instance of BLE */
00085     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00086         return;
00087     }
00088 
00089     ble.gap().onDisconnection(disconnectionCallback);
00090     ble.gattServer().onDataWritten(onDataWrittenCallback);
00091 
00092     button.fall(buttonPressedCallback);
00093 
00094     buttonServicePtr = new ButtonService(ble, 0);
00095     ledServicePtr    = new GroveLEDService(ble, 0xff0000);
00096 
00097     /* setup advertising */
00098     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00099     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
00100     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00101     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00102     ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
00103     ble.gap().startAdvertising();
00104 }
00105 
00106 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
00107     BLE &ble = BLE::Instance();
00108     eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
00109 }
00110 
00111 int main()
00112 {
00113     grove_led.setColorRGB(0, 0xff, 0, 0); // red
00114 
00115     eventQueue.call_every(500, blinkCallback);
00116 
00117     BLE &ble = BLE::Instance();
00118     ble.onEventsToProcess(scheduleBleEventsProcessing);
00119     ble.init(bleInitComplete);
00120 
00121     eventQueue.dispatch_forever();
00122 
00123     return 0;
00124 }