Code used for Sensor Expo 2016 - Balloon Game. More details can be found here: https://github.com/ROHMUSDC/ROHM-SensorExpo2016-Pressure-Sensor-Demo/

Dependencies:   BLE_API mbed nRF51822

Fork of Nordic_UART_TEMPLATE_ROHM_SHLD1Update by ROHMUSDC

ROHM Balloon Game Demo Code featured at Sensors Expo 2016

This code was written to be used with the Nordic Semiconductor nRF51-DK.

This Code allows the user to configure two known pressure distances and save pressure readings onto the application. Then it will automatically extrapolate these values and allow the user to see the height of the board. When connected to a balloon, greater heights can be achieved and the board will return the current height of the board.

Additional information about the ROHM MultiSensor Shield Board can be found at the following link: https://github.com/ROHMUSDC/ROHM-SensorExpo2016-Pressure-Sensor-Demo/

For code example for the ROHM SENSORSHLD0-EVK-101, please see the following link: https://developer.mbed.org/teams/ROHMUSDC/code/ROHMSensorShield_BALOONGAME/

Operation

See Github Repositoy for additional information on how to operate this demo application.

Supported ROHM Sensor Devices

  • BM1383GLV Pressure Sensor

Questions/Feedback

Please feel free to let us know any questions/feedback/comments/concerns on the ROHM shield implementation by contacting the following e-mail:

Committer:
kbahar3
Date:
Mon Jun 20 00:24:31 2016 +0000
Revision:
9:3a6fb5dd522c
Parent:
8:2a19622864c2
Child:
10:aa05fb52aaa3
Balloon Game, First Revision

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kbahar3 6:6860e53dc7ae 1 /*
kbahar3 6:6860e53dc7ae 2 * mbed Microcontroller Library
Daniel Veilleux 0:442c7a6f1978 3 * Copyright (c) 2006-2013 ARM Limited
Daniel Veilleux 0:442c7a6f1978 4 *
Daniel Veilleux 0:442c7a6f1978 5 * Licensed under the Apache License, Version 2.0 (the "License");
Daniel Veilleux 0:442c7a6f1978 6 * you may not use this file except in compliance with the License.
Daniel Veilleux 0:442c7a6f1978 7 * You may obtain a copy of the License at
Daniel Veilleux 0:442c7a6f1978 8 *
Daniel Veilleux 0:442c7a6f1978 9 * http://www.apache.org/licenses/LICENSE-2.0
Daniel Veilleux 0:442c7a6f1978 10 *
Daniel Veilleux 0:442c7a6f1978 11 * Unless required by applicable law or agreed to in writing, software
Daniel Veilleux 0:442c7a6f1978 12 * distributed under the License is distributed on an "AS IS" BASIS,
Daniel Veilleux 0:442c7a6f1978 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Daniel Veilleux 0:442c7a6f1978 14 * See the License for the specific language governing permissions and
Daniel Veilleux 0:442c7a6f1978 15 * limitations under the License.
Daniel Veilleux 0:442c7a6f1978 16 */
kbahar3 2:c7b9d588c80f 17
kbahar3 9:3a6fb5dd522c 18 //BALLOON GAME!
kbahar3 2:c7b9d588c80f 19 /*
kbahar3 9:3a6fb5dd522c 20 * Code Example for ROHM Sensor Expo Balloon Game
kbahar3 6:6860e53dc7ae 21 *
kbahar3 9:3a6fb5dd522c 22 * Description: This Application interfaces ROHM's BM1383AGLV with Nordic's nRF51-DK
kbahar3 9:3a6fb5dd522c 23 * This Code supports the following sensor devices:
kbahar3 8:2a19622864c2 24 * > BM1383AGLV Pressure Sensor
kbahar3 2:c7b9d588c80f 25 *
kbahar3 9:3a6fb5dd522c 26 * This Code allows the user to configure two known pressure distances and save pressure readings
kbahar3 9:3a6fb5dd522c 27 * onto the application. Then it will automatically extrapolate these values and allow the user to see
kbahar3 9:3a6fb5dd522c 28 * the height of the board. When connected to a balloon, greater heights can be achieved and the board
kbahar3 9:3a6fb5dd522c 29 * will return the current height of the board.
kbahar3 9:3a6fb5dd522c 30 *
kbahar3 9:3a6fb5dd522c 31 * Additional information about the this Balloon Game can be found at the following link:
kbahar3 9:3a6fb5dd522c 32 * asdfasdfasdf
kbahar3 6:6860e53dc7ae 33 *
kbahar3 9:3a6fb5dd522c 34 * Last Upadtaed: 6/19/2016
kbahar3 6:6860e53dc7ae 35 * Author: ROHM USDC
kbahar3 6:6860e53dc7ae 36 * Contact Information: engineering@rohmsemiconductor.com
kbahar3 9:3a6fb5dd522c 37 * GitHub Link: https://github.com/ROHMUSDC/ROHM-SensorExpo2016-Pressure-Sensor-Demo/
kbahar3 6:6860e53dc7ae 38 */
Daniel Veilleux 0:442c7a6f1978 39
kbahar3 7:71046927a0e9 40 #define nRF52DevKit
kbahar3 6:6860e53dc7ae 41 #define Pressure //BM1383, Barometric Pressure Sensor
kbahar3 1:2c0ab5cd1a7f 42
Daniel Veilleux 0:442c7a6f1978 43 #include "mbed.h"
Daniel Veilleux 0:442c7a6f1978 44 #include "BLEDevice.h"
Daniel Veilleux 0:442c7a6f1978 45 #include "UARTService.h"
Daniel Veilleux 0:442c7a6f1978 46 #include "nrf_temp.h"
kbahar3 2:c7b9d588c80f 47 #include "I2C.h"
kbahar3 9:3a6fb5dd522c 48 #include <string>
Daniel Veilleux 0:442c7a6f1978 49
kbahar3 1:2c0ab5cd1a7f 50 #define MAX_REPLY_LEN (UARTService::BLE_UART_SERVICE_MAX_DATA_LEN) //Actually equal to 20
kbahar3 6:6860e53dc7ae 51 #define SENSOR_READ_INTERVAL_S (1.0F)
Daniel Veilleux 0:442c7a6f1978 52 #define ADV_INTERVAL_MS (1000UL)
Daniel Veilleux 0:442c7a6f1978 53 #define UART_BAUD_RATE (19200UL)
kbahar3 9:3a6fb5dd522c 54 #define DEVICE_NAME ("ROHM DEMO ") // This can be read AFTER connecting to the device.
kbahar3 9:3a6fb5dd522c 55 #define SHORT_NAME ("BALLOON1") // Keep this short: max 8 chars if a 128bit UUID is also advertised.
Daniel Veilleux 0:442c7a6f1978 56 #define DEBUG(...) { m_serial_port.printf(__VA_ARGS__); }
Daniel Veilleux 0:442c7a6f1978 57
kbahar3 1:2c0ab5cd1a7f 58 // Function Prototypes
kbahar3 6:6860e53dc7ae 59 void PBTrigger(); //Interrupt function for PB4
kbahar3 9:3a6fb5dd522c 60 void BTLE_DataWrittenHandler();
Daniel Veilleux 0:442c7a6f1978 61
kbahar3 1:2c0ab5cd1a7f 62 // Global Variables
Daniel Veilleux 0:442c7a6f1978 63 BLEDevice m_ble;
kbahar3 7:71046927a0e9 64 Serial m_serial_port(p9, p11); // TX pin, RX pin Original
kbahar3 7:71046927a0e9 65 //Serial m_serial_port(p8, p10); // TX pin, RX pin
Daniel Veilleux 0:442c7a6f1978 66 DigitalOut m_cmd_led(LED1);
Daniel Veilleux 0:442c7a6f1978 67 DigitalOut m_error_led(LED2);
Daniel Veilleux 0:442c7a6f1978 68 UARTService *m_uart_service_ptr;
kbahar3 9:3a6fb5dd522c 69 DigitalIn testButton(p20);
kbahar3 9:3a6fb5dd522c 70 InterruptIn sw4Press(p20);
kbahar3 9:3a6fb5dd522c 71 I2C i2c(p30,p7);
kbahar3 6:6860e53dc7ae 72 bool RepStart = true;
kbahar3 6:6860e53dc7ae 73 bool NoRepStart = false;
kbahar3 6:6860e53dc7ae 74 int i = 1;
kbahar3 9:3a6fb5dd522c 75 unsigned char printQue = 0;
kbahar3 9:3a6fb5dd522c 76 string ReceivedValue;
kbahar3 9:3a6fb5dd522c 77 char FormattedData[30];
kbahar3 9:3a6fb5dd522c 78 uint8_t buf[MAX_REPLY_LEN];
kbahar3 9:3a6fb5dd522c 79 uint32_t len = 0;
Daniel Veilleux 0:442c7a6f1978 80
kbahar3 1:2c0ab5cd1a7f 81 //Sensor Variables
kbahar3 5:d39ffc5638a3 82 #ifdef Pressure
kbahar3 6:6860e53dc7ae 83 int Press_addr_w = 0xBA;
kbahar3 6:6860e53dc7ae 84 int Press_addr_r = 0xBB;
kbahar3 5:d39ffc5638a3 85
kbahar3 9:3a6fb5dd522c 86 char PWR_ON[2] = {0x12, 0x01};
kbahar3 9:3a6fb5dd522c 87 char PWR_OFF[2] = {0x12, 0x00};
kbahar3 9:3a6fb5dd522c 88 char SLEEP_OFF[2] = {0x13, 0x01};
kbahar3 9:3a6fb5dd522c 89 char SLEEP_ON[2] = {0x13, 0x00};
kbahar3 9:3a6fb5dd522c 90 char Mode_Control[2] = {0x14, 0xCA};
kbahar3 5:d39ffc5638a3 91
kbahar3 5:d39ffc5638a3 92 char Press_Content_ReadData[6];
kbahar3 5:d39ffc5638a3 93 char Press_Addr_ReadData =0x1A;
kbahar3 5:d39ffc5638a3 94
kbahar3 5:d39ffc5638a3 95 int BM1383_Temp_highByte;
kbahar3 5:d39ffc5638a3 96 int BM1383_Temp_lowByte;
kbahar3 5:d39ffc5638a3 97 int BM1383_Pres_highByte;
kbahar3 5:d39ffc5638a3 98 int BM1383_Pres_lowByte;
kbahar3 5:d39ffc5638a3 99 int BM1383_Pres_leastByte;
kbahar3 5:d39ffc5638a3 100
kbahar3 6:6860e53dc7ae 101 short int BM1383_Temp_Out;
kbahar3 5:d39ffc5638a3 102 float BM1383_Temp_Conv_Out;
kbahar3 5:d39ffc5638a3 103 float BM1383_Pres_Conv_Out;
kbahar3 5:d39ffc5638a3 104
kbahar3 5:d39ffc5638a3 105 float BM1383_Var;
kbahar3 5:d39ffc5638a3 106 float BM1383_Deci;
kbahar3 8:2a19622864c2 107
kbahar3 9:3a6fb5dd522c 108 uint32_t BM1383_TempPressure;
kbahar3 8:2a19622864c2 109
kbahar3 9:3a6fb5dd522c 110
kbahar3 8:2a19622864c2 111 #endif
kbahar3 8:2a19622864c2 112
kbahar3 9:3a6fb5dd522c 113 //Balloon Game Variables
kbahar3 9:3a6fb5dd522c 114 float BM1383_Pres_0Level;
kbahar3 9:3a6fb5dd522c 115 float BM1383_Pres_KnownLevel;
kbahar3 9:3a6fb5dd522c 116 float HeightBase = 0;
kbahar3 9:3a6fb5dd522c 117 float HeightKnown_Total = 68;
kbahar3 9:3a6fb5dd522c 118 float HeightKnown_Foot = 60;
kbahar3 9:3a6fb5dd522c 119 float HeightKnown_Inches = 8;
kbahar3 9:3a6fb5dd522c 120 float slope;
kbahar3 9:3a6fb5dd522c 121 float yInt;
kbahar3 9:3a6fb5dd522c 122 float pressureCurr;
kbahar3 9:3a6fb5dd522c 123 float HeightExtrapolated; //Assuming linear pressure curve
kbahar3 9:3a6fb5dd522c 124 float HeightExtrapolated_Foot = 60;
kbahar3 9:3a6fb5dd522c 125 float HeightExtrapolated_Inches = 8;
kbahar3 9:3a6fb5dd522c 126
kbahar3 7:71046927a0e9 127
Daniel Veilleux 0:442c7a6f1978 128 /**
Daniel Veilleux 0:442c7a6f1978 129 * This callback is used whenever a disconnection occurs.
Daniel Veilleux 0:442c7a6f1978 130 */
Daniel Veilleux 0:442c7a6f1978 131 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
Daniel Veilleux 0:442c7a6f1978 132 {
Daniel Veilleux 0:442c7a6f1978 133 switch (reason) {
Daniel Veilleux 0:442c7a6f1978 134 case Gap::REMOTE_USER_TERMINATED_CONNECTION:
Daniel Veilleux 0:442c7a6f1978 135 DEBUG("Disconnected (REMOTE_USER_TERMINATED_CONNECTION)\n\r");
Daniel Veilleux 0:442c7a6f1978 136 break;
Daniel Veilleux 0:442c7a6f1978 137 case Gap::LOCAL_HOST_TERMINATED_CONNECTION:
Daniel Veilleux 0:442c7a6f1978 138 DEBUG("Disconnected (LOCAL_HOST_TERMINATED_CONNECTION)\n\r");
Daniel Veilleux 0:442c7a6f1978 139 break;
Daniel Veilleux 0:442c7a6f1978 140 case Gap::CONN_INTERVAL_UNACCEPTABLE:
Daniel Veilleux 0:442c7a6f1978 141 DEBUG("Disconnected (CONN_INTERVAL_UNACCEPTABLE)\n\r");
Daniel Veilleux 0:442c7a6f1978 142 break;
Daniel Veilleux 0:442c7a6f1978 143 }
Daniel Veilleux 0:442c7a6f1978 144
Daniel Veilleux 0:442c7a6f1978 145 DEBUG("Restarting the advertising process\n\r");
Daniel Veilleux 0:442c7a6f1978 146 m_ble.startAdvertising();
Daniel Veilleux 0:442c7a6f1978 147 }
Daniel Veilleux 0:442c7a6f1978 148
Daniel Veilleux 0:442c7a6f1978 149 /**
Daniel Veilleux 0:442c7a6f1978 150 * This callback is used whenever the host writes data to one of our GATT characteristics.
Daniel Veilleux 0:442c7a6f1978 151 */
Daniel Veilleux 0:442c7a6f1978 152 void dataWrittenCallback(const GattCharacteristicWriteCBParams *params)
Daniel Veilleux 0:442c7a6f1978 153 {
Daniel Veilleux 0:442c7a6f1978 154 // Ensure that initialization is finished and the host has written to the TX characteristic.
Daniel Veilleux 0:442c7a6f1978 155 if ((m_uart_service_ptr != NULL) && (params->charHandle == m_uart_service_ptr->getTXCharacteristicHandle())) {
kbahar3 9:3a6fb5dd522c 156 int i;
kbahar3 9:3a6fb5dd522c 157 ReceivedValue.clear();
kbahar3 9:3a6fb5dd522c 158 for(i = 0; i < params->len; i++)
kbahar3 9:3a6fb5dd522c 159 {
kbahar3 9:3a6fb5dd522c 160 ReceivedValue += params->data[i];
Daniel Veilleux 0:442c7a6f1978 161 }
kbahar3 9:3a6fb5dd522c 162 printQue = 1;
Daniel Veilleux 0:442c7a6f1978 163 }
Daniel Veilleux 0:442c7a6f1978 164 }
Daniel Veilleux 0:442c7a6f1978 165
Daniel Veilleux 0:442c7a6f1978 166 /**
Daniel Veilleux 0:442c7a6f1978 167 * This callback is used whenever a write to a GATT characteristic causes data to be sent to the host.
Daniel Veilleux 0:442c7a6f1978 168 */
Daniel Veilleux 0:442c7a6f1978 169 void dataSentCallback(unsigned count)
Daniel Veilleux 0:442c7a6f1978 170 {
Daniel Veilleux 0:442c7a6f1978 171 // NOTE: The count always seems to be 1 regardless of data.
Daniel Veilleux 0:442c7a6f1978 172 DEBUG("%d bytes sent to host\n\r", count);
Daniel Veilleux 0:442c7a6f1978 173 }
Daniel Veilleux 0:442c7a6f1978 174
Daniel Veilleux 0:442c7a6f1978 175
Daniel Veilleux 0:442c7a6f1978 176 /**
Daniel Veilleux 0:442c7a6f1978 177 * This callback is scheduled to be called periodically via a low-priority interrupt.
Daniel Veilleux 0:442c7a6f1978 178 */
Daniel Veilleux 0:442c7a6f1978 179 void periodicCallback(void)
Daniel Veilleux 0:442c7a6f1978 180 {
kbahar3 9:3a6fb5dd522c 181 //uint8_t buf[MAX_REPLY_LEN];
kbahar3 9:3a6fb5dd522c 182 //uint32_t len = 0;
kbahar3 5:d39ffc5638a3 183 }
kbahar3 5:d39ffc5638a3 184
Daniel Veilleux 0:442c7a6f1978 185 void error(ble_error_t err, uint32_t line)
Daniel Veilleux 0:442c7a6f1978 186 {
Daniel Veilleux 0:442c7a6f1978 187 m_error_led = 1;
Daniel Veilleux 0:442c7a6f1978 188 DEBUG("Error %d on line number %d\n\r", err, line);
Daniel Veilleux 0:442c7a6f1978 189 }
Daniel Veilleux 0:442c7a6f1978 190
kbahar3 1:2c0ab5cd1a7f 191 void PBTrigger()
kbahar3 1:2c0ab5cd1a7f 192 {
kbahar3 1:2c0ab5cd1a7f 193 uint8_t buf[MAX_REPLY_LEN];
kbahar3 1:2c0ab5cd1a7f 194 uint32_t len = 0;
kbahar3 1:2c0ab5cd1a7f 195
kbahar3 1:2c0ab5cd1a7f 196 m_cmd_led = !m_cmd_led;
kbahar3 1:2c0ab5cd1a7f 197
kbahar3 1:2c0ab5cd1a7f 198 if (m_ble.getGapState().connected) {
kbahar3 6:6860e53dc7ae 199 len = snprintf((char*) buf, MAX_REPLY_LEN, "Button Pressed!");
kbahar3 1:2c0ab5cd1a7f 200 m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
kbahar3 1:2c0ab5cd1a7f 201 }
kbahar3 1:2c0ab5cd1a7f 202 }
Daniel Veilleux 0:442c7a6f1978 203
Daniel Veilleux 0:442c7a6f1978 204 int main(void)
Daniel Veilleux 0:442c7a6f1978 205 {
Daniel Veilleux 0:442c7a6f1978 206 ble_error_t err;
Daniel Veilleux 0:442c7a6f1978 207 Ticker ticker;
Daniel Veilleux 0:442c7a6f1978 208
Daniel Veilleux 0:442c7a6f1978 209 m_serial_port.baud(UART_BAUD_RATE);
Daniel Veilleux 0:442c7a6f1978 210
kbahar3 2:c7b9d588c80f 211 DEBUG("Initialising...\n\r");
Daniel Veilleux 0:442c7a6f1978 212
Daniel Veilleux 0:442c7a6f1978 213 m_cmd_led = 0;
Daniel Veilleux 0:442c7a6f1978 214 m_error_led = 0;
Daniel Veilleux 0:442c7a6f1978 215
Daniel Veilleux 0:442c7a6f1978 216 ticker.attach(periodicCallback, SENSOR_READ_INTERVAL_S);
Daniel Veilleux 0:442c7a6f1978 217
kbahar3 1:2c0ab5cd1a7f 218 sw4Press.fall(&PBTrigger);
kbahar3 1:2c0ab5cd1a7f 219
kbahar3 9:3a6fb5dd522c 220 #ifdef Pressure //no Initialization because we keep in low power mode until we need to measure pressure
kbahar3 9:3a6fb5dd522c 221 //i2c.write(Press_addr_w, &PWR_OFF[0], 2, false);
kbahar3 9:3a6fb5dd522c 222 //i2c.write(Press_addr_w, &SLEEP_ON[0], 2, false);
kbahar3 9:3a6fb5dd522c 223 //i2c.write(Press_addr_w, &Mode_Control[0], 2, false);
kbahar3 6:6860e53dc7ae 224 #endif
kbahar3 5:d39ffc5638a3 225
kbahar3 6:6860e53dc7ae 226 //Start BTLE Initialization Section
Daniel Veilleux 0:442c7a6f1978 227 m_ble.init();
Daniel Veilleux 0:442c7a6f1978 228 m_ble.onDisconnection(disconnectionCallback);
Daniel Veilleux 0:442c7a6f1978 229 m_ble.onDataWritten(dataWrittenCallback);
Daniel Veilleux 0:442c7a6f1978 230 m_ble.onDataSent(dataSentCallback);
Daniel Veilleux 0:442c7a6f1978 231
Daniel Veilleux 0:442c7a6f1978 232 // Set the TX power in dBm units.
Daniel Veilleux 0:442c7a6f1978 233 // Possible values (in decreasing order): 4, 0, -4, -8, -12, -16, -20.
Daniel Veilleux 0:442c7a6f1978 234 err = m_ble.setTxPower(4);
Daniel Veilleux 0:442c7a6f1978 235 if (BLE_ERROR_NONE != err) {
Daniel Veilleux 0:442c7a6f1978 236 error(err, __LINE__);
Daniel Veilleux 0:442c7a6f1978 237 }
Daniel Veilleux 0:442c7a6f1978 238
Daniel Veilleux 0:442c7a6f1978 239 // Setup advertising (GAP stuff).
Daniel Veilleux 0:442c7a6f1978 240 err = m_ble.setDeviceName(DEVICE_NAME);
Daniel Veilleux 0:442c7a6f1978 241 if (BLE_ERROR_NONE != err) {
Daniel Veilleux 0:442c7a6f1978 242 error(err, __LINE__);
Daniel Veilleux 0:442c7a6f1978 243 }
Daniel Veilleux 0:442c7a6f1978 244
Daniel Veilleux 0:442c7a6f1978 245 err = m_ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
Daniel Veilleux 0:442c7a6f1978 246 if (BLE_ERROR_NONE != err) {
Daniel Veilleux 0:442c7a6f1978 247 error(err, __LINE__);
Daniel Veilleux 0:442c7a6f1978 248 }
Daniel Veilleux 0:442c7a6f1978 249
Daniel Veilleux 0:442c7a6f1978 250 m_ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
Daniel Veilleux 0:442c7a6f1978 251
Daniel Veilleux 0:442c7a6f1978 252 err = m_ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
Daniel Veilleux 0:442c7a6f1978 253 (const uint8_t *)SHORT_NAME,
Daniel Veilleux 0:442c7a6f1978 254 (sizeof(SHORT_NAME) - 1));
Daniel Veilleux 0:442c7a6f1978 255 if (BLE_ERROR_NONE != err) {
Daniel Veilleux 0:442c7a6f1978 256 error(err, __LINE__);
Daniel Veilleux 0:442c7a6f1978 257 }
Daniel Veilleux 0:442c7a6f1978 258
Daniel Veilleux 0:442c7a6f1978 259 err = m_ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
Daniel Veilleux 0:442c7a6f1978 260 (const uint8_t *)UARTServiceUUID_reversed,
Daniel Veilleux 0:442c7a6f1978 261 sizeof(UARTServiceUUID_reversed));
Daniel Veilleux 0:442c7a6f1978 262 if (BLE_ERROR_NONE != err) {
Daniel Veilleux 0:442c7a6f1978 263 error(err, __LINE__);
Daniel Veilleux 0:442c7a6f1978 264 }
Daniel Veilleux 0:442c7a6f1978 265
Daniel Veilleux 0:442c7a6f1978 266 m_ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(ADV_INTERVAL_MS));
Daniel Veilleux 0:442c7a6f1978 267 m_ble.startAdvertising();
Daniel Veilleux 0:442c7a6f1978 268
Daniel Veilleux 0:442c7a6f1978 269 // Create a UARTService object (GATT stuff).
Daniel Veilleux 0:442c7a6f1978 270 UARTService uartService(m_ble);
Daniel Veilleux 0:442c7a6f1978 271 m_uart_service_ptr = &uartService;
Daniel Veilleux 0:442c7a6f1978 272
Daniel Veilleux 0:442c7a6f1978 273 while (true) {
Daniel Veilleux 0:442c7a6f1978 274 m_ble.waitForEvent();
kbahar3 9:3a6fb5dd522c 275
kbahar3 9:3a6fb5dd522c 276 #ifdef Pressure
kbahar3 9:3a6fb5dd522c 277 /*
kbahar3 9:3a6fb5dd522c 278 //Read color Portion from the IC
kbahar3 9:3a6fb5dd522c 279 i2c.write(Press_addr_w, &Press_Addr_ReadData, 1, RepStart);
kbahar3 9:3a6fb5dd522c 280 i2c.read(Press_addr_r, &Press_Content_ReadData[0], 3, NoRepStart);
kbahar3 9:3a6fb5dd522c 281
kbahar3 9:3a6fb5dd522c 282 BM1383_Var = (Press_Content_ReadData[0]<<3) | (Press_Content_ReadData[1] >> 5);
kbahar3 9:3a6fb5dd522c 283 BM1383_Deci = ((Press_Content_ReadData[1] & 0x1f) << 6 | ((Press_Content_ReadData[2] >> 2)));
kbahar3 9:3a6fb5dd522c 284 BM1383_Deci = (float)BM1383_Deci* 0.00048828125; //0.00048828125 = 2^-11
kbahar3 9:3a6fb5dd522c 285 BM1383_Pres_Conv_Out = (BM1383_Var + BM1383_Deci); //question pending here...
kbahar3 9:3a6fb5dd522c 286 */
kbahar3 9:3a6fb5dd522c 287 #endif
kbahar3 9:3a6fb5dd522c 288
kbahar3 9:3a6fb5dd522c 289
kbahar3 9:3a6fb5dd522c 290 if(printQue) //Handle Data Written Interrupt
kbahar3 9:3a6fb5dd522c 291 {
kbahar3 9:3a6fb5dd522c 292 BTLE_DataWrittenHandler();
kbahar3 9:3a6fb5dd522c 293 printQue = 0;
kbahar3 9:3a6fb5dd522c 294 }
Daniel Veilleux 0:442c7a6f1978 295 }
kbahar3 9:3a6fb5dd522c 296 }
kbahar3 9:3a6fb5dd522c 297
kbahar3 9:3a6fb5dd522c 298 void BTLE_DataWrittenHandler(){
kbahar3 9:3a6fb5dd522c 299 int i;
kbahar3 9:3a6fb5dd522c 300 if (ReceivedValue.length() == 1) {
kbahar3 9:3a6fb5dd522c 301 switch (ReceivedValue[0]) {
kbahar3 9:3a6fb5dd522c 302 case 'F':
kbahar3 9:3a6fb5dd522c 303 len = snprintf((char*) buf, MAX_REPLY_LEN, " Pres= %0.2f hPa", BM1383_Pres_Conv_Out);
kbahar3 9:3a6fb5dd522c 304 break;
kbahar3 9:3a6fb5dd522c 305
kbahar3 9:3a6fb5dd522c 306 case 'r': //System Reset
kbahar3 9:3a6fb5dd522c 307 NVIC_SystemReset(); //Q. What is this? no break statement?
kbahar3 9:3a6fb5dd522c 308 //A. SoftReset... No need for break because this resets the program...
kbahar3 9:3a6fb5dd522c 309 break;
kbahar3 9:3a6fb5dd522c 310
kbahar3 9:3a6fb5dd522c 311 default:
kbahar3 9:3a6fb5dd522c 312 len = snprintf((char*) buf, MAX_REPLY_LEN, "1b,ERROR");
kbahar3 9:3a6fb5dd522c 313 break;
kbahar3 9:3a6fb5dd522c 314 }
kbahar3 9:3a6fb5dd522c 315 }
kbahar3 9:3a6fb5dd522c 316 else if (ReceivedValue.length() > 1) {
kbahar3 9:3a6fb5dd522c 317
kbahar3 9:3a6fb5dd522c 318 //BTLE Interface Code, Added 5/29/2016
kbahar3 9:3a6fb5dd522c 319 for(i = 0; i < 29; i++)
kbahar3 9:3a6fb5dd522c 320 {
kbahar3 9:3a6fb5dd522c 321 FormattedData[i] = '\0';
kbahar3 9:3a6fb5dd522c 322 }
kbahar3 9:3a6fb5dd522c 323
kbahar3 9:3a6fb5dd522c 324 if(ReceivedValue.compare(0,4,"CAL0") == 0)
kbahar3 9:3a6fb5dd522c 325 {
kbahar3 9:3a6fb5dd522c 326 pressureCurr = 0;
kbahar3 9:3a6fb5dd522c 327 i2c.write(Press_addr_w, &PWR_ON[0], 2, false);
kbahar3 9:3a6fb5dd522c 328 i2c.write(Press_addr_w, &SLEEP_OFF[0], 2, false);
kbahar3 9:3a6fb5dd522c 329 i2c.write(Press_addr_w, &Mode_Control[0], 2, false);
kbahar3 9:3a6fb5dd522c 330 for(i = 0; i < 10; i++)
kbahar3 9:3a6fb5dd522c 331 {
kbahar3 9:3a6fb5dd522c 332 #ifdef Pressure
kbahar3 9:3a6fb5dd522c 333 wait_ms(200);
kbahar3 9:3a6fb5dd522c 334 i2c.write(Press_addr_w, &Press_Addr_ReadData, 1, RepStart);
kbahar3 9:3a6fb5dd522c 335 i2c.read(Press_addr_r, &Press_Content_ReadData[0], 3, NoRepStart);
kbahar3 9:3a6fb5dd522c 336 BM1383_TempPressure = (Press_Content_ReadData[0]<<14)|(Press_Content_ReadData[1]<<6)|(Press_Content_ReadData[2]);
kbahar3 9:3a6fb5dd522c 337 BM1383_Pres_Conv_Out = (float)BM1383_TempPressure / (float)2048;
kbahar3 9:3a6fb5dd522c 338 #endif
kbahar3 9:3a6fb5dd522c 339 pressureCurr += BM1383_Pres_Conv_Out;
kbahar3 9:3a6fb5dd522c 340 }
kbahar3 9:3a6fb5dd522c 341 i2c.write(Press_addr_w, &PWR_OFF[0], 2, false);
kbahar3 9:3a6fb5dd522c 342 i2c.write(Press_addr_w, &SLEEP_ON[0], 2, false);
kbahar3 9:3a6fb5dd522c 343 BM1383_Pres_0Level = pressureCurr/10;
kbahar3 9:3a6fb5dd522c 344 len = snprintf((char*) buf, MAX_REPLY_LEN, "BaseLvl=%.3f",BM1383_Pres_0Level);
kbahar3 9:3a6fb5dd522c 345 }
kbahar3 9:3a6fb5dd522c 346
kbahar3 9:3a6fb5dd522c 347 else if(ReceivedValue.compare(0,4,"CAL1") == 0)
kbahar3 9:3a6fb5dd522c 348 {
kbahar3 9:3a6fb5dd522c 349 pressureCurr = 0;
kbahar3 9:3a6fb5dd522c 350 i2c.write(Press_addr_w, &PWR_ON[0], 2, false);
kbahar3 9:3a6fb5dd522c 351 i2c.write(Press_addr_w, &SLEEP_OFF[0], 2, false);
kbahar3 9:3a6fb5dd522c 352 i2c.write(Press_addr_w, &Mode_Control[0], 2, false);
kbahar3 9:3a6fb5dd522c 353 for(i = 0; i < 10; i++)
kbahar3 9:3a6fb5dd522c 354 {
kbahar3 9:3a6fb5dd522c 355 #ifdef Pressure
kbahar3 9:3a6fb5dd522c 356 wait_ms(200);
kbahar3 9:3a6fb5dd522c 357 i2c.write(Press_addr_w, &Press_Addr_ReadData, 1, RepStart);
kbahar3 9:3a6fb5dd522c 358 i2c.read(Press_addr_r, &Press_Content_ReadData[0], 3, NoRepStart);
kbahar3 9:3a6fb5dd522c 359 BM1383_TempPressure = (Press_Content_ReadData[0]<<14)|(Press_Content_ReadData[1]<<6)|(Press_Content_ReadData[2]);
kbahar3 9:3a6fb5dd522c 360 BM1383_Pres_Conv_Out = (float)BM1383_TempPressure / (float)2048;
kbahar3 9:3a6fb5dd522c 361 #endif
kbahar3 9:3a6fb5dd522c 362 pressureCurr += BM1383_Pres_Conv_Out;
kbahar3 9:3a6fb5dd522c 363 }
kbahar3 9:3a6fb5dd522c 364 i2c.write(Press_addr_w, &PWR_OFF[0], 2, false);
kbahar3 9:3a6fb5dd522c 365 i2c.write(Press_addr_w, &SLEEP_ON[0], 2, false);
kbahar3 9:3a6fb5dd522c 366 BM1383_Pres_KnownLevel = pressureCurr/10;
kbahar3 9:3a6fb5dd522c 367 len = snprintf((char*) buf, MAX_REPLY_LEN, "KnownLv=%.3f",BM1383_Pres_KnownLevel);
kbahar3 9:3a6fb5dd522c 368 }
kbahar3 9:3a6fb5dd522c 369
kbahar3 9:3a6fb5dd522c 370
kbahar3 9:3a6fb5dd522c 371 else if(ReceivedValue.compare(0,4,"HEI?") == 0)
kbahar3 9:3a6fb5dd522c 372 {
kbahar3 9:3a6fb5dd522c 373 HeightKnown_Total = HeightKnown_Foot + HeightKnown_Inches;
kbahar3 9:3a6fb5dd522c 374 slope = (HeightKnown_Total - HeightBase) / (BM1383_Pres_KnownLevel - BM1383_Pres_0Level);
kbahar3 9:3a6fb5dd522c 375 yInt = HeightBase - (slope * BM1383_Pres_0Level);
kbahar3 9:3a6fb5dd522c 376 pressureCurr = 0;
kbahar3 9:3a6fb5dd522c 377 i2c.write(Press_addr_w, &PWR_ON[0], 2, false);
kbahar3 9:3a6fb5dd522c 378 i2c.write(Press_addr_w, &SLEEP_OFF[0], 2, false);
kbahar3 9:3a6fb5dd522c 379 i2c.write(Press_addr_w, &Mode_Control[0], 2, false);
kbahar3 9:3a6fb5dd522c 380 for(i = 0; i < 10; i++)
kbahar3 9:3a6fb5dd522c 381 {
kbahar3 9:3a6fb5dd522c 382 #ifdef Pressure
kbahar3 9:3a6fb5dd522c 383 wait_ms(200);
kbahar3 9:3a6fb5dd522c 384 i2c.write(Press_addr_w, &Press_Addr_ReadData, 1, RepStart);
kbahar3 9:3a6fb5dd522c 385 i2c.read(Press_addr_r, &Press_Content_ReadData[0], 3, NoRepStart);
kbahar3 9:3a6fb5dd522c 386 BM1383_TempPressure = (Press_Content_ReadData[0]<<14)|(Press_Content_ReadData[1]<<6)|(Press_Content_ReadData[2]);
kbahar3 9:3a6fb5dd522c 387 BM1383_Pres_Conv_Out = (float)BM1383_TempPressure / (float)2048;
kbahar3 9:3a6fb5dd522c 388 #endif
kbahar3 9:3a6fb5dd522c 389 pressureCurr += BM1383_Pres_Conv_Out;
kbahar3 9:3a6fb5dd522c 390 }
kbahar3 9:3a6fb5dd522c 391 i2c.write(Press_addr_w, &PWR_OFF[0], 2, false);
kbahar3 9:3a6fb5dd522c 392 i2c.write(Press_addr_w, &SLEEP_ON[0], 2, false);
kbahar3 9:3a6fb5dd522c 393 pressureCurr = pressureCurr/10;
kbahar3 9:3a6fb5dd522c 394
kbahar3 9:3a6fb5dd522c 395 HeightExtrapolated = ((pressureCurr * slope) + yInt)/12;
kbahar3 9:3a6fb5dd522c 396 //len = snprintf((char*) buf, MAX_REPLY_LEN, "Height=%f FT",HeightExtrapolated);
kbahar3 9:3a6fb5dd522c 397 //m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
kbahar3 9:3a6fb5dd522c 398
kbahar3 9:3a6fb5dd522c 399 HeightExtrapolated_Foot = int(HeightExtrapolated);
kbahar3 9:3a6fb5dd522c 400 HeightExtrapolated_Inches = (HeightExtrapolated - HeightExtrapolated_Foot)* 12;
kbahar3 9:3a6fb5dd522c 401 len = snprintf((char*) buf, MAX_REPLY_LEN, "H=%.0fFT, %.0fIN",HeightExtrapolated_Foot,HeightExtrapolated_Inches);
kbahar3 9:3a6fb5dd522c 402 }
kbahar3 9:3a6fb5dd522c 403
kbahar3 9:3a6fb5dd522c 404 else if(ReceivedValue.compare(0,2,"FT") == 0)
kbahar3 9:3a6fb5dd522c 405 {
kbahar3 9:3a6fb5dd522c 406 HeightKnown_Foot = (ReceivedValue[2]-48) * 12; //Convert ASCII to INT, then Convert to Inches
kbahar3 9:3a6fb5dd522c 407 sprintf(FormattedData, "%2.0f", HeightKnown_Foot);
kbahar3 9:3a6fb5dd522c 408 len = snprintf((char*) buf, MAX_REPLY_LEN, "FT = %s inchs", FormattedData);
kbahar3 9:3a6fb5dd522c 409 wait_ms(200);
kbahar3 9:3a6fb5dd522c 410 }
kbahar3 9:3a6fb5dd522c 411
kbahar3 9:3a6fb5dd522c 412 else if(ReceivedValue.compare(0,2,"IN") == 0)
kbahar3 9:3a6fb5dd522c 413 {
kbahar3 9:3a6fb5dd522c 414 if((ReceivedValue[3]>= 48) && (ReceivedValue[3]<=57)){
kbahar3 9:3a6fb5dd522c 415 HeightKnown_Inches = ((ReceivedValue[2]-48)*10) + (ReceivedValue[3]-48); //Convert ASCII to INT, then Convert to Inches
kbahar3 9:3a6fb5dd522c 416
kbahar3 9:3a6fb5dd522c 417 }
kbahar3 9:3a6fb5dd522c 418 else{
kbahar3 9:3a6fb5dd522c 419 HeightKnown_Inches = (ReceivedValue[2]-48); //Convert ASCII to INT
kbahar3 9:3a6fb5dd522c 420 }
kbahar3 9:3a6fb5dd522c 421 sprintf(FormattedData, "%2.0f", HeightKnown_Inches);
kbahar3 9:3a6fb5dd522c 422 len = snprintf((char*) buf, MAX_REPLY_LEN, "IN = %s inchs", FormattedData);
kbahar3 9:3a6fb5dd522c 423 wait_ms(200);
kbahar3 9:3a6fb5dd522c 424 }
kbahar3 9:3a6fb5dd522c 425
kbahar3 9:3a6fb5dd522c 426 else
kbahar3 9:3a6fb5dd522c 427 {
kbahar3 9:3a6fb5dd522c 428 len = snprintf((char*) buf, MAX_REPLY_LEN, "??? = %s", ReceivedValue.c_str());
kbahar3 9:3a6fb5dd522c 429 //m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
kbahar3 9:3a6fb5dd522c 430 }
kbahar3 9:3a6fb5dd522c 431
kbahar3 9:3a6fb5dd522c 432 }
kbahar3 9:3a6fb5dd522c 433 else
kbahar3 9:3a6fb5dd522c 434 {
kbahar3 9:3a6fb5dd522c 435 len = snprintf((char*) buf, MAX_REPLY_LEN, "ERR:NUL");
kbahar3 9:3a6fb5dd522c 436 }
kbahar3 9:3a6fb5dd522c 437 m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
kbahar3 5:d39ffc5638a3 438 }