Access different power mode of BNO055 via nRF51-DK BLE

Dependencies:   BLE_API BNO055 mbed nRF51822 nrf51_rtc

Fork of BLE_LoopbackUART_with_LSM6DS3 by Sherry Yang

Committer:
5hel2l2y
Date:
Mon Jun 20 20:10:37 2016 +0000
Revision:
2:514af7294ad0
Parent:
1:7562cc147e26
Child:
3:c0d70871aa4d
Switch BNO055 power mode via nRF51-DK BLE

Who changed what in which revision?

UserRevisionLine numberNew contents of line
5hel2l2y 0:d0291b4a856a 1 /* mbed Microcontroller Library
5hel2l2y 0:d0291b4a856a 2 * Copyright (c) 2006-2013 ARM Limited
5hel2l2y 0:d0291b4a856a 3 *
5hel2l2y 0:d0291b4a856a 4 * Licensed under the Apache License, Version 2.0 (the "License");
5hel2l2y 0:d0291b4a856a 5 * you may not use this file except in compliance with the License.
5hel2l2y 0:d0291b4a856a 6 * You may obtain a copy of the License at
5hel2l2y 0:d0291b4a856a 7 *
5hel2l2y 0:d0291b4a856a 8 * http://www.apache.org/licenses/LICENSE-2.0
5hel2l2y 0:d0291b4a856a 9 *
5hel2l2y 0:d0291b4a856a 10 * Unless required by applicable law or agreed to in writing, software
5hel2l2y 0:d0291b4a856a 11 * distributed under the License is distributed on an "AS IS" BASIS,
5hel2l2y 0:d0291b4a856a 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5hel2l2y 0:d0291b4a856a 13 * See the License for the specific language governing permissions and
5hel2l2y 0:d0291b4a856a 14 * limitations under the License.
5hel2l2y 0:d0291b4a856a 15 */
5hel2l2y 0:d0291b4a856a 16
5hel2l2y 0:d0291b4a856a 17 #include "mbed.h"
5hel2l2y 0:d0291b4a856a 18 #include "ble/BLE.h"
5hel2l2y 0:d0291b4a856a 19
5hel2l2y 0:d0291b4a856a 20 #include "ble/services/UARTService.h"
5hel2l2y 2:514af7294ad0 21 #include "BNO055/BNO055.h"
5hel2l2y 0:d0291b4a856a 22
5hel2l2y 2:514af7294ad0 23 BNO055 imu(p30, p7);
5hel2l2y 0:d0291b4a856a 24 Serial pc(p9, p11);
5hel2l2y 0:d0291b4a856a 25
5hel2l2y 0:d0291b4a856a 26 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
5hel2l2y 0:d0291b4a856a 27 * it will have an impact on code-size and power consumption. */
5hel2l2y 0:d0291b4a856a 28
5hel2l2y 0:d0291b4a856a 29 #if NEED_CONSOLE_OUTPUT
5hel2l2y 0:d0291b4a856a 30 #define DEBUG(...) { pc.printf(__VA_ARGS__); }
5hel2l2y 0:d0291b4a856a 31 #else
5hel2l2y 0:d0291b4a856a 32 #define DEBUG(...) /* nothing */
5hel2l2y 0:d0291b4a856a 33 #endif /* #if NEED_CONSOLE_OUTPUT */
5hel2l2y 0:d0291b4a856a 34
5hel2l2y 0:d0291b4a856a 35 BLEDevice ble;
5hel2l2y 0:d0291b4a856a 36 DigitalOut led1(LED1);
5hel2l2y 0:d0291b4a856a 37
5hel2l2y 0:d0291b4a856a 38 UARTService *uartServicePtr;
5hel2l2y 0:d0291b4a856a 39
5hel2l2y 0:d0291b4a856a 40 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
5hel2l2y 0:d0291b4a856a 41 {
5hel2l2y 0:d0291b4a856a 42 DEBUG("Disconnected!\n\r");
5hel2l2y 0:d0291b4a856a 43 DEBUG("Restarting the advertising process\n\r");
5hel2l2y 0:d0291b4a856a 44 ble.startAdvertising();
5hel2l2y 0:d0291b4a856a 45 }
5hel2l2y 0:d0291b4a856a 46
5hel2l2y 0:d0291b4a856a 47 void onDataWritten(const GattWriteCallbackParams *params)
5hel2l2y 0:d0291b4a856a 48 {
5hel2l2y 0:d0291b4a856a 49 if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
5hel2l2y 0:d0291b4a856a 50 uint16_t bytesRead = params->len;
5hel2l2y 0:d0291b4a856a 51 uint8_t value = *(params->data);
5hel2l2y 0:d0291b4a856a 52 DEBUG("received %u bytes\n\r", bytesRead);
5hel2l2y 0:d0291b4a856a 53 DEBUG("recevied %d data\n\r", value);
5hel2l2y 0:d0291b4a856a 54
5hel2l2y 2:514af7294ad0 55 // Set power mode, POWER_MODE_NORMAL(default), POWER_MODE_LOWPOWER, or POWER_MODE_SUSPEND(sleep)
5hel2l2y 2:514af7294ad0 56 // By default, I2C will pull high when bus is free(pg90 of datasheet)
5hel2l2y 0:d0291b4a856a 57 switch(value) {
5hel2l2y 0:d0291b4a856a 58 case 49:
5hel2l2y 0:d0291b4a856a 59 DEBUG("Off power\n\r");
5hel2l2y 2:514af7294ad0 60 imu.setpowermode(POWER_MODE_SUSPEND);
5hel2l2y 0:d0291b4a856a 61 break;
5hel2l2y 0:d0291b4a856a 62 case 50:
5hel2l2y 0:d0291b4a856a 63 DEBUG("Low power\n\r");
5hel2l2y 2:514af7294ad0 64 imu.setpowermode(POWER_MODE_LOWPOWER);
5hel2l2y 0:d0291b4a856a 65 break;
5hel2l2y 0:d0291b4a856a 66 case 51:
5hel2l2y 0:d0291b4a856a 67 DEBUG("High power\n\r");
5hel2l2y 2:514af7294ad0 68 imu.setpowermode(POWER_MODE_NORMAL);
5hel2l2y 0:d0291b4a856a 69 break;
5hel2l2y 0:d0291b4a856a 70 default:
5hel2l2y 0:d0291b4a856a 71 DEBUG("Nothing happened\n\r");
5hel2l2y 0:d0291b4a856a 72 break;
5hel2l2y 0:d0291b4a856a 73 }
5hel2l2y 0:d0291b4a856a 74
5hel2l2y 2:514af7294ad0 75 if (imu.check())
5hel2l2y 2:514af7294ad0 76 {
5hel2l2y 2:514af7294ad0 77 pc.printf("BNO055 found\r\n");
5hel2l2y 2:514af7294ad0 78 }
5hel2l2y 0:d0291b4a856a 79
5hel2l2y 0:d0291b4a856a 80 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
5hel2l2y 0:d0291b4a856a 81 }
5hel2l2y 0:d0291b4a856a 82 }
5hel2l2y 0:d0291b4a856a 83
5hel2l2y 0:d0291b4a856a 84 void periodicCallback(void)
5hel2l2y 0:d0291b4a856a 85 {
5hel2l2y 0:d0291b4a856a 86 led1 = !led1;
5hel2l2y 0:d0291b4a856a 87 }
5hel2l2y 0:d0291b4a856a 88
5hel2l2y 0:d0291b4a856a 89 int main(void)
5hel2l2y 0:d0291b4a856a 90 {
5hel2l2y 0:d0291b4a856a 91 led1 = 1;
5hel2l2y 0:d0291b4a856a 92 Ticker ticker;
5hel2l2y 0:d0291b4a856a 93 ticker.attach(periodicCallback, 1);
5hel2l2y 0:d0291b4a856a 94
5hel2l2y 0:d0291b4a856a 95 DEBUG("Initialising the nRF51822\n\r");
5hel2l2y 0:d0291b4a856a 96 pc.printf("PC: Initialising the nRF51822\n\r");
5hel2l2y 0:d0291b4a856a 97 ble.init();
5hel2l2y 0:d0291b4a856a 98 ble.onDisconnection(disconnectionCallback);
5hel2l2y 0:d0291b4a856a 99 ble.onDataWritten(onDataWritten);
5hel2l2y 0:d0291b4a856a 100
5hel2l2y 0:d0291b4a856a 101 /* setup advertising */
5hel2l2y 0:d0291b4a856a 102 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
5hel2l2y 0:d0291b4a856a 103 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
5hel2l2y 0:d0291b4a856a 104 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
5hel2l2y 0:d0291b4a856a 105 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
5hel2l2y 0:d0291b4a856a 106 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
5hel2l2y 0:d0291b4a856a 107 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
5hel2l2y 0:d0291b4a856a 108
5hel2l2y 0:d0291b4a856a 109 ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
5hel2l2y 0:d0291b4a856a 110 ble.startAdvertising();
5hel2l2y 0:d0291b4a856a 111
5hel2l2y 0:d0291b4a856a 112 UARTService uartService(ble);
5hel2l2y 0:d0291b4a856a 113 uartServicePtr = &uartService;
5hel2l2y 2:514af7294ad0 114
5hel2l2y 2:514af7294ad0 115 // setup baud rate and reset sensor
5hel2l2y 2:514af7294ad0 116 pc.baud(115200);
5hel2l2y 2:514af7294ad0 117 imu.reset();
5hel2l2y 0:d0291b4a856a 118
5hel2l2y 0:d0291b4a856a 119 while (true) {
5hel2l2y 0:d0291b4a856a 120 ble.waitForEvent();
5hel2l2y 0:d0291b4a856a 121 }
5hel2l2y 0:d0291b4a856a 122 }