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
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:
main.cpp@5:d39ffc5638a3, 2015-09-24 (annotated)
- Committer:
- kbahar3
- Date:
- Thu Sep 24 22:23:31 2015 +0000
- Revision:
- 5:d39ffc5638a3
- Parent:
- 4:eabae2996ecc
- Child:
- 6:6860e53dc7ae
Example code for interfacing ROHM's Multi-Sensor Shield onto the Nordic Semiconductor nRF51-DK BTLE Host Board
Who changed what in which revision?
User | Revision | Line number | New 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 | 5:d39ffc5638a3 | 18 | * Added Functions for interfacing with 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 | 5:d39ffc5638a3 | 30 | * |
kbahar3 | 5:d39ffc5638a3 | 31 | * Last Upadtaed: 9/11/15 |
kbahar3 | 5:d39ffc5638a3 | 32 | * |
kbahar3 | 2:c7b9d588c80f | 33 | * New Code: |
kbahar3 | 2:c7b9d588c80f | 34 | * Added a Section in "Main" to act as initialization |
kbahar3 | 2:c7b9d588c80f | 35 | * Added to the "Periodic Callback" to read sensor data and return to Phone/Host |
kbahar3 | 5:d39ffc5638a3 | 36 | * |
kbahar3 | 5:d39ffc5638a3 | 37 | * Additional information about the ROHM MultiSensor Shield Board can be found at the following link: |
kbahar3 | 5:d39ffc5638a3 | 38 | * https://github.com/ROHMUSDC/ROHM_SensorPlatform_Multi-Sensor-Shield |
kbahar3 | 2:c7b9d588c80f | 39 | */ |
kbahar3 | 2:c7b9d588c80f | 40 | |
Daniel Veilleux |
0:442c7a6f1978 | 41 | |
kbahar3 | 2:c7b9d588c80f | 42 | //#define AnalogALS //BH1620 //Change 0: Remove this completely |
kbahar3 | 1:2c0ab5cd1a7f | 43 | #define AnalogTemp //BDE0600 |
kbahar3 | 1:2c0ab5cd1a7f | 44 | #define AnalogUV //ML8511 |
kbahar3 | 3:c3ee9d663fb8 | 45 | #define HallSensor //BU52011 //Change 1: Change to use GPIO for BU52014 |
kbahar3 | 2:c7b9d588c80f | 46 | #define RPR0521 //RPR0521 //Change 2: Remove This and add in the RPR-0521 |
kbahar3 | 5:d39ffc5638a3 | 47 | #define KMX62 //Change 3: Add Code For -BH1745-, -KX022, BM1383GLV, -KMX62- |
kbahar3 | 5:d39ffc5638a3 | 48 | #define color |
kbahar3 | 5:d39ffc5638a3 | 49 | #define KX022 |
kbahar3 | 5:d39ffc5638a3 | 50 | #define Pressure |
kbahar3 | 3:c3ee9d663fb8 | 51 | |
kbahar3 | 5:d39ffc5638a3 | 52 | |
kbahar3 | 3:c3ee9d663fb8 | 53 | |
kbahar3 | 1:2c0ab5cd1a7f | 54 | |
Daniel Veilleux |
0:442c7a6f1978 | 55 | #include "mbed.h" |
Daniel Veilleux |
0:442c7a6f1978 | 56 | #include "BLEDevice.h" |
Daniel Veilleux |
0:442c7a6f1978 | 57 | #include "UARTService.h" |
Daniel Veilleux |
0:442c7a6f1978 | 58 | #include "nrf_temp.h" |
kbahar3 | 2:c7b9d588c80f | 59 | #include "I2C.h" |
Daniel Veilleux |
0:442c7a6f1978 | 60 | |
kbahar3 | 5:d39ffc5638a3 | 61 | |
kbahar3 | 1:2c0ab5cd1a7f | 62 | #define MAX_REPLY_LEN (UARTService::BLE_UART_SERVICE_MAX_DATA_LEN) //Actually equal to 20 |
kbahar3 | 5:d39ffc5638a3 | 63 | #define SENSOR_READ_INTERVAL_S (5.0F) |
Daniel Veilleux |
0:442c7a6f1978 | 64 | #define ADV_INTERVAL_MS (1000UL) |
Daniel Veilleux |
0:442c7a6f1978 | 65 | #define UART_BAUD_RATE (19200UL) |
Daniel Veilleux |
0:442c7a6f1978 | 66 | #define DEVICE_NAME ("DEMO SENSOR") // This can be read AFTER connecting to the device. |
kbahar3 | 1:2c0ab5cd1a7f | 67 | #define SHORT_NAME ("ROHMKRIS") // Keep this short: max 8 chars if a 128bit UUID is also advertised. |
Daniel Veilleux |
0:442c7a6f1978 | 68 | |
Daniel Veilleux |
0:442c7a6f1978 | 69 | #define DEBUG(...) { m_serial_port.printf(__VA_ARGS__); } |
Daniel Veilleux |
0:442c7a6f1978 | 70 | |
kbahar3 | 1:2c0ab5cd1a7f | 71 | // Function Prototypes |
kbahar3 | 1:2c0ab5cd1a7f | 72 | void PBTrigger(); //Interrupt function for PB4 |
Daniel Veilleux |
0:442c7a6f1978 | 73 | |
kbahar3 | 1:2c0ab5cd1a7f | 74 | // Global Variables |
Daniel Veilleux |
0:442c7a6f1978 | 75 | BLEDevice m_ble; |
Daniel Veilleux |
0:442c7a6f1978 | 76 | Serial m_serial_port(p9, p11); // TX pin, RX pin |
Daniel Veilleux |
0:442c7a6f1978 | 77 | DigitalOut m_cmd_led(LED1); |
Daniel Veilleux |
0:442c7a6f1978 | 78 | DigitalOut m_error_led(LED2); |
Daniel Veilleux |
0:442c7a6f1978 | 79 | UARTService *m_uart_service_ptr; |
kbahar3 | 1:2c0ab5cd1a7f | 80 | DigitalIn testButton(p20); |
kbahar3 | 1:2c0ab5cd1a7f | 81 | InterruptIn sw4Press(p20); |
kbahar3 | 1:2c0ab5cd1a7f | 82 | I2C i2c(p30,p7); |
Daniel Veilleux |
0:442c7a6f1978 | 83 | |
kbahar3 | 1:2c0ab5cd1a7f | 84 | //Sensor Variables |
kbahar3 | 2:c7b9d588c80f | 85 | /* |
kbahar3 | 2:c7b9d588c80f | 86 | AnalogIn BH1620_ALS(p1); //No Analog ALS on the shield |
kbahar3 | 1:2c0ab5cd1a7f | 87 | uint16_t BH1620_ALS_value; |
kbahar3 | 1:2c0ab5cd1a7f | 88 | float BH1620_output; |
kbahar3 | 2:c7b9d588c80f | 89 | */ |
kbahar3 | 1:2c0ab5cd1a7f | 90 | |
kbahar3 | 2:c7b9d588c80f | 91 | AnalogIn BDE0600_Temp(p3); //p2 on the prior evk, p3 on the shield |
kbahar3 | 1:2c0ab5cd1a7f | 92 | uint16_t BDE0600_Temp_value; |
kbahar3 | 1:2c0ab5cd1a7f | 93 | float BDE0600_output; |
kbahar3 | 1:2c0ab5cd1a7f | 94 | |
kbahar3 | 2:c7b9d588c80f | 95 | AnalogIn ML8511_UV(p5); //p3 on prior EVK, p5 on the shield |
kbahar3 | 1:2c0ab5cd1a7f | 96 | uint16_t ML8511_UV_value; |
kbahar3 | 1:2c0ab5cd1a7f | 97 | float ML8511_output; |
kbahar3 | 1:2c0ab5cd1a7f | 98 | |
kbahar3 | 2:c7b9d588c80f | 99 | DigitalIn Hall_GPIO0(p14); // |
kbahar3 | 2:c7b9d588c80f | 100 | DigitalIn Hall_GPIO1(p15); // |
kbahar3 | 1:2c0ab5cd1a7f | 101 | int Hall_Return1; |
kbahar3 | 1:2c0ab5cd1a7f | 102 | int Hall_Return0; |
kbahar3 | 1:2c0ab5cd1a7f | 103 | |
kbahar3 | 2:c7b9d588c80f | 104 | bool RepStart = true; |
kbahar3 | 2:c7b9d588c80f | 105 | bool NoRepStart = false; |
kbahar3 | 2:c7b9d588c80f | 106 | |
kbahar3 | 5:d39ffc5638a3 | 107 | int i=1; |
kbahar3 | 5:d39ffc5638a3 | 108 | |
kbahar3 | 5:d39ffc5638a3 | 109 | |
kbahar3 | 5:d39ffc5638a3 | 110 | #ifdef RPR0521 //als digital |
kbahar3 | 2:c7b9d588c80f | 111 | int RPR0521_addr_w = 0x70; //7bit addr = 0x38, with write bit 0 |
kbahar3 | 2:c7b9d588c80f | 112 | int RPR0521_addr_r = 0x71; //7bit addr = 0x38, with read bit 1 |
kbahar3 | 2:c7b9d588c80f | 113 | |
kbahar3 | 2:c7b9d588c80f | 114 | char RPR0521_ModeControl[2] = {0x41, 0xE6}; |
kbahar3 | 2:c7b9d588c80f | 115 | char RPR0521_ALSPSControl[2] = {0x42, 0x03}; |
kbahar3 | 2:c7b9d588c80f | 116 | char RPR0521_Persist[2] = {0x43, 0x20}; |
kbahar3 | 2:c7b9d588c80f | 117 | char RPR0521_Addr_ReadData = 0x44; |
kbahar3 | 2:c7b9d588c80f | 118 | char RPR0521_Content_ReadData[6]; |
kbahar3 | 2:c7b9d588c80f | 119 | |
kbahar3 | 2:c7b9d588c80f | 120 | int RPR0521_PS_RAWOUT = 0; |
kbahar3 | 2:c7b9d588c80f | 121 | float RPR0521_PS_OUT = 0; |
kbahar3 | 2:c7b9d588c80f | 122 | int RPR0521_ALS_D0_RAWOUT = 0; |
kbahar3 | 2:c7b9d588c80f | 123 | int RPR0521_ALS_D1_RAWOUT = 0; |
kbahar3 | 2:c7b9d588c80f | 124 | float RPR0521_ALS_DataRatio = 0; |
kbahar3 | 2:c7b9d588c80f | 125 | float RPR0521_ALS_OUT = 0; |
kbahar3 | 2:c7b9d588c80f | 126 | #endif |
Daniel Veilleux |
0:442c7a6f1978 | 127 | |
kbahar3 | 3:c3ee9d663fb8 | 128 | #ifdef KMX62 |
kbahar3 | 3:c3ee9d663fb8 | 129 | int KMX62_addr_w = 0x1C; //7bit addr = 0x38, with write bit 0 |
kbahar3 | 3:c3ee9d663fb8 | 130 | int KMX62_addr_r = 0x1D; //7bit addr = 0x38, with read bit 1 |
kbahar3 | 3:c3ee9d663fb8 | 131 | |
kbahar3 | 3:c3ee9d663fb8 | 132 | char KMX62_CNTL2[2] = {0x3A, 0x5F}; |
kbahar3 | 3:c3ee9d663fb8 | 133 | char KMX62_Addr_Accel_ReadData = 0x0A; |
kbahar3 | 3:c3ee9d663fb8 | 134 | char KMX62_Content_Accel_ReadData[6]; |
kbahar3 | 3:c3ee9d663fb8 | 135 | char KMX62_Addr_Mag_ReadData = 0x10; |
kbahar3 | 3:c3ee9d663fb8 | 136 | char KMX62_Content_Mag_ReadData[6]; |
kbahar3 | 3:c3ee9d663fb8 | 137 | |
kbahar3 | 5:d39ffc5638a3 | 138 | short int MEMS_Accel_Xout = 0; |
kbahar3 | 5:d39ffc5638a3 | 139 | short int MEMS_Accel_Yout = 0; |
kbahar3 | 5:d39ffc5638a3 | 140 | short int MEMS_Accel_Zout = 0; |
kbahar3 | 5:d39ffc5638a3 | 141 | double MEMS_Accel_Conv_Xout = 0; |
kbahar3 | 5:d39ffc5638a3 | 142 | double MEMS_Accel_Conv_Yout = 0; |
kbahar3 | 5:d39ffc5638a3 | 143 | double MEMS_Accel_Conv_Zout = 0; |
kbahar3 | 5:d39ffc5638a3 | 144 | short int MEMS_Mag_Xout = 0; |
kbahar3 | 5:d39ffc5638a3 | 145 | short int MEMS_Mag_Yout = 0; |
kbahar3 | 5:d39ffc5638a3 | 146 | short int MEMS_Mag_Zout = 0; |
kbahar3 | 3:c3ee9d663fb8 | 147 | float MEMS_Mag_Conv_Xout = 0; |
kbahar3 | 3:c3ee9d663fb8 | 148 | float MEMS_Mag_Conv_Yout = 0; |
kbahar3 | 3:c3ee9d663fb8 | 149 | float MEMS_Mag_Conv_Zout = 0; |
kbahar3 | 3:c3ee9d663fb8 | 150 | #endif |
kbahar3 | 3:c3ee9d663fb8 | 151 | |
kbahar3 | 5:d39ffc5638a3 | 152 | #ifdef color |
kbahar3 | 5:d39ffc5638a3 | 153 | int BH1745_addr_w = 0x72; //write |
kbahar3 | 5:d39ffc5638a3 | 154 | int BH1745_addr_r = 0x73; //read |
kbahar3 | 5:d39ffc5638a3 | 155 | |
kbahar3 | 5:d39ffc5638a3 | 156 | char BH1745_persistence[2] = {0x61, 0x03}; |
kbahar3 | 5:d39ffc5638a3 | 157 | char BH1745_mode1[2] = {0x41, 0x00}; |
kbahar3 | 5:d39ffc5638a3 | 158 | char BH1745_mode2[2] = {0x42, 0x92}; |
kbahar3 | 5:d39ffc5638a3 | 159 | char BH1745_mode3[2] = {0x43, 0x02}; |
kbahar3 | 5:d39ffc5638a3 | 160 | |
kbahar3 | 5:d39ffc5638a3 | 161 | char BH1745_Content_ReadData[6]; |
kbahar3 | 5:d39ffc5638a3 | 162 | char BH1745_Addr_color_ReadData = 0x50; |
kbahar3 | 5:d39ffc5638a3 | 163 | |
kbahar3 | 5:d39ffc5638a3 | 164 | int BH1745_Red; |
kbahar3 | 5:d39ffc5638a3 | 165 | int BH1745_Blue; |
kbahar3 | 5:d39ffc5638a3 | 166 | int BH1745_Green; |
kbahar3 | 5:d39ffc5638a3 | 167 | |
kbahar3 | 5:d39ffc5638a3 | 168 | #endif |
kbahar3 | 5:d39ffc5638a3 | 169 | |
kbahar3 | 5:d39ffc5638a3 | 170 | #ifdef KX022 |
kbahar3 | 5:d39ffc5638a3 | 171 | int KX022_addr_w = 0x3C; //write |
kbahar3 | 5:d39ffc5638a3 | 172 | int KX022_addr_r = 0x3D; //read |
kbahar3 | 5:d39ffc5638a3 | 173 | |
kbahar3 | 5:d39ffc5638a3 | 174 | char KX022_Accel_CNTL1[2] = {0x18, 0x41}; |
kbahar3 | 5:d39ffc5638a3 | 175 | char KX022_Accel_ODCNTL[2] = {0x1B, 0x02}; |
kbahar3 | 5:d39ffc5638a3 | 176 | char KX022_Accel_CNTL3[2] = {0x1A, 0xD8}; |
kbahar3 | 5:d39ffc5638a3 | 177 | char KX022_Accel_TILT_TIMER[2] = {0x22, 0x01}; |
kbahar3 | 5:d39ffc5638a3 | 178 | char KX022_Accel_CNTL2[2] = {0x18, 0xC1}; |
kbahar3 | 5:d39ffc5638a3 | 179 | |
kbahar3 | 5:d39ffc5638a3 | 180 | char KX022_Content_ReadData[6]; |
kbahar3 | 5:d39ffc5638a3 | 181 | char KX022_Addr_Accel_ReadData = 0x06; |
kbahar3 | 5:d39ffc5638a3 | 182 | |
kbahar3 | 5:d39ffc5638a3 | 183 | float KX022_Accel_X; |
kbahar3 | 5:d39ffc5638a3 | 184 | float KX022_Accel_Y; |
kbahar3 | 5:d39ffc5638a3 | 185 | float KX022_Accel_Z; |
kbahar3 | 5:d39ffc5638a3 | 186 | |
kbahar3 | 5:d39ffc5638a3 | 187 | short int KX022_Accel_X_RawOUT = 0; |
kbahar3 | 5:d39ffc5638a3 | 188 | short int KX022_Accel_Y_RawOUT = 0; |
kbahar3 | 5:d39ffc5638a3 | 189 | short int KX022_Accel_Z_RawOUT = 0; |
kbahar3 | 5:d39ffc5638a3 | 190 | |
kbahar3 | 5:d39ffc5638a3 | 191 | int KX022_Accel_X_LB = 0; |
kbahar3 | 5:d39ffc5638a3 | 192 | int KX022_Accel_X_HB = 0; |
kbahar3 | 5:d39ffc5638a3 | 193 | int KX022_Accel_Y_LB = 0; |
kbahar3 | 5:d39ffc5638a3 | 194 | int KX022_Accel_Y_HB = 0; |
kbahar3 | 5:d39ffc5638a3 | 195 | int KX022_Accel_Z_LB = 0; |
kbahar3 | 5:d39ffc5638a3 | 196 | int KX022_Accel_Z_HB = 0; |
kbahar3 | 5:d39ffc5638a3 | 197 | |
kbahar3 | 5:d39ffc5638a3 | 198 | #endif |
kbahar3 | 5:d39ffc5638a3 | 199 | |
kbahar3 | 5:d39ffc5638a3 | 200 | #ifdef Pressure |
kbahar3 | 5:d39ffc5638a3 | 201 | int Press_addr_w = 0xBA; //write |
kbahar3 | 5:d39ffc5638a3 | 202 | int Press_addr_r = 0xBB; //read |
kbahar3 | 5:d39ffc5638a3 | 203 | |
kbahar3 | 5:d39ffc5638a3 | 204 | char PWR_DOWN[2] = {0x12, 0x01}; |
kbahar3 | 5:d39ffc5638a3 | 205 | char SLEEP[2] = {0x13, 0x01}; |
kbahar3 | 5:d39ffc5638a3 | 206 | char Mode_Control[2] = {0x14, 0xC4}; |
kbahar3 | 5:d39ffc5638a3 | 207 | |
kbahar3 | 5:d39ffc5638a3 | 208 | char Press_Content_ReadData[6]; |
kbahar3 | 5:d39ffc5638a3 | 209 | char Press_Addr_ReadData =0x1A; |
kbahar3 | 5:d39ffc5638a3 | 210 | |
kbahar3 | 5:d39ffc5638a3 | 211 | int BM1383_Temp_highByte; |
kbahar3 | 5:d39ffc5638a3 | 212 | int BM1383_Temp_lowByte; |
kbahar3 | 5:d39ffc5638a3 | 213 | int BM1383_Pres_highByte; |
kbahar3 | 5:d39ffc5638a3 | 214 | int BM1383_Pres_lowByte; |
kbahar3 | 5:d39ffc5638a3 | 215 | int BM1383_Pres_leastByte; |
kbahar3 | 5:d39ffc5638a3 | 216 | |
kbahar3 | 5:d39ffc5638a3 | 217 | float BM1383_Temp_Out; |
kbahar3 | 5:d39ffc5638a3 | 218 | float BM1383_Temp_Conv_Out; |
kbahar3 | 5:d39ffc5638a3 | 219 | float BM1383_Pres_Conv_Out; |
kbahar3 | 5:d39ffc5638a3 | 220 | |
kbahar3 | 5:d39ffc5638a3 | 221 | float BM1383_Var; |
kbahar3 | 5:d39ffc5638a3 | 222 | float BM1383_Deci; |
kbahar3 | 5:d39ffc5638a3 | 223 | #endif |
kbahar3 | 5:d39ffc5638a3 | 224 | |
Daniel Veilleux |
0:442c7a6f1978 | 225 | /** |
Daniel Veilleux |
0:442c7a6f1978 | 226 | * This callback is used whenever a disconnection occurs. |
Daniel Veilleux |
0:442c7a6f1978 | 227 | */ |
Daniel Veilleux |
0:442c7a6f1978 | 228 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
Daniel Veilleux |
0:442c7a6f1978 | 229 | { |
Daniel Veilleux |
0:442c7a6f1978 | 230 | switch (reason) { |
Daniel Veilleux |
0:442c7a6f1978 | 231 | case Gap::REMOTE_USER_TERMINATED_CONNECTION: |
Daniel Veilleux |
0:442c7a6f1978 | 232 | DEBUG("Disconnected (REMOTE_USER_TERMINATED_CONNECTION)\n\r"); |
Daniel Veilleux |
0:442c7a6f1978 | 233 | break; |
Daniel Veilleux |
0:442c7a6f1978 | 234 | case Gap::LOCAL_HOST_TERMINATED_CONNECTION: |
Daniel Veilleux |
0:442c7a6f1978 | 235 | DEBUG("Disconnected (LOCAL_HOST_TERMINATED_CONNECTION)\n\r"); |
Daniel Veilleux |
0:442c7a6f1978 | 236 | break; |
Daniel Veilleux |
0:442c7a6f1978 | 237 | case Gap::CONN_INTERVAL_UNACCEPTABLE: |
Daniel Veilleux |
0:442c7a6f1978 | 238 | DEBUG("Disconnected (CONN_INTERVAL_UNACCEPTABLE)\n\r"); |
Daniel Veilleux |
0:442c7a6f1978 | 239 | break; |
Daniel Veilleux |
0:442c7a6f1978 | 240 | } |
Daniel Veilleux |
0:442c7a6f1978 | 241 | |
Daniel Veilleux |
0:442c7a6f1978 | 242 | DEBUG("Restarting the advertising process\n\r"); |
Daniel Veilleux |
0:442c7a6f1978 | 243 | m_ble.startAdvertising(); |
Daniel Veilleux |
0:442c7a6f1978 | 244 | } |
Daniel Veilleux |
0:442c7a6f1978 | 245 | |
Daniel Veilleux |
0:442c7a6f1978 | 246 | |
Daniel Veilleux |
0:442c7a6f1978 | 247 | /** |
Daniel Veilleux |
0:442c7a6f1978 | 248 | * This callback is used whenever the host writes data to one of our GATT characteristics. |
Daniel Veilleux |
0:442c7a6f1978 | 249 | */ |
Daniel Veilleux |
0:442c7a6f1978 | 250 | void dataWrittenCallback(const GattCharacteristicWriteCBParams *params) |
Daniel Veilleux |
0:442c7a6f1978 | 251 | { |
Daniel Veilleux |
0:442c7a6f1978 | 252 | // Ensure that initialization is finished and the host has written to the TX characteristic. |
Daniel Veilleux |
0:442c7a6f1978 | 253 | if ((m_uart_service_ptr != NULL) && (params->charHandle == m_uart_service_ptr->getTXCharacteristicHandle())) { |
Daniel Veilleux |
0:442c7a6f1978 | 254 | uint8_t buf[MAX_REPLY_LEN]; |
Daniel Veilleux |
0:442c7a6f1978 | 255 | uint32_t len = 0; |
Daniel Veilleux |
0:442c7a6f1978 | 256 | |
Daniel Veilleux |
0:442c7a6f1978 | 257 | if (1 == params->len) { |
Daniel Veilleux |
0:442c7a6f1978 | 258 | switch (params->data[0]) { |
Daniel Veilleux |
0:442c7a6f1978 | 259 | case '0': |
kbahar3 | 1:2c0ab5cd1a7f | 260 | m_cmd_led = m_cmd_led ^ 1; |
kbahar3 | 1:2c0ab5cd1a7f | 261 | len = snprintf((char*) buf, MAX_REPLY_LEN, "OK... LED ON"); |
Daniel Veilleux |
0:442c7a6f1978 | 262 | break; |
Daniel Veilleux |
0:442c7a6f1978 | 263 | case '1': |
kbahar3 | 1:2c0ab5cd1a7f | 264 | m_cmd_led = m_cmd_led ^ 1; |
kbahar3 | 1:2c0ab5cd1a7f | 265 | len = snprintf((char*) buf, MAX_REPLY_LEN, "OK... LED OFF"); |
Daniel Veilleux |
0:442c7a6f1978 | 266 | break; |
Daniel Veilleux |
0:442c7a6f1978 | 267 | case 'a': |
kbahar3 | 2:c7b9d588c80f | 268 | //len = snprintf((char*) buf, MAX_REPLY_LEN, "ALSRaw = %d", BH1620_ALS_value); |
kbahar3 | 1:2c0ab5cd1a7f | 269 | break; |
kbahar3 | 1:2c0ab5cd1a7f | 270 | case 'b': |
kbahar3 | 2:c7b9d588c80f | 271 | //len = snprintf((char*) buf, MAX_REPLY_LEN, "ALS = %.2f lx", BH1620_output); |
Daniel Veilleux |
0:442c7a6f1978 | 272 | break; |
Daniel Veilleux |
0:442c7a6f1978 | 273 | default: |
kbahar3 | 1:2c0ab5cd1a7f | 274 | len = snprintf((char*) buf, MAX_REPLY_LEN, "ERROR"); |
Daniel Veilleux |
0:442c7a6f1978 | 275 | break; |
Daniel Veilleux |
0:442c7a6f1978 | 276 | } |
Daniel Veilleux |
0:442c7a6f1978 | 277 | } |
Daniel Veilleux |
0:442c7a6f1978 | 278 | else |
Daniel Veilleux |
0:442c7a6f1978 | 279 | { |
kbahar3 | 1:2c0ab5cd1a7f | 280 | len = snprintf((char*) buf, MAX_REPLY_LEN, "ERROR"); |
kbahar3 | 4:eabae2996ecc | 281 | } |
Daniel Veilleux |
0:442c7a6f1978 | 282 | |
Daniel Veilleux |
0:442c7a6f1978 | 283 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
Daniel Veilleux |
0:442c7a6f1978 | 284 | |
Daniel Veilleux |
0:442c7a6f1978 | 285 | DEBUG("%d bytes received from host\n\r", params->len); |
Daniel Veilleux |
0:442c7a6f1978 | 286 | } |
Daniel Veilleux |
0:442c7a6f1978 | 287 | } |
Daniel Veilleux |
0:442c7a6f1978 | 288 | |
Daniel Veilleux |
0:442c7a6f1978 | 289 | |
Daniel Veilleux |
0:442c7a6f1978 | 290 | /** |
Daniel Veilleux |
0:442c7a6f1978 | 291 | * This callback is used whenever a write to a GATT characteristic causes data to be sent to the host. |
Daniel Veilleux |
0:442c7a6f1978 | 292 | */ |
Daniel Veilleux |
0:442c7a6f1978 | 293 | void dataSentCallback(unsigned count) |
Daniel Veilleux |
0:442c7a6f1978 | 294 | { |
Daniel Veilleux |
0:442c7a6f1978 | 295 | // NOTE: The count always seems to be 1 regardless of data. |
Daniel Veilleux |
0:442c7a6f1978 | 296 | DEBUG("%d bytes sent to host\n\r", count); |
Daniel Veilleux |
0:442c7a6f1978 | 297 | } |
Daniel Veilleux |
0:442c7a6f1978 | 298 | |
Daniel Veilleux |
0:442c7a6f1978 | 299 | |
Daniel Veilleux |
0:442c7a6f1978 | 300 | /** |
Daniel Veilleux |
0:442c7a6f1978 | 301 | * This callback is scheduled to be called periodically via a low-priority interrupt. |
Daniel Veilleux |
0:442c7a6f1978 | 302 | */ |
Daniel Veilleux |
0:442c7a6f1978 | 303 | void periodicCallback(void) |
Daniel Veilleux |
0:442c7a6f1978 | 304 | { |
kbahar3 | 1:2c0ab5cd1a7f | 305 | uint8_t buf[MAX_REPLY_LEN]; |
kbahar3 | 1:2c0ab5cd1a7f | 306 | uint32_t len = 0; |
kbahar3 | 1:2c0ab5cd1a7f | 307 | |
kbahar3 | 2:c7b9d588c80f | 308 | |
kbahar3 | 5:d39ffc5638a3 | 309 | if(i==1) |
kbahar3 | 5:d39ffc5638a3 | 310 | { |
kbahar3 | 5:d39ffc5638a3 | 311 | /* |
kbahar3 | 2:c7b9d588c80f | 312 | #ifdef AnalogALS |
kbahar3 | 1:2c0ab5cd1a7f | 313 | if (m_ble.getGapState().connected) { |
kbahar3 | 1:2c0ab5cd1a7f | 314 | BH1620_ALS_value = BH1620_ALS.read_u16(); |
kbahar3 | 1:2c0ab5cd1a7f | 315 | BH1620_output = (float)BH1620_ALS_value * 1.543; |
kbahar3 | 1:2c0ab5cd1a7f | 316 | |
kbahar3 | 5:d39ffc5638a3 | 317 | len = snprintf((char*) buf, MAX_REPLY_LEN, "Analog ALS = %.2f lx", BH1620_output); |
kbahar3 | 1:2c0ab5cd1a7f | 318 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 1:2c0ab5cd1a7f | 319 | } |
kbahar3 | 1:2c0ab5cd1a7f | 320 | #endif |
kbahar3 | 2:c7b9d588c80f | 321 | */ |
kbahar3 | 1:2c0ab5cd1a7f | 322 | |
kbahar3 | 5:d39ffc5638a3 | 323 | #ifdef color |
kbahar3 | 5:d39ffc5638a3 | 324 | if (m_ble.getGapState().connected) { |
kbahar3 | 5:d39ffc5638a3 | 325 | //Read color Portion from the IC |
kbahar3 | 5:d39ffc5638a3 | 326 | i2c.write(BH1745_addr_w, &BH1745_Addr_color_ReadData, 1, RepStart); |
kbahar3 | 5:d39ffc5638a3 | 327 | i2c.read(BH1745_addr_r, &BH1745_Content_ReadData[0], 6, NoRepStart); |
kbahar3 | 5:d39ffc5638a3 | 328 | |
kbahar3 | 5:d39ffc5638a3 | 329 | //separate all data read into colors |
kbahar3 | 5:d39ffc5638a3 | 330 | |
kbahar3 | 5:d39ffc5638a3 | 331 | BH1745_Red = (BH1745_Content_ReadData[1]<<8) | (BH1745_Content_ReadData[0]); |
kbahar3 | 5:d39ffc5638a3 | 332 | BH1745_Green = (BH1745_Content_ReadData[3]<<8) | (BH1745_Content_ReadData[2]); |
kbahar3 | 5:d39ffc5638a3 | 333 | BH1745_Blue = (BH1745_Content_ReadData[5]<<8) | (BH1745_Content_ReadData[4]); |
kbahar3 | 5:d39ffc5638a3 | 334 | |
kbahar3 | 5:d39ffc5638a3 | 335 | |
kbahar3 | 5:d39ffc5638a3 | 336 | //transmit data |
kbahar3 | 5:d39ffc5638a3 | 337 | len = snprintf((char*) buf, MAX_REPLY_LEN, "Red= %d", BH1745_Red); |
kbahar3 | 5:d39ffc5638a3 | 338 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 5:d39ffc5638a3 | 339 | wait_ms(25); |
kbahar3 | 5:d39ffc5638a3 | 340 | |
kbahar3 | 5:d39ffc5638a3 | 341 | |
kbahar3 | 5:d39ffc5638a3 | 342 | len = snprintf((char*) buf, MAX_REPLY_LEN, "Green= %d", BH1745_Green); |
kbahar3 | 5:d39ffc5638a3 | 343 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 5:d39ffc5638a3 | 344 | wait_ms(25); |
kbahar3 | 5:d39ffc5638a3 | 345 | |
kbahar3 | 5:d39ffc5638a3 | 346 | len = snprintf((char*) buf, MAX_REPLY_LEN, "Blue= %d", BH1745_Blue); |
kbahar3 | 5:d39ffc5638a3 | 347 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 5:d39ffc5638a3 | 348 | wait_ms(25); |
kbahar3 | 5:d39ffc5638a3 | 349 | |
kbahar3 | 5:d39ffc5638a3 | 350 | } |
kbahar3 | 5:d39ffc5638a3 | 351 | #endif |
kbahar3 | 5:d39ffc5638a3 | 352 | |
kbahar3 | 1:2c0ab5cd1a7f | 353 | #ifdef AnalogTemp |
kbahar3 | 1:2c0ab5cd1a7f | 354 | if (m_ble.getGapState().connected) { |
kbahar3 | 1:2c0ab5cd1a7f | 355 | BDE0600_Temp_value = BDE0600_Temp.read_u16(); |
kbahar3 | 1:2c0ab5cd1a7f | 356 | BDE0600_output = (float)BDE0600_Temp_value * 0.00283; //(value * (2.9V/1024)) |
kbahar3 | 1:2c0ab5cd1a7f | 357 | BDE0600_output = (BDE0600_output-1.753)/(-0.01068) + 30; |
kbahar3 | 1:2c0ab5cd1a7f | 358 | |
kbahar3 | 1:2c0ab5cd1a7f | 359 | len = snprintf((char*) buf, MAX_REPLY_LEN, "Temp = %.2f C", BDE0600_output); |
kbahar3 | 1:2c0ab5cd1a7f | 360 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 1:2c0ab5cd1a7f | 361 | } |
kbahar3 | 1:2c0ab5cd1a7f | 362 | #endif |
kbahar3 | 1:2c0ab5cd1a7f | 363 | |
kbahar3 | 1:2c0ab5cd1a7f | 364 | #ifdef AnalogUV |
kbahar3 | 1:2c0ab5cd1a7f | 365 | if (m_ble.getGapState().connected) { |
kbahar3 | 1:2c0ab5cd1a7f | 366 | ML8511_UV_value = ML8511_UV.read_u16(); |
kbahar3 | 5:d39ffc5638a3 | 367 | ML8511_output = (float)ML8511_UV_value * 0.0029; //(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 | 5:d39ffc5638a3 | 368 | ML8511_output = (ML8511_output-2.2)/(0.129) + 10; // 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 | 369 | |
kbahar3 | 1:2c0ab5cd1a7f | 370 | len = snprintf((char*) buf, MAX_REPLY_LEN, "UV = %.1f mW/cm2", ML8511_output); |
kbahar3 | 1:2c0ab5cd1a7f | 371 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 1:2c0ab5cd1a7f | 372 | } |
kbahar3 | 1:2c0ab5cd1a7f | 373 | #endif |
kbahar3 | 1:2c0ab5cd1a7f | 374 | |
kbahar3 | 1:2c0ab5cd1a7f | 375 | #ifdef HallSensor |
kbahar3 | 1:2c0ab5cd1a7f | 376 | if (m_ble.getGapState().connected) { |
kbahar3 | 1:2c0ab5cd1a7f | 377 | Hall_Return0 = Hall_GPIO0; |
kbahar3 | 1:2c0ab5cd1a7f | 378 | Hall_Return1 = Hall_GPIO1; |
kbahar3 | 1:2c0ab5cd1a7f | 379 | |
kbahar3 | 1:2c0ab5cd1a7f | 380 | len = snprintf((char*) buf, MAX_REPLY_LEN, "H0 = %d, H1 = %d", Hall_Return0, Hall_Return1); |
kbahar3 | 1:2c0ab5cd1a7f | 381 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 1:2c0ab5cd1a7f | 382 | } |
kbahar3 | 1:2c0ab5cd1a7f | 383 | #endif |
kbahar3 | 1:2c0ab5cd1a7f | 384 | |
kbahar3 | 1:2c0ab5cd1a7f | 385 | #ifdef DigitalALS |
kbahar3 | 1:2c0ab5cd1a7f | 386 | if (m_ble.getGapState().connected) { |
kbahar3 | 1:2c0ab5cd1a7f | 387 | i2c.read(ALS_addr_r, ALS_ReturnData_raw, 2); |
kbahar3 | 1:2c0ab5cd1a7f | 388 | ALS_Return = (ALS_ReturnData_raw[0]<<8) | ALS_ReturnData_raw[1]; |
kbahar3 | 1:2c0ab5cd1a7f | 389 | ALS_Return = ALS_Return/1.2; |
kbahar3 | 1:2c0ab5cd1a7f | 390 | |
kbahar3 | 5:d39ffc5638a3 | 391 | len = snprintf((char*) buf, MAX_REPLY_LEN, "DAL1= %0.2f lx", ALS_Return); |
kbahar3 | 1:2c0ab5cd1a7f | 392 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 1:2c0ab5cd1a7f | 393 | } |
kbahar3 | 1:2c0ab5cd1a7f | 394 | #endif |
kbahar3 | 1:2c0ab5cd1a7f | 395 | |
kbahar3 | 5:d39ffc5638a3 | 396 | #ifdef RPR0521 //als digital |
kbahar3 | 2:c7b9d588c80f | 397 | if (m_ble.getGapState().connected) { |
kbahar3 | 2:c7b9d588c80f | 398 | |
kbahar3 | 2:c7b9d588c80f | 399 | i2c.write(RPR0521_addr_w, &RPR0521_Addr_ReadData, 1, RepStart); |
kbahar3 | 2:c7b9d588c80f | 400 | i2c.read(RPR0521_addr_r, &RPR0521_Content_ReadData[0], 6, NoRepStart); |
kbahar3 | 2:c7b9d588c80f | 401 | |
kbahar3 | 2:c7b9d588c80f | 402 | RPR0521_PS_RAWOUT = (RPR0521_Content_ReadData[1]<<8) | (RPR0521_Content_ReadData[0]); |
kbahar3 | 2:c7b9d588c80f | 403 | RPR0521_ALS_D0_RAWOUT = (RPR0521_Content_ReadData[3]<<8) | (RPR0521_Content_ReadData[2]); |
kbahar3 | 2:c7b9d588c80f | 404 | RPR0521_ALS_D1_RAWOUT = (RPR0521_Content_ReadData[5]<<8) | (RPR0521_Content_ReadData[4]); |
kbahar3 | 2:c7b9d588c80f | 405 | RPR0521_ALS_DataRatio = (float)RPR0521_ALS_D1_RAWOUT / (float)RPR0521_ALS_D0_RAWOUT; |
kbahar3 | 2:c7b9d588c80f | 406 | |
kbahar3 | 2:c7b9d588c80f | 407 | if(RPR0521_ALS_DataRatio < 0.595){ |
kbahar3 | 2:c7b9d588c80f | 408 | RPR0521_ALS_OUT = (1.682*(float)RPR0521_ALS_D0_RAWOUT - 1.877*(float)RPR0521_ALS_D1_RAWOUT); |
kbahar3 | 2:c7b9d588c80f | 409 | } |
kbahar3 | 2:c7b9d588c80f | 410 | else if(RPR0521_ALS_DataRatio < 1.015){ |
kbahar3 | 2:c7b9d588c80f | 411 | RPR0521_ALS_OUT = (0.644*(float)RPR0521_ALS_D0_RAWOUT - 0.132*(float)RPR0521_ALS_D1_RAWOUT); |
kbahar3 | 2:c7b9d588c80f | 412 | } |
kbahar3 | 2:c7b9d588c80f | 413 | else if(RPR0521_ALS_DataRatio < 1.352){ |
kbahar3 | 2:c7b9d588c80f | 414 | RPR0521_ALS_OUT = (0.756*(float)RPR0521_ALS_D0_RAWOUT - 0.243*(float)RPR0521_ALS_D1_RAWOUT); |
kbahar3 | 2:c7b9d588c80f | 415 | } |
kbahar3 | 2:c7b9d588c80f | 416 | else if(RPR0521_ALS_DataRatio < 3.053){ |
kbahar3 | 2:c7b9d588c80f | 417 | RPR0521_ALS_OUT = (0.766*(float)RPR0521_ALS_D0_RAWOUT - 0.25*(float)RPR0521_ALS_D1_RAWOUT); |
kbahar3 | 2:c7b9d588c80f | 418 | } |
kbahar3 | 2:c7b9d588c80f | 419 | else{ |
kbahar3 | 2:c7b9d588c80f | 420 | RPR0521_ALS_OUT = 0; |
kbahar3 | 2:c7b9d588c80f | 421 | } |
kbahar3 | 2:c7b9d588c80f | 422 | |
kbahar3 | 2:c7b9d588c80f | 423 | len = snprintf((char*) buf, MAX_REPLY_LEN, "DALS= %0.2f lx", RPR0521_ALS_OUT); |
kbahar3 | 2:c7b9d588c80f | 424 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 5:d39ffc5638a3 | 425 | |
kbahar3 | 2:c7b9d588c80f | 426 | } |
kbahar3 | 2:c7b9d588c80f | 427 | #endif |
kbahar3 | 1:2c0ab5cd1a7f | 428 | |
kbahar3 | 5:d39ffc5638a3 | 429 | i++; |
kbahar3 | 5:d39ffc5638a3 | 430 | |
kbahar3 | 5:d39ffc5638a3 | 431 | } |
kbahar3 | 5:d39ffc5638a3 | 432 | |
kbahar3 | 5:d39ffc5638a3 | 433 | else if(i==2) |
kbahar3 | 5:d39ffc5638a3 | 434 | { |
kbahar3 | 5:d39ffc5638a3 | 435 | |
kbahar3 | 3:c3ee9d663fb8 | 436 | #ifdef KMX62 |
kbahar3 | 1:2c0ab5cd1a7f | 437 | if (m_ble.getGapState().connected) { |
kbahar3 | 3:c3ee9d663fb8 | 438 | //Read Accel Portion from the IC |
kbahar3 | 3:c3ee9d663fb8 | 439 | i2c.write(KMX62_addr_w, &KMX62_Addr_Accel_ReadData, 1, RepStart); |
kbahar3 | 3:c3ee9d663fb8 | 440 | i2c.read(KMX62_addr_r, &KMX62_Content_Accel_ReadData[0], 6, NoRepStart); |
kbahar3 | 3:c3ee9d663fb8 | 441 | |
kbahar3 | 3:c3ee9d663fb8 | 442 | //Note: The highbyte and low byte return a 14bit value, dropping the two LSB in the Low byte. |
kbahar3 | 3:c3ee9d663fb8 | 443 | // However, because we need the signed value, we will adjust the value when converting to "g" |
kbahar3 | 3:c3ee9d663fb8 | 444 | MEMS_Accel_Xout = (KMX62_Content_Accel_ReadData[1]<<8) | (KMX62_Content_Accel_ReadData[0]); |
kbahar3 | 3:c3ee9d663fb8 | 445 | MEMS_Accel_Yout = (KMX62_Content_Accel_ReadData[3]<<8) | (KMX62_Content_Accel_ReadData[2]); |
kbahar3 | 3:c3ee9d663fb8 | 446 | MEMS_Accel_Zout = (KMX62_Content_Accel_ReadData[5]<<8) | (KMX62_Content_Accel_ReadData[4]); |
kbahar3 | 3:c3ee9d663fb8 | 447 | |
kbahar3 | 3:c3ee9d663fb8 | 448 | //Note: Conversion to G is as follows: |
kbahar3 | 3:c3ee9d663fb8 | 449 | // Axis_ValueInG = MEMS_Accel_axis / 1024 |
kbahar3 | 3:c3ee9d663fb8 | 450 | // However, since we did not remove the LSB previously, we need to divide by 4 again |
kbahar3 | 5:d39ffc5638a3 | 451 | // Thus, we will divide the output by 4096 (1024*4) to convert and cancel out the LSB |
kbahar3 | 5:d39ffc5638a3 | 452 | MEMS_Accel_Conv_Xout = ((float)MEMS_Accel_Xout/4096/2); |
kbahar3 | 5:d39ffc5638a3 | 453 | MEMS_Accel_Conv_Yout = ((float)MEMS_Accel_Yout/4096/2); |
kbahar3 | 5:d39ffc5638a3 | 454 | MEMS_Accel_Conv_Zout = ((float)MEMS_Accel_Zout/4096/2); |
kbahar3 | 3:c3ee9d663fb8 | 455 | |
kbahar3 | 3:c3ee9d663fb8 | 456 | //Read MAg portion from the IC |
kbahar3 | 3:c3ee9d663fb8 | 457 | i2c.write(KMX62_addr_w, &KMX62_Addr_Mag_ReadData, 1, RepStart); |
kbahar3 | 3:c3ee9d663fb8 | 458 | i2c.read(KMX62_addr_r, &KMX62_Content_Mag_ReadData[0], 6, NoRepStart); |
kbahar3 | 3:c3ee9d663fb8 | 459 | |
kbahar3 | 3:c3ee9d663fb8 | 460 | //Note: The highbyte and low byte return a 14bit value, dropping the two LSB in the Low byte. |
kbahar3 | 3:c3ee9d663fb8 | 461 | // However, because we need the signed value, we will adjust the value when converting to "g" |
kbahar3 | 3:c3ee9d663fb8 | 462 | MEMS_Mag_Xout = (KMX62_Content_Mag_ReadData[1]<<8) | (KMX62_Content_Mag_ReadData[0]); |
kbahar3 | 3:c3ee9d663fb8 | 463 | MEMS_Mag_Yout = (KMX62_Content_Mag_ReadData[3]<<8) | (KMX62_Content_Mag_ReadData[2]); |
kbahar3 | 3:c3ee9d663fb8 | 464 | MEMS_Mag_Zout = (KMX62_Content_Mag_ReadData[5]<<8) | (KMX62_Content_Mag_ReadData[4]); |
kbahar3 | 3:c3ee9d663fb8 | 465 | |
kbahar3 | 3:c3ee9d663fb8 | 466 | //Note: Conversion to G is as follows: |
kbahar3 | 3:c3ee9d663fb8 | 467 | // Axis_ValueInG = MEMS_Accel_axis / 1024 |
kbahar3 | 3:c3ee9d663fb8 | 468 | // However, since we did not remove the LSB previously, we need to divide by 4 again |
kbahar3 | 3:c3ee9d663fb8 | 469 | // Thus, we will divide the output by 4095 (1024*4) to convert and cancel out the LSB |
kbahar3 | 5:d39ffc5638a3 | 470 | MEMS_Mag_Conv_Xout = (float)MEMS_Mag_Xout/4096*0.146; |
kbahar3 | 5:d39ffc5638a3 | 471 | MEMS_Mag_Conv_Yout = (float)MEMS_Mag_Yout/4096*0.146; |
kbahar3 | 5:d39ffc5638a3 | 472 | MEMS_Mag_Conv_Zout = (float)MEMS_Mag_Zout/4096*0.146; |
kbahar3 | 5:d39ffc5638a3 | 473 | |
kbahar3 | 5:d39ffc5638a3 | 474 | // transmit data |
kbahar3 | 5:d39ffc5638a3 | 475 | |
kbahar3 | 3:c3ee9d663fb8 | 476 | |
kbahar3 | 3:c3ee9d663fb8 | 477 | len = snprintf((char*) buf, MAX_REPLY_LEN, "KMX61SensorData:"); |
kbahar3 | 3:c3ee9d663fb8 | 478 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 5:d39ffc5638a3 | 479 | wait_ms(20); |
kbahar3 | 3:c3ee9d663fb8 | 480 | |
kbahar3 | 3:c3ee9d663fb8 | 481 | len = snprintf((char*) buf, MAX_REPLY_LEN, " AccX= %0.2f g", MEMS_Accel_Conv_Xout); |
kbahar3 | 3:c3ee9d663fb8 | 482 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 5:d39ffc5638a3 | 483 | wait_ms(20); |
kbahar3 | 3:c3ee9d663fb8 | 484 | |
kbahar3 | 3:c3ee9d663fb8 | 485 | len = snprintf((char*) buf, MAX_REPLY_LEN, " AccY= %0.2f g", MEMS_Accel_Conv_Yout); |
kbahar3 | 3:c3ee9d663fb8 | 486 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 5:d39ffc5638a3 | 487 | wait_ms(20); |
kbahar3 | 3:c3ee9d663fb8 | 488 | |
kbahar3 | 3:c3ee9d663fb8 | 489 | len = snprintf((char*) buf, MAX_REPLY_LEN, " AccZ= %0.2f g", MEMS_Accel_Conv_Zout); |
kbahar3 | 3:c3ee9d663fb8 | 490 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 5:d39ffc5638a3 | 491 | wait_ms(20); |
kbahar3 | 3:c3ee9d663fb8 | 492 | |
kbahar3 | 3:c3ee9d663fb8 | 493 | len = snprintf((char*) buf, MAX_REPLY_LEN, " MagX= %0.2f g", MEMS_Mag_Conv_Xout); |
kbahar3 | 3:c3ee9d663fb8 | 494 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 5:d39ffc5638a3 | 495 | wait_ms(20); |
kbahar3 | 3:c3ee9d663fb8 | 496 | |
kbahar3 | 3:c3ee9d663fb8 | 497 | len = snprintf((char*) buf, MAX_REPLY_LEN, " MagY= %0.2f g", MEMS_Mag_Conv_Yout); |
kbahar3 | 3:c3ee9d663fb8 | 498 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 5:d39ffc5638a3 | 499 | wait_ms(20); |
kbahar3 | 3:c3ee9d663fb8 | 500 | |
kbahar3 | 3:c3ee9d663fb8 | 501 | len = snprintf((char*) buf, MAX_REPLY_LEN, " MagZ= %0.2f g", MEMS_Mag_Conv_Zout); |
kbahar3 | 3:c3ee9d663fb8 | 502 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 5:d39ffc5638a3 | 503 | wait_ms(20); |
kbahar3 | 5:d39ffc5638a3 | 504 | |
kbahar3 | 3:c3ee9d663fb8 | 505 | } |
kbahar3 | 3:c3ee9d663fb8 | 506 | #endif |
kbahar3 | 3:c3ee9d663fb8 | 507 | |
kbahar3 | 5:d39ffc5638a3 | 508 | i++; |
kbahar3 | 5:d39ffc5638a3 | 509 | |
Daniel Veilleux |
0:442c7a6f1978 | 510 | } |
Daniel Veilleux |
0:442c7a6f1978 | 511 | |
Daniel Veilleux |
0:442c7a6f1978 | 512 | |
kbahar3 | 5:d39ffc5638a3 | 513 | else if(i==3) |
kbahar3 | 5:d39ffc5638a3 | 514 | { |
kbahar3 | 5:d39ffc5638a3 | 515 | |
kbahar3 | 5:d39ffc5638a3 | 516 | |
kbahar3 | 5:d39ffc5638a3 | 517 | |
kbahar3 | 5:d39ffc5638a3 | 518 | #ifdef KX022 |
kbahar3 | 5:d39ffc5638a3 | 519 | if (m_ble.getGapState().connected) { |
kbahar3 | 5:d39ffc5638a3 | 520 | //Read KX022 Portion from the IC |
kbahar3 | 5:d39ffc5638a3 | 521 | i2c.write(KX022_addr_w, &KX022_Addr_Accel_ReadData, 1, RepStart); |
kbahar3 | 5:d39ffc5638a3 | 522 | i2c.read(KX022_addr_r, &KX022_Content_ReadData[0], 6, NoRepStart); |
kbahar3 | 5:d39ffc5638a3 | 523 | |
kbahar3 | 5:d39ffc5638a3 | 524 | |
kbahar3 | 5:d39ffc5638a3 | 525 | //reconfigure the data (taken from arduino code) |
kbahar3 | 5:d39ffc5638a3 | 526 | KX022_Accel_X_RawOUT = (KX022_Content_ReadData[1]<<8) | (KX022_Content_ReadData[0]); |
kbahar3 | 5:d39ffc5638a3 | 527 | KX022_Accel_Y_RawOUT = (KX022_Content_ReadData[3]<<8) | (KX022_Content_ReadData[2]); |
kbahar3 | 5:d39ffc5638a3 | 528 | KX022_Accel_Z_RawOUT = (KX022_Content_ReadData[5]<<8) | (KX022_Content_ReadData[4]); |
kbahar3 | 5:d39ffc5638a3 | 529 | |
kbahar3 | 5:d39ffc5638a3 | 530 | //apply needed changes (taken from arduino code) |
kbahar3 | 5:d39ffc5638a3 | 531 | KX022_Accel_X = (float)KX022_Accel_X_RawOUT / 16384; |
kbahar3 | 5:d39ffc5638a3 | 532 | KX022_Accel_Y = (float)KX022_Accel_Y_RawOUT / 16384; |
kbahar3 | 5:d39ffc5638a3 | 533 | KX022_Accel_Z = (float)KX022_Accel_Z_RawOUT / 16384; |
kbahar3 | 5:d39ffc5638a3 | 534 | |
kbahar3 | 5:d39ffc5638a3 | 535 | |
kbahar3 | 5:d39ffc5638a3 | 536 | |
kbahar3 | 5:d39ffc5638a3 | 537 | //transmit the data |
kbahar3 | 5:d39ffc5638a3 | 538 | len = snprintf((char*) buf, MAX_REPLY_LEN, "KX022-X= %0.2f", KX022_Accel_X); |
kbahar3 | 5:d39ffc5638a3 | 539 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 5:d39ffc5638a3 | 540 | wait_ms(25); |
kbahar3 | 5:d39ffc5638a3 | 541 | |
kbahar3 | 5:d39ffc5638a3 | 542 | len = snprintf((char*) buf, MAX_REPLY_LEN, "KX022-Y= %0.2f", KX022_Accel_Y); |
kbahar3 | 5:d39ffc5638a3 | 543 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 5:d39ffc5638a3 | 544 | wait_ms(25); |
kbahar3 | 5:d39ffc5638a3 | 545 | |
kbahar3 | 5:d39ffc5638a3 | 546 | len = snprintf((char*) buf, MAX_REPLY_LEN, "KX022-Z= %0.2f", KX022_Accel_Z); |
kbahar3 | 5:d39ffc5638a3 | 547 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 5:d39ffc5638a3 | 548 | wait_ms(25); |
kbahar3 | 5:d39ffc5638a3 | 549 | |
kbahar3 | 5:d39ffc5638a3 | 550 | } |
kbahar3 | 5:d39ffc5638a3 | 551 | #endif |
kbahar3 | 5:d39ffc5638a3 | 552 | |
kbahar3 | 5:d39ffc5638a3 | 553 | #ifdef Pressure |
kbahar3 | 5:d39ffc5638a3 | 554 | if (m_ble.getGapState().connected) { |
kbahar3 | 5:d39ffc5638a3 | 555 | //Read color Portion from the IC |
kbahar3 | 5:d39ffc5638a3 | 556 | i2c.write(Press_addr_w, &Press_Addr_ReadData, 1, RepStart); |
kbahar3 | 5:d39ffc5638a3 | 557 | i2c.read(Press_addr_r, &Press_Content_ReadData[0], 6, NoRepStart); |
kbahar3 | 5:d39ffc5638a3 | 558 | |
kbahar3 | 5:d39ffc5638a3 | 559 | |
kbahar3 | 5:d39ffc5638a3 | 560 | |
kbahar3 | 5:d39ffc5638a3 | 561 | BM1383_Temp_Out = (Press_Content_ReadData[1]<<8) | (Press_Content_ReadData[0]); |
kbahar3 | 5:d39ffc5638a3 | 562 | BM1383_Temp_Conv_Out = (float)BM1383_Temp_Out/32; |
kbahar3 | 5:d39ffc5638a3 | 563 | |
kbahar3 | 5:d39ffc5638a3 | 564 | BM1383_Var = (Press_Content_ReadData[2]<<3) | (Press_Content_ReadData[3] >> 5); |
kbahar3 | 5:d39ffc5638a3 | 565 | BM1383_Deci = ((Press_Content_ReadData[3] & 0x1f) << 6 | ((Press_Content_ReadData[4] >> 2))); |
kbahar3 | 5:d39ffc5638a3 | 566 | BM1383_Deci = (float)BM1383_Deci* 0.00048828125; //0.00048828125 = 2^-11 |
kbahar3 | 5:d39ffc5638a3 | 567 | BM1383_Pres_Conv_Out = (BM1383_Var + BM1383_Deci); //question pending here... |
kbahar3 | 5:d39ffc5638a3 | 568 | |
kbahar3 | 5:d39ffc5638a3 | 569 | len = snprintf((char*) buf, MAX_REPLY_LEN, "Temp_out= %0.2f", BM1383_Temp_Out); |
kbahar3 | 5:d39ffc5638a3 | 570 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 5:d39ffc5638a3 | 571 | wait_ms(25); |
kbahar3 | 5:d39ffc5638a3 | 572 | |
kbahar3 | 5:d39ffc5638a3 | 573 | len = snprintf((char*) buf, MAX_REPLY_LEN, "Temp_conv= %0.2f", BM1383_Temp_Conv_Out); |
kbahar3 | 5:d39ffc5638a3 | 574 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 5:d39ffc5638a3 | 575 | wait_ms(25); |
kbahar3 | 5:d39ffc5638a3 | 576 | |
kbahar3 | 5:d39ffc5638a3 | 577 | len = snprintf((char*) buf, MAX_REPLY_LEN, "Press_conv= %0.2f", BM1383_Pres_Conv_Out); |
kbahar3 | 5:d39ffc5638a3 | 578 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 5:d39ffc5638a3 | 579 | wait_ms(25); |
kbahar3 | 5:d39ffc5638a3 | 580 | } |
kbahar3 | 5:d39ffc5638a3 | 581 | #endif |
kbahar3 | 5:d39ffc5638a3 | 582 | |
kbahar3 | 5:d39ffc5638a3 | 583 | i=1; |
kbahar3 | 5:d39ffc5638a3 | 584 | |
kbahar3 | 5:d39ffc5638a3 | 585 | } |
kbahar3 | 5:d39ffc5638a3 | 586 | |
kbahar3 | 5:d39ffc5638a3 | 587 | |
kbahar3 | 5:d39ffc5638a3 | 588 | |
kbahar3 | 5:d39ffc5638a3 | 589 | } |
Daniel Veilleux |
0:442c7a6f1978 | 590 | void error(ble_error_t err, uint32_t line) |
Daniel Veilleux |
0:442c7a6f1978 | 591 | { |
Daniel Veilleux |
0:442c7a6f1978 | 592 | m_error_led = 1; |
Daniel Veilleux |
0:442c7a6f1978 | 593 | DEBUG("Error %d on line number %d\n\r", err, line); |
Daniel Veilleux |
0:442c7a6f1978 | 594 | } |
Daniel Veilleux |
0:442c7a6f1978 | 595 | |
kbahar3 | 1:2c0ab5cd1a7f | 596 | void PBTrigger() |
kbahar3 | 1:2c0ab5cd1a7f | 597 | { |
kbahar3 | 1:2c0ab5cd1a7f | 598 | uint8_t buf[MAX_REPLY_LEN]; |
kbahar3 | 1:2c0ab5cd1a7f | 599 | uint32_t len = 0; |
kbahar3 | 1:2c0ab5cd1a7f | 600 | |
kbahar3 | 1:2c0ab5cd1a7f | 601 | m_cmd_led = !m_cmd_led; |
kbahar3 | 1:2c0ab5cd1a7f | 602 | |
kbahar3 | 2:c7b9d588c80f | 603 | |
kbahar3 | 1:2c0ab5cd1a7f | 604 | if (m_ble.getGapState().connected) { |
kbahar3 | 2:c7b9d588c80f | 605 | /* |
kbahar3 | 1:2c0ab5cd1a7f | 606 | BH1620_ALS_value = BH1620_ALS.read_u16(); |
kbahar3 | 1:2c0ab5cd1a7f | 607 | BH1620_output = (float)BH1620_ALS_value * 1.543; |
kbahar3 | 1:2c0ab5cd1a7f | 608 | |
kbahar3 | 1:2c0ab5cd1a7f | 609 | len = snprintf((char*) buf, MAX_REPLY_LEN, "ALS = %.2f lx", BH1620_output); |
kbahar3 | 1:2c0ab5cd1a7f | 610 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 2:c7b9d588c80f | 611 | */ |
kbahar3 | 1:2c0ab5cd1a7f | 612 | } |
kbahar3 | 1:2c0ab5cd1a7f | 613 | } |
Daniel Veilleux |
0:442c7a6f1978 | 614 | |
Daniel Veilleux |
0:442c7a6f1978 | 615 | int main(void) |
Daniel Veilleux |
0:442c7a6f1978 | 616 | { |
Daniel Veilleux |
0:442c7a6f1978 | 617 | ble_error_t err; |
Daniel Veilleux |
0:442c7a6f1978 | 618 | Ticker ticker; |
Daniel Veilleux |
0:442c7a6f1978 | 619 | |
Daniel Veilleux |
0:442c7a6f1978 | 620 | m_serial_port.baud(UART_BAUD_RATE); |
Daniel Veilleux |
0:442c7a6f1978 | 621 | |
kbahar3 | 2:c7b9d588c80f | 622 | DEBUG("Initialising...\n\r"); |
Daniel Veilleux |
0:442c7a6f1978 | 623 | |
Daniel Veilleux |
0:442c7a6f1978 | 624 | m_cmd_led = 0; |
Daniel Veilleux |
0:442c7a6f1978 | 625 | m_error_led = 0; |
kbahar3 | 2:c7b9d588c80f | 626 | //BH1620_ALS_value = 0; |
Daniel Veilleux |
0:442c7a6f1978 | 627 | |
Daniel Veilleux |
0:442c7a6f1978 | 628 | ticker.attach(periodicCallback, SENSOR_READ_INTERVAL_S); |
Daniel Veilleux |
0:442c7a6f1978 | 629 | |
kbahar3 | 1:2c0ab5cd1a7f | 630 | sw4Press.fall(&PBTrigger); |
kbahar3 | 1:2c0ab5cd1a7f | 631 | |
kbahar3 | 5:d39ffc5638a3 | 632 | #ifdef RPR0521 //als digital |
kbahar3 | 2:c7b9d588c80f | 633 | // 1. Mode Control (0x41), write (0xC6): ALS EN, PS EN, 100ms measurement for ALS and PS, PS_PULSE=1 |
kbahar3 | 2:c7b9d588c80f | 634 | // 2. ALS_PS_CONTROL (0x42), write (0x03): LED Current = 200mA |
kbahar3 | 2:c7b9d588c80f | 635 | // 3. PERSIST (0x43), write (0x20): PS Gain x4 |
kbahar3 | 2:c7b9d588c80f | 636 | i2c.write(RPR0521_addr_w, &RPR0521_ModeControl[0], 2, false); |
kbahar3 | 2:c7b9d588c80f | 637 | i2c.write(RPR0521_addr_w, &RPR0521_ALSPSControl[0], 2, false); |
kbahar3 | 2:c7b9d588c80f | 638 | i2c.write(RPR0521_addr_w, &RPR0521_Persist[0], 2, false); |
kbahar3 | 1:2c0ab5cd1a7f | 639 | #endif |
kbahar3 | 1:2c0ab5cd1a7f | 640 | |
kbahar3 | 3:c3ee9d663fb8 | 641 | #ifdef KMX62 |
kbahar3 | 3:c3ee9d663fb8 | 642 | // 1. CNTL2 (0x3A), write (0x5F): 4g, Max RES, EN temp mag and accel |
kbahar3 | 3:c3ee9d663fb8 | 643 | i2c.write(KMX62_addr_w, &KMX62_CNTL2[0], 2, false); |
kbahar3 | 3:c3ee9d663fb8 | 644 | #endif |
kbahar3 | 3:c3ee9d663fb8 | 645 | |
kbahar3 | 5:d39ffc5638a3 | 646 | #ifdef color |
kbahar3 | 5:d39ffc5638a3 | 647 | // 1. CNTL2 (0x3A), write (0x5F): 4g, Max RES, EN temp mag and accel |
kbahar3 | 5:d39ffc5638a3 | 648 | i2c.write(BH1745_addr_w, &BH1745_persistence[0], 2, false); |
kbahar3 | 5:d39ffc5638a3 | 649 | i2c.write(BH1745_addr_w, &BH1745_mode1[0], 2, false); |
kbahar3 | 5:d39ffc5638a3 | 650 | i2c.write(BH1745_addr_w, &BH1745_mode2[0], 2, false); |
kbahar3 | 5:d39ffc5638a3 | 651 | i2c.write(BH1745_addr_w, &BH1745_mode3[0], 2, false); |
kbahar3 | 5:d39ffc5638a3 | 652 | #endif |
kbahar3 | 5:d39ffc5638a3 | 653 | |
kbahar3 | 5:d39ffc5638a3 | 654 | #ifdef KX022 |
kbahar3 | 5:d39ffc5638a3 | 655 | |
kbahar3 | 5:d39ffc5638a3 | 656 | i2c.write(KX022_addr_w, &KX022_Accel_CNTL1[0], 2, false); |
kbahar3 | 5:d39ffc5638a3 | 657 | i2c.write(KX022_addr_w, &KX022_Accel_ODCNTL[0], 2, false); |
kbahar3 | 5:d39ffc5638a3 | 658 | i2c.write(KX022_addr_w, &KX022_Accel_CNTL3[0], 2, false); |
kbahar3 | 5:d39ffc5638a3 | 659 | i2c.write(KX022_addr_w, &KX022_Accel_TILT_TIMER[0], 2, false); |
kbahar3 | 5:d39ffc5638a3 | 660 | i2c.write(KX022_addr_w, &KX022_Accel_CNTL2[0], 2, false); |
kbahar3 | 5:d39ffc5638a3 | 661 | |
kbahar3 | 5:d39ffc5638a3 | 662 | #endif |
kbahar3 | 5:d39ffc5638a3 | 663 | |
kbahar3 | 5:d39ffc5638a3 | 664 | #ifdef Pressure |
kbahar3 | 5:d39ffc5638a3 | 665 | |
kbahar3 | 5:d39ffc5638a3 | 666 | i2c.write(Press_addr_w, &PWR_DOWN[0], 2, false); |
kbahar3 | 5:d39ffc5638a3 | 667 | i2c.write(Press_addr_w, &SLEEP[0], 2, false); |
kbahar3 | 5:d39ffc5638a3 | 668 | i2c.write(Press_addr_w, &Mode_Control[0], 2, false); |
kbahar3 | 5:d39ffc5638a3 | 669 | |
kbahar3 | 5:d39ffc5638a3 | 670 | #endif |
kbahar3 | 5:d39ffc5638a3 | 671 | |
kbahar3 | 2:c7b9d588c80f | 672 | //Start BTLE Initialization Section |
Daniel Veilleux |
0:442c7a6f1978 | 673 | m_ble.init(); |
Daniel Veilleux |
0:442c7a6f1978 | 674 | m_ble.onDisconnection(disconnectionCallback); |
Daniel Veilleux |
0:442c7a6f1978 | 675 | m_ble.onDataWritten(dataWrittenCallback); |
Daniel Veilleux |
0:442c7a6f1978 | 676 | m_ble.onDataSent(dataSentCallback); |
Daniel Veilleux |
0:442c7a6f1978 | 677 | |
Daniel Veilleux |
0:442c7a6f1978 | 678 | // Set the TX power in dBm units. |
Daniel Veilleux |
0:442c7a6f1978 | 679 | // Possible values (in decreasing order): 4, 0, -4, -8, -12, -16, -20. |
Daniel Veilleux |
0:442c7a6f1978 | 680 | err = m_ble.setTxPower(4); |
Daniel Veilleux |
0:442c7a6f1978 | 681 | if (BLE_ERROR_NONE != err) { |
Daniel Veilleux |
0:442c7a6f1978 | 682 | error(err, __LINE__); |
Daniel Veilleux |
0:442c7a6f1978 | 683 | } |
Daniel Veilleux |
0:442c7a6f1978 | 684 | |
Daniel Veilleux |
0:442c7a6f1978 | 685 | // Setup advertising (GAP stuff). |
Daniel Veilleux |
0:442c7a6f1978 | 686 | err = m_ble.setDeviceName(DEVICE_NAME); |
Daniel Veilleux |
0:442c7a6f1978 | 687 | if (BLE_ERROR_NONE != err) { |
Daniel Veilleux |
0:442c7a6f1978 | 688 | error(err, __LINE__); |
Daniel Veilleux |
0:442c7a6f1978 | 689 | } |
Daniel Veilleux |
0:442c7a6f1978 | 690 | |
Daniel Veilleux |
0:442c7a6f1978 | 691 | err = m_ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
Daniel Veilleux |
0:442c7a6f1978 | 692 | if (BLE_ERROR_NONE != err) { |
Daniel Veilleux |
0:442c7a6f1978 | 693 | error(err, __LINE__); |
Daniel Veilleux |
0:442c7a6f1978 | 694 | } |
Daniel Veilleux |
0:442c7a6f1978 | 695 | |
Daniel Veilleux |
0:442c7a6f1978 | 696 | m_ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Daniel Veilleux |
0:442c7a6f1978 | 697 | |
Daniel Veilleux |
0:442c7a6f1978 | 698 | err = m_ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
Daniel Veilleux |
0:442c7a6f1978 | 699 | (const uint8_t *)SHORT_NAME, |
Daniel Veilleux |
0:442c7a6f1978 | 700 | (sizeof(SHORT_NAME) - 1)); |
Daniel Veilleux |
0:442c7a6f1978 | 701 | if (BLE_ERROR_NONE != err) { |
Daniel Veilleux |
0:442c7a6f1978 | 702 | error(err, __LINE__); |
Daniel Veilleux |
0:442c7a6f1978 | 703 | } |
Daniel Veilleux |
0:442c7a6f1978 | 704 | |
Daniel Veilleux |
0:442c7a6f1978 | 705 | err = m_ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
Daniel Veilleux |
0:442c7a6f1978 | 706 | (const uint8_t *)UARTServiceUUID_reversed, |
Daniel Veilleux |
0:442c7a6f1978 | 707 | sizeof(UARTServiceUUID_reversed)); |
Daniel Veilleux |
0:442c7a6f1978 | 708 | if (BLE_ERROR_NONE != err) { |
Daniel Veilleux |
0:442c7a6f1978 | 709 | error(err, __LINE__); |
Daniel Veilleux |
0:442c7a6f1978 | 710 | } |
Daniel Veilleux |
0:442c7a6f1978 | 711 | |
Daniel Veilleux |
0:442c7a6f1978 | 712 | m_ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(ADV_INTERVAL_MS)); |
Daniel Veilleux |
0:442c7a6f1978 | 713 | m_ble.startAdvertising(); |
Daniel Veilleux |
0:442c7a6f1978 | 714 | |
Daniel Veilleux |
0:442c7a6f1978 | 715 | // Create a UARTService object (GATT stuff). |
Daniel Veilleux |
0:442c7a6f1978 | 716 | UARTService uartService(m_ble); |
Daniel Veilleux |
0:442c7a6f1978 | 717 | m_uart_service_ptr = &uartService; |
Daniel Veilleux |
0:442c7a6f1978 | 718 | |
Daniel Veilleux |
0:442c7a6f1978 | 719 | while (true) { |
Daniel Veilleux |
0:442c7a6f1978 | 720 | m_ble.waitForEvent(); |
Daniel Veilleux |
0:442c7a6f1978 | 721 | } |
kbahar3 | 5:d39ffc5638a3 | 722 | } |