Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2014 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 00017 #include <events/mbed_events.h> 00018 #include <mbed.h> 00019 #include "ble/BLE.h" 00020 #include "ble/Gap.h" 00021 #include "BMA250E.h" 00022 #include "BMG160.h" 00023 00024 00025 /* UART printf */ 00026 #ifdef NRF52 00027 Serial pc(p20, p24); 00028 #else 00029 Serial pc(p5, p4); 00030 #endif 00031 00032 00033 /* LED blink */ 00034 Ticker ledBlinkTicker; 00035 DigitalOut ledR(p16, 1); 00036 DigitalOut ledG(p15, 1); 00037 DigitalOut ledB(p6 , 1); 00038 00039 /* Sensor */ 00040 uint8_t acceRange = 0x0C; // 2G(0x03), 4G(0x05), 8G(0x08), 16G(0x0C) 00041 uint8_t gyroRange = 0x00; // 2000deg/s(0x00), 1000deg/s(0x01), 500deg/s(0x02), 250deg/s(0x03), 125deg/s(0x04) 00042 int16_t acceXYZ[3]; 00043 int16_t gyroXYZ[3]; 00044 uint8_t accePayload[7]; 00045 uint8_t gyroPayload[7]; 00046 00047 /* UART printf */ 00048 #ifdef NRF52 00049 BMA250E acclerameter(p14, p28, NC, NC, acceRange, 0x0D); 00050 BMG160 gyro(p14, p28, NC, NC, gyroRange, 0x00); 00051 #else 00052 BMA250E acclerameter(p14, p13, NC, NC, acceRange, 0x0D); 00053 BMG160 gyro(p14, p13, NC, NC, gyroRange, 0x00); 00054 #endif 00055 00056 static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE); 00057 00058 /* UUID, Device name */ 00059 uint16_t sensServUUID = /*0xA000*/0x1811; 00060 uint16_t acceCharUUID = /*0xA001*/0x2A56; 00061 uint16_t gyroCharUUID = /*0xA002*/0x2A57; 00062 static const char DEVICE_NAME[] = "Mt5MtSense03"; 00063 static const uint16_t uuid16_list[] = { /*0xA000*/0x1811 }; 00064 00065 /* Setup custom characteristics */ 00066 GattCharacteristic acceChar( acceCharUUID, accePayload, 00067 sizeof(accePayload), sizeof(accePayload), 00068 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | 00069 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); 00070 00071 GattCharacteristic gyroChar( gyroCharUUID, gyroPayload, 00072 sizeof(gyroPayload), sizeof(gyroPayload), 00073 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | 00074 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); 00075 00076 /* Setup custom service */ 00077 GattCharacteristic *characteristics[] = {&acceChar, &gyroChar}; 00078 GattService sensServ(sensServUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); 00079 00080 00081 00082 void ledBlinkCallback(void) 00083 { 00084 ledR = !ledR; 00085 ledG = !ledG; 00086 ledB = !ledB; 00087 } 00088 00089 void updateSensorCallback(void) 00090 { 00091 /* Get sensor data */ 00092 acclerameter.ReadXYZ(acceXYZ); 00093 gyro.ReadXYZ(gyroXYZ); 00094 // pc.printf("aXYZ(%6d,%6d,%6d), gXYZ(%6d,%6d,%6d)\r\n", acceXYZ[0], acceXYZ[1], acceXYZ[2], gyroXYZ[0], gyroXYZ[1], gyroXYZ[2]); 00095 BLE &ble = BLE::Instance(); 00096 if (ble.getGapState().connected) { 00097 /* Write data to client */ 00098 accePayload[0] = acceRange; 00099 accePayload[1] = (uint8_t)(acceXYZ[0] >> 8); 00100 accePayload[2] = (uint8_t)(acceXYZ[0] >> 0); 00101 accePayload[3] = (uint8_t)(acceXYZ[1] >> 8); 00102 accePayload[4] = (uint8_t)(acceXYZ[1] >> 0); 00103 accePayload[5] = (uint8_t)(acceXYZ[2] >> 8); 00104 accePayload[6] = (uint8_t)(acceXYZ[2] >> 0); 00105 ble.gattServer().write(acceChar.getValueHandle(), accePayload, sizeof(accePayload)); 00106 gyroPayload[0] = gyroRange; 00107 gyroPayload[1] = (uint8_t)(gyroXYZ[0] >> 8); 00108 gyroPayload[2] = (uint8_t)(gyroXYZ[0] >> 0); 00109 gyroPayload[3] = (uint8_t)(gyroXYZ[1] >> 8); 00110 gyroPayload[4] = (uint8_t)(gyroXYZ[1] >> 0); 00111 gyroPayload[5] = (uint8_t)(gyroXYZ[2] >> 8); 00112 gyroPayload[6] = (uint8_t)(gyroXYZ[2] >> 0); 00113 ble.gattServer().write(gyroChar.getValueHandle(), gyroPayload, sizeof(gyroPayload)); 00114 } 00115 } 00116 00117 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00118 { 00119 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising 00120 } 00121 00122 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00123 { 00124 BLE &ble = params->ble; 00125 ble_error_t error = params->error; 00126 00127 if (error != BLE_ERROR_NONE) { 00128 return; 00129 } 00130 00131 ble.gap().onDisconnection(disconnectionCallback); 00132 00133 /* Setup primary service. */ 00134 ble.addService(sensServ); 00135 00136 /* Setup advertising. */ 00137 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00138 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); 00139 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_TAG); 00140 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00141 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00142 ble.gap().setAdvertisingInterval(500); /* 500ms */ 00143 ble.gap().startAdvertising(); 00144 } 00145 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { 00146 BLE &ble = BLE::Instance(); 00147 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); 00148 } 00149 int main(void) 00150 { 00151 pc.set_flow_control(SerialBase::Disabled); 00152 pc.baud(115200); 00153 pc.printf("~ Hell World ~\n"); 00154 00155 /* LED blink timer */ 00156 eventQueue.call_every(1000, ledBlinkCallback); 00157 00158 /* Update Sensor timer */ 00159 eventQueue.call_every(200, updateSensorCallback); 00160 00161 /* Init BLE */ 00162 BLE &ble = BLE::Instance(); 00163 ble.onEventsToProcess(scheduleBleEventsProcessing); 00164 ble.init(bleInitComplete); 00165 00166 eventQueue.dispatch_forever(); 00167 00168 return 0; 00169 }
Generated on Tue Jul 19 2022 15:01:38 by
1.7.2