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 03:00:38 2016 +0000
Revision:
11:c989bcb5d497
Parent:
10:aa05fb52aaa3
Added Functionality for both SHLD0 and SHLD1 since these boards contain different pressure sensors

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