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:
Wed Jul 22 01:05:56 2015 +0000
Revision:
2:c7b9d588c80f
Parent:
1:2c0ab5cd1a7f
Child:
3:c3ee9d663fb8
Switched things to work with the shield... now DALS is actually RPR-0521

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Daniel Veilleux 0:442c7a6f1978 1 /* mbed Microcontroller Library
Daniel Veilleux 0:442c7a6f1978 2 * Copyright (c) 2006-2013 ARM Limited
Daniel Veilleux 0:442c7a6f1978 3 *
Daniel Veilleux 0:442c7a6f1978 4 * Licensed under the Apache License, Version 2.0 (the "License");
Daniel Veilleux 0:442c7a6f1978 5 * you may not use this file except in compliance with the License.
Daniel Veilleux 0:442c7a6f1978 6 * You may obtain a copy of the License at
Daniel Veilleux 0:442c7a6f1978 7 *
Daniel Veilleux 0:442c7a6f1978 8 * http://www.apache.org/licenses/LICENSE-2.0
Daniel Veilleux 0:442c7a6f1978 9 *
Daniel Veilleux 0:442c7a6f1978 10 * Unless required by applicable law or agreed to in writing, software
Daniel Veilleux 0:442c7a6f1978 11 * distributed under the License is distributed on an "AS IS" BASIS,
Daniel Veilleux 0:442c7a6f1978 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Daniel Veilleux 0:442c7a6f1978 13 * See the License for the specific language governing permissions and
Daniel Veilleux 0:442c7a6f1978 14 * limitations under the License.
Daniel Veilleux 0:442c7a6f1978 15 */
kbahar3 2:c7b9d588c80f 16
kbahar3 2:c7b9d588c80f 17 /*
kbahar3 2:c7b9d588c80f 18 * Added Functions for ROHM's Multi-Sensor Shield Board
kbahar3 2:c7b9d588c80f 19 * Supports the following Sensor Devices
kbahar3 2:c7b9d588c80f 20 * > BDE0600G Temperature Sensor
kbahar3 2:c7b9d588c80f 21 * > BM1383GLV Pressure Sensor
kbahar3 2:c7b9d588c80f 22 * > BU52014 Hall Sensor
kbahar3 2:c7b9d588c80f 23 * > ML8511 UV Sensor
kbahar3 2:c7b9d588c80f 24 * > RPR-0521 ALS/PROX Sensor
kbahar3 2:c7b9d588c80f 25 * > BH1745NUC Color Sensor
kbahar3 2:c7b9d588c80f 26 * > KMX62 Accel/Mag Sensor
kbahar3 2:c7b9d588c80f 27 * > KX122 Accel Sensor
kbahar3 2:c7b9d588c80f 28 * > KXG03 Gyro (Currently Unavailable as IC hasn't docked yet)
kbahar3 2:c7b9d588c80f 29 *
kbahar3 2:c7b9d588c80f 30 * New Code:
kbahar3 2:c7b9d588c80f 31 * Added a Section in "Main" to act as initialization
kbahar3 2:c7b9d588c80f 32 * Added to the "Periodic Callback" to read sensor data and return to Phone/Host
kbahar3 2:c7b9d588c80f 33 */
kbahar3 2:c7b9d588c80f 34
Daniel Veilleux 0:442c7a6f1978 35
kbahar3 2:c7b9d588c80f 36 //#define AnalogALS //BH1620 //Change 0: Remove this completely
kbahar3 1:2c0ab5cd1a7f 37 #define AnalogTemp //BDE0600
kbahar3 1:2c0ab5cd1a7f 38 #define AnalogUV //ML8511
kbahar3 1:2c0ab5cd1a7f 39 #define HallSensor //BU52011 //Change 1: Change to use GPIO for BU52014
kbahar3 2:c7b9d588c80f 40 #define RPR0521 //RPR0521 //Change 2: Remove This and add in the RPR-0521
kbahar3 1:2c0ab5cd1a7f 41 //Change 3: Add Code For BH1745, KX022, BM1383GLV, KMX62
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"
Daniel Veilleux 0:442c7a6f1978 48
kbahar3 1:2c0ab5cd1a7f 49 #define MAX_REPLY_LEN (UARTService::BLE_UART_SERVICE_MAX_DATA_LEN) //Actually equal to 20
kbahar3 1:2c0ab5cd1a7f 50 #define SENSOR_READ_INTERVAL_S (2.0F)
Daniel Veilleux 0:442c7a6f1978 51 #define ADV_INTERVAL_MS (1000UL)
Daniel Veilleux 0:442c7a6f1978 52 #define UART_BAUD_RATE (19200UL)
Daniel Veilleux 0:442c7a6f1978 53 #define DEVICE_NAME ("DEMO SENSOR") // This can be read AFTER connecting to the device.
kbahar3 1:2c0ab5cd1a7f 54 #define SHORT_NAME ("ROHMKRIS") // Keep this short: max 8 chars if a 128bit UUID is also advertised.
Daniel Veilleux 0:442c7a6f1978 55
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 1:2c0ab5cd1a7f 59 void PBTrigger(); //Interrupt function for PB4
Daniel Veilleux 0:442c7a6f1978 60
kbahar3 1:2c0ab5cd1a7f 61 // Global Variables
Daniel Veilleux 0:442c7a6f1978 62 BLEDevice m_ble;
Daniel Veilleux 0:442c7a6f1978 63 Serial m_serial_port(p9, p11); // TX pin, RX pin
Daniel Veilleux 0:442c7a6f1978 64 DigitalOut m_cmd_led(LED1);
Daniel Veilleux 0:442c7a6f1978 65 DigitalOut m_error_led(LED2);
Daniel Veilleux 0:442c7a6f1978 66 UARTService *m_uart_service_ptr;
kbahar3 1:2c0ab5cd1a7f 67 DigitalIn testButton(p20);
kbahar3 1:2c0ab5cd1a7f 68 InterruptIn sw4Press(p20);
kbahar3 1:2c0ab5cd1a7f 69 I2C i2c(p30,p7);
Daniel Veilleux 0:442c7a6f1978 70
kbahar3 1:2c0ab5cd1a7f 71 //Sensor Variables
kbahar3 2:c7b9d588c80f 72 /*
kbahar3 2:c7b9d588c80f 73 AnalogIn BH1620_ALS(p1); //No Analog ALS on the shield
kbahar3 1:2c0ab5cd1a7f 74 uint16_t BH1620_ALS_value;
kbahar3 1:2c0ab5cd1a7f 75 float BH1620_output;
kbahar3 2:c7b9d588c80f 76 */
kbahar3 1:2c0ab5cd1a7f 77
kbahar3 2:c7b9d588c80f 78 AnalogIn BDE0600_Temp(p3); //p2 on the prior evk, p3 on the shield
kbahar3 1:2c0ab5cd1a7f 79 uint16_t BDE0600_Temp_value;
kbahar3 1:2c0ab5cd1a7f 80 float BDE0600_output;
kbahar3 1:2c0ab5cd1a7f 81
kbahar3 2:c7b9d588c80f 82 AnalogIn ML8511_UV(p5); //p3 on prior EVK, p5 on the shield
kbahar3 1:2c0ab5cd1a7f 83 uint16_t ML8511_UV_value;
kbahar3 1:2c0ab5cd1a7f 84 float ML8511_output;
kbahar3 1:2c0ab5cd1a7f 85
kbahar3 2:c7b9d588c80f 86 DigitalIn Hall_GPIO0(p14); //
kbahar3 2:c7b9d588c80f 87 DigitalIn Hall_GPIO1(p15); //
kbahar3 1:2c0ab5cd1a7f 88 int Hall_Return1;
kbahar3 1:2c0ab5cd1a7f 89 int Hall_Return0;
kbahar3 1:2c0ab5cd1a7f 90
kbahar3 2:c7b9d588c80f 91 bool RepStart = true;
kbahar3 2:c7b9d588c80f 92 bool NoRepStart = false;
kbahar3 2:c7b9d588c80f 93
kbahar3 2:c7b9d588c80f 94 #ifdef RPR0521
kbahar3 2:c7b9d588c80f 95 int RPR0521_addr_w = 0x70; //7bit addr = 0x38, with write bit 0
kbahar3 2:c7b9d588c80f 96 int RPR0521_addr_r = 0x71; //7bit addr = 0x38, with read bit 1
kbahar3 2:c7b9d588c80f 97
kbahar3 2:c7b9d588c80f 98 char RPR0521_ModeControl[2] = {0x41, 0xE6};
kbahar3 2:c7b9d588c80f 99 char RPR0521_ALSPSControl[2] = {0x42, 0x03};
kbahar3 2:c7b9d588c80f 100 char RPR0521_Persist[2] = {0x43, 0x20};
kbahar3 2:c7b9d588c80f 101 char RPR0521_Addr_ReadData = 0x44;
kbahar3 2:c7b9d588c80f 102 char RPR0521_Content_ReadData[6];
kbahar3 2:c7b9d588c80f 103
kbahar3 2:c7b9d588c80f 104 int RPR0521_PS_RAWOUT = 0;
kbahar3 2:c7b9d588c80f 105 float RPR0521_PS_OUT = 0;
kbahar3 2:c7b9d588c80f 106 int RPR0521_ALS_D0_RAWOUT = 0;
kbahar3 2:c7b9d588c80f 107 int RPR0521_ALS_D1_RAWOUT = 0;
kbahar3 2:c7b9d588c80f 108 float RPR0521_ALS_DataRatio = 0;
kbahar3 2:c7b9d588c80f 109 float RPR0521_ALS_OUT = 0;
kbahar3 2:c7b9d588c80f 110 #endif
Daniel Veilleux 0:442c7a6f1978 111
Daniel Veilleux 0:442c7a6f1978 112 /**
Daniel Veilleux 0:442c7a6f1978 113 * This callback is used whenever a disconnection occurs.
Daniel Veilleux 0:442c7a6f1978 114 */
Daniel Veilleux 0:442c7a6f1978 115 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
Daniel Veilleux 0:442c7a6f1978 116 {
Daniel Veilleux 0:442c7a6f1978 117 switch (reason) {
Daniel Veilleux 0:442c7a6f1978 118 case Gap::REMOTE_USER_TERMINATED_CONNECTION:
Daniel Veilleux 0:442c7a6f1978 119 DEBUG("Disconnected (REMOTE_USER_TERMINATED_CONNECTION)\n\r");
Daniel Veilleux 0:442c7a6f1978 120 break;
Daniel Veilleux 0:442c7a6f1978 121 case Gap::LOCAL_HOST_TERMINATED_CONNECTION:
Daniel Veilleux 0:442c7a6f1978 122 DEBUG("Disconnected (LOCAL_HOST_TERMINATED_CONNECTION)\n\r");
Daniel Veilleux 0:442c7a6f1978 123 break;
Daniel Veilleux 0:442c7a6f1978 124 case Gap::CONN_INTERVAL_UNACCEPTABLE:
Daniel Veilleux 0:442c7a6f1978 125 DEBUG("Disconnected (CONN_INTERVAL_UNACCEPTABLE)\n\r");
Daniel Veilleux 0:442c7a6f1978 126 break;
Daniel Veilleux 0:442c7a6f1978 127 }
Daniel Veilleux 0:442c7a6f1978 128
Daniel Veilleux 0:442c7a6f1978 129 DEBUG("Restarting the advertising process\n\r");
Daniel Veilleux 0:442c7a6f1978 130 m_ble.startAdvertising();
Daniel Veilleux 0:442c7a6f1978 131 }
Daniel Veilleux 0:442c7a6f1978 132
Daniel Veilleux 0:442c7a6f1978 133
Daniel Veilleux 0:442c7a6f1978 134 /**
Daniel Veilleux 0:442c7a6f1978 135 * This callback is used whenever the host writes data to one of our GATT characteristics.
Daniel Veilleux 0:442c7a6f1978 136 */
Daniel Veilleux 0:442c7a6f1978 137 void dataWrittenCallback(const GattCharacteristicWriteCBParams *params)
Daniel Veilleux 0:442c7a6f1978 138 {
Daniel Veilleux 0:442c7a6f1978 139 // Ensure that initialization is finished and the host has written to the TX characteristic.
Daniel Veilleux 0:442c7a6f1978 140 if ((m_uart_service_ptr != NULL) && (params->charHandle == m_uart_service_ptr->getTXCharacteristicHandle())) {
Daniel Veilleux 0:442c7a6f1978 141 uint8_t buf[MAX_REPLY_LEN];
Daniel Veilleux 0:442c7a6f1978 142 uint32_t len = 0;
Daniel Veilleux 0:442c7a6f1978 143
Daniel Veilleux 0:442c7a6f1978 144 if (1 == params->len) {
Daniel Veilleux 0:442c7a6f1978 145 switch (params->data[0]) {
Daniel Veilleux 0:442c7a6f1978 146 case '0':
kbahar3 1:2c0ab5cd1a7f 147 m_cmd_led = m_cmd_led ^ 1;
kbahar3 1:2c0ab5cd1a7f 148 len = snprintf((char*) buf, MAX_REPLY_LEN, "OK... LED ON");
Daniel Veilleux 0:442c7a6f1978 149 break;
Daniel Veilleux 0:442c7a6f1978 150 case '1':
kbahar3 1:2c0ab5cd1a7f 151 m_cmd_led = m_cmd_led ^ 1;
kbahar3 1:2c0ab5cd1a7f 152 len = snprintf((char*) buf, MAX_REPLY_LEN, "OK... LED OFF");
Daniel Veilleux 0:442c7a6f1978 153 break;
Daniel Veilleux 0:442c7a6f1978 154 case 'a':
kbahar3 2:c7b9d588c80f 155 //len = snprintf((char*) buf, MAX_REPLY_LEN, "ALSRaw = %d", BH1620_ALS_value);
kbahar3 1:2c0ab5cd1a7f 156 break;
kbahar3 1:2c0ab5cd1a7f 157 case 'b':
kbahar3 2:c7b9d588c80f 158 //len = snprintf((char*) buf, MAX_REPLY_LEN, "ALS = %.2f lx", BH1620_output);
Daniel Veilleux 0:442c7a6f1978 159 break;
Daniel Veilleux 0:442c7a6f1978 160 default:
kbahar3 1:2c0ab5cd1a7f 161 len = snprintf((char*) buf, MAX_REPLY_LEN, "ERROR");
Daniel Veilleux 0:442c7a6f1978 162 break;
Daniel Veilleux 0:442c7a6f1978 163 }
Daniel Veilleux 0:442c7a6f1978 164 }
Daniel Veilleux 0:442c7a6f1978 165 else
Daniel Veilleux 0:442c7a6f1978 166 {
kbahar3 1:2c0ab5cd1a7f 167 len = snprintf((char*) buf, MAX_REPLY_LEN, "ERROR");
Daniel Veilleux 0:442c7a6f1978 168 }
Daniel Veilleux 0:442c7a6f1978 169
Daniel Veilleux 0:442c7a6f1978 170 m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
Daniel Veilleux 0:442c7a6f1978 171
Daniel Veilleux 0:442c7a6f1978 172 DEBUG("%d bytes received from host\n\r", params->len);
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 /**
Daniel Veilleux 0:442c7a6f1978 178 * This callback is used whenever a write to a GATT characteristic causes data to be sent to the host.
Daniel Veilleux 0:442c7a6f1978 179 */
Daniel Veilleux 0:442c7a6f1978 180 void dataSentCallback(unsigned count)
Daniel Veilleux 0:442c7a6f1978 181 {
Daniel Veilleux 0:442c7a6f1978 182 // NOTE: The count always seems to be 1 regardless of data.
Daniel Veilleux 0:442c7a6f1978 183 DEBUG("%d bytes sent to host\n\r", count);
Daniel Veilleux 0:442c7a6f1978 184 }
Daniel Veilleux 0:442c7a6f1978 185
Daniel Veilleux 0:442c7a6f1978 186
Daniel Veilleux 0:442c7a6f1978 187 /**
Daniel Veilleux 0:442c7a6f1978 188 * This callback is scheduled to be called periodically via a low-priority interrupt.
Daniel Veilleux 0:442c7a6f1978 189 */
Daniel Veilleux 0:442c7a6f1978 190 void periodicCallback(void)
Daniel Veilleux 0:442c7a6f1978 191 {
kbahar3 1:2c0ab5cd1a7f 192 uint8_t buf[MAX_REPLY_LEN];
kbahar3 1:2c0ab5cd1a7f 193 uint32_t len = 0;
kbahar3 1:2c0ab5cd1a7f 194
kbahar3 2:c7b9d588c80f 195
kbahar3 2:c7b9d588c80f 196 /*
kbahar3 2:c7b9d588c80f 197 #ifdef AnalogALS
kbahar3 1:2c0ab5cd1a7f 198 if (m_ble.getGapState().connected) {
kbahar3 1:2c0ab5cd1a7f 199 BH1620_ALS_value = BH1620_ALS.read_u16();
kbahar3 1:2c0ab5cd1a7f 200 BH1620_output = (float)BH1620_ALS_value * 1.543;
kbahar3 1:2c0ab5cd1a7f 201
kbahar3 1:2c0ab5cd1a7f 202 len = snprintf((char*) buf, MAX_REPLY_LEN, "ALS = %.2f lx", BH1620_output);
kbahar3 1:2c0ab5cd1a7f 203 m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
kbahar3 1:2c0ab5cd1a7f 204 }
kbahar3 1:2c0ab5cd1a7f 205 #endif
kbahar3 2:c7b9d588c80f 206 */
kbahar3 1:2c0ab5cd1a7f 207
kbahar3 1:2c0ab5cd1a7f 208 #ifdef AnalogTemp
kbahar3 1:2c0ab5cd1a7f 209 if (m_ble.getGapState().connected) {
kbahar3 1:2c0ab5cd1a7f 210 BDE0600_Temp_value = BDE0600_Temp.read_u16();
kbahar3 1:2c0ab5cd1a7f 211 BDE0600_output = (float)BDE0600_Temp_value * 0.00283; //(value * (2.9V/1024))
kbahar3 1:2c0ab5cd1a7f 212 BDE0600_output = (BDE0600_output-1.753)/(-0.01068) + 30;
kbahar3 1:2c0ab5cd1a7f 213
kbahar3 1:2c0ab5cd1a7f 214 len = snprintf((char*) buf, MAX_REPLY_LEN, "Temp = %.2f C", BDE0600_output);
kbahar3 1:2c0ab5cd1a7f 215 m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
kbahar3 1:2c0ab5cd1a7f 216
kbahar3 1:2c0ab5cd1a7f 217 }
kbahar3 1:2c0ab5cd1a7f 218 #endif
kbahar3 1:2c0ab5cd1a7f 219
kbahar3 1:2c0ab5cd1a7f 220 #ifdef AnalogUV
kbahar3 1:2c0ab5cd1a7f 221 if (m_ble.getGapState().connected) {
kbahar3 1:2c0ab5cd1a7f 222 ML8511_UV_value = ML8511_UV.read_u16();
kbahar3 1:2c0ab5cd1a7f 223 ML8511_output = (float)ML8511_UV_value * 0.00283; //(value * (2.9V/1024)) //Note to self: when playing with this, a negative value is seen... Honestly, I think this has to do with my ADC converstion...
kbahar3 1:2c0ab5cd1a7f 224 ML8511_output = (ML8511_output-2.2)/(0.129) + 15; // Added +5 to the offset so when inside (aka, no UV, readings show 0)... this is the wrong approach... and the readings don't make sense... Fix this.
kbahar3 1:2c0ab5cd1a7f 225
kbahar3 1:2c0ab5cd1a7f 226 len = snprintf((char*) buf, MAX_REPLY_LEN, "UV = %.1f mW/cm2", ML8511_output);
kbahar3 1:2c0ab5cd1a7f 227 m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
kbahar3 1:2c0ab5cd1a7f 228 }
kbahar3 1:2c0ab5cd1a7f 229 #endif
kbahar3 1:2c0ab5cd1a7f 230
kbahar3 1:2c0ab5cd1a7f 231 #ifdef HallSensor
kbahar3 1:2c0ab5cd1a7f 232 if (m_ble.getGapState().connected) {
kbahar3 1:2c0ab5cd1a7f 233 Hall_Return0 = Hall_GPIO0;
kbahar3 1:2c0ab5cd1a7f 234 Hall_Return1 = Hall_GPIO1;
kbahar3 1:2c0ab5cd1a7f 235
kbahar3 1:2c0ab5cd1a7f 236 len = snprintf((char*) buf, MAX_REPLY_LEN, "H0 = %d, H1 = %d", Hall_Return0, Hall_Return1);
kbahar3 1:2c0ab5cd1a7f 237 m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
kbahar3 1:2c0ab5cd1a7f 238 }
kbahar3 1:2c0ab5cd1a7f 239 #endif
kbahar3 1:2c0ab5cd1a7f 240
kbahar3 1:2c0ab5cd1a7f 241 #ifdef DigitalALS
kbahar3 1:2c0ab5cd1a7f 242 if (m_ble.getGapState().connected) {
kbahar3 1:2c0ab5cd1a7f 243 i2c.read(ALS_addr_r, ALS_ReturnData_raw, 2);
kbahar3 1:2c0ab5cd1a7f 244 ALS_Return = (ALS_ReturnData_raw[0]<<8) | ALS_ReturnData_raw[1];
kbahar3 1:2c0ab5cd1a7f 245 ALS_Return = ALS_Return/1.2;
kbahar3 1:2c0ab5cd1a7f 246
kbahar3 1:2c0ab5cd1a7f 247 len = snprintf((char*) buf, MAX_REPLY_LEN, "DALS= %0.2f lx", ALS_Return);
kbahar3 1:2c0ab5cd1a7f 248 m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
kbahar3 1:2c0ab5cd1a7f 249 }
kbahar3 1:2c0ab5cd1a7f 250 #endif
kbahar3 1:2c0ab5cd1a7f 251
kbahar3 2:c7b9d588c80f 252 #ifdef RPR0521
kbahar3 2:c7b9d588c80f 253 if (m_ble.getGapState().connected) {
kbahar3 2:c7b9d588c80f 254
kbahar3 2:c7b9d588c80f 255 i2c.write(RPR0521_addr_w, &RPR0521_Addr_ReadData, 1, RepStart);
kbahar3 2:c7b9d588c80f 256 i2c.read(RPR0521_addr_r, &RPR0521_Content_ReadData[0], 6, NoRepStart);
kbahar3 2:c7b9d588c80f 257
kbahar3 2:c7b9d588c80f 258 RPR0521_PS_RAWOUT = (RPR0521_Content_ReadData[1]<<8) | (RPR0521_Content_ReadData[0]);
kbahar3 2:c7b9d588c80f 259 RPR0521_ALS_D0_RAWOUT = (RPR0521_Content_ReadData[3]<<8) | (RPR0521_Content_ReadData[2]);
kbahar3 2:c7b9d588c80f 260 RPR0521_ALS_D1_RAWOUT = (RPR0521_Content_ReadData[5]<<8) | (RPR0521_Content_ReadData[4]);
kbahar3 2:c7b9d588c80f 261 RPR0521_ALS_DataRatio = (float)RPR0521_ALS_D1_RAWOUT / (float)RPR0521_ALS_D0_RAWOUT;
kbahar3 2:c7b9d588c80f 262
kbahar3 2:c7b9d588c80f 263 if(RPR0521_ALS_DataRatio < 0.595){
kbahar3 2:c7b9d588c80f 264 RPR0521_ALS_OUT = (1.682*(float)RPR0521_ALS_D0_RAWOUT - 1.877*(float)RPR0521_ALS_D1_RAWOUT);
kbahar3 2:c7b9d588c80f 265 }
kbahar3 2:c7b9d588c80f 266 else if(RPR0521_ALS_DataRatio < 1.015){
kbahar3 2:c7b9d588c80f 267 RPR0521_ALS_OUT = (0.644*(float)RPR0521_ALS_D0_RAWOUT - 0.132*(float)RPR0521_ALS_D1_RAWOUT);
kbahar3 2:c7b9d588c80f 268 }
kbahar3 2:c7b9d588c80f 269 else if(RPR0521_ALS_DataRatio < 1.352){
kbahar3 2:c7b9d588c80f 270 RPR0521_ALS_OUT = (0.756*(float)RPR0521_ALS_D0_RAWOUT - 0.243*(float)RPR0521_ALS_D1_RAWOUT);
kbahar3 2:c7b9d588c80f 271 }
kbahar3 2:c7b9d588c80f 272 else if(RPR0521_ALS_DataRatio < 3.053){
kbahar3 2:c7b9d588c80f 273 RPR0521_ALS_OUT = (0.766*(float)RPR0521_ALS_D0_RAWOUT - 0.25*(float)RPR0521_ALS_D1_RAWOUT);
kbahar3 2:c7b9d588c80f 274 }
kbahar3 2:c7b9d588c80f 275 else{
kbahar3 2:c7b9d588c80f 276 RPR0521_ALS_OUT = 0;
kbahar3 2:c7b9d588c80f 277 }
kbahar3 2:c7b9d588c80f 278
kbahar3 2:c7b9d588c80f 279 len = snprintf((char*) buf, MAX_REPLY_LEN, "DALS= %0.2f lx", RPR0521_ALS_OUT);
kbahar3 2:c7b9d588c80f 280 m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
kbahar3 2:c7b9d588c80f 281 }
kbahar3 2:c7b9d588c80f 282 #endif
kbahar3 1:2c0ab5cd1a7f 283
kbahar3 1:2c0ab5cd1a7f 284 if (m_ble.getGapState().connected) {
kbahar3 1:2c0ab5cd1a7f 285 len = snprintf((char*) buf, MAX_REPLY_LEN, " "); //Print and Extra Line to show new data
kbahar3 1:2c0ab5cd1a7f 286 m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
kbahar3 1:2c0ab5cd1a7f 287 }
Daniel Veilleux 0:442c7a6f1978 288 }
Daniel Veilleux 0:442c7a6f1978 289
Daniel Veilleux 0:442c7a6f1978 290
Daniel Veilleux 0:442c7a6f1978 291 void error(ble_error_t err, uint32_t line)
Daniel Veilleux 0:442c7a6f1978 292 {
Daniel Veilleux 0:442c7a6f1978 293 m_error_led = 1;
Daniel Veilleux 0:442c7a6f1978 294 DEBUG("Error %d on line number %d\n\r", err, line);
Daniel Veilleux 0:442c7a6f1978 295 }
Daniel Veilleux 0:442c7a6f1978 296
kbahar3 1:2c0ab5cd1a7f 297 void PBTrigger()
kbahar3 1:2c0ab5cd1a7f 298 {
kbahar3 1:2c0ab5cd1a7f 299 uint8_t buf[MAX_REPLY_LEN];
kbahar3 1:2c0ab5cd1a7f 300 uint32_t len = 0;
kbahar3 1:2c0ab5cd1a7f 301
kbahar3 1:2c0ab5cd1a7f 302 m_cmd_led = !m_cmd_led;
kbahar3 1:2c0ab5cd1a7f 303
kbahar3 2:c7b9d588c80f 304
kbahar3 1:2c0ab5cd1a7f 305 if (m_ble.getGapState().connected) {
kbahar3 2:c7b9d588c80f 306 /*
kbahar3 1:2c0ab5cd1a7f 307 BH1620_ALS_value = BH1620_ALS.read_u16();
kbahar3 1:2c0ab5cd1a7f 308 BH1620_output = (float)BH1620_ALS_value * 1.543;
kbahar3 1:2c0ab5cd1a7f 309
kbahar3 1:2c0ab5cd1a7f 310 len = snprintf((char*) buf, MAX_REPLY_LEN, "ALS = %.2f lx", BH1620_output);
kbahar3 1:2c0ab5cd1a7f 311 m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len);
kbahar3 2:c7b9d588c80f 312 */
kbahar3 1:2c0ab5cd1a7f 313 }
kbahar3 1:2c0ab5cd1a7f 314 }
Daniel Veilleux 0:442c7a6f1978 315
Daniel Veilleux 0:442c7a6f1978 316 int main(void)
Daniel Veilleux 0:442c7a6f1978 317 {
Daniel Veilleux 0:442c7a6f1978 318 ble_error_t err;
Daniel Veilleux 0:442c7a6f1978 319 Ticker ticker;
Daniel Veilleux 0:442c7a6f1978 320
Daniel Veilleux 0:442c7a6f1978 321 m_serial_port.baud(UART_BAUD_RATE);
Daniel Veilleux 0:442c7a6f1978 322
kbahar3 2:c7b9d588c80f 323 DEBUG("Initialising...\n\r");
Daniel Veilleux 0:442c7a6f1978 324
Daniel Veilleux 0:442c7a6f1978 325 m_cmd_led = 0;
Daniel Veilleux 0:442c7a6f1978 326 m_error_led = 0;
kbahar3 2:c7b9d588c80f 327 //BH1620_ALS_value = 0;
Daniel Veilleux 0:442c7a6f1978 328
Daniel Veilleux 0:442c7a6f1978 329 ticker.attach(periodicCallback, SENSOR_READ_INTERVAL_S);
Daniel Veilleux 0:442c7a6f1978 330
kbahar3 1:2c0ab5cd1a7f 331 sw4Press.fall(&PBTrigger);
kbahar3 1:2c0ab5cd1a7f 332
kbahar3 2:c7b9d588c80f 333 #ifdef RPR0521
kbahar3 2:c7b9d588c80f 334 // 1. Mode Control (0x41), write (0xC6): ALS EN, PS EN, 100ms measurement for ALS and PS, PS_PULSE=1
kbahar3 2:c7b9d588c80f 335 // 2. ALS_PS_CONTROL (0x42), write (0x03): LED Current = 200mA
kbahar3 2:c7b9d588c80f 336 // 3. PERSIST (0x43), write (0x20): PS Gain x4
kbahar3 2:c7b9d588c80f 337 i2c.write(RPR0521_addr_w, &RPR0521_ModeControl[0], 2, false);
kbahar3 2:c7b9d588c80f 338 i2c.write(RPR0521_addr_w, &RPR0521_ALSPSControl[0], 2, false);
kbahar3 2:c7b9d588c80f 339 i2c.write(RPR0521_addr_w, &RPR0521_Persist[0], 2, false);
kbahar3 1:2c0ab5cd1a7f 340 #endif
kbahar3 1:2c0ab5cd1a7f 341
kbahar3 2:c7b9d588c80f 342 //Start BTLE Initialization Section
Daniel Veilleux 0:442c7a6f1978 343 m_ble.init();
Daniel Veilleux 0:442c7a6f1978 344 m_ble.onDisconnection(disconnectionCallback);
Daniel Veilleux 0:442c7a6f1978 345 m_ble.onDataWritten(dataWrittenCallback);
Daniel Veilleux 0:442c7a6f1978 346 m_ble.onDataSent(dataSentCallback);
Daniel Veilleux 0:442c7a6f1978 347
Daniel Veilleux 0:442c7a6f1978 348 // Set the TX power in dBm units.
Daniel Veilleux 0:442c7a6f1978 349 // Possible values (in decreasing order): 4, 0, -4, -8, -12, -16, -20.
Daniel Veilleux 0:442c7a6f1978 350 err = m_ble.setTxPower(4);
Daniel Veilleux 0:442c7a6f1978 351 if (BLE_ERROR_NONE != err) {
Daniel Veilleux 0:442c7a6f1978 352 error(err, __LINE__);
Daniel Veilleux 0:442c7a6f1978 353 }
Daniel Veilleux 0:442c7a6f1978 354
Daniel Veilleux 0:442c7a6f1978 355 // Setup advertising (GAP stuff).
Daniel Veilleux 0:442c7a6f1978 356 err = m_ble.setDeviceName(DEVICE_NAME);
Daniel Veilleux 0:442c7a6f1978 357 if (BLE_ERROR_NONE != err) {
Daniel Veilleux 0:442c7a6f1978 358 error(err, __LINE__);
Daniel Veilleux 0:442c7a6f1978 359 }
Daniel Veilleux 0:442c7a6f1978 360
Daniel Veilleux 0:442c7a6f1978 361 err = m_ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
Daniel Veilleux 0:442c7a6f1978 362 if (BLE_ERROR_NONE != err) {
Daniel Veilleux 0:442c7a6f1978 363 error(err, __LINE__);
Daniel Veilleux 0:442c7a6f1978 364 }
Daniel Veilleux 0:442c7a6f1978 365
Daniel Veilleux 0:442c7a6f1978 366 m_ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
Daniel Veilleux 0:442c7a6f1978 367
Daniel Veilleux 0:442c7a6f1978 368 err = m_ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
Daniel Veilleux 0:442c7a6f1978 369 (const uint8_t *)SHORT_NAME,
Daniel Veilleux 0:442c7a6f1978 370 (sizeof(SHORT_NAME) - 1));
Daniel Veilleux 0:442c7a6f1978 371 if (BLE_ERROR_NONE != err) {
Daniel Veilleux 0:442c7a6f1978 372 error(err, __LINE__);
Daniel Veilleux 0:442c7a6f1978 373 }
Daniel Veilleux 0:442c7a6f1978 374
Daniel Veilleux 0:442c7a6f1978 375 err = m_ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
Daniel Veilleux 0:442c7a6f1978 376 (const uint8_t *)UARTServiceUUID_reversed,
Daniel Veilleux 0:442c7a6f1978 377 sizeof(UARTServiceUUID_reversed));
Daniel Veilleux 0:442c7a6f1978 378 if (BLE_ERROR_NONE != err) {
Daniel Veilleux 0:442c7a6f1978 379 error(err, __LINE__);
Daniel Veilleux 0:442c7a6f1978 380 }
Daniel Veilleux 0:442c7a6f1978 381
Daniel Veilleux 0:442c7a6f1978 382 m_ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(ADV_INTERVAL_MS));
Daniel Veilleux 0:442c7a6f1978 383 m_ble.startAdvertising();
Daniel Veilleux 0:442c7a6f1978 384
Daniel Veilleux 0:442c7a6f1978 385 // Create a UARTService object (GATT stuff).
Daniel Veilleux 0:442c7a6f1978 386 UARTService uartService(m_ble);
Daniel Veilleux 0:442c7a6f1978 387 m_uart_service_ptr = &uartService;
Daniel Veilleux 0:442c7a6f1978 388
Daniel Veilleux 0:442c7a6f1978 389 while (true) {
Daniel Veilleux 0:442c7a6f1978 390 m_ble.waitForEvent();
Daniel Veilleux 0:442c7a6f1978 391 }
Daniel Veilleux 0:442c7a6f1978 392 }