Send continuous stream to mobile

Dependencies:   MPU9250

Fork of pdiot-ble-notify-array by Andrew Bates

Committer:
vladb
Date:
Wed Oct 04 10:58:38 2017 +0000
Revision:
47:4905acf20758
Parent:
30:84797119c14a
continuous stream sent to mobile

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Vincent Coubard 0:3fe9d5124576 1 /* mbed Microcontroller Library
Vincent Coubard 0:3fe9d5124576 2 * Copyright (c) 2006-2013 ARM Limited
Vincent Coubard 0:3fe9d5124576 3 *
Vincent Coubard 0:3fe9d5124576 4 * Licensed under the Apache License, Version 2.0 (the "License");
Vincent Coubard 0:3fe9d5124576 5 * you may not use this file except in compliance with the License.
Vincent Coubard 0:3fe9d5124576 6 * You may obtain a copy of the License at
Vincent Coubard 0:3fe9d5124576 7 *
Vincent Coubard 0:3fe9d5124576 8 * http://www.apache.org/licenses/LICENSE-2.0
Vincent Coubard 0:3fe9d5124576 9 *
Vincent Coubard 0:3fe9d5124576 10 * Unless required by applicable law or agreed to in writing, software
Vincent Coubard 0:3fe9d5124576 11 * distributed under the License is distributed on an "AS IS" BASIS,
Vincent Coubard 0:3fe9d5124576 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Vincent Coubard 0:3fe9d5124576 13 * See the License for the specific language governing permissions and
Vincent Coubard 0:3fe9d5124576 14 * limitations under the License.
Vincent Coubard 0:3fe9d5124576 15 */
mbed_official 14:014670ad5e18 16 #include <events/mbed_events.h>
Vincent Coubard 0:3fe9d5124576 17
Vincent Coubard 0:3fe9d5124576 18 #include <mbed.h>
Vincent Coubard 0:3fe9d5124576 19 #include "ble/BLE.h"
Vincent Coubard 0:3fe9d5124576 20 #include "ble/Gap.h"
Vincent Coubard 0:3fe9d5124576 21 #include "ButtonService.h"
Vincent Coubard 0:3fe9d5124576 22
Vincent Coubard 0:3fe9d5124576 23 DigitalOut led1(LED1, 1);
mbed_official 5:b630d2a88c51 24 InterruptIn button(BLE_BUTTON_PIN_NAME);
Vincent Coubard 0:3fe9d5124576 25
mbed_official 30:84797119c14a 26 static EventQueue eventQueue(/* event count */ 10 * EVENTS_EVENT_SIZE);
Vincent Coubard 0:3fe9d5124576 27
Vincent Coubard 0:3fe9d5124576 28 const static char DEVICE_NAME[] = "Button";
Vincent Coubard 0:3fe9d5124576 29 static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID};
Vincent Coubard 0:3fe9d5124576 30
Vincent Coubard 0:3fe9d5124576 31 ButtonService *buttonServicePtr;
Vincent Coubard 0:3fe9d5124576 32
Vincent Coubard 0:3fe9d5124576 33 void buttonPressedCallback(void)
vladb 47:4905acf20758 34 {
vladb 47:4905acf20758 35
mbed_official 14:014670ad5e18 36 eventQueue.call(Callback<void(bool)>(buttonServicePtr, &ButtonService::updateButtonState), true);
vladb 47:4905acf20758 37
Vincent Coubard 0:3fe9d5124576 38 }
Vincent Coubard 0:3fe9d5124576 39
Vincent Coubard 0:3fe9d5124576 40 void buttonReleasedCallback(void)
Vincent Coubard 0:3fe9d5124576 41 {
mbed_official 14:014670ad5e18 42 eventQueue.call(Callback<void(bool)>(buttonServicePtr, &ButtonService::updateButtonState), false);
Vincent Coubard 0:3fe9d5124576 43 }
Vincent Coubard 0:3fe9d5124576 44
Vincent Coubard 0:3fe9d5124576 45 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
Vincent Coubard 0:3fe9d5124576 46 {
Vincent Coubard 0:3fe9d5124576 47 BLE::Instance().gap().startAdvertising(); // restart advertising
Vincent Coubard 0:3fe9d5124576 48 }
Vincent Coubard 0:3fe9d5124576 49
Vincent Coubard 0:3fe9d5124576 50 void blinkCallback(void)
Vincent Coubard 0:3fe9d5124576 51 {
Vincent Coubard 0:3fe9d5124576 52 led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */
vladb 47:4905acf20758 53 pc.printf("blink");
vladb 47:4905acf20758 54
vladb 47:4905acf20758 55 eventQueue.call(Callback<void(bool)>(buttonServicePtr, &ButtonService::updateButtonState), true);
vladb 47:4905acf20758 56
Vincent Coubard 0:3fe9d5124576 57 }
Vincent Coubard 0:3fe9d5124576 58
Vincent Coubard 0:3fe9d5124576 59 void onBleInitError(BLE &ble, ble_error_t error)
Vincent Coubard 0:3fe9d5124576 60 {
Vincent Coubard 0:3fe9d5124576 61 /* Initialization error handling should go here */
Vincent Coubard 0:3fe9d5124576 62 }
Vincent Coubard 0:3fe9d5124576 63
Vincent Coubard 0:3fe9d5124576 64 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
Vincent Coubard 0:3fe9d5124576 65 {
Vincent Coubard 0:3fe9d5124576 66 BLE& ble = params->ble;
Vincent Coubard 0:3fe9d5124576 67 ble_error_t error = params->error;
Vincent Coubard 0:3fe9d5124576 68
Vincent Coubard 0:3fe9d5124576 69 if (error != BLE_ERROR_NONE) {
Vincent Coubard 0:3fe9d5124576 70 /* In case of error, forward the error handling to onBleInitError */
Vincent Coubard 0:3fe9d5124576 71 onBleInitError(ble, error);
Vincent Coubard 0:3fe9d5124576 72 return;
Vincent Coubard 0:3fe9d5124576 73 }
Vincent Coubard 0:3fe9d5124576 74
Vincent Coubard 0:3fe9d5124576 75 /* Ensure that it is the default instance of BLE */
Vincent Coubard 0:3fe9d5124576 76 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
Vincent Coubard 0:3fe9d5124576 77 return;
Vincent Coubard 0:3fe9d5124576 78 }
Vincent Coubard 0:3fe9d5124576 79
Vincent Coubard 0:3fe9d5124576 80 ble.gap().onDisconnection(disconnectionCallback);
Vincent Coubard 0:3fe9d5124576 81
Vincent Coubard 0:3fe9d5124576 82 button.fall(buttonPressedCallback);
Vincent Coubard 0:3fe9d5124576 83 button.rise(buttonReleasedCallback);
Vincent Coubard 0:3fe9d5124576 84
Vincent Coubard 0:3fe9d5124576 85 /* Setup primary service. */
Vincent Coubard 0:3fe9d5124576 86 buttonServicePtr = new ButtonService(ble, false /* initial value for button pressed */);
Vincent Coubard 0:3fe9d5124576 87
Vincent Coubard 0:3fe9d5124576 88 /* setup advertising */
Vincent Coubard 0:3fe9d5124576 89 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
Vincent Coubard 0:3fe9d5124576 90 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
Vincent Coubard 0:3fe9d5124576 91 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
Vincent Coubard 0:3fe9d5124576 92 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
Vincent Coubard 0:3fe9d5124576 93 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
Vincent Coubard 0:3fe9d5124576 94 ble.gap().startAdvertising();
Vincent Coubard 0:3fe9d5124576 95 }
Vincent Coubard 0:3fe9d5124576 96
Vincent Coubard 0:3fe9d5124576 97 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
Vincent Coubard 0:3fe9d5124576 98 BLE &ble = BLE::Instance();
mbed_official 14:014670ad5e18 99 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
Vincent Coubard 0:3fe9d5124576 100 }
Vincent Coubard 0:3fe9d5124576 101
Vincent Coubard 0:3fe9d5124576 102 int main()
Vincent Coubard 0:3fe9d5124576 103 {
mbed_official 14:014670ad5e18 104 eventQueue.call_every(500, blinkCallback);
Vincent Coubard 0:3fe9d5124576 105
Vincent Coubard 0:3fe9d5124576 106 BLE &ble = BLE::Instance();
Vincent Coubard 0:3fe9d5124576 107 ble.onEventsToProcess(scheduleBleEventsProcessing);
Vincent Coubard 0:3fe9d5124576 108 ble.init(bleInitComplete);
Vincent Coubard 0:3fe9d5124576 109
mbed_official 14:014670ad5e18 110 eventQueue.dispatch_forever();
vladb 47:4905acf20758 111
vladb 47:4905acf20758 112
Vincent Coubard 0:3fe9d5124576 113 return 0;
Vincent Coubard 0:3fe9d5124576 114 }