Petr Manas / Mbed OS pdiot-f-mbed

Dependencies:   MPU9250

Committer:
fasand
Date:
Wed Oct 23 12:17:05 2019 +0000
Revision:
2:6d171062f1e2
Parent:
1:eb38d5821d79
Child:
3:7ad91f59dcfa
add MPU9250 library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fasand 1:eb38d5821d79 1 #include "mbed.h"
fasand 1:eb38d5821d79 2 #include "ble/BLE.h"
fasand 2:6d171062f1e2 3 #include "MPU9250.h"
fasand 1:eb38d5821d79 4
fasand 1:eb38d5821d79 5 DigitalOut led(LED1, 1);
fasand 1:eb38d5821d79 6 uint16_t customServiceUUID = 0xA000;
fasand 1:eb38d5821d79 7 uint16_t readCharUUID = 0xA001;
fasand 1:eb38d5821d79 8 uint16_t writeCharUUID = 0xA002;
fasand 1:eb38d5821d79 9
fasand 1:eb38d5821d79 10 const static char DEVICE_NAME[] = "ChangeMe!!"; // change this
fasand 1:eb38d5821d79 11 static const uint16_t uuid16_list[] = {0xFFFF}; //Custom UUID, FFFF is reserved for development
fasand 1:eb38d5821d79 12
fasand 1:eb38d5821d79 13 /* Set Up custom Characteristics */
fasand 1:eb38d5821d79 14 static uint8_t readValue[10] = {0};
fasand 1:eb38d5821d79 15 ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(readCharUUID, readValue);
fasand 0:68da41d2fe5c 16
fasand 1:eb38d5821d79 17 static uint8_t writeValue[10] = {0};
fasand 1:eb38d5821d79 18 WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue);
fasand 1:eb38d5821d79 19
fasand 1:eb38d5821d79 20 /* Set up custom service */
fasand 1:eb38d5821d79 21 GattCharacteristic *characteristics[] = {&readChar, &writeChar};
fasand 1:eb38d5821d79 22 GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
fasand 1:eb38d5821d79 23
fasand 0:68da41d2fe5c 24
fasand 1:eb38d5821d79 25 /*
fasand 1:eb38d5821d79 26 * Restart advertising when phone app disconnects
fasand 1:eb38d5821d79 27 */
fasand 1:eb38d5821d79 28 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
fasand 1:eb38d5821d79 29 {
fasand 1:eb38d5821d79 30 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising();
fasand 1:eb38d5821d79 31 }
fasand 0:68da41d2fe5c 32
fasand 1:eb38d5821d79 33 /*
fasand 1:eb38d5821d79 34 * Handle writes to writeCharacteristic
fasand 1:eb38d5821d79 35 */
fasand 1:eb38d5821d79 36 void writeCharCallback(const GattWriteCallbackParams *params)
fasand 1:eb38d5821d79 37 {
fasand 1:eb38d5821d79 38 /* Check to see what characteristic was written, by handle */
fasand 1:eb38d5821d79 39 if(params->handle == writeChar.getValueHandle()) {
fasand 1:eb38d5821d79 40 /* toggle LED if only 1 byte is written */
fasand 1:eb38d5821d79 41 if(params->len == 1) {
fasand 1:eb38d5821d79 42 led = params->data[0];
fasand 1:eb38d5821d79 43 (params->data[0] == 0x00) ? printf("led on\n\r") : printf("led off\n\r"); // print led toggle
fasand 1:eb38d5821d79 44 }
fasand 1:eb38d5821d79 45 /* Print the data if more than 1 byte is written */
fasand 1:eb38d5821d79 46 else {
fasand 1:eb38d5821d79 47 printf("Data received: length = %d, data = 0x",params->len);
fasand 1:eb38d5821d79 48 for(int x=0; x < params->len; x++) {
fasand 1:eb38d5821d79 49 printf("%x", params->data[x]);
fasand 1:eb38d5821d79 50 }
fasand 1:eb38d5821d79 51 printf("\n\r");
fasand 1:eb38d5821d79 52 }
fasand 1:eb38d5821d79 53 /* Update the readChar with the value of writeChar */
fasand 1:eb38d5821d79 54 BLE::Instance(BLE::DEFAULT_INSTANCE).gattServer().write(readChar.getValueHandle(), params->data, params->len);
fasand 1:eb38d5821d79 55 }
fasand 1:eb38d5821d79 56 }
fasand 1:eb38d5821d79 57 /*
fasand 1:eb38d5821d79 58 * Initialization callback
fasand 1:eb38d5821d79 59 */
fasand 1:eb38d5821d79 60 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
fasand 1:eb38d5821d79 61 {
fasand 1:eb38d5821d79 62 BLE &ble = params->ble;
fasand 1:eb38d5821d79 63 ble_error_t error = params->error;
fasand 0:68da41d2fe5c 64
fasand 1:eb38d5821d79 65 if (error != BLE_ERROR_NONE) {
fasand 1:eb38d5821d79 66 return;
fasand 0:68da41d2fe5c 67 }
fasand 0:68da41d2fe5c 68
fasand 1:eb38d5821d79 69 ble.gap().onDisconnection(disconnectionCallback);
fasand 1:eb38d5821d79 70 ble.gattServer().onDataWritten(writeCharCallback);
fasand 0:68da41d2fe5c 71
fasand 1:eb38d5821d79 72 /* Setup advertising */
fasand 1:eb38d5821d79 73 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT
fasand 1:eb38d5821d79 74 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type
fasand 1:eb38d5821d79 75 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name
fasand 1:eb38d5821d79 76 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet
fasand 1:eb38d5821d79 77 ble.gap().setAdvertisingInterval(100); // 100ms.
fasand 0:68da41d2fe5c 78
fasand 1:eb38d5821d79 79 /* Add our custom service */
fasand 1:eb38d5821d79 80 ble.addService(customService);
fasand 0:68da41d2fe5c 81
fasand 1:eb38d5821d79 82 /* Start advertising */
fasand 1:eb38d5821d79 83 ble.gap().startAdvertising();
fasand 0:68da41d2fe5c 84 }
fasand 0:68da41d2fe5c 85
fasand 1:eb38d5821d79 86 /*
fasand 1:eb38d5821d79 87 * Main loop
fasand 1:eb38d5821d79 88 */
fasand 1:eb38d5821d79 89 int main(void)
fasand 0:68da41d2fe5c 90 {
fasand 1:eb38d5821d79 91 /* initialize stuff */
fasand 1:eb38d5821d79 92 printf("\n\r********* Starting Main Loop *********\n\r");
fasand 0:68da41d2fe5c 93
fasand 1:eb38d5821d79 94 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
fasand 1:eb38d5821d79 95 ble.init(bleInitComplete);
fasand 0:68da41d2fe5c 96
fasand 1:eb38d5821d79 97 /* SpinWait for initialization to complete. This is necessary because the
fasand 1:eb38d5821d79 98 * BLE object is used in the main loop below. */
fasand 1:eb38d5821d79 99 while (ble.hasInitialized() == false) { /* spin loop */ }
fasand 1:eb38d5821d79 100
fasand 1:eb38d5821d79 101 /* Infinite loop waiting for BLE interrupt events */
fasand 1:eb38d5821d79 102 while (true) {
fasand 1:eb38d5821d79 103 ble.waitForEvent(); /* Save power */
fasand 1:eb38d5821d79 104 }
fasand 1:eb38d5821d79 105 }