Openwear Life logger example
Dependencies: BLE_API MPL6_1 TCS3472_I2C mbed-src-openwear nRF51822_openwear
Fork of BLE_LoopbackUART by
main.cpp@6:68a02e91836e, 2014-09-04 (annotated)
- Committer:
- janekm
- Date:
- Thu Sep 04 21:19:18 2014 +0000
- Revision:
- 6:68a02e91836e
- Parent:
- 5:4bc41267a03a
- Child:
- 7:9a474d182e4b
about to start implementing AHRS
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" |
yihui | 0:e910d9bb040f | 20 | |
janekm | 6:68a02e91836e | 21 | I2C i2c(p7, p6); |
janekm | 6:68a02e91836e | 22 | TCS3472_I2C rgb_sensor( &i2c ); |
yihui | 0:e910d9bb040f | 23 | |
Rohit Grover |
2:e060367b9024 | 24 | BLEDevice ble; |
janekm | 6:68a02e91836e | 25 | DigitalOut led1(p27); |
janekm | 6:68a02e91836e | 26 | PwmOut led2(p28); |
janekm | 6:68a02e91836e | 27 | DigitalOut LDOOn(p5); |
janekm | 6:68a02e91836e | 28 | DigitalOut SoundOn(p2); |
janekm | 6:68a02e91836e | 29 | AnalogIn SoundIn(p1); |
janekm | 6:68a02e91836e | 30 | |
janekm | 6:68a02e91836e | 31 | static volatile bool triggerSensorPolling = false; |
yihui | 0:e910d9bb040f | 32 | |
yihui | 0:e910d9bb040f | 33 | // The Nordic UART Service |
janekm | 6:68a02e91836e | 34 | 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 | 35 | 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 | 36 | 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 | 37 | 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 | 38 | static const uint16_t uuid16_list[] = {GattService::UUID_DEVICE_INFORMATION_SERVICE}; |
rgrover1 | 5:4bc41267a03a | 39 | |
rgrover1 | 5:4bc41267a03a | 40 | static const uint8_t SIZEOF_TX_RX_BUFFER = 128; |
rgrover1 | 5:4bc41267a03a | 41 | uint8_t rxPayload[SIZEOF_TX_RX_BUFFER] = {0,}; |
rgrover1 | 5:4bc41267a03a | 42 | uint8_t txPayload[SIZEOF_TX_RX_BUFFER] = {0,}; |
janekm | 6:68a02e91836e | 43 | |
janekm | 6:68a02e91836e | 44 | uint8_t hardwareRevision[] = "0.1"; |
rgrover1 | 5:4bc41267a03a | 45 | GattCharacteristic rxCharacteristic (uart_tx_uuid, rxPayload, 1, SIZEOF_TX_RX_BUFFER, |
Rohit Grover |
2:e060367b9024 | 46 | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); |
rgrover1 | 5:4bc41267a03a | 47 | GattCharacteristic txCharacteristic (uart_rx_uuid, txPayload, 1, SIZEOF_TX_RX_BUFFER, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
rgrover1 | 5:4bc41267a03a | 48 | GattCharacteristic *uartChars[] = {&rxCharacteristic, &txCharacteristic}; |
janekm | 6:68a02e91836e | 49 | GattService uartService(uart_service_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); |
janekm | 6:68a02e91836e | 50 | |
janekm | 6:68a02e91836e | 51 | GattCharacteristic hardwareRevCharacteristic(GattCharacteristic::UUID_HARDWARE_REVISION_STRING_CHAR, hardwareRevision, sizeof(hardwareRevision), sizeof(hardwareRevision), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); |
janekm | 6:68a02e91836e | 52 | GattCharacteristic *deviceInfoChars[] = {&hardwareRevCharacteristic}; |
janekm | 6:68a02e91836e | 53 | GattService deviceInfoService(GattService::UUID_DEVICE_INFORMATION_SERVICE, deviceInfoChars, sizeof(deviceInfoChars) / sizeof(GattCharacteristic *)); |
yihui | 0:e910d9bb040f | 54 | |
rgrover1 | 5:4bc41267a03a | 55 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
yihui | 0:e910d9bb040f | 56 | { |
Rohit Grover |
2:e060367b9024 | 57 | ble.startAdvertising(); |
Rohit Grover |
2:e060367b9024 | 58 | } |
yihui | 0:e910d9bb040f | 59 | |
rgrover1 | 5:4bc41267a03a | 60 | void onDataWritten(uint16_t charHandle, const GattCharacteristicWriteCBParams *params) |
yihui | 0:e910d9bb040f | 61 | { |
rgrover1 | 5:4bc41267a03a | 62 | if (charHandle == rxCharacteristic.getValueAttribute().getHandle()) { |
rgrover1 | 5:4bc41267a03a | 63 | uint16_t bytesRead = params->len; |
rgrover1 | 5:4bc41267a03a | 64 | if (bytesRead < sizeof(rxPayload)) { |
rgrover1 | 5:4bc41267a03a | 65 | memcpy(rxPayload, params->data, bytesRead); |
rgrover1 | 5:4bc41267a03a | 66 | rxPayload[bytesRead] = 0; |
rgrover1 | 5:4bc41267a03a | 67 | } |
rgrover1 | 5:4bc41267a03a | 68 | ble.updateCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), rxPayload, bytesRead); |
yihui | 0:e910d9bb040f | 69 | } |
Rohit Grover |
2:e060367b9024 | 70 | } |
yihui | 0:e910d9bb040f | 71 | |
Rohit Grover |
2:e060367b9024 | 72 | void periodicCallback(void) |
Rohit Grover |
2:e060367b9024 | 73 | { |
janekm | 6:68a02e91836e | 74 | triggerSensorPolling = true; |
Rohit Grover |
2:e060367b9024 | 75 | } |
yihui | 0:e910d9bb040f | 76 | |
yihui | 0:e910d9bb040f | 77 | int main(void) |
yihui | 0:e910d9bb040f | 78 | { |
Rohit Grover |
2:e060367b9024 | 79 | led1 = 1; |
janekm | 6:68a02e91836e | 80 | led2 = 1.0; |
janekm | 6:68a02e91836e | 81 | LDOOn = 1; |
janekm | 6:68a02e91836e | 82 | SoundOn = 1; |
Rohit Grover |
2:e060367b9024 | 83 | Ticker ticker; |
janekm | 6:68a02e91836e | 84 | i2c.frequency(400000); |
janekm | 6:68a02e91836e | 85 | ticker.attach(periodicCallback, 4); |
yihui | 0:e910d9bb040f | 86 | |
Rohit Grover |
2:e060367b9024 | 87 | ble.init(); |
janekm | 6:68a02e91836e | 88 | rgb_sensor.enablePowerAndRGBC(); |
janekm | 6:68a02e91836e | 89 | rgb_sensor.setIntegrationTime( 100 ); |
janekm | 6:68a02e91836e | 90 | rgb_sensor.setWaitTime(900); |
janekm | 6:68a02e91836e | 91 | //rgb_sensor.enableWait(); |
Rohit Grover |
2:e060367b9024 | 92 | ble.onDisconnection(disconnectionCallback); |
Rohit Grover |
2:e060367b9024 | 93 | ble.onDataWritten(onDataWritten); |
yihui | 0:e910d9bb040f | 94 | |
Rohit Grover |
2:e060367b9024 | 95 | /* setup advertising */ |
Rohit Grover |
2:e060367b9024 | 96 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
Rohit Grover |
2:e060367b9024 | 97 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Rohit Grover |
2:e060367b9024 | 98 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
Rohit Grover |
2:e060367b9024 | 99 | (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1); |
Rohit Grover |
2:e060367b9024 | 100 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
janekm | 6:68a02e91836e | 101 | (const uint8_t *)uart_service_uuid_rev, sizeof(uart_service_uuid_rev)); |
janekm | 6:68a02e91836e | 102 | ble.accumulateScanResponse(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
janekm | 6:68a02e91836e | 103 | //ble.accumulateAdvertisingPayload(GapAdvertisingData::HEART_RATE_SENSOR_HEART_RATE_BELT); |
yihui | 0:e910d9bb040f | 104 | |
Rohit Grover |
2:e060367b9024 | 105 | ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ |
janekm | 6:68a02e91836e | 106 | ble.addService(uartService); |
janekm | 6:68a02e91836e | 107 | ble.addService(deviceInfoService); |
Rohit Grover |
2:e060367b9024 | 108 | ble.startAdvertising(); |
yihui | 0:e910d9bb040f | 109 | |
yihui | 0:e910d9bb040f | 110 | |
Rohit Grover |
2:e060367b9024 | 111 | while (true) { |
janekm | 6:68a02e91836e | 112 | if (triggerSensorPolling) { |
janekm | 6:68a02e91836e | 113 | triggerSensorPolling = false; |
janekm | 6:68a02e91836e | 114 | //led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
janekm | 6:68a02e91836e | 115 | char reading[20]; |
janekm | 6:68a02e91836e | 116 | int rgb_readings[4]; |
janekm | 6:68a02e91836e | 117 | int len; |
janekm | 6:68a02e91836e | 118 | rgb_sensor.getAllColors( rgb_readings ); |
janekm | 6:68a02e91836e | 119 | rgb_sensor.clearInterrupt(); |
janekm | 6:68a02e91836e | 120 | char whoami; |
janekm | 6:68a02e91836e | 121 | float max = 0.0; |
janekm | 6:68a02e91836e | 122 | float min = 1.0; |
janekm | 6:68a02e91836e | 123 | float current = 0.0; |
janekm | 6:68a02e91836e | 124 | for (int i = 0; i < 20; i++) { |
janekm | 6:68a02e91836e | 125 | current = SoundIn; |
janekm | 6:68a02e91836e | 126 | if (current > max) max = current; |
janekm | 6:68a02e91836e | 127 | if (current < min) min = current; |
janekm | 6:68a02e91836e | 128 | } |
janekm | 6:68a02e91836e | 129 | len = sprintf((char *)reading, "%d,%d,%d,%d", rgb_readings[0], rgb_readings[1], rgb_readings[2], rgb_readings[3]); |
janekm | 6:68a02e91836e | 130 | ble.updateCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(),(uint8_t *) reading, len); |
janekm | 6:68a02e91836e | 131 | len = sprintf((char *)reading, "%1.4f", (max - min) * 10.0); |
janekm | 6:68a02e91836e | 132 | ble.updateCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(),(uint8_t *) reading, len); |
janekm | 6:68a02e91836e | 133 | whoami = readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250); |
janekm | 6:68a02e91836e | 134 | len = sprintf((char *)reading, "%d", whoami); |
janekm | 6:68a02e91836e | 135 | ble.updateCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(),(uint8_t *) reading, len); |
janekm | 6:68a02e91836e | 136 | |
janekm | 6:68a02e91836e | 137 | } else { |
janekm | 6:68a02e91836e | 138 | //ble.waitForEvent(); |
janekm | 6:68a02e91836e | 139 | float max = 0.0; |
janekm | 6:68a02e91836e | 140 | float min = 1.0; |
janekm | 6:68a02e91836e | 141 | float current = 0.0; |
janekm | 6:68a02e91836e | 142 | for (int i = 0; i < 100; i++) { |
janekm | 6:68a02e91836e | 143 | current = SoundIn; |
janekm | 6:68a02e91836e | 144 | if (current > max) max = current; |
janekm | 6:68a02e91836e | 145 | if (current < min) min = current; |
janekm | 6:68a02e91836e | 146 | } |
janekm | 6:68a02e91836e | 147 | if ((max - min) > 0.005) { |
janekm | 6:68a02e91836e | 148 | led2 = 1.0 - (max - min)*5.0; |
janekm | 6:68a02e91836e | 149 | if ((max - min) > 0.06) { |
janekm | 6:68a02e91836e | 150 | led1 = 0; |
janekm | 6:68a02e91836e | 151 | } else { |
janekm | 6:68a02e91836e | 152 | led1 = 1; |
janekm | 6:68a02e91836e | 153 | } |
janekm | 6:68a02e91836e | 154 | } else { |
janekm | 6:68a02e91836e | 155 | led2 = 1.0; |
janekm | 6:68a02e91836e | 156 | led1 = 1; |
janekm | 6:68a02e91836e | 157 | } |
janekm | 6:68a02e91836e | 158 | } |
yihui | 0:e910d9bb040f | 159 | } |
yihui | 0:e910d9bb040f | 160 | } |