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
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:
main.cpp@1:2c0ab5cd1a7f, 2015-07-19 (annotated)
- Committer:
- kbahar3
- Date:
- Sun Jul 19 23:14:07 2015 +0000
- Revision:
- 1:2c0ab5cd1a7f
- Parent:
- 0:442c7a6f1978
- Child:
- 2:c7b9d588c80f
Added some ROHM Functions
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 | */ |
Daniel Veilleux |
0:442c7a6f1978 | 16 | |
kbahar3 | 1:2c0ab5cd1a7f | 17 | #define AnalogALS //BH1620 //Change 0: Remove this completely |
kbahar3 | 1:2c0ab5cd1a7f | 18 | #define AnalogTemp //BDE0600 |
kbahar3 | 1:2c0ab5cd1a7f | 19 | #define AnalogUV //ML8511 |
kbahar3 | 1:2c0ab5cd1a7f | 20 | #define HallSensor //BU52011 //Change 1: Change to use GPIO for BU52014 |
kbahar3 | 1:2c0ab5cd1a7f | 21 | #define DigitalALS //BH1721 //Change 2: Remove This and add in the RPR-0521 |
kbahar3 | 1:2c0ab5cd1a7f | 22 | //Change 3: Add Code For BH1745, KX022, BM1383GLV, KMX62 |
kbahar3 | 1:2c0ab5cd1a7f | 23 | |
kbahar3 | 1:2c0ab5cd1a7f | 24 | |
Daniel Veilleux |
0:442c7a6f1978 | 25 | #include "mbed.h" |
Daniel Veilleux |
0:442c7a6f1978 | 26 | #include "BLEDevice.h" |
Daniel Veilleux |
0:442c7a6f1978 | 27 | #include "UARTService.h" |
Daniel Veilleux |
0:442c7a6f1978 | 28 | #include "nrf_temp.h" |
Daniel Veilleux |
0:442c7a6f1978 | 29 | |
kbahar3 | 1:2c0ab5cd1a7f | 30 | #define MAX_REPLY_LEN (UARTService::BLE_UART_SERVICE_MAX_DATA_LEN) //Actually equal to 20 |
kbahar3 | 1:2c0ab5cd1a7f | 31 | #define SENSOR_READ_INTERVAL_S (2.0F) |
Daniel Veilleux |
0:442c7a6f1978 | 32 | #define ADV_INTERVAL_MS (1000UL) |
Daniel Veilleux |
0:442c7a6f1978 | 33 | #define UART_BAUD_RATE (19200UL) |
Daniel Veilleux |
0:442c7a6f1978 | 34 | #define DEVICE_NAME ("DEMO SENSOR") // This can be read AFTER connecting to the device. |
kbahar3 | 1:2c0ab5cd1a7f | 35 | #define SHORT_NAME ("ROHMKRIS") // Keep this short: max 8 chars if a 128bit UUID is also advertised. |
Daniel Veilleux |
0:442c7a6f1978 | 36 | |
Daniel Veilleux |
0:442c7a6f1978 | 37 | #define DEBUG(...) { m_serial_port.printf(__VA_ARGS__); } |
Daniel Veilleux |
0:442c7a6f1978 | 38 | |
kbahar3 | 1:2c0ab5cd1a7f | 39 | // Function Prototypes |
kbahar3 | 1:2c0ab5cd1a7f | 40 | void PBTrigger(); //Interrupt function for PB4 |
Daniel Veilleux |
0:442c7a6f1978 | 41 | |
kbahar3 | 1:2c0ab5cd1a7f | 42 | // Global Variables |
Daniel Veilleux |
0:442c7a6f1978 | 43 | BLEDevice m_ble; |
Daniel Veilleux |
0:442c7a6f1978 | 44 | Serial m_serial_port(p9, p11); // TX pin, RX pin |
Daniel Veilleux |
0:442c7a6f1978 | 45 | DigitalOut m_cmd_led(LED1); |
Daniel Veilleux |
0:442c7a6f1978 | 46 | DigitalOut m_error_led(LED2); |
Daniel Veilleux |
0:442c7a6f1978 | 47 | UARTService *m_uart_service_ptr; |
kbahar3 | 1:2c0ab5cd1a7f | 48 | DigitalIn testButton(p20); |
kbahar3 | 1:2c0ab5cd1a7f | 49 | InterruptIn sw4Press(p20); |
kbahar3 | 1:2c0ab5cd1a7f | 50 | I2C i2c(p30,p7); |
Daniel Veilleux |
0:442c7a6f1978 | 51 | |
kbahar3 | 1:2c0ab5cd1a7f | 52 | //Sensor Variables |
kbahar3 | 1:2c0ab5cd1a7f | 53 | AnalogIn BH1620_ALS(p1); |
kbahar3 | 1:2c0ab5cd1a7f | 54 | uint16_t BH1620_ALS_value; |
kbahar3 | 1:2c0ab5cd1a7f | 55 | float BH1620_output; |
kbahar3 | 1:2c0ab5cd1a7f | 56 | |
kbahar3 | 1:2c0ab5cd1a7f | 57 | AnalogIn BDE0600_Temp(p2); |
kbahar3 | 1:2c0ab5cd1a7f | 58 | uint16_t BDE0600_Temp_value; |
kbahar3 | 1:2c0ab5cd1a7f | 59 | float BDE0600_output; |
kbahar3 | 1:2c0ab5cd1a7f | 60 | |
kbahar3 | 1:2c0ab5cd1a7f | 61 | AnalogIn ML8511_UV(p3); |
kbahar3 | 1:2c0ab5cd1a7f | 62 | uint16_t ML8511_UV_value; |
kbahar3 | 1:2c0ab5cd1a7f | 63 | float ML8511_output; |
kbahar3 | 1:2c0ab5cd1a7f | 64 | |
kbahar3 | 1:2c0ab5cd1a7f | 65 | DigitalIn Hall_GPIO0(p28); |
kbahar3 | 1:2c0ab5cd1a7f | 66 | DigitalIn Hall_GPIO1(p29); |
kbahar3 | 1:2c0ab5cd1a7f | 67 | int Hall_Return1; |
kbahar3 | 1:2c0ab5cd1a7f | 68 | int Hall_Return0; |
kbahar3 | 1:2c0ab5cd1a7f | 69 | |
kbahar3 | 1:2c0ab5cd1a7f | 70 | int ALS_addr_w = 0x46; //7bit addr = 0x23, with write bit 0 |
kbahar3 | 1:2c0ab5cd1a7f | 71 | int ALS_addr_r = 0x47; //7bit addr = 0x23, with read bit 1 |
kbahar3 | 1:2c0ab5cd1a7f | 72 | char ALS_PwrOn_cmd = 0x01; |
kbahar3 | 1:2c0ab5cd1a7f | 73 | char ALS_ContAuto_cmd = 0x10; |
kbahar3 | 1:2c0ab5cd1a7f | 74 | char ALS_ReturnData_raw[2]; |
kbahar3 | 1:2c0ab5cd1a7f | 75 | float ALS_Return = 0; |
Daniel Veilleux |
0:442c7a6f1978 | 76 | |
Daniel Veilleux |
0:442c7a6f1978 | 77 | /** |
Daniel Veilleux |
0:442c7a6f1978 | 78 | * This callback is used whenever a disconnection occurs. |
Daniel Veilleux |
0:442c7a6f1978 | 79 | */ |
Daniel Veilleux |
0:442c7a6f1978 | 80 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
Daniel Veilleux |
0:442c7a6f1978 | 81 | { |
Daniel Veilleux |
0:442c7a6f1978 | 82 | switch (reason) { |
Daniel Veilleux |
0:442c7a6f1978 | 83 | case Gap::REMOTE_USER_TERMINATED_CONNECTION: |
Daniel Veilleux |
0:442c7a6f1978 | 84 | DEBUG("Disconnected (REMOTE_USER_TERMINATED_CONNECTION)\n\r"); |
Daniel Veilleux |
0:442c7a6f1978 | 85 | break; |
Daniel Veilleux |
0:442c7a6f1978 | 86 | case Gap::LOCAL_HOST_TERMINATED_CONNECTION: |
Daniel Veilleux |
0:442c7a6f1978 | 87 | DEBUG("Disconnected (LOCAL_HOST_TERMINATED_CONNECTION)\n\r"); |
Daniel Veilleux |
0:442c7a6f1978 | 88 | break; |
Daniel Veilleux |
0:442c7a6f1978 | 89 | case Gap::CONN_INTERVAL_UNACCEPTABLE: |
Daniel Veilleux |
0:442c7a6f1978 | 90 | DEBUG("Disconnected (CONN_INTERVAL_UNACCEPTABLE)\n\r"); |
Daniel Veilleux |
0:442c7a6f1978 | 91 | break; |
Daniel Veilleux |
0:442c7a6f1978 | 92 | } |
Daniel Veilleux |
0:442c7a6f1978 | 93 | |
Daniel Veilleux |
0:442c7a6f1978 | 94 | DEBUG("Restarting the advertising process\n\r"); |
Daniel Veilleux |
0:442c7a6f1978 | 95 | m_ble.startAdvertising(); |
Daniel Veilleux |
0:442c7a6f1978 | 96 | } |
Daniel Veilleux |
0:442c7a6f1978 | 97 | |
Daniel Veilleux |
0:442c7a6f1978 | 98 | |
Daniel Veilleux |
0:442c7a6f1978 | 99 | /** |
Daniel Veilleux |
0:442c7a6f1978 | 100 | * This callback is used whenever the host writes data to one of our GATT characteristics. |
Daniel Veilleux |
0:442c7a6f1978 | 101 | */ |
Daniel Veilleux |
0:442c7a6f1978 | 102 | void dataWrittenCallback(const GattCharacteristicWriteCBParams *params) |
Daniel Veilleux |
0:442c7a6f1978 | 103 | { |
Daniel Veilleux |
0:442c7a6f1978 | 104 | // Ensure that initialization is finished and the host has written to the TX characteristic. |
Daniel Veilleux |
0:442c7a6f1978 | 105 | if ((m_uart_service_ptr != NULL) && (params->charHandle == m_uart_service_ptr->getTXCharacteristicHandle())) { |
Daniel Veilleux |
0:442c7a6f1978 | 106 | uint8_t buf[MAX_REPLY_LEN]; |
Daniel Veilleux |
0:442c7a6f1978 | 107 | uint32_t len = 0; |
Daniel Veilleux |
0:442c7a6f1978 | 108 | |
Daniel Veilleux |
0:442c7a6f1978 | 109 | if (1 == params->len) { |
Daniel Veilleux |
0:442c7a6f1978 | 110 | switch (params->data[0]) { |
Daniel Veilleux |
0:442c7a6f1978 | 111 | case '0': |
kbahar3 | 1:2c0ab5cd1a7f | 112 | m_cmd_led = m_cmd_led ^ 1; |
kbahar3 | 1:2c0ab5cd1a7f | 113 | len = snprintf((char*) buf, MAX_REPLY_LEN, "OK... LED ON"); |
Daniel Veilleux |
0:442c7a6f1978 | 114 | break; |
Daniel Veilleux |
0:442c7a6f1978 | 115 | case '1': |
kbahar3 | 1:2c0ab5cd1a7f | 116 | m_cmd_led = m_cmd_led ^ 1; |
kbahar3 | 1:2c0ab5cd1a7f | 117 | len = snprintf((char*) buf, MAX_REPLY_LEN, "OK... LED OFF"); |
Daniel Veilleux |
0:442c7a6f1978 | 118 | break; |
Daniel Veilleux |
0:442c7a6f1978 | 119 | case 'a': |
kbahar3 | 1:2c0ab5cd1a7f | 120 | len = snprintf((char*) buf, MAX_REPLY_LEN, "ALSRaw = %d", BH1620_ALS_value); |
kbahar3 | 1:2c0ab5cd1a7f | 121 | break; |
kbahar3 | 1:2c0ab5cd1a7f | 122 | case 'b': |
kbahar3 | 1:2c0ab5cd1a7f | 123 | len = snprintf((char*) buf, MAX_REPLY_LEN, "ALS = %.2f lx", BH1620_output); |
Daniel Veilleux |
0:442c7a6f1978 | 124 | break; |
Daniel Veilleux |
0:442c7a6f1978 | 125 | default: |
kbahar3 | 1:2c0ab5cd1a7f | 126 | len = snprintf((char*) buf, MAX_REPLY_LEN, "ERROR"); |
Daniel Veilleux |
0:442c7a6f1978 | 127 | break; |
Daniel Veilleux |
0:442c7a6f1978 | 128 | } |
Daniel Veilleux |
0:442c7a6f1978 | 129 | } |
Daniel Veilleux |
0:442c7a6f1978 | 130 | else |
Daniel Veilleux |
0:442c7a6f1978 | 131 | { |
kbahar3 | 1:2c0ab5cd1a7f | 132 | len = snprintf((char*) buf, MAX_REPLY_LEN, "ERROR"); |
Daniel Veilleux |
0:442c7a6f1978 | 133 | } |
Daniel Veilleux |
0:442c7a6f1978 | 134 | |
Daniel Veilleux |
0:442c7a6f1978 | 135 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
Daniel Veilleux |
0:442c7a6f1978 | 136 | |
Daniel Veilleux |
0:442c7a6f1978 | 137 | DEBUG("%d bytes received from host\n\r", params->len); |
Daniel Veilleux |
0:442c7a6f1978 | 138 | } |
Daniel Veilleux |
0:442c7a6f1978 | 139 | } |
Daniel Veilleux |
0:442c7a6f1978 | 140 | |
Daniel Veilleux |
0:442c7a6f1978 | 141 | |
Daniel Veilleux |
0:442c7a6f1978 | 142 | /** |
Daniel Veilleux |
0:442c7a6f1978 | 143 | * This callback is used whenever a write to a GATT characteristic causes data to be sent to the host. |
Daniel Veilleux |
0:442c7a6f1978 | 144 | */ |
Daniel Veilleux |
0:442c7a6f1978 | 145 | void dataSentCallback(unsigned count) |
Daniel Veilleux |
0:442c7a6f1978 | 146 | { |
Daniel Veilleux |
0:442c7a6f1978 | 147 | // NOTE: The count always seems to be 1 regardless of data. |
Daniel Veilleux |
0:442c7a6f1978 | 148 | DEBUG("%d bytes sent to host\n\r", count); |
Daniel Veilleux |
0:442c7a6f1978 | 149 | } |
Daniel Veilleux |
0:442c7a6f1978 | 150 | |
Daniel Veilleux |
0:442c7a6f1978 | 151 | |
Daniel Veilleux |
0:442c7a6f1978 | 152 | /** |
Daniel Veilleux |
0:442c7a6f1978 | 153 | * This callback is scheduled to be called periodically via a low-priority interrupt. |
Daniel Veilleux |
0:442c7a6f1978 | 154 | */ |
Daniel Veilleux |
0:442c7a6f1978 | 155 | void periodicCallback(void) |
Daniel Veilleux |
0:442c7a6f1978 | 156 | { |
kbahar3 | 1:2c0ab5cd1a7f | 157 | uint8_t buf[MAX_REPLY_LEN]; |
kbahar3 | 1:2c0ab5cd1a7f | 158 | uint32_t len = 0; |
kbahar3 | 1:2c0ab5cd1a7f | 159 | |
kbahar3 | 1:2c0ab5cd1a7f | 160 | |
kbahar3 | 1:2c0ab5cd1a7f | 161 | #ifdef AnalogALS |
kbahar3 | 1:2c0ab5cd1a7f | 162 | if (m_ble.getGapState().connected) { |
kbahar3 | 1:2c0ab5cd1a7f | 163 | BH1620_ALS_value = BH1620_ALS.read_u16(); |
kbahar3 | 1:2c0ab5cd1a7f | 164 | BH1620_output = (float)BH1620_ALS_value * 1.543; |
kbahar3 | 1:2c0ab5cd1a7f | 165 | |
kbahar3 | 1:2c0ab5cd1a7f | 166 | len = snprintf((char*) buf, MAX_REPLY_LEN, "ALS = %.2f lx", BH1620_output); |
kbahar3 | 1:2c0ab5cd1a7f | 167 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 1:2c0ab5cd1a7f | 168 | } |
kbahar3 | 1:2c0ab5cd1a7f | 169 | #endif |
kbahar3 | 1:2c0ab5cd1a7f | 170 | |
kbahar3 | 1:2c0ab5cd1a7f | 171 | #ifdef AnalogTemp |
kbahar3 | 1:2c0ab5cd1a7f | 172 | if (m_ble.getGapState().connected) { |
kbahar3 | 1:2c0ab5cd1a7f | 173 | BDE0600_Temp_value = BDE0600_Temp.read_u16(); |
kbahar3 | 1:2c0ab5cd1a7f | 174 | BDE0600_output = (float)BDE0600_Temp_value * 0.00283; //(value * (2.9V/1024)) |
kbahar3 | 1:2c0ab5cd1a7f | 175 | BDE0600_output = (BDE0600_output-1.753)/(-0.01068) + 30; |
kbahar3 | 1:2c0ab5cd1a7f | 176 | |
kbahar3 | 1:2c0ab5cd1a7f | 177 | len = snprintf((char*) buf, MAX_REPLY_LEN, "Temp = %.2f C", BDE0600_output); |
kbahar3 | 1:2c0ab5cd1a7f | 178 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 1:2c0ab5cd1a7f | 179 | |
kbahar3 | 1:2c0ab5cd1a7f | 180 | } |
kbahar3 | 1:2c0ab5cd1a7f | 181 | #endif |
kbahar3 | 1:2c0ab5cd1a7f | 182 | |
kbahar3 | 1:2c0ab5cd1a7f | 183 | #ifdef AnalogUV |
kbahar3 | 1:2c0ab5cd1a7f | 184 | if (m_ble.getGapState().connected) { |
kbahar3 | 1:2c0ab5cd1a7f | 185 | ML8511_UV_value = ML8511_UV.read_u16(); |
kbahar3 | 1:2c0ab5cd1a7f | 186 | ML8511_output = (float)ML8511_UV_value * 0.00283; //(value * (2.9V/1024)) //Note to self: when playing with this, a negative value is seen... Honestly, I think this has to do with my ADC converstion... |
kbahar3 | 1:2c0ab5cd1a7f | 187 | ML8511_output = (ML8511_output-2.2)/(0.129) + 15; // Added +5 to the offset so when inside (aka, no UV, readings show 0)... this is the wrong approach... and the readings don't make sense... Fix this. |
kbahar3 | 1:2c0ab5cd1a7f | 188 | |
kbahar3 | 1:2c0ab5cd1a7f | 189 | len = snprintf((char*) buf, MAX_REPLY_LEN, "UV = %.1f mW/cm2", ML8511_output); |
kbahar3 | 1:2c0ab5cd1a7f | 190 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 1:2c0ab5cd1a7f | 191 | } |
kbahar3 | 1:2c0ab5cd1a7f | 192 | #endif |
kbahar3 | 1:2c0ab5cd1a7f | 193 | |
kbahar3 | 1:2c0ab5cd1a7f | 194 | #ifdef HallSensor |
kbahar3 | 1:2c0ab5cd1a7f | 195 | if (m_ble.getGapState().connected) { |
kbahar3 | 1:2c0ab5cd1a7f | 196 | Hall_Return0 = Hall_GPIO0; |
kbahar3 | 1:2c0ab5cd1a7f | 197 | Hall_Return1 = Hall_GPIO1; |
kbahar3 | 1:2c0ab5cd1a7f | 198 | |
kbahar3 | 1:2c0ab5cd1a7f | 199 | len = snprintf((char*) buf, MAX_REPLY_LEN, "H0 = %d, H1 = %d", Hall_Return0, Hall_Return1); |
kbahar3 | 1:2c0ab5cd1a7f | 200 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 1:2c0ab5cd1a7f | 201 | } |
kbahar3 | 1:2c0ab5cd1a7f | 202 | #endif |
kbahar3 | 1:2c0ab5cd1a7f | 203 | |
kbahar3 | 1:2c0ab5cd1a7f | 204 | #ifdef DigitalALS |
kbahar3 | 1:2c0ab5cd1a7f | 205 | if (m_ble.getGapState().connected) { |
kbahar3 | 1:2c0ab5cd1a7f | 206 | i2c.read(ALS_addr_r, ALS_ReturnData_raw, 2); |
kbahar3 | 1:2c0ab5cd1a7f | 207 | ALS_Return = (ALS_ReturnData_raw[0]<<8) | ALS_ReturnData_raw[1]; |
kbahar3 | 1:2c0ab5cd1a7f | 208 | ALS_Return = ALS_Return/1.2; |
kbahar3 | 1:2c0ab5cd1a7f | 209 | |
kbahar3 | 1:2c0ab5cd1a7f | 210 | len = snprintf((char*) buf, MAX_REPLY_LEN, "DALS= %0.2f lx", ALS_Return); |
kbahar3 | 1:2c0ab5cd1a7f | 211 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 1:2c0ab5cd1a7f | 212 | } |
kbahar3 | 1:2c0ab5cd1a7f | 213 | #endif |
kbahar3 | 1:2c0ab5cd1a7f | 214 | |
kbahar3 | 1:2c0ab5cd1a7f | 215 | |
kbahar3 | 1:2c0ab5cd1a7f | 216 | |
kbahar3 | 1:2c0ab5cd1a7f | 217 | |
kbahar3 | 1:2c0ab5cd1a7f | 218 | if (m_ble.getGapState().connected) { |
kbahar3 | 1:2c0ab5cd1a7f | 219 | len = snprintf((char*) buf, MAX_REPLY_LEN, " "); //Print and Extra Line to show new data |
kbahar3 | 1:2c0ab5cd1a7f | 220 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 1:2c0ab5cd1a7f | 221 | } |
kbahar3 | 1:2c0ab5cd1a7f | 222 | |
Daniel Veilleux |
0:442c7a6f1978 | 223 | } |
Daniel Veilleux |
0:442c7a6f1978 | 224 | |
Daniel Veilleux |
0:442c7a6f1978 | 225 | |
Daniel Veilleux |
0:442c7a6f1978 | 226 | void error(ble_error_t err, uint32_t line) |
Daniel Veilleux |
0:442c7a6f1978 | 227 | { |
Daniel Veilleux |
0:442c7a6f1978 | 228 | m_error_led = 1; |
Daniel Veilleux |
0:442c7a6f1978 | 229 | DEBUG("Error %d on line number %d\n\r", err, line); |
Daniel Veilleux |
0:442c7a6f1978 | 230 | } |
Daniel Veilleux |
0:442c7a6f1978 | 231 | |
kbahar3 | 1:2c0ab5cd1a7f | 232 | void PBTrigger() |
kbahar3 | 1:2c0ab5cd1a7f | 233 | { |
kbahar3 | 1:2c0ab5cd1a7f | 234 | uint8_t buf[MAX_REPLY_LEN]; |
kbahar3 | 1:2c0ab5cd1a7f | 235 | uint32_t len = 0; |
kbahar3 | 1:2c0ab5cd1a7f | 236 | |
kbahar3 | 1:2c0ab5cd1a7f | 237 | m_cmd_led = !m_cmd_led; |
kbahar3 | 1:2c0ab5cd1a7f | 238 | |
kbahar3 | 1:2c0ab5cd1a7f | 239 | if (m_ble.getGapState().connected) { |
kbahar3 | 1:2c0ab5cd1a7f | 240 | BH1620_ALS_value = BH1620_ALS.read_u16(); |
kbahar3 | 1:2c0ab5cd1a7f | 241 | BH1620_output = (float)BH1620_ALS_value * 1.543; |
kbahar3 | 1:2c0ab5cd1a7f | 242 | |
kbahar3 | 1:2c0ab5cd1a7f | 243 | len = snprintf((char*) buf, MAX_REPLY_LEN, "ALS = %.2f lx", BH1620_output); |
kbahar3 | 1:2c0ab5cd1a7f | 244 | m_ble.updateCharacteristicValue(m_uart_service_ptr->getRXCharacteristicHandle(), buf, len); |
kbahar3 | 1:2c0ab5cd1a7f | 245 | } |
kbahar3 | 1:2c0ab5cd1a7f | 246 | } |
Daniel Veilleux |
0:442c7a6f1978 | 247 | |
Daniel Veilleux |
0:442c7a6f1978 | 248 | int main(void) |
Daniel Veilleux |
0:442c7a6f1978 | 249 | { |
Daniel Veilleux |
0:442c7a6f1978 | 250 | ble_error_t err; |
Daniel Veilleux |
0:442c7a6f1978 | 251 | Ticker ticker; |
Daniel Veilleux |
0:442c7a6f1978 | 252 | |
Daniel Veilleux |
0:442c7a6f1978 | 253 | m_serial_port.baud(UART_BAUD_RATE); |
Daniel Veilleux |
0:442c7a6f1978 | 254 | |
Daniel Veilleux |
0:442c7a6f1978 | 255 | DEBUG("Initialising\n\r"); |
Daniel Veilleux |
0:442c7a6f1978 | 256 | |
Daniel Veilleux |
0:442c7a6f1978 | 257 | m_cmd_led = 0; |
Daniel Veilleux |
0:442c7a6f1978 | 258 | m_error_led = 0; |
kbahar3 | 1:2c0ab5cd1a7f | 259 | BH1620_ALS_value = 0; |
Daniel Veilleux |
0:442c7a6f1978 | 260 | |
Daniel Veilleux |
0:442c7a6f1978 | 261 | ticker.attach(periodicCallback, SENSOR_READ_INTERVAL_S); |
Daniel Veilleux |
0:442c7a6f1978 | 262 | |
kbahar3 | 1:2c0ab5cd1a7f | 263 | sw4Press.fall(&PBTrigger); |
kbahar3 | 1:2c0ab5cd1a7f | 264 | |
kbahar3 | 1:2c0ab5cd1a7f | 265 | #ifdef DigitalALS |
kbahar3 | 1:2c0ab5cd1a7f | 266 | i2c.write(ALS_addr_w, &ALS_PwrOn_cmd, 1); |
kbahar3 | 1:2c0ab5cd1a7f | 267 | i2c.write(ALS_addr_w, &ALS_ContAuto_cmd, 1); |
kbahar3 | 1:2c0ab5cd1a7f | 268 | #endif |
kbahar3 | 1:2c0ab5cd1a7f | 269 | |
Daniel Veilleux |
0:442c7a6f1978 | 270 | m_ble.init(); |
Daniel Veilleux |
0:442c7a6f1978 | 271 | m_ble.onDisconnection(disconnectionCallback); |
Daniel Veilleux |
0:442c7a6f1978 | 272 | m_ble.onDataWritten(dataWrittenCallback); |
Daniel Veilleux |
0:442c7a6f1978 | 273 | m_ble.onDataSent(dataSentCallback); |
Daniel Veilleux |
0:442c7a6f1978 | 274 | |
Daniel Veilleux |
0:442c7a6f1978 | 275 | // Set the TX power in dBm units. |
Daniel Veilleux |
0:442c7a6f1978 | 276 | // Possible values (in decreasing order): 4, 0, -4, -8, -12, -16, -20. |
Daniel Veilleux |
0:442c7a6f1978 | 277 | err = m_ble.setTxPower(4); |
Daniel Veilleux |
0:442c7a6f1978 | 278 | if (BLE_ERROR_NONE != err) { |
Daniel Veilleux |
0:442c7a6f1978 | 279 | error(err, __LINE__); |
Daniel Veilleux |
0:442c7a6f1978 | 280 | } |
Daniel Veilleux |
0:442c7a6f1978 | 281 | |
Daniel Veilleux |
0:442c7a6f1978 | 282 | // Setup advertising (GAP stuff). |
Daniel Veilleux |
0:442c7a6f1978 | 283 | err = m_ble.setDeviceName(DEVICE_NAME); |
Daniel Veilleux |
0:442c7a6f1978 | 284 | if (BLE_ERROR_NONE != err) { |
Daniel Veilleux |
0:442c7a6f1978 | 285 | error(err, __LINE__); |
Daniel Veilleux |
0:442c7a6f1978 | 286 | } |
Daniel Veilleux |
0:442c7a6f1978 | 287 | |
Daniel Veilleux |
0:442c7a6f1978 | 288 | err = m_ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
Daniel Veilleux |
0:442c7a6f1978 | 289 | if (BLE_ERROR_NONE != err) { |
Daniel Veilleux |
0:442c7a6f1978 | 290 | error(err, __LINE__); |
Daniel Veilleux |
0:442c7a6f1978 | 291 | } |
Daniel Veilleux |
0:442c7a6f1978 | 292 | |
Daniel Veilleux |
0:442c7a6f1978 | 293 | m_ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Daniel Veilleux |
0:442c7a6f1978 | 294 | |
Daniel Veilleux |
0:442c7a6f1978 | 295 | err = m_ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
Daniel Veilleux |
0:442c7a6f1978 | 296 | (const uint8_t *)SHORT_NAME, |
Daniel Veilleux |
0:442c7a6f1978 | 297 | (sizeof(SHORT_NAME) - 1)); |
Daniel Veilleux |
0:442c7a6f1978 | 298 | if (BLE_ERROR_NONE != err) { |
Daniel Veilleux |
0:442c7a6f1978 | 299 | error(err, __LINE__); |
Daniel Veilleux |
0:442c7a6f1978 | 300 | } |
Daniel Veilleux |
0:442c7a6f1978 | 301 | |
Daniel Veilleux |
0:442c7a6f1978 | 302 | err = m_ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, |
Daniel Veilleux |
0:442c7a6f1978 | 303 | (const uint8_t *)UARTServiceUUID_reversed, |
Daniel Veilleux |
0:442c7a6f1978 | 304 | sizeof(UARTServiceUUID_reversed)); |
Daniel Veilleux |
0:442c7a6f1978 | 305 | if (BLE_ERROR_NONE != err) { |
Daniel Veilleux |
0:442c7a6f1978 | 306 | error(err, __LINE__); |
Daniel Veilleux |
0:442c7a6f1978 | 307 | } |
Daniel Veilleux |
0:442c7a6f1978 | 308 | |
Daniel Veilleux |
0:442c7a6f1978 | 309 | m_ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(ADV_INTERVAL_MS)); |
Daniel Veilleux |
0:442c7a6f1978 | 310 | m_ble.startAdvertising(); |
Daniel Veilleux |
0:442c7a6f1978 | 311 | |
Daniel Veilleux |
0:442c7a6f1978 | 312 | // Create a UARTService object (GATT stuff). |
Daniel Veilleux |
0:442c7a6f1978 | 313 | UARTService uartService(m_ble); |
Daniel Veilleux |
0:442c7a6f1978 | 314 | m_uart_service_ptr = &uartService; |
Daniel Veilleux |
0:442c7a6f1978 | 315 | |
Daniel Veilleux |
0:442c7a6f1978 | 316 | while (true) { |
Daniel Veilleux |
0:442c7a6f1978 | 317 | m_ble.waitForEvent(); |
Daniel Veilleux |
0:442c7a6f1978 | 318 | } |
Daniel Veilleux |
0:442c7a6f1978 | 319 | } |