MtM+
/
Mt05_MtSense03
51 52 with same code
source/main.cpp@1:b8bbe971b26d, 2018-04-27 (annotated)
- Committer:
- johnathanlyu
- Date:
- Fri Apr 27 09:56:41 2018 +0000
- Revision:
- 1:b8bbe971b26d
- Parent:
- 0:e71874215e23
51 52 with same code
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:b8bbe971b26d | 22 | #include "BMG160.h" |
johnathanlyu | 1:b8bbe971b26d | 23 | |
johnathanlyu | 1:b8bbe971b26d | 24 | |
johnathanlyu | 0:e71874215e23 | 25 | /* UART printf */ |
johnathanlyu | 1:b8bbe971b26d | 26 | #ifdef NRF52 |
johnathanlyu | 1:b8bbe971b26d | 27 | Serial pc(p20, p24); |
johnathanlyu | 1:b8bbe971b26d | 28 | #else |
johnathanlyu | 0:e71874215e23 | 29 | Serial pc(p5, p4); |
johnathanlyu | 1:b8bbe971b26d | 30 | #endif |
johnathanlyu | 1:b8bbe971b26d | 31 | |
johnathanlyu | 1:b8bbe971b26d | 32 | |
johnathanlyu | 1:b8bbe971b26d | 33 | /* LED blink */ |
johnathanlyu | 1:b8bbe971b26d | 34 | Ticker ledBlinkTicker; |
johnathanlyu | 1:b8bbe971b26d | 35 | DigitalOut ledR(p16, 1); |
johnathanlyu | 1:b8bbe971b26d | 36 | DigitalOut ledG(p15, 1); |
johnathanlyu | 1:b8bbe971b26d | 37 | DigitalOut ledB(p6 , 1); |
johnathanlyu | 0:e71874215e23 | 38 | |
johnathanlyu | 0:e71874215e23 | 39 | /* Sensor */ |
johnathanlyu | 1:b8bbe971b26d | 40 | uint8_t acceRange = 0x0C; // 2G(0x03), 4G(0x05), 8G(0x08), 16G(0x0C) |
johnathanlyu | 1:b8bbe971b26d | 41 | uint8_t gyroRange = 0x00; // 2000deg/s(0x00), 1000deg/s(0x01), 500deg/s(0x02), 250deg/s(0x03), 125deg/s(0x04) |
johnathanlyu | 1:b8bbe971b26d | 42 | int16_t acceXYZ[3]; |
johnathanlyu | 1:b8bbe971b26d | 43 | int16_t gyroXYZ[3]; |
johnathanlyu | 1:b8bbe971b26d | 44 | uint8_t accePayload[7]; |
johnathanlyu | 1:b8bbe971b26d | 45 | uint8_t gyroPayload[7]; |
johnathanlyu | 0:e71874215e23 | 46 | |
johnathanlyu | 1:b8bbe971b26d | 47 | /* UART printf */ |
johnathanlyu | 1:b8bbe971b26d | 48 | #ifdef NRF52 |
johnathanlyu | 1:b8bbe971b26d | 49 | BMA250E acclerameter(p14, p28, NC, NC, acceRange, 0x0D); |
johnathanlyu | 1:b8bbe971b26d | 50 | BMG160 gyro(p14, p28, NC, NC, gyroRange, 0x00); |
johnathanlyu | 1:b8bbe971b26d | 51 | #else |
johnathanlyu | 1:b8bbe971b26d | 52 | BMA250E acclerameter(p14, p13, NC, NC, acceRange, 0x0D); |
johnathanlyu | 1:b8bbe971b26d | 53 | BMG160 gyro(p14, p13, NC, NC, gyroRange, 0x00); |
johnathanlyu | 1:b8bbe971b26d | 54 | #endif |
johnathanlyu | 1:b8bbe971b26d | 55 | |
johnathanlyu | 1:b8bbe971b26d | 56 | static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE); |
johnathanlyu | 0:e71874215e23 | 57 | |
johnathanlyu | 0:e71874215e23 | 58 | /* UUID, Device name */ |
johnathanlyu | 0:e71874215e23 | 59 | uint16_t sensServUUID = /*0xA000*/0x1811; |
johnathanlyu | 0:e71874215e23 | 60 | uint16_t acceCharUUID = /*0xA001*/0x2A56; |
johnathanlyu | 0:e71874215e23 | 61 | uint16_t gyroCharUUID = /*0xA002*/0x2A57; |
johnathanlyu | 1:b8bbe971b26d | 62 | static const char DEVICE_NAME[] = "Mt5MtSense03"; |
johnathanlyu | 0:e71874215e23 | 63 | static const uint16_t uuid16_list[] = { /*0xA000*/0x1811 }; |
johnathanlyu | 0:e71874215e23 | 64 | |
johnathanlyu | 0:e71874215e23 | 65 | /* Setup custom characteristics */ |
johnathanlyu | 0:e71874215e23 | 66 | GattCharacteristic acceChar( acceCharUUID, accePayload, |
johnathanlyu | 0:e71874215e23 | 67 | sizeof(accePayload), sizeof(accePayload), |
johnathanlyu | 0:e71874215e23 | 68 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | |
johnathanlyu | 0:e71874215e23 | 69 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
johnathanlyu | 0:e71874215e23 | 70 | |
johnathanlyu | 0:e71874215e23 | 71 | GattCharacteristic gyroChar( gyroCharUUID, gyroPayload, |
johnathanlyu | 0:e71874215e23 | 72 | sizeof(gyroPayload), sizeof(gyroPayload), |
johnathanlyu | 0:e71874215e23 | 73 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | |
johnathanlyu | 0:e71874215e23 | 74 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
johnathanlyu | 0:e71874215e23 | 75 | |
johnathanlyu | 0:e71874215e23 | 76 | /* Setup custom service */ |
johnathanlyu | 0:e71874215e23 | 77 | GattCharacteristic *characteristics[] = {&acceChar, &gyroChar}; |
johnathanlyu | 0:e71874215e23 | 78 | GattService sensServ(sensServUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); |
johnathanlyu | 0:e71874215e23 | 79 | |
johnathanlyu | 1:b8bbe971b26d | 80 | |
johnathanlyu | 0:e71874215e23 | 81 | |
johnathanlyu | 1:b8bbe971b26d | 82 | void ledBlinkCallback(void) |
johnathanlyu | 0:e71874215e23 | 83 | { |
johnathanlyu | 1:b8bbe971b26d | 84 | ledR = !ledR; |
johnathanlyu | 1:b8bbe971b26d | 85 | ledG = !ledG; |
johnathanlyu | 1:b8bbe971b26d | 86 | ledB = !ledB; |
johnathanlyu | 0:e71874215e23 | 87 | } |
johnathanlyu | 0:e71874215e23 | 88 | |
johnathanlyu | 1:b8bbe971b26d | 89 | void updateSensorCallback(void) |
johnathanlyu | 0:e71874215e23 | 90 | { |
johnathanlyu | 1:b8bbe971b26d | 91 | /* Get sensor data */ |
johnathanlyu | 1:b8bbe971b26d | 92 | acclerameter.ReadXYZ(acceXYZ); |
johnathanlyu | 1:b8bbe971b26d | 93 | gyro.ReadXYZ(gyroXYZ); |
johnathanlyu | 1:b8bbe971b26d | 94 | // pc.printf("aXYZ(%6d,%6d,%6d), gXYZ(%6d,%6d,%6d)\r\n", acceXYZ[0], acceXYZ[1], acceXYZ[2], gyroXYZ[0], gyroXYZ[1], gyroXYZ[2]); |
johnathanlyu | 1:b8bbe971b26d | 95 | BLE &ble = BLE::Instance(); |
johnathanlyu | 1:b8bbe971b26d | 96 | if (ble.getGapState().connected) { |
johnathanlyu | 1:b8bbe971b26d | 97 | /* Write data to client */ |
johnathanlyu | 1:b8bbe971b26d | 98 | accePayload[0] = acceRange; |
johnathanlyu | 1:b8bbe971b26d | 99 | accePayload[1] = (uint8_t)(acceXYZ[0] >> 8); |
johnathanlyu | 1:b8bbe971b26d | 100 | accePayload[2] = (uint8_t)(acceXYZ[0] >> 0); |
johnathanlyu | 1:b8bbe971b26d | 101 | accePayload[3] = (uint8_t)(acceXYZ[1] >> 8); |
johnathanlyu | 1:b8bbe971b26d | 102 | accePayload[4] = (uint8_t)(acceXYZ[1] >> 0); |
johnathanlyu | 1:b8bbe971b26d | 103 | accePayload[5] = (uint8_t)(acceXYZ[2] >> 8); |
johnathanlyu | 1:b8bbe971b26d | 104 | accePayload[6] = (uint8_t)(acceXYZ[2] >> 0); |
johnathanlyu | 1:b8bbe971b26d | 105 | ble.gattServer().write(acceChar.getValueHandle(), accePayload, sizeof(accePayload)); |
johnathanlyu | 1:b8bbe971b26d | 106 | gyroPayload[0] = gyroRange; |
johnathanlyu | 1:b8bbe971b26d | 107 | gyroPayload[1] = (uint8_t)(gyroXYZ[0] >> 8); |
johnathanlyu | 1:b8bbe971b26d | 108 | gyroPayload[2] = (uint8_t)(gyroXYZ[0] >> 0); |
johnathanlyu | 1:b8bbe971b26d | 109 | gyroPayload[3] = (uint8_t)(gyroXYZ[1] >> 8); |
johnathanlyu | 1:b8bbe971b26d | 110 | gyroPayload[4] = (uint8_t)(gyroXYZ[1] >> 0); |
johnathanlyu | 1:b8bbe971b26d | 111 | gyroPayload[5] = (uint8_t)(gyroXYZ[2] >> 8); |
johnathanlyu | 1:b8bbe971b26d | 112 | gyroPayload[6] = (uint8_t)(gyroXYZ[2] >> 0); |
johnathanlyu | 1:b8bbe971b26d | 113 | ble.gattServer().write(gyroChar.getValueHandle(), gyroPayload, sizeof(gyroPayload)); |
johnathanlyu | 1:b8bbe971b26d | 114 | } |
johnathanlyu | 0:e71874215e23 | 115 | } |
johnathanlyu | 0:e71874215e23 | 116 | |
johnathanlyu | 0:e71874215e23 | 117 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
johnathanlyu | 0:e71874215e23 | 118 | { |
johnathanlyu | 0:e71874215e23 | 119 | BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising |
johnathanlyu | 0:e71874215e23 | 120 | } |
johnathanlyu | 0:e71874215e23 | 121 | |
johnathanlyu | 0:e71874215e23 | 122 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
johnathanlyu | 0:e71874215e23 | 123 | { |
johnathanlyu | 1:b8bbe971b26d | 124 | BLE &ble = params->ble; |
johnathanlyu | 0:e71874215e23 | 125 | ble_error_t error = params->error; |
johnathanlyu | 0:e71874215e23 | 126 | |
johnathanlyu | 0:e71874215e23 | 127 | if (error != BLE_ERROR_NONE) { |
johnathanlyu | 0:e71874215e23 | 128 | return; |
johnathanlyu | 0:e71874215e23 | 129 | } |
johnathanlyu | 0:e71874215e23 | 130 | |
johnathanlyu | 0:e71874215e23 | 131 | ble.gap().onDisconnection(disconnectionCallback); |
johnathanlyu | 0:e71874215e23 | 132 | |
johnathanlyu | 0:e71874215e23 | 133 | /* Setup primary service. */ |
johnathanlyu | 0:e71874215e23 | 134 | ble.addService(sensServ); |
johnathanlyu | 0:e71874215e23 | 135 | |
johnathanlyu | 0:e71874215e23 | 136 | /* Setup advertising. */ |
johnathanlyu | 0:e71874215e23 | 137 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
johnathanlyu | 0:e71874215e23 | 138 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
johnathanlyu | 0:e71874215e23 | 139 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_TAG); |
johnathanlyu | 0:e71874215e23 | 140 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
johnathanlyu | 0:e71874215e23 | 141 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
johnathanlyu | 0:e71874215e23 | 142 | ble.gap().setAdvertisingInterval(500); /* 500ms */ |
johnathanlyu | 0:e71874215e23 | 143 | ble.gap().startAdvertising(); |
johnathanlyu | 1:b8bbe971b26d | 144 | } |
johnathanlyu | 1:b8bbe971b26d | 145 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
johnathanlyu | 1:b8bbe971b26d | 146 | BLE &ble = BLE::Instance(); |
johnathanlyu | 1:b8bbe971b26d | 147 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
johnathanlyu | 0:e71874215e23 | 148 | } |
johnathanlyu | 1:b8bbe971b26d | 149 | int main(void) |
johnathanlyu | 1:b8bbe971b26d | 150 | { |
johnathanlyu | 1:b8bbe971b26d | 151 | pc.set_flow_control(SerialBase::Disabled); |
johnathanlyu | 1:b8bbe971b26d | 152 | pc.baud(115200); |
johnathanlyu | 1:b8bbe971b26d | 153 | pc.printf("~ Hell World ~\n"); |
johnathanlyu | 0:e71874215e23 | 154 | |
johnathanlyu | 1:b8bbe971b26d | 155 | /* LED blink timer */ |
johnathanlyu | 1:b8bbe971b26d | 156 | eventQueue.call_every(1000, ledBlinkCallback); |
johnathanlyu | 0:e71874215e23 | 157 | |
johnathanlyu | 1:b8bbe971b26d | 158 | /* Update Sensor timer */ |
johnathanlyu | 1:b8bbe971b26d | 159 | eventQueue.call_every(200, updateSensorCallback); |
johnathanlyu | 0:e71874215e23 | 160 | |
johnathanlyu | 1:b8bbe971b26d | 161 | /* Init BLE */ |
johnathanlyu | 0:e71874215e23 | 162 | BLE &ble = BLE::Instance(); |
johnathanlyu | 0:e71874215e23 | 163 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
johnathanlyu | 0:e71874215e23 | 164 | ble.init(bleInitComplete); |
johnathanlyu | 0:e71874215e23 | 165 | |
johnathanlyu | 0:e71874215e23 | 166 | eventQueue.dispatch_forever(); |
johnathanlyu | 0:e71874215e23 | 167 | |
johnathanlyu | 0:e71874215e23 | 168 | return 0; |
johnathanlyu | 0:e71874215e23 | 169 | } |