This is sample code for interfacing ROHM's SENSORSHLD1-EVK-101 with Nordic Semiconductor's nRF51-DK Development Kit Host BTLE Board

Dependencies:   BLE_API mbed nRF51822

Fork of Nordic_UART_TEMPLATE_ROHM by ROHMUSDC

Code Example for ROHM Mutli-Sensor Shield on the Nordic Semiconductor nRF51-DK

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

This is the basic example code for interfacing ROHM's Multi-sensor Shield Board onto this board.

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

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

Operation

Ultimately, this code will initialize all the sensors on the Multi-sensor shield board and then poll the sensors. The sensor data will then be returned to the BTLE COM port link and will be view-able on any BTLE enabled phone that can connect to the Nordic UART Application.

Supported ROHM Sensor Devices

  • BDE0600G Temperature Sensor
  • BM1383GLV Pressure Sensor
  • BU52014 Hall Sensor
  • ML8511 UV Sensor
  • RPR-0521 ALS/PROX Sensor
  • BH1745NUC Color Sensor
  • KMX62 Accel/Mag Sensor
  • KX122 Accel Sensor
  • BM1422 MI Magnetometer Sensor
  • KXG03 Gyro/Accel Sensor

Updates from SHLD0 to SHLD1

  • Pressure Sensor Changes: Fixed Register Map Changes for BM1383AGLV, See Pressure Sensor Datasheet for more details - TEMP and PRES output switched
  • Added new #ifdef section for Magnetometer
  • Changed Gyro Device Address (7bit addr now 0x4F, not 0x4E)

Sensor Applicable Code Sections

  • Added a Section in "Main" to act as initialization
  • Added to the "Periodic Callback" to read sensor data and return to Phone/Host

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 Sep 28 19:00:02 2015 +0000
Revision:
6:6860e53dc7ae
Parent:
5:d39ffc5638a3
Child:
7:71046927a0e9
Fixed Periodic Callback to send one sensor point every second.  Also, fixed comments sand general formatting to make the code easier to read.  Removed miscellaneous content/comments

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