51 52 with same code

Dependencies:   BMA250E BMG160

Committer:
johnathanlyu
Date:
Mon Apr 17 01:56:57 2017 +0000
Revision:
0:e71874215e23
Child:
1:b8bbe971b26d
Initial the m904s & mt_sense03 project

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnathanlyu 0:e71874215e23 1 /* mbed Microcontroller Library
johnathanlyu 0:e71874215e23 2 * Copyright (c) 2006-2014 ARM Limited
johnathanlyu 0:e71874215e23 3 *
johnathanlyu 0:e71874215e23 4 * Licensed under the Apache License, Version 2.0 (the "License");
johnathanlyu 0:e71874215e23 5 * you may not use this file except in compliance with the License.
johnathanlyu 0:e71874215e23 6 * You may obtain a copy of the License at
johnathanlyu 0:e71874215e23 7 *
johnathanlyu 0:e71874215e23 8 * http://www.apache.org/licenses/LICENSE-2.0
johnathanlyu 0:e71874215e23 9 *
johnathanlyu 0:e71874215e23 10 * Unless required by applicable law or agreed to in writing, software
johnathanlyu 0:e71874215e23 11 * distributed under the License is distributed on an "AS IS" BASIS,
johnathanlyu 0:e71874215e23 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
johnathanlyu 0:e71874215e23 13 * See the License for the specific language governing permissions and
johnathanlyu 0:e71874215e23 14 * limitations under the License.
johnathanlyu 0:e71874215e23 15 */
johnathanlyu 0:e71874215e23 16
johnathanlyu 0:e71874215e23 17 #include <events/mbed_events.h>
johnathanlyu 0:e71874215e23 18 #include <mbed.h>
johnathanlyu 0:e71874215e23 19 #include "ble/BLE.h"
johnathanlyu 0:e71874215e23 20 #include "ble/Gap.h"
johnathanlyu 0:e71874215e23 21 #include "BMA250E.h"
johnathanlyu 0:e71874215e23 22 /* UART printf */
johnathanlyu 0:e71874215e23 23 Serial pc(p5, p4);
johnathanlyu 0:e71874215e23 24
johnathanlyu 0:e71874215e23 25 /* Sensor */
johnathanlyu 0:e71874215e23 26 BMA250E acclerameter(p14, p13, NC, NC);
johnathanlyu 0:e71874215e23 27
johnathanlyu 0:e71874215e23 28 DigitalOut ledRed(p16, 1);
johnathanlyu 0:e71874215e23 29
johnathanlyu 0:e71874215e23 30 /* UUID, Device name */
johnathanlyu 0:e71874215e23 31 uint16_t sensServUUID = /*0xA000*/0x1811;
johnathanlyu 0:e71874215e23 32 uint16_t acceCharUUID = /*0xA001*/0x2A56;
johnathanlyu 0:e71874215e23 33 uint16_t gyroCharUUID = /*0xA002*/0x2A57;
johnathanlyu 0:e71874215e23 34 static const char DEVICE_NAME[] = "mbed Motion";
johnathanlyu 0:e71874215e23 35 static const uint16_t uuid16_list[] = { /*0xA000*/0x1811 };
johnathanlyu 0:e71874215e23 36
johnathanlyu 0:e71874215e23 37 uint8_t accePayload[7];
johnathanlyu 0:e71874215e23 38 uint8_t gyroPayload[7];
johnathanlyu 0:e71874215e23 39
johnathanlyu 0:e71874215e23 40 /* Setup custom characteristics */
johnathanlyu 0:e71874215e23 41 GattCharacteristic acceChar( acceCharUUID, accePayload,
johnathanlyu 0:e71874215e23 42 sizeof(accePayload), sizeof(accePayload),
johnathanlyu 0:e71874215e23 43 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY |
johnathanlyu 0:e71874215e23 44 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
johnathanlyu 0:e71874215e23 45
johnathanlyu 0:e71874215e23 46 GattCharacteristic gyroChar( gyroCharUUID, gyroPayload,
johnathanlyu 0:e71874215e23 47 sizeof(gyroPayload), sizeof(gyroPayload),
johnathanlyu 0:e71874215e23 48 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY |
johnathanlyu 0:e71874215e23 49 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
johnathanlyu 0:e71874215e23 50
johnathanlyu 0:e71874215e23 51 /* Setup custom service */
johnathanlyu 0:e71874215e23 52 GattCharacteristic *characteristics[] = {&acceChar, &gyroChar};
johnathanlyu 0:e71874215e23 53 GattService sensServ(sensServUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
johnathanlyu 0:e71874215e23 54
johnathanlyu 0:e71874215e23 55 static EventQueue eventQueue(
johnathanlyu 0:e71874215e23 56 /* event count */ 16 * /* event size */ 32
johnathanlyu 0:e71874215e23 57 );
johnathanlyu 0:e71874215e23 58
johnathanlyu 0:e71874215e23 59 void blinkCallback(void)
johnathanlyu 0:e71874215e23 60 {
johnathanlyu 0:e71874215e23 61 ledRed = !ledRed; /* Do blinky on LED1 while we're waiting for BLE events */
johnathanlyu 0:e71874215e23 62 }
johnathanlyu 0:e71874215e23 63
johnathanlyu 0:e71874215e23 64 /**
johnathanlyu 0:e71874215e23 65 * This function is called when the ble initialization process has failled
johnathanlyu 0:e71874215e23 66 */
johnathanlyu 0:e71874215e23 67 void onBleInitError(BLE &ble, ble_error_t error)
johnathanlyu 0:e71874215e23 68 {
johnathanlyu 0:e71874215e23 69 /* Initialization error handling should go here */
johnathanlyu 0:e71874215e23 70 }
johnathanlyu 0:e71874215e23 71
johnathanlyu 0:e71874215e23 72 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
johnathanlyu 0:e71874215e23 73 {
johnathanlyu 0:e71874215e23 74 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising
johnathanlyu 0:e71874215e23 75 }
johnathanlyu 0:e71874215e23 76
johnathanlyu 0:e71874215e23 77 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
johnathanlyu 0:e71874215e23 78 BLE &ble = BLE::Instance();
johnathanlyu 0:e71874215e23 79 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
johnathanlyu 0:e71874215e23 80 }
johnathanlyu 0:e71874215e23 81
johnathanlyu 0:e71874215e23 82 /**
johnathanlyu 0:e71874215e23 83 * Callback triggered when the ble initialization process has finished
johnathanlyu 0:e71874215e23 84 */
johnathanlyu 0:e71874215e23 85 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
johnathanlyu 0:e71874215e23 86 {
johnathanlyu 0:e71874215e23 87 BLE& ble = params->ble;
johnathanlyu 0:e71874215e23 88 ble_error_t error = params->error;
johnathanlyu 0:e71874215e23 89
johnathanlyu 0:e71874215e23 90 if (error != BLE_ERROR_NONE) {
johnathanlyu 0:e71874215e23 91 /* In case of error, forward the error handling to onBleInitError */
johnathanlyu 0:e71874215e23 92 onBleInitError(ble, error);
johnathanlyu 0:e71874215e23 93 return;
johnathanlyu 0:e71874215e23 94 }
johnathanlyu 0:e71874215e23 95
johnathanlyu 0:e71874215e23 96 /* Ensure that it is the default instance of BLE */
johnathanlyu 0:e71874215e23 97 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
johnathanlyu 0:e71874215e23 98 return;
johnathanlyu 0:e71874215e23 99 }
johnathanlyu 0:e71874215e23 100
johnathanlyu 0:e71874215e23 101 ble.gap().onDisconnection(disconnectionCallback);
johnathanlyu 0:e71874215e23 102
johnathanlyu 0:e71874215e23 103 /* Setup primary service. */
johnathanlyu 0:e71874215e23 104 ble.addService(sensServ);
johnathanlyu 0:e71874215e23 105
johnathanlyu 0:e71874215e23 106 /* Setup advertising. */
johnathanlyu 0:e71874215e23 107 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
johnathanlyu 0:e71874215e23 108 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
johnathanlyu 0:e71874215e23 109 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_TAG);
johnathanlyu 0:e71874215e23 110 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
johnathanlyu 0:e71874215e23 111 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
johnathanlyu 0:e71874215e23 112 ble.gap().setAdvertisingInterval(500); /* 500ms */
johnathanlyu 0:e71874215e23 113 ble.gap().startAdvertising();
johnathanlyu 0:e71874215e23 114
johnathanlyu 0:e71874215e23 115 }
johnathanlyu 0:e71874215e23 116
johnathanlyu 0:e71874215e23 117 void BMA250Ecallback(void) {
johnathanlyu 0:e71874215e23 118 int16_t xyz[3];
johnathanlyu 0:e71874215e23 119
johnathanlyu 0:e71874215e23 120 acclerameter.ReadXYZ(xyz);
johnathanlyu 0:e71874215e23 121 pc.printf("%d %d %d \r\n", xyz[0], xyz[1], xyz[2]);
johnathanlyu 0:e71874215e23 122 }
johnathanlyu 0:e71874215e23 123
johnathanlyu 0:e71874215e23 124
johnathanlyu 0:e71874215e23 125
johnathanlyu 0:e71874215e23 126 int main()
johnathanlyu 0:e71874215e23 127 {
johnathanlyu 0:e71874215e23 128 pc.printf("Hi \r\n");
johnathanlyu 0:e71874215e23 129
johnathanlyu 0:e71874215e23 130 eventQueue.call_every(200, blinkCallback);
johnathanlyu 0:e71874215e23 131 eventQueue.call_every(1000, BMA250Ecallback);
johnathanlyu 0:e71874215e23 132
johnathanlyu 0:e71874215e23 133 BLE &ble = BLE::Instance();
johnathanlyu 0:e71874215e23 134 ble.onEventsToProcess(scheduleBleEventsProcessing);
johnathanlyu 0:e71874215e23 135 ble.init(bleInitComplete);
johnathanlyu 0:e71874215e23 136
johnathanlyu 0:e71874215e23 137 eventQueue.dispatch_forever();
johnathanlyu 0:e71874215e23 138
johnathanlyu 0:e71874215e23 139 return 0;
johnathanlyu 0:e71874215e23 140 }