![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
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
main.cpp@3:c0d70871aa4d, 2016-06-21 (annotated)
- Committer:
- 5hel2l2y
- Date:
- Tue Jun 21 18:13:49 2016 +0000
- Revision:
- 3:c0d70871aa4d
- Parent:
- 2:514af7294ad0
- Child:
- 4:65f91237b687
Added interrupt to be signaled with LED light
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:c0d70871aa4d | 37 | DigitalOut led4(LED4); |
5hel2l2y | 0:d0291b4a856a | 38 | |
5hel2l2y | 0:d0291b4a856a | 39 | UARTService *uartServicePtr; |
5hel2l2y | 0:d0291b4a856a | 40 | |
5hel2l2y | 0:d0291b4a856a | 41 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
5hel2l2y | 0:d0291b4a856a | 42 | { |
5hel2l2y | 0:d0291b4a856a | 43 | DEBUG("Disconnected!\n\r"); |
5hel2l2y | 0:d0291b4a856a | 44 | DEBUG("Restarting the advertising process\n\r"); |
5hel2l2y | 0:d0291b4a856a | 45 | ble.startAdvertising(); |
5hel2l2y | 0:d0291b4a856a | 46 | } |
5hel2l2y | 0:d0291b4a856a | 47 | |
5hel2l2y | 0:d0291b4a856a | 48 | void onDataWritten(const GattWriteCallbackParams *params) |
5hel2l2y | 3:c0d70871aa4d | 49 | { |
5hel2l2y | 0:d0291b4a856a | 50 | if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) { |
5hel2l2y | 0:d0291b4a856a | 51 | uint16_t bytesRead = params->len; |
5hel2l2y | 0:d0291b4a856a | 52 | uint8_t value = *(params->data); |
5hel2l2y | 0:d0291b4a856a | 53 | DEBUG("received %u bytes\n\r", bytesRead); |
5hel2l2y | 0:d0291b4a856a | 54 | DEBUG("recevied %d data\n\r", value); |
5hel2l2y | 3:c0d70871aa4d | 55 | |
5hel2l2y | 3:c0d70871aa4d | 56 | imu.reset(); |
5hel2l2y | 0:d0291b4a856a | 57 | |
5hel2l2y | 2:514af7294ad0 | 58 | // Set power mode, POWER_MODE_NORMAL(default), POWER_MODE_LOWPOWER, or POWER_MODE_SUSPEND(sleep) |
5hel2l2y | 2:514af7294ad0 | 59 | // By default, I2C will pull high when bus is free(pg90 of datasheet) |
5hel2l2y | 0:d0291b4a856a | 60 | switch(value) { |
5hel2l2y | 0:d0291b4a856a | 61 | case 49: |
5hel2l2y | 0:d0291b4a856a | 62 | DEBUG("Off power\n\r"); |
5hel2l2y | 2:514af7294ad0 | 63 | imu.setpowermode(POWER_MODE_SUSPEND); |
5hel2l2y | 0:d0291b4a856a | 64 | break; |
5hel2l2y | 0:d0291b4a856a | 65 | case 50: |
5hel2l2y | 0:d0291b4a856a | 66 | DEBUG("Low power\n\r"); |
5hel2l2y | 2:514af7294ad0 | 67 | imu.setpowermode(POWER_MODE_LOWPOWER); |
5hel2l2y | 0:d0291b4a856a | 68 | break; |
5hel2l2y | 0:d0291b4a856a | 69 | case 51: |
5hel2l2y | 0:d0291b4a856a | 70 | DEBUG("High power\n\r"); |
5hel2l2y | 2:514af7294ad0 | 71 | imu.setpowermode(POWER_MODE_NORMAL); |
5hel2l2y | 0:d0291b4a856a | 72 | break; |
5hel2l2y | 0:d0291b4a856a | 73 | default: |
5hel2l2y | 0:d0291b4a856a | 74 | DEBUG("Nothing happened\n\r"); |
5hel2l2y | 0:d0291b4a856a | 75 | break; |
5hel2l2y | 0:d0291b4a856a | 76 | } |
5hel2l2y | 0:d0291b4a856a | 77 | |
5hel2l2y | 2:514af7294ad0 | 78 | if (imu.check()) |
5hel2l2y | 2:514af7294ad0 | 79 | { |
5hel2l2y | 2:514af7294ad0 | 80 | pc.printf("BNO055 found\r\n"); |
5hel2l2y | 2:514af7294ad0 | 81 | } |
5hel2l2y | 0:d0291b4a856a | 82 | |
5hel2l2y | 0:d0291b4a856a | 83 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); |
5hel2l2y | 0:d0291b4a856a | 84 | } |
5hel2l2y | 0:d0291b4a856a | 85 | } |
5hel2l2y | 0:d0291b4a856a | 86 | |
5hel2l2y | 0:d0291b4a856a | 87 | void periodicCallback(void) |
5hel2l2y | 0:d0291b4a856a | 88 | { |
5hel2l2y | 0:d0291b4a856a | 89 | led1 = !led1; |
5hel2l2y | 0:d0291b4a856a | 90 | } |
5hel2l2y | 0:d0291b4a856a | 91 | |
5hel2l2y | 3:c0d70871aa4d | 92 | // Interrupt signal |
5hel2l2y | 3:c0d70871aa4d | 93 | void flip() { |
5hel2l2y | 3:c0d70871aa4d | 94 | led4 = !led4; |
5hel2l2y | 3:c0d70871aa4d | 95 | } |
5hel2l2y | 3:c0d70871aa4d | 96 | |
5hel2l2y | 0:d0291b4a856a | 97 | int main(void) |
5hel2l2y | 0:d0291b4a856a | 98 | { |
5hel2l2y | 0:d0291b4a856a | 99 | led1 = 1; |
5hel2l2y | 0:d0291b4a856a | 100 | Ticker ticker; |
5hel2l2y | 0:d0291b4a856a | 101 | ticker.attach(periodicCallback, 1); |
5hel2l2y | 0:d0291b4a856a | 102 | |
5hel2l2y | 0:d0291b4a856a | 103 | DEBUG("Initialising the nRF51822\n\r"); |
5hel2l2y | 0:d0291b4a856a | 104 | pc.printf("PC: Initialising the nRF51822\n\r"); |
5hel2l2y | 0:d0291b4a856a | 105 | ble.init(); |
5hel2l2y | 0:d0291b4a856a | 106 | ble.onDisconnection(disconnectionCallback); |
5hel2l2y | 0:d0291b4a856a | 107 | ble.onDataWritten(onDataWritten); |
5hel2l2y | 0:d0291b4a856a | 108 | |
5hel2l2y | 0:d0291b4a856a | 109 | /* setup advertising */ |
5hel2l2y | 0:d0291b4a856a | 110 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
5hel2l2y | 0:d0291b4a856a | 111 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
5hel2l2y | 0:d0291b4a856a | 112 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
5hel2l2y | 0:d0291b4a856a | 113 | (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); |
5hel2l2y | 0:d0291b4a856a | 114 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
5hel2l2y | 0:d0291b4a856a | 115 | (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
5hel2l2y | 0:d0291b4a856a | 116 | |
5hel2l2y | 0:d0291b4a856a | 117 | ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */ |
5hel2l2y | 0:d0291b4a856a | 118 | ble.startAdvertising(); |
5hel2l2y | 0:d0291b4a856a | 119 | |
5hel2l2y | 0:d0291b4a856a | 120 | UARTService uartService(ble); |
5hel2l2y | 0:d0291b4a856a | 121 | uartServicePtr = &uartService; |
5hel2l2y | 2:514af7294ad0 | 122 | |
5hel2l2y | 2:514af7294ad0 | 123 | // setup baud rate and reset sensor |
5hel2l2y | 2:514af7294ad0 | 124 | pc.baud(115200); |
5hel2l2y | 3:c0d70871aa4d | 125 | led4 = 1; |
5hel2l2y | 0:d0291b4a856a | 126 | |
5hel2l2y | 0:d0291b4a856a | 127 | while (true) { |
5hel2l2y | 0:d0291b4a856a | 128 | ble.waitForEvent(); |
5hel2l2y | 3:c0d70871aa4d | 129 | |
5hel2l2y | 3:c0d70871aa4d | 130 | imu.setmode(OPERATION_MODE_AMG); |
5hel2l2y | 3:c0d70871aa4d | 131 | imu.get_accel(); |
5hel2l2y | 3:c0d70871aa4d | 132 | pc.printf("%2f,%2f,%2f\r\n", imu.accel.x, imu.accel.y, imu.accel.z); |
5hel2l2y | 3:c0d70871aa4d | 133 | imu.get_gyro(); |
5hel2l2y | 3:c0d70871aa4d | 134 | pc.printf("%2f,%2f,%2f\r\n", imu.gyro.x, imu.gyro.y, imu.gyro.z); |
5hel2l2y | 3:c0d70871aa4d | 135 | imu.get_mag(); |
5hel2l2y | 3:c0d70871aa4d | 136 | pc.printf("%2f,%2f,%2f\r\n", imu.mag.x, imu.mag.y, imu.mag.z); |
5hel2l2y | 3:c0d70871aa4d | 137 | // Interrupt stats: This is only enabled in Low Power Mode |
5hel2l2y | 3:c0d70871aa4d | 138 | // 0 - no interrupt |
5hel2l2y | 3:c0d70871aa4d | 139 | // 64 - interrupt |
5hel2l2y | 3:c0d70871aa4d | 140 | // 128 - sleep |
5hel2l2y | 3:c0d70871aa4d | 141 | imu.get_intr(); |
5hel2l2y | 3:c0d70871aa4d | 142 | pc.printf("intr: %d\r\n", imu.intr); |
5hel2l2y | 3:c0d70871aa4d | 143 | if(imu.intr != 0) { |
5hel2l2y | 3:c0d70871aa4d | 144 | pc.printf(" == interrupted == \r\n"); |
5hel2l2y | 3:c0d70871aa4d | 145 | flip(); |
5hel2l2y | 3:c0d70871aa4d | 146 | } |
5hel2l2y | 3:c0d70871aa4d | 147 | |
5hel2l2y | 3:c0d70871aa4d | 148 | wait(1.0); |
5hel2l2y | 0:d0291b4a856a | 149 | } |
5hel2l2y | 0:d0291b4a856a | 150 | } |