Openwear Life logger example
Dependencies: BLE_API MPL6_1 TCS3472_I2C mbed-src-openwear nRF51822_openwear
Fork of BLE_LoopbackUART by
main.cpp@8:150a9cb60210, 2014-09-16 (annotated)
- Committer:
- janekm
- Date:
- Tue Sep 16 22:43:44 2014 +0000
- Revision:
- 8:150a9cb60210
- Parent:
- 7:9a474d182e4b
I forgot
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 0:e910d9bb040f | 1 | /* mbed Microcontroller Library |
yihui | 0:e910d9bb040f | 2 | * Copyright (c) 2006-2013 ARM Limited |
yihui | 0:e910d9bb040f | 3 | * |
yihui | 0:e910d9bb040f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
yihui | 0:e910d9bb040f | 5 | * you may not use this file except in compliance with the License. |
yihui | 0:e910d9bb040f | 6 | * You may obtain a copy of the License at |
yihui | 0:e910d9bb040f | 7 | * |
yihui | 0:e910d9bb040f | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
yihui | 0:e910d9bb040f | 9 | * |
yihui | 0:e910d9bb040f | 10 | * Unless required by applicable law or agreed to in writing, software |
yihui | 0:e910d9bb040f | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
yihui | 0:e910d9bb040f | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
yihui | 0:e910d9bb040f | 13 | * See the License for the specific language governing permissions and |
yihui | 0:e910d9bb040f | 14 | * limitations under the License. |
yihui | 0:e910d9bb040f | 15 | */ |
yihui | 0:e910d9bb040f | 16 | |
yihui | 0:e910d9bb040f | 17 | #include "mbed.h" |
Rohit Grover |
2:e060367b9024 | 18 | #include "BLEDevice.h" |
janekm | 6:68a02e91836e | 19 | #include "TCS3472_I2C.h" |
janekm | 8:150a9cb60210 | 20 | #include "mpu_hal.h" |
janekm | 6:68a02e91836e | 21 | I2C i2c(p7, p6); |
janekm | 7:9a474d182e4b | 22 | TCS3472_I2C rgb_sensor(&i2c); |
janekm | 7:9a474d182e4b | 23 | |
janekm | 7:9a474d182e4b | 24 | Timer t; |
yihui | 0:e910d9bb040f | 25 | |
Rohit Grover |
2:e060367b9024 | 26 | BLEDevice ble; |
janekm | 6:68a02e91836e | 27 | DigitalOut led1(p27); |
janekm | 7:9a474d182e4b | 28 | PwmOut led2(p28); |
janekm | 6:68a02e91836e | 29 | DigitalOut LDOOn(p5); |
janekm | 6:68a02e91836e | 30 | DigitalOut SoundOn(p2); |
janekm | 6:68a02e91836e | 31 | AnalogIn SoundIn(p1); |
janekm | 6:68a02e91836e | 32 | |
janekm | 7:9a474d182e4b | 33 | float sum = 0; |
janekm | 7:9a474d182e4b | 34 | float q[4] = {1.0f, 0.0f, 0.0f, 0.0f}; // vector to hold quaternion |
janekm | 7:9a474d182e4b | 35 | float pitch, yaw, roll; |
janekm | 7:9a474d182e4b | 36 | float deltat = 0.0f; // integration interval for both filter schemes |
janekm | 7:9a474d182e4b | 37 | |
janekm | 7:9a474d182e4b | 38 | |
janekm | 7:9a474d182e4b | 39 | uint32_t sumCount = 0; |
janekm | 7:9a474d182e4b | 40 | char buffer[14]; |
janekm | 7:9a474d182e4b | 41 | |
janekm | 6:68a02e91836e | 42 | static volatile bool triggerSensorPolling = false; |
yihui | 0:e910d9bb040f | 43 | |
yihui | 0:e910d9bb040f | 44 | // The Nordic UART Service |
janekm | 6:68a02e91836e | 45 | static const uint8_t uart_service_uuid[] = {0x6e, 0x40, 0x00, 0x01, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5,0x0e, 0x24, 0xdc, 0xca, 0x9e}; |
Rohit Grover |
2:e060367b9024 | 46 | static const uint8_t uart_tx_uuid[] = {0x6e, 0x40, 0x00, 0x02, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5,0x0e, 0x24, 0xdc, 0xca, 0x9e}; |
Rohit Grover |
2:e060367b9024 | 47 | static const uint8_t uart_rx_uuid[] = {0x6e, 0x40, 0x00, 0x03, 0xb5, 0xa3, 0xf3, 0x93, 0xe0, 0xa9, 0xe5,0x0e, 0x24, 0xdc, 0xca, 0x9e}; |
janekm | 6:68a02e91836e | 48 | static const uint8_t uart_service_uuid_rev[] = {0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x01, 0x00, 0x40, 0x6e}; |
janekm | 6:68a02e91836e | 49 | static const uint16_t uuid16_list[] = {GattService::UUID_DEVICE_INFORMATION_SERVICE}; |
rgrover1 | 5:4bc41267a03a | 50 | |
rgrover1 | 5:4bc41267a03a | 51 | static const uint8_t SIZEOF_TX_RX_BUFFER = 128; |
rgrover1 | 5:4bc41267a03a | 52 | uint8_t rxPayload[SIZEOF_TX_RX_BUFFER] = {0,}; |
rgrover1 | 5:4bc41267a03a | 53 | uint8_t txPayload[SIZEOF_TX_RX_BUFFER] = {0,}; |
janekm | 6:68a02e91836e | 54 | |
janekm | 6:68a02e91836e | 55 | uint8_t hardwareRevision[] = "0.1"; |
rgrover1 | 5:4bc41267a03a | 56 | GattCharacteristic rxCharacteristic (uart_tx_uuid, rxPayload, 1, SIZEOF_TX_RX_BUFFER, |
Rohit Grover |
2:e060367b9024 | 57 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
rgrover1 | 5:4bc41267a03a | 58 | GattCharacteristic txCharacteristic (uart_rx_uuid, txPayload, 1, SIZEOF_TX_RX_BUFFER, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
rgrover1 | 5:4bc41267a03a | 59 | GattCharacteristic *uartChars[] = {&rxCharacteristic, &txCharacteristic}; |
janekm | 6:68a02e91836e | 60 | GattService uartService(uart_service_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); |
janekm | 6:68a02e91836e | 61 | |
janekm | 7:9a474d182e4b | 62 | GattCharacteristic hardwareRevCharacteristic(GattCharacteristic::UUID_HARDWARE_REVISION_STRING_CHAR, hardwareRevision, sizeof(hardwareRevision), sizeof(hardwareRevision), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
janekm | 6:68a02e91836e | 63 | GattCharacteristic *deviceInfoChars[] = {&hardwareRevCharacteristic}; |
janekm | 6:68a02e91836e | 64 | GattService deviceInfoService(GattService::UUID_DEVICE_INFORMATION_SERVICE, deviceInfoChars, sizeof(deviceInfoChars) / sizeof(GattCharacteristic *)); |
yihui | 0:e910d9bb040f | 65 | |
rgrover1 | 5:4bc41267a03a | 66 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
yihui | 0:e910d9bb040f | 67 | { |
Rohit Grover |
2:e060367b9024 | 68 | ble.startAdvertising(); |
Rohit Grover |
2:e060367b9024 | 69 | } |
yihui | 0:e910d9bb040f | 70 | |
rgrover1 | 5:4bc41267a03a | 71 | void onDataWritten(uint16_t charHandle, const GattCharacteristicWriteCBParams *params) |
yihui | 0:e910d9bb040f | 72 | { |
rgrover1 | 5:4bc41267a03a | 73 | if (charHandle == rxCharacteristic.getValueAttribute().getHandle()) { |
rgrover1 | 5:4bc41267a03a | 74 | uint16_t bytesRead = params->len; |
rgrover1 | 5:4bc41267a03a | 75 | if (bytesRead < sizeof(rxPayload)) { |
rgrover1 | 5:4bc41267a03a | 76 | memcpy(rxPayload, params->data, bytesRead); |
rgrover1 | 5:4bc41267a03a | 77 | rxPayload[bytesRead] = 0; |
rgrover1 | 5:4bc41267a03a | 78 | } |
rgrover1 | 5:4bc41267a03a | 79 | ble.updateCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), rxPayload, bytesRead); |
yihui | 0:e910d9bb040f | 80 | } |
Rohit Grover |
2:e060367b9024 | 81 | } |
yihui | 0:e910d9bb040f | 82 | |
Rohit Grover |
2:e060367b9024 | 83 | void periodicCallback(void) |
Rohit Grover |
2:e060367b9024 | 84 | { |
janekm | 6:68a02e91836e | 85 | triggerSensorPolling = true; |
Rohit Grover |
2:e060367b9024 | 86 | } |
yihui | 0:e910d9bb040f | 87 | |
yihui | 0:e910d9bb040f | 88 | int main(void) |
yihui | 0:e910d9bb040f | 89 | { |
Rohit Grover |
2:e060367b9024 | 90 | led1 = 1; |
janekm | 7:9a474d182e4b | 91 | led2 = 1; |
janekm | 6:68a02e91836e | 92 | LDOOn = 1; |
janekm | 6:68a02e91836e | 93 | SoundOn = 1; |
Rohit Grover |
2:e060367b9024 | 94 | Ticker ticker; |
janekm | 7:9a474d182e4b | 95 | i2c.frequency(100000); |
janekm | 7:9a474d182e4b | 96 | ticker.attach(periodicCallback, 0.3); |
janekm | 7:9a474d182e4b | 97 | t.start(); |
janekm | 8:150a9cb60210 | 98 | mpu_i2c_dev = &(i2c._i2c); |
janekm | 7:9a474d182e4b | 99 | |
yihui | 0:e910d9bb040f | 100 | |
Rohit Grover |
2:e060367b9024 | 101 | ble.init(); |
janekm | 6:68a02e91836e | 102 | //rgb_sensor.enableWait(); |
Rohit Grover |
2:e060367b9024 | 103 | ble.onDisconnection(disconnectionCallback); |
Rohit Grover |
2:e060367b9024 | 104 | ble.onDataWritten(onDataWritten); |
yihui | 0:e910d9bb040f | 105 | |
Rohit Grover |
2:e060367b9024 | 106 | /* setup advertising */ |
Rohit Grover |
2:e060367b9024 | 107 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
Rohit Grover |
2:e060367b9024 | 108 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Rohit Grover |
2:e060367b9024 | 109 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
janekm | 7:9a474d182e4b | 110 | (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); |
Rohit Grover |
2:e060367b9024 | 111 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
janekm | 7:9a474d182e4b | 112 | (const uint8_t *)uart_service_uuid_rev, sizeof(uart_service_uuid_rev)); |
janekm | 6:68a02e91836e | 113 | ble.accumulateScanResponse(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
janekm | 6:68a02e91836e | 114 | //ble.accumulateAdvertisingPayload(GapAdvertisingData::HEART_RATE_SENSOR_HEART_RATE_BELT); |
yihui | 0:e910d9bb040f | 115 | |
Rohit Grover |
2:e060367b9024 | 116 | ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
janekm | 6:68a02e91836e | 117 | ble.addService(uartService); |
janekm | 6:68a02e91836e | 118 | ble.addService(deviceInfoService); |
janekm | 7:9a474d182e4b | 119 | rgb_sensor.enablePowerAndRGBC(); |
janekm | 7:9a474d182e4b | 120 | rgb_sensor.setIntegrationTime( 100 ); |
janekm | 7:9a474d182e4b | 121 | rgb_sensor.setWaitTime(900); |
Rohit Grover |
2:e060367b9024 | 122 | ble.startAdvertising(); |
yihui | 0:e910d9bb040f | 123 | |
yihui | 0:e910d9bb040f | 124 | |
Rohit Grover |
2:e060367b9024 | 125 | while (true) { |
janekm | 6:68a02e91836e | 126 | if (triggerSensorPolling) { |
janekm | 6:68a02e91836e | 127 | triggerSensorPolling = false; |
janekm | 7:9a474d182e4b | 128 | //led2 = !led2; |
janekm | 6:68a02e91836e | 129 | //led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
janekm | 6:68a02e91836e | 130 | char reading[20]; |
janekm | 6:68a02e91836e | 131 | int rgb_readings[4]; |
janekm | 6:68a02e91836e | 132 | int len; |
janekm | 6:68a02e91836e | 133 | rgb_sensor.getAllColors( rgb_readings ); |
janekm | 6:68a02e91836e | 134 | rgb_sensor.clearInterrupt(); |
janekm | 6:68a02e91836e | 135 | char whoami; |
janekm | 6:68a02e91836e | 136 | float max = 0.0; |
janekm | 6:68a02e91836e | 137 | float min = 1.0; |
janekm | 6:68a02e91836e | 138 | float current = 0.0; |
janekm | 6:68a02e91836e | 139 | for (int i = 0; i < 20; i++) { |
janekm | 6:68a02e91836e | 140 | current = SoundIn; |
janekm | 6:68a02e91836e | 141 | if (current > max) max = current; |
janekm | 6:68a02e91836e | 142 | if (current < min) min = current; |
janekm | 6:68a02e91836e | 143 | } |
janekm | 6:68a02e91836e | 144 | len = sprintf((char *)reading, "%d,%d,%d,%d", rgb_readings[0], rgb_readings[1], rgb_readings[2], rgb_readings[3]); |
janekm | 6:68a02e91836e | 145 | ble.updateCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(),(uint8_t *) reading, len); |
janekm | 6:68a02e91836e | 146 | len = sprintf((char *)reading, "%1.4f", (max - min) * 10.0); |
janekm | 6:68a02e91836e | 147 | ble.updateCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(),(uint8_t *) reading, len); |
janekm | 6:68a02e91836e | 148 | |
janekm | 7:9a474d182e4b | 149 | //whoami = readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250); |
janekm | 7:9a474d182e4b | 150 | //len = sprintf((char *)reading, "%d", whoami); |
janekm | 7:9a474d182e4b | 151 | //ble.updateCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(),(uint8_t *) reading, len); |
janekm | 7:9a474d182e4b | 152 | |
janekm | 6:68a02e91836e | 153 | } else { |
janekm | 7:9a474d182e4b | 154 | //ble.waitForEvent(); |
janekm | 6:68a02e91836e | 155 | float max = 0.0; |
janekm | 6:68a02e91836e | 156 | float min = 1.0; |
janekm | 6:68a02e91836e | 157 | float current = 0.0; |
janekm | 6:68a02e91836e | 158 | for (int i = 0; i < 100; i++) { |
janekm | 6:68a02e91836e | 159 | current = SoundIn; |
janekm | 6:68a02e91836e | 160 | if (current > max) max = current; |
janekm | 6:68a02e91836e | 161 | if (current < min) min = current; |
janekm | 6:68a02e91836e | 162 | } |
janekm | 6:68a02e91836e | 163 | if ((max - min) > 0.005) { |
janekm | 6:68a02e91836e | 164 | led2 = 1.0 - (max - min)*5.0; |
janekm | 6:68a02e91836e | 165 | if ((max - min) > 0.06) { |
janekm | 6:68a02e91836e | 166 | led1 = 0; |
janekm | 6:68a02e91836e | 167 | } else { |
janekm | 6:68a02e91836e | 168 | led1 = 1; |
janekm | 6:68a02e91836e | 169 | } |
janekm | 6:68a02e91836e | 170 | } else { |
janekm | 6:68a02e91836e | 171 | led2 = 1.0; |
janekm | 6:68a02e91836e | 172 | led1 = 1; |
janekm | 6:68a02e91836e | 173 | } |
janekm | 6:68a02e91836e | 174 | } |
yihui | 0:e910d9bb040f | 175 | } |
yihui | 0:e910d9bb040f | 176 | } |