SunTracker_BLE_Remote
Dependencies: BLE_API X_NUCLEO_IDB0XA1 X_NUCLEO_IKS01A1 mbed
Fork of SunTracker_BLE_Remote by
This application is the BLE Remote Control for the SunTracker demo application that you can find
here.
Please refer to that page for further information.
main.cpp@10:d00d09bc31d6, 2016-05-12 (annotated)
- Committer:
- fabiombed
- Date:
- Thu May 12 09:27:25 2016 +0000
- Revision:
- 10:d00d09bc31d6
- Parent:
- 9:d530044d91b9
Update mbed libraries
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fabiombed | 4:17f96ffc073a | 1 | /** |
fabiombed | 4:17f96ffc073a | 2 | ****************************************************************************** |
fabiombed | 4:17f96ffc073a | 3 | * @file main.cpp |
fabiombed | 4:17f96ffc073a | 4 | * @author Fabio Brembilla |
fabiombed | 4:17f96ffc073a | 5 | * @version V2.0.0 |
fabiombed | 9:d530044d91b9 | 6 | * @date May, 2016 |
fabiombed | 5:5e62f661a7ab | 7 | * @brief SunTracker + BLE (Client) Vertical Application |
fabiombed | 4:17f96ffc073a | 8 | * This application use IKS01A1, IDB0XA1 expansion boards |
fabiombed | 4:17f96ffc073a | 9 | ****************************************************************************** |
fabiombed | 4:17f96ffc073a | 10 | * @attention |
apalmieri | 0:2e972e7d3f64 | 11 | * |
fabiombed | 4:17f96ffc073a | 12 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
apalmieri | 0:2e972e7d3f64 | 13 | * |
fabiombed | 4:17f96ffc073a | 14 | * Redistribution and use in source and binary forms, with or without modification, |
fabiombed | 4:17f96ffc073a | 15 | * are permitted provided that the following conditions are met: |
fabiombed | 4:17f96ffc073a | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
fabiombed | 4:17f96ffc073a | 17 | * this list of conditions and the following disclaimer. |
fabiombed | 4:17f96ffc073a | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
fabiombed | 4:17f96ffc073a | 19 | * this list of conditions and the following disclaimer in the documentation |
fabiombed | 4:17f96ffc073a | 20 | * and/or other materials provided with the distribution. |
fabiombed | 4:17f96ffc073a | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
fabiombed | 4:17f96ffc073a | 22 | * may be used to endorse or promote products derived from this software |
fabiombed | 4:17f96ffc073a | 23 | * without specific prior written permission. |
apalmieri | 0:2e972e7d3f64 | 24 | * |
fabiombed | 4:17f96ffc073a | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
fabiombed | 4:17f96ffc073a | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
fabiombed | 4:17f96ffc073a | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
fabiombed | 4:17f96ffc073a | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
fabiombed | 4:17f96ffc073a | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
fabiombed | 4:17f96ffc073a | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
fabiombed | 4:17f96ffc073a | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
fabiombed | 4:17f96ffc073a | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
fabiombed | 4:17f96ffc073a | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
fabiombed | 4:17f96ffc073a | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
fabiombed | 4:17f96ffc073a | 35 | * |
fabiombed | 4:17f96ffc073a | 36 | ****************************************************************************** |
apalmieri | 0:2e972e7d3f64 | 37 | */ |
fabiombed | 4:17f96ffc073a | 38 | |
fabiombed | 4:17f96ffc073a | 39 | /* Define --------------------------------------------------------------------*/ |
fabiombed | 4:17f96ffc073a | 40 | |
fabiombed | 4:17f96ffc073a | 41 | #define Sensors //IKS01A1 Option |
fabiombed | 4:17f96ffc073a | 42 | |
fabiombed | 4:17f96ffc073a | 43 | /* Includes ------------------------------------------------------------------*/ |
fabiombed | 4:17f96ffc073a | 44 | |
apalmieri | 0:2e972e7d3f64 | 45 | #include "mbed.h" |
apalmieri | 0:2e972e7d3f64 | 46 | |
fabiombed | 4:17f96ffc073a | 47 | #ifdef Sensors |
fabiombed | 4:17f96ffc073a | 48 | #include "DevI2C.h" |
fabiombed | 4:17f96ffc073a | 49 | #include "x_nucleo_iks01a1.h" |
fabiombed | 4:17f96ffc073a | 50 | #endif |
fabiombed | 4:17f96ffc073a | 51 | |
fabiombed | 4:17f96ffc073a | 52 | /* BlueTooth -----------------------------------------------------------------*/ |
fabiombed | 4:17f96ffc073a | 53 | |
fabiombed | 7:eb2215fe86b2 | 54 | #include "BLE.h" |
fabiombed | 7:eb2215fe86b2 | 55 | #include "UUID.h" |
fabiombed | 7:eb2215fe86b2 | 56 | #include "DiscoveredCharacteristic.h" |
fabiombed | 7:eb2215fe86b2 | 57 | #include "DiscoveredService.h" |
fabiombed | 5:5e62f661a7ab | 58 | #include "Utils.h" // Need for STORE_LE_16 and STORE_LE_32 |
fabiombed | 5:5e62f661a7ab | 59 | |
apalmieri | 0:2e972e7d3f64 | 60 | #define SCAN_INT 0x30 // 30 ms = 48 * 0.625 ms |
apalmieri | 0:2e972e7d3f64 | 61 | #define SCAN_WIND 0x30 // 30 ms = 48 * 0.625 ms |
apalmieri | 0:2e972e7d3f64 | 62 | |
fabiombed | 7:eb2215fe86b2 | 63 | // Not need to re-define it, it is already defined into UUID.h, just use UUID:: when you call it |
fabiombed | 7:eb2215fe86b2 | 64 | //const unsigned LENGTH_OF_LONG_UUID = 16; |
fabiombed | 7:eb2215fe86b2 | 65 | //typedef uint16_t ShortUUIDBytes_t; |
fabiombed | 7:eb2215fe86b2 | 66 | //typedef uint8_t LongUUIDBytes_t[LENGTH_OF_LONG_UUID]; |
fabiombed | 7:eb2215fe86b2 | 67 | |
fabiombed | 9:d530044d91b9 | 68 | const Gap::Address_t BLE_address_BE = {0xCC, 0x06, 0x05, 0x04, 0xCB, 0xCA}; // CLIENT address |
fabiombed | 9:d530044d91b9 | 69 | const Gap::Address_t BLE_peer_address_BE = {0xFC, 0x03, 0x02, 0x01, 0xFB, 0xFA}; // SERVER address (must be the same of SERVER) |
apalmieri | 0:2e972e7d3f64 | 70 | |
fabiombed | 7:eb2215fe86b2 | 71 | //const UUID::ShortUUIDBytes_t CONTROL_COMMAND_CHAR_UUID = 0xA001; |
fabiombed | 7:eb2215fe86b2 | 72 | //const UUID::ShortUUIDBytes_t SENS_STATUS_CHAR_UUID = 0xB001; |
fabiombed | 7:eb2215fe86b2 | 73 | //const UUID::ShortUUIDBytes_t SENS_DIFFERENCE_CHAR_UUID = 0xB002; |
fabiombed | 7:eb2215fe86b2 | 74 | //const UUID::ShortUUIDBytes_t SENS_POSITION_CHAR_UUID = 0xB003; |
fabiombed | 7:eb2215fe86b2 | 75 | //const UUID::ShortUUIDBytes_t SENS_SUNPANEL_CHAR_UUID = 0xB004; |
fabiombed | 7:eb2215fe86b2 | 76 | const UUID::LongUUIDBytes_t CONTROL_COMMAND_CHAR_UUID = {0x00,0x00,0x00,0x02,0x00,0x0F,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b}; |
fabiombed | 8:63b01a643f4d | 77 | const UUID::LongUUIDBytes_t SENS_STATUS_CHAR_UUID = {0x00,0x00,0x08,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b}; |
fabiombed | 8:63b01a643f4d | 78 | const UUID::LongUUIDBytes_t SENS_DIFFERENCE_CHAR_UUID = {0x00,0x00,0x10,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b}; |
fabiombed | 8:63b01a643f4d | 79 | const UUID::LongUUIDBytes_t SENS_POSITION_CHAR_UUID = {0x00,0x00,0x20,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b}; |
fabiombed | 8:63b01a643f4d | 80 | const UUID::LongUUIDBytes_t SENS_SUNPANEL_CHAR_UUID = {0x00,0x00,0x40,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b}; |
fabiombed | 7:eb2215fe86b2 | 81 | |
fabiombed | 7:eb2215fe86b2 | 82 | DiscoveredCharacteristic command_bleCharacteristic; |
fabiombed | 7:eb2215fe86b2 | 83 | DiscoveredCharacteristic status_bleCharacteristic; |
fabiombed | 4:17f96ffc073a | 84 | DiscoveredCharacteristic difference_bleCharacteristic; |
fabiombed | 4:17f96ffc073a | 85 | DiscoveredCharacteristic position_bleCharacteristic; |
fabiombed | 4:17f96ffc073a | 86 | DiscoveredCharacteristic sunpanel_bleCharacteristic; |
fabiombed | 4:17f96ffc073a | 87 | |
fabiombed | 7:eb2215fe86b2 | 88 | #define COMMAND_DATA_LEN 2+4+1+2 // TimeStamp (only for Response) + Feature + Type + Data |
fabiombed | 5:5e62f661a7ab | 89 | // 1 byte 0xFF 8 bit int8 |
fabiombed | 5:5e62f661a7ab | 90 | // 2 byte 0xFFFF 16 bit int16 |
fabiombed | 5:5e62f661a7ab | 91 | |
fabiombed | 7:eb2215fe86b2 | 92 | uint8_t value_write[COMMAND_DATA_LEN]; |
fabiombed | 7:eb2215fe86b2 | 93 | |
fabiombed | 8:63b01a643f4d | 94 | #define FeatureStatus 0x00000800 |
fabiombed | 8:63b01a643f4d | 95 | #define FeatureDifference 0x00001000 |
fabiombed | 8:63b01a643f4d | 96 | #define FeaturePosition 0x00002000 |
fabiombed | 8:63b01a643f4d | 97 | #define FeatureSunPanel 0x00004000 |
fabiombed | 5:5e62f661a7ab | 98 | |
fabiombed | 5:5e62f661a7ab | 99 | /* Variables ------------------------------------------------------------------*/ |
fabiombed | 5:5e62f661a7ab | 100 | |
fabiombed | 5:5e62f661a7ab | 101 | enum { |
fabiombed | 5:5e62f661a7ab | 102 | IDLE = 0, |
fabiombed | 7:eb2215fe86b2 | 103 | READ_STA = 1, |
fabiombed | 5:5e62f661a7ab | 104 | READ_DIF = 2, |
fabiombed | 5:5e62f661a7ab | 105 | READ_POS = 3, |
fabiombed | 5:5e62f661a7ab | 106 | READ_SUN = 4, |
fabiombed | 7:eb2215fe86b2 | 107 | WRITE_STA = 5, |
fabiombed | 5:5e62f661a7ab | 108 | WRITE_DIF = 6 |
fabiombed | 5:5e62f661a7ab | 109 | }; |
fabiombed | 5:5e62f661a7ab | 110 | int trigger_Op = IDLE; |
fabiombed | 5:5e62f661a7ab | 111 | bool trigger_button = false; |
fabiombed | 8:63b01a643f4d | 112 | int16_t Status_to_Write=0; // to avoid to read the old Status by BLE before send the new Status by BLE |
fabiombed | 5:5e62f661a7ab | 113 | |
fabiombed | 7:eb2215fe86b2 | 114 | int16_t Status=0; // Status Shown on Display: 0 = Idle, 1 = Motor Speed, 2 = Solar Panel Value, 3 = Manual Control [--> Receive BLE] |
fabiombed | 5:5e62f661a7ab | 115 | int16_t diff=0; // Babybear or Accelerometer difference [--> Receive BLE] |
fabiombed | 5:5e62f661a7ab | 116 | int16_t pos=0; // Motor Position [--> Receive BLE] |
fabiombed | 5:5e62f661a7ab | 117 | int16_t measure=0; // ADC Value from SunPanel [--> Receive BLE] |
fabiombed | 4:17f96ffc073a | 118 | |
fabiombed | 4:17f96ffc073a | 119 | /* Initializations ------------------------------------------------------------*/ |
fabiombed | 4:17f96ffc073a | 120 | |
fabiombed | 7:eb2215fe86b2 | 121 | #ifdef Sensors |
apalmieri | 0:2e972e7d3f64 | 122 | |
fabiombed | 4:17f96ffc073a | 123 | // Initializing I2C bus |
fabiombed | 4:17f96ffc073a | 124 | DevI2C dev_i2c(D14, D15); |
fabiombed | 4:17f96ffc073a | 125 | |
fabiombed | 4:17f96ffc073a | 126 | // Initializing Sensors Component IKS01A1 |
fabiombed | 4:17f96ffc073a | 127 | static X_NUCLEO_IKS01A1 *mems; |
fabiombed | 4:17f96ffc073a | 128 | MotionSensor *accelerometer; |
apalmieri | 0:2e972e7d3f64 | 129 | |
fabiombed | 4:17f96ffc073a | 130 | int32_t acc_data[3]; // Accelerometer difference |
fabiombed | 5:5e62f661a7ab | 131 | int16_t acc_diff=0; // Accelerometer difference |
fabiombed | 7:eb2215fe86b2 | 132 | |
fabiombed | 4:17f96ffc073a | 133 | #endif |
fabiombed | 4:17f96ffc073a | 134 | |
fabiombed | 8:63b01a643f4d | 135 | DigitalOut myled(LED1, 0); |
fabiombed | 4:17f96ffc073a | 136 | InterruptIn mybutton(USER_BUTTON); |
fabiombed | 4:17f96ffc073a | 137 | |
fabiombed | 4:17f96ffc073a | 138 | /* User_Button_Pressed -------------------------------------------------------*/ |
fabiombed | 4:17f96ffc073a | 139 | |
fabiombed | 4:17f96ffc073a | 140 | void User_Button_Pressed(void) |
fabiombed | 8:63b01a643f4d | 141 | { |
fabiombed | 8:63b01a643f4d | 142 | |
fabiombed | 5:5e62f661a7ab | 143 | trigger_button = true; |
fabiombed | 7:eb2215fe86b2 | 144 | Status++; |
fabiombed | 8:63b01a643f4d | 145 | Status_to_Write = Status; |
fabiombed | 7:eb2215fe86b2 | 146 | |
fabiombed | 7:eb2215fe86b2 | 147 | #ifdef Sensors |
fabiombed | 8:63b01a643f4d | 148 | if (Status_to_Write>3) { Status=1; Status_to_Write=1; } |
fabiombed | 7:eb2215fe86b2 | 149 | #else |
fabiombed | 8:63b01a643f4d | 150 | if (Status_to_Write>2) { Status=1; Status_to_Write=1; } |
fabiombed | 7:eb2215fe86b2 | 151 | #endif |
fabiombed | 7:eb2215fe86b2 | 152 | |
apalmieri | 0:2e972e7d3f64 | 153 | } |
apalmieri | 0:2e972e7d3f64 | 154 | |
fabiombed | 4:17f96ffc073a | 155 | /* Bluetooth CallBack ---------------------------------------------------------*/ |
fabiombed | 4:17f96ffc073a | 156 | |
fabiombed | 4:17f96ffc073a | 157 | void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) |
fabiombed | 4:17f96ffc073a | 158 | { |
fabiombed | 4:17f96ffc073a | 159 | if (params->peerAddr[0] != BLE_peer_address_BE[0]) // return if miss the server MAC address |
fabiombed | 4:17f96ffc073a | 160 | { |
fabiombed | 6:ce0e5024e3fa | 161 | printf("Missing Expected MAC Address\n\r"); |
fabiombed | 4:17f96ffc073a | 162 | return; // exit from advertisementCallback |
apalmieri | 0:2e972e7d3f64 | 163 | } |
apalmieri | 0:2e972e7d3f64 | 164 | |
fabiombed | 4:17f96ffc073a | 165 | // printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", |
fabiombed | 4:17f96ffc073a | 166 | // params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], |
fabiombed | 4:17f96ffc073a | 167 | // params->rssi, params->isScanResponse, params->type); |
fabiombed | 4:17f96ffc073a | 168 | |
fabiombed | 4:17f96ffc073a | 169 | printf("Found Expected MAC Address: isScanResponse %u, AdvertisementType %u\r\n",params->isScanResponse, params->type); |
apalmieri | 0:2e972e7d3f64 | 170 | |
fabiombed | 4:17f96ffc073a | 171 | if(!params->isScanResponse) |
fabiombed | 4:17f96ffc073a | 172 | { |
apalmieri | 0:2e972e7d3f64 | 173 | BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_PUBLIC, NULL, NULL); |
apalmieri | 0:2e972e7d3f64 | 174 | } |
apalmieri | 0:2e972e7d3f64 | 175 | } |
apalmieri | 0:2e972e7d3f64 | 176 | |
fabiombed | 4:17f96ffc073a | 177 | void serviceDiscoveryCallback(const DiscoveredService *service) |
fabiombed | 4:17f96ffc073a | 178 | { |
fabiombed | 4:17f96ffc073a | 179 | printf("Start Service Discovery\r\n"); |
fabiombed | 7:eb2215fe86b2 | 180 | |
fabiombed | 4:17f96ffc073a | 181 | if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) |
fabiombed | 4:17f96ffc073a | 182 | { |
apalmieri | 0:2e972e7d3f64 | 183 | printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle()); |
fabiombed | 4:17f96ffc073a | 184 | } |
fabiombed | 4:17f96ffc073a | 185 | else |
fabiombed | 4:17f96ffc073a | 186 | { |
apalmieri | 0:2e972e7d3f64 | 187 | printf("S UUID-"); |
apalmieri | 0:2e972e7d3f64 | 188 | const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); |
fabiombed | 4:17f96ffc073a | 189 | for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) |
fabiombed | 4:17f96ffc073a | 190 | { |
apalmieri | 0:2e972e7d3f64 | 191 | printf("%02X", longUUIDBytes[i]); |
apalmieri | 0:2e972e7d3f64 | 192 | } |
apalmieri | 0:2e972e7d3f64 | 193 | printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle()); |
apalmieri | 0:2e972e7d3f64 | 194 | } |
fabiombed | 7:eb2215fe86b2 | 195 | |
apalmieri | 0:2e972e7d3f64 | 196 | } |
fabiombed | 4:17f96ffc073a | 197 | |
fabiombed | 4:17f96ffc073a | 198 | void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) |
fabiombed | 4:17f96ffc073a | 199 | { |
fabiombed | 7:eb2215fe86b2 | 200 | int COMMAND_CHAR_count=0, STATUS_CHAR_count=0, DIFFERENCE_CHAR_count=0, POSITION_CHAR_count=0, SUNPANEL_CHAR_count=0; |
fabiombed | 7:eb2215fe86b2 | 201 | const uint8_t *longUUIDBytes = characteristicP->getUUID().getBaseUUID(); |
fabiombed | 7:eb2215fe86b2 | 202 | |
fabiombed | 7:eb2215fe86b2 | 203 | for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) |
fabiombed | 7:eb2215fe86b2 | 204 | { |
fabiombed | 7:eb2215fe86b2 | 205 | if (longUUIDBytes[i] == CONTROL_COMMAND_CHAR_UUID[i]) COMMAND_CHAR_count++; |
fabiombed | 7:eb2215fe86b2 | 206 | if (longUUIDBytes[i] == SENS_STATUS_CHAR_UUID[i]) STATUS_CHAR_count++; |
fabiombed | 7:eb2215fe86b2 | 207 | if (longUUIDBytes[i] == SENS_DIFFERENCE_CHAR_UUID[i]) DIFFERENCE_CHAR_count++; |
fabiombed | 7:eb2215fe86b2 | 208 | if (longUUIDBytes[i] == SENS_POSITION_CHAR_UUID[i]) POSITION_CHAR_count++; |
fabiombed | 7:eb2215fe86b2 | 209 | if (longUUIDBytes[i] == SENS_SUNPANEL_CHAR_UUID[i]) SUNPANEL_CHAR_count++; |
fabiombed | 4:17f96ffc073a | 210 | } |
fabiombed | 4:17f96ffc073a | 211 | |
fabiombed | 7:eb2215fe86b2 | 212 | //if (characteristicP->getUUID().getShortUUID() == CONTROL_COMMAND_CHAR_UUID) |
fabiombed | 7:eb2215fe86b2 | 213 | if (COMMAND_CHAR_count == UUID::LENGTH_OF_LONG_UUID) |
fabiombed | 7:eb2215fe86b2 | 214 | { |
fabiombed | 7:eb2215fe86b2 | 215 | command_bleCharacteristic = *characteristicP; |
fabiombed | 7:eb2215fe86b2 | 216 | printf("Found Command Characteristic\r\n"); |
fabiombed | 7:eb2215fe86b2 | 217 | } |
fabiombed | 7:eb2215fe86b2 | 218 | |
fabiombed | 7:eb2215fe86b2 | 219 | //if (characteristicP->getUUID().getShortUUID() == SENS_STATUS_CHAR_UUID) |
fabiombed | 7:eb2215fe86b2 | 220 | if (STATUS_CHAR_count == UUID::LENGTH_OF_LONG_UUID) |
fabiombed | 7:eb2215fe86b2 | 221 | { |
fabiombed | 7:eb2215fe86b2 | 222 | status_bleCharacteristic = *characteristicP; |
fabiombed | 7:eb2215fe86b2 | 223 | printf("Found Status Characteristic\r\n"); |
fabiombed | 7:eb2215fe86b2 | 224 | } |
fabiombed | 7:eb2215fe86b2 | 225 | |
fabiombed | 7:eb2215fe86b2 | 226 | //if (characteristicP->getUUID().getShortUUID() == SENS_DIFFERENCE_CHAR_UUID) |
fabiombed | 7:eb2215fe86b2 | 227 | if (DIFFERENCE_CHAR_count == UUID::LENGTH_OF_LONG_UUID) |
fabiombed | 4:17f96ffc073a | 228 | { |
fabiombed | 4:17f96ffc073a | 229 | difference_bleCharacteristic = *characteristicP; |
fabiombed | 7:eb2215fe86b2 | 230 | printf("Found Difference Characteristic\r\n"); |
fabiombed | 4:17f96ffc073a | 231 | } |
fabiombed | 7:eb2215fe86b2 | 232 | |
fabiombed | 7:eb2215fe86b2 | 233 | //if (characteristicP->getUUID().getShortUUID() == SENS_POSITION_CHAR_UUID) |
fabiombed | 7:eb2215fe86b2 | 234 | if (POSITION_CHAR_count == UUID::LENGTH_OF_LONG_UUID) |
fabiombed | 4:17f96ffc073a | 235 | { |
fabiombed | 4:17f96ffc073a | 236 | position_bleCharacteristic = *characteristicP; |
fabiombed | 7:eb2215fe86b2 | 237 | printf("Found Position Characteristic\r\n"); |
fabiombed | 4:17f96ffc073a | 238 | } |
fabiombed | 7:eb2215fe86b2 | 239 | |
fabiombed | 7:eb2215fe86b2 | 240 | //if (characteristicP->getUUID().getShortUUID() == SENS_SUNPANEL_CHAR_UUID) |
fabiombed | 7:eb2215fe86b2 | 241 | if (SUNPANEL_CHAR_count == UUID::LENGTH_OF_LONG_UUID) |
fabiombed | 4:17f96ffc073a | 242 | { |
fabiombed | 4:17f96ffc073a | 243 | sunpanel_bleCharacteristic = *characteristicP; |
fabiombed | 7:eb2215fe86b2 | 244 | printf("Found Sunpanel Characteristic\r\n"); |
apalmieri | 0:2e972e7d3f64 | 245 | } |
apalmieri | 0:2e972e7d3f64 | 246 | } |
apalmieri | 0:2e972e7d3f64 | 247 | |
fabiombed | 4:17f96ffc073a | 248 | void discoveryTerminationCallback(Gap::Handle_t connectionHandle) |
fabiombed | 4:17f96ffc073a | 249 | { |
fabiombed | 4:17f96ffc073a | 250 | //printf("discoveryTerminationCallback for handle %u\r\n", connectionHandle); |
fabiombed | 4:17f96ffc073a | 251 | printf("Stop Service Discovery\r\n\r\n"); |
fabiombed | 5:5e62f661a7ab | 252 | |
fabiombed | 7:eb2215fe86b2 | 253 | trigger_Op = READ_STA; |
apalmieri | 0:2e972e7d3f64 | 254 | } |
apalmieri | 0:2e972e7d3f64 | 255 | |
fabiombed | 4:17f96ffc073a | 256 | void onDataReadCallback(const GattReadCallbackParams *response) |
fabiombed | 4:17f96ffc073a | 257 | { |
fabiombed | 4:17f96ffc073a | 258 | // Read SERVER --> CLIENT |
fabiombed | 4:17f96ffc073a | 259 | |
fabiombed | 7:eb2215fe86b2 | 260 | // From SERVER it receives a sensor in this format: TimeStamp (2) + Data (2) |
fabiombed | 10:d00d09bc31d6 | 261 | //uint16_t TimeStamp = response->data[0]<<8 | response->data[1]; |
fabiombed | 7:eb2215fe86b2 | 262 | int16_t Data = response->data[2] | response->data[3]<<8; // Return original value after inverted with STORE_LE_16 into SERVER before send by BLE |
fabiombed | 5:5e62f661a7ab | 263 | |
fabiombed | 7:eb2215fe86b2 | 264 | //printf("\r\n\ronDataReadCallback (TimeStamp %x)", TimeStamp); |
fabiombed | 7:eb2215fe86b2 | 265 | //printf("\n\ronDataReadCallback (Data Hex %x)", Data); |
fabiombed | 7:eb2215fe86b2 | 266 | //printf("\n\ronDataReadCallback (Data Dec %d)", Data); |
fabiombed | 5:5e62f661a7ab | 267 | |
fabiombed | 7:eb2215fe86b2 | 268 | if (response->handle == status_bleCharacteristic.getValueHandle()) |
fabiombed | 5:5e62f661a7ab | 269 | { |
fabiombed | 7:eb2215fe86b2 | 270 | Status = Data; |
fabiombed | 7:eb2215fe86b2 | 271 | printf("\n\rReceive BLE Display Status %d\n\r", Status); |
fabiombed | 5:5e62f661a7ab | 272 | |
fabiombed | 5:5e62f661a7ab | 273 | trigger_Op = READ_DIF; |
fabiombed | 4:17f96ffc073a | 274 | } |
fabiombed | 4:17f96ffc073a | 275 | |
fabiombed | 4:17f96ffc073a | 276 | if (response->handle == difference_bleCharacteristic.getValueHandle()) |
fabiombed | 5:5e62f661a7ab | 277 | { |
fabiombed | 7:eb2215fe86b2 | 278 | diff = Data; |
fabiombed | 5:5e62f661a7ab | 279 | printf("Receive BLE Difference %d lux/mems\n\r", diff); |
fabiombed | 5:5e62f661a7ab | 280 | |
fabiombed | 5:5e62f661a7ab | 281 | trigger_Op = READ_POS; |
apalmieri | 0:2e972e7d3f64 | 282 | } |
fabiombed | 4:17f96ffc073a | 283 | |
fabiombed | 4:17f96ffc073a | 284 | if (response->handle == position_bleCharacteristic.getValueHandle()) |
fabiombed | 5:5e62f661a7ab | 285 | { |
fabiombed | 7:eb2215fe86b2 | 286 | pos = Data; |
fabiombed | 5:5e62f661a7ab | 287 | printf("Receive BLE Position %d\n\r", pos); |
fabiombed | 5:5e62f661a7ab | 288 | |
fabiombed | 5:5e62f661a7ab | 289 | trigger_Op = READ_SUN; |
fabiombed | 4:17f96ffc073a | 290 | } |
apalmieri | 0:2e972e7d3f64 | 291 | |
fabiombed | 4:17f96ffc073a | 292 | if (response->handle == sunpanel_bleCharacteristic.getValueHandle()) |
fabiombed | 5:5e62f661a7ab | 293 | { |
fabiombed | 7:eb2215fe86b2 | 294 | measure = Data; |
fabiombed | 5:5e62f661a7ab | 295 | printf("Receive BLE Sunpanel %d mV\n\r", measure); |
fabiombed | 4:17f96ffc073a | 296 | |
fabiombed | 7:eb2215fe86b2 | 297 | trigger_Op = WRITE_STA; |
fabiombed | 4:17f96ffc073a | 298 | } |
apalmieri | 0:2e972e7d3f64 | 299 | } |
apalmieri | 0:2e972e7d3f64 | 300 | |
fabiombed | 4:17f96ffc073a | 301 | void myonDataWriteCallback(const GattWriteCallbackParams *response) |
fabiombed | 4:17f96ffc073a | 302 | { |
fabiombed | 4:17f96ffc073a | 303 | // Write CLIENT --> SERVER |
fabiombed | 4:17f96ffc073a | 304 | |
fabiombed | 7:eb2215fe86b2 | 305 | if (response->handle == command_bleCharacteristic.getValueHandle()) |
fabiombed | 7:eb2215fe86b2 | 306 | { |
fabiombed | 7:eb2215fe86b2 | 307 | |
fabiombed | 7:eb2215fe86b2 | 308 | // From CLIENT it writes a command in this format: Feature (4) + Type (1) + Data (2) |
fabiombed | 7:eb2215fe86b2 | 309 | uint32_t Feature = response->data[0]<<24 | response->data[1]<<16 | response->data[2]<<8 | response->data[3]; |
fabiombed | 10:d00d09bc31d6 | 310 | //uint8_t Type = response->data[4]; |
fabiombed | 7:eb2215fe86b2 | 311 | int16_t Data = response->data[5]<<8 | response->data[6]; |
fabiombed | 7:eb2215fe86b2 | 312 | |
fabiombed | 7:eb2215fe86b2 | 313 | //printf("\r\nmyonDataWriteCallback (Feature %x)", Feature); |
fabiombed | 7:eb2215fe86b2 | 314 | //printf("\r\nmyonDataWriteCallback (Type %x)", Type); // Not Used |
fabiombed | 7:eb2215fe86b2 | 315 | //printf("\r\nmyonDataWriteCallback (Data Hex %x)", Data); |
fabiombed | 7:eb2215fe86b2 | 316 | //printf("\r\nmyonDataWriteCallback (Data Dec %d)", Data); |
fabiombed | 5:5e62f661a7ab | 317 | |
fabiombed | 7:eb2215fe86b2 | 318 | switch(Feature) |
fabiombed | 7:eb2215fe86b2 | 319 | { |
fabiombed | 7:eb2215fe86b2 | 320 | case FeatureStatus: |
fabiombed | 7:eb2215fe86b2 | 321 | printf("\r\nSend STATUS %d\r\n", Data); |
fabiombed | 7:eb2215fe86b2 | 322 | Status = Data; |
fabiombed | 7:eb2215fe86b2 | 323 | trigger_Op = WRITE_DIF; |
fabiombed | 7:eb2215fe86b2 | 324 | break; |
fabiombed | 7:eb2215fe86b2 | 325 | |
fabiombed | 7:eb2215fe86b2 | 326 | case FeatureDifference: |
fabiombed | 7:eb2215fe86b2 | 327 | printf("\r\nSend DIFF %d\r\n", Data); |
fabiombed | 7:eb2215fe86b2 | 328 | diff = Data; |
fabiombed | 7:eb2215fe86b2 | 329 | trigger_Op = READ_STA; |
fabiombed | 7:eb2215fe86b2 | 330 | break; |
fabiombed | 7:eb2215fe86b2 | 331 | |
fabiombed | 7:eb2215fe86b2 | 332 | case FeaturePosition: |
fabiombed | 7:eb2215fe86b2 | 333 | break; |
fabiombed | 7:eb2215fe86b2 | 334 | |
fabiombed | 7:eb2215fe86b2 | 335 | case FeatureSunPanel: |
fabiombed | 7:eb2215fe86b2 | 336 | break; |
fabiombed | 7:eb2215fe86b2 | 337 | |
fabiombed | 7:eb2215fe86b2 | 338 | default: |
fabiombed | 7:eb2215fe86b2 | 339 | break; |
fabiombed | 7:eb2215fe86b2 | 340 | } |
fabiombed | 5:5e62f661a7ab | 341 | |
fabiombed | 5:5e62f661a7ab | 342 | } |
fabiombed | 7:eb2215fe86b2 | 343 | |
apalmieri | 0:2e972e7d3f64 | 344 | } |
apalmieri | 0:2e972e7d3f64 | 345 | |
apalmieri | 0:2e972e7d3f64 | 346 | /** |
apalmieri | 0:2e972e7d3f64 | 347 | * This function is called when the ble initialization process has failled |
apalmieri | 0:2e972e7d3f64 | 348 | */ |
apalmieri | 0:2e972e7d3f64 | 349 | void onBleInitError(BLE &ble, ble_error_t error) |
apalmieri | 0:2e972e7d3f64 | 350 | { |
apalmieri | 0:2e972e7d3f64 | 351 | /* Initialization error handling should go here */ |
fabiombed | 5:5e62f661a7ab | 352 | |
fabiombed | 5:5e62f661a7ab | 353 | printf("Inizialization Error\n\r"); |
apalmieri | 0:2e972e7d3f64 | 354 | } |
apalmieri | 0:2e972e7d3f64 | 355 | |
fabiombed | 4:17f96ffc073a | 356 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) |
fabiombed | 4:17f96ffc073a | 357 | { |
fabiombed | 7:eb2215fe86b2 | 358 | // Good only for Short UUID |
fabiombed | 7:eb2215fe86b2 | 359 | //uint16_t CONTROL_SERVICE_UUID = 0x00,0x00,0x00,0x00,0x00,0x0F,0x11,0xe1,0x9a,0xb4,0x00,0x02,0xa5,0xd5,0xc5,0x1b; |
fabiombed | 7:eb2215fe86b2 | 360 | //uint16_t COMMAND_CHARACTERISTIC_UUID = 0x00,0x00,0x00,0x02,0x00,0x0F,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b; |
fabiombed | 7:eb2215fe86b2 | 361 | //uint16_t SENSORS_SERVICE_UUID = 0x00,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0x9a,0xb4,0x00,0x02,0xa5,0xd5,0xc5,0x1b; |
fabiombed | 7:eb2215fe86b2 | 362 | //uint16_t STATUS_CHARACTERISTIC_UUID = 0x00,0x00,0x04,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b; |
fabiombed | 7:eb2215fe86b2 | 363 | //uint16_t DIFFERENCE_CHARACTERISTIC_UUID = 0x00,0x00,0x08,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b; |
fabiombed | 7:eb2215fe86b2 | 364 | //uint16_t POSITION_CHARACTERISTIC_UUID = 0x00,0x00,0x10,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b; |
fabiombed | 7:eb2215fe86b2 | 365 | //uint16_t SUNPANEL_CHARACTERISTIC_UUID = 0x00,0x00,0x20,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b; |
fabiombed | 4:17f96ffc073a | 366 | |
fabiombed | 4:17f96ffc073a | 367 | if (params->role == Gap::CENTRAL) |
fabiombed | 4:17f96ffc073a | 368 | { |
fabiombed | 4:17f96ffc073a | 369 | BLE &ble = BLE::Instance(); |
fabiombed | 4:17f96ffc073a | 370 | ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); |
fabiombed | 4:17f96ffc073a | 371 | // Discover all SERVICES and CHARACTERISTICS |
fabiombed | 4:17f96ffc073a | 372 | ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback); |
fabiombed | 4:17f96ffc073a | 373 | |
fabiombed | 5:5e62f661a7ab | 374 | // Works only for one characteristic, if you need to discover all characteristic, use the above command |
fabiombed | 7:eb2215fe86b2 | 375 | //ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, CONTROL_SERVICE_UUID, COMMAND_CHARACTERISTIC_UUID); |
fabiombed | 7:eb2215fe86b2 | 376 | //ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, SENSORS_SERVICE_UUID, STATUS_CHARACTERISTIC_UUID); |
fabiombed | 4:17f96ffc073a | 377 | //ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, SENSORS_SERVICE_UUID, DIFFERENCE_CHARACTERISTIC_UUID); |
fabiombed | 4:17f96ffc073a | 378 | //ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, SENSORS_SERVICE_UUID, POSITION_CHARACTERISTIC_UUID); |
fabiombed | 4:17f96ffc073a | 379 | //ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, SENSORS_SERVICE_UUID, SUNPANEL_CHARACTERISTIC_UUID); |
fabiombed | 4:17f96ffc073a | 380 | } |
fabiombed | 4:17f96ffc073a | 381 | |
fabiombed | 4:17f96ffc073a | 382 | printf("Remote Connected\n\r"); |
fabiombed | 8:63b01a643f4d | 383 | |
fabiombed | 8:63b01a643f4d | 384 | myled = 1; // Power ON Led Green on CLIENT Board to indicate Connection between CLIENT and SERVER |
fabiombed | 4:17f96ffc073a | 385 | } |
fabiombed | 4:17f96ffc073a | 386 | |
fabiombed | 4:17f96ffc073a | 387 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
fabiombed | 4:17f96ffc073a | 388 | { |
fabiombed | 4:17f96ffc073a | 389 | (void)params; |
fabiombed | 5:5e62f661a7ab | 390 | |
fabiombed | 4:17f96ffc073a | 391 | printf("Remote Disconnected\n\r"); |
fabiombed | 8:63b01a643f4d | 392 | |
fabiombed | 8:63b01a643f4d | 393 | myled = 0; // Power OFF Led Green on CLIENT Board to indicate Disconnection between CLIENT and SERVER |
fabiombed | 4:17f96ffc073a | 394 | } |
fabiombed | 4:17f96ffc073a | 395 | |
apalmieri | 0:2e972e7d3f64 | 396 | /** |
apalmieri | 0:2e972e7d3f64 | 397 | * Callback triggered when the ble initialization process has finished |
apalmieri | 0:2e972e7d3f64 | 398 | */ |
apalmieri | 0:2e972e7d3f64 | 399 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
apalmieri | 0:2e972e7d3f64 | 400 | { |
apalmieri | 0:2e972e7d3f64 | 401 | BLE& ble = params->ble; |
apalmieri | 0:2e972e7d3f64 | 402 | ble_error_t error = params->error; |
apalmieri | 0:2e972e7d3f64 | 403 | |
fabiombed | 4:17f96ffc073a | 404 | if (error != BLE_ERROR_NONE) |
fabiombed | 4:17f96ffc073a | 405 | { |
apalmieri | 0:2e972e7d3f64 | 406 | /* In case of error, forward the error handling to onBleInitError */ |
apalmieri | 0:2e972e7d3f64 | 407 | onBleInitError(ble, error); |
apalmieri | 0:2e972e7d3f64 | 408 | return; |
apalmieri | 0:2e972e7d3f64 | 409 | } |
apalmieri | 0:2e972e7d3f64 | 410 | |
apalmieri | 0:2e972e7d3f64 | 411 | /* Ensure that it is the default instance of BLE */ |
fabiombed | 4:17f96ffc073a | 412 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { return; } |
apalmieri | 0:2e972e7d3f64 | 413 | |
apalmieri | 0:2e972e7d3f64 | 414 | // Set BT Address |
apalmieri | 1:e0f4bcce540c | 415 | ble.gap().setAddress(BLEProtocol::AddressType::PUBLIC, BLE_address_BE); |
apalmieri | 0:2e972e7d3f64 | 416 | |
apalmieri | 0:2e972e7d3f64 | 417 | ble.gap().onConnection(connectionCallback); |
apalmieri | 0:2e972e7d3f64 | 418 | ble.onDisconnection(disconnectionCallback); |
apalmieri | 0:2e972e7d3f64 | 419 | |
fabiombed | 4:17f96ffc073a | 420 | ble.gattClient().onDataRead(onDataReadCallback); |
fabiombed | 4:17f96ffc073a | 421 | ble.gattClient().onDataWrite(myonDataWriteCallback); |
apalmieri | 0:2e972e7d3f64 | 422 | |
apalmieri | 0:2e972e7d3f64 | 423 | ble.gap().setScanParams(SCAN_INT, SCAN_WIND); |
apalmieri | 0:2e972e7d3f64 | 424 | ble.gap().startScan(advertisementCallback); |
fabiombed | 7:eb2215fe86b2 | 425 | |
fabiombed | 4:17f96ffc073a | 426 | // Main Loop |
fabiombed | 7:eb2215fe86b2 | 427 | while (true) |
fabiombed | 7:eb2215fe86b2 | 428 | { |
fabiombed | 5:5e62f661a7ab | 429 | |
fabiombed | 5:5e62f661a7ab | 430 | //if (!ble.gattClient().isServiceDiscoveryActive()) |
fabiombed | 5:5e62f661a7ab | 431 | if (trigger_Op!=IDLE) // Wait until discoveryTerminationCallback |
fabiombed | 5:5e62f661a7ab | 432 | { |
fabiombed | 6:ce0e5024e3fa | 433 | |
fabiombed | 5:5e62f661a7ab | 434 | // At present ARM BLE_API don't support Enable/Disable Notify from Client |
fabiombed | 7:eb2215fe86b2 | 435 | //command_bleCharacteristic.notify(); |
fabiombed | 5:5e62f661a7ab | 436 | //difference_bleCharacteristic.notify(); |
fabiombed | 5:5e62f661a7ab | 437 | //position_bleCharacteristic.notify(); |
fabiombed | 5:5e62f661a7ab | 438 | //sunpanel_bleCharacteristic.notify(); |
fabiombed | 5:5e62f661a7ab | 439 | |
fabiombed | 5:5e62f661a7ab | 440 | // Don't run more ble .read or .write in the same loop |
fabiombed | 5:5e62f661a7ab | 441 | // because the callback is an interrupt and it doesn't work correctly if overloaded |
fabiombed | 7:eb2215fe86b2 | 442 | |
fabiombed | 8:63b01a643f4d | 443 | if (Status == 0) |
fabiombed | 8:63b01a643f4d | 444 | { if (trigger_Op == READ_DIF) trigger_Op = WRITE_STA; } // if Display 0, skip READ_DIF,READ_POS,READ_SUN |
fabiombed | 8:63b01a643f4d | 445 | |
fabiombed | 8:63b01a643f4d | 446 | if (Status == 0 || Status == 1 || Status == 2) |
fabiombed | 8:63b01a643f4d | 447 | { if (trigger_Op == WRITE_DIF) trigger_Op = READ_STA; } // if Display 0 or 1 or 2, skip WRITE_DIF |
fabiombed | 5:5e62f661a7ab | 448 | |
fabiombed | 7:eb2215fe86b2 | 449 | if (Status == 3) |
fabiombed | 7:eb2215fe86b2 | 450 | { if (trigger_Op == READ_DIF) trigger_Op = READ_POS; } // if Display 3, skip READ_DIF |
fabiombed | 8:63b01a643f4d | 451 | |
fabiombed | 8:63b01a643f4d | 452 | if (trigger_button == true) { trigger_Op = WRITE_STA; } // jump directly to WRITE_STA |
fabiombed | 8:63b01a643f4d | 453 | |
fabiombed | 5:5e62f661a7ab | 454 | switch(trigger_Op) |
fabiombed | 4:17f96ffc073a | 455 | { |
fabiombed | 7:eb2215fe86b2 | 456 | case READ_STA: |
fabiombed | 7:eb2215fe86b2 | 457 | status_bleCharacteristic.read(); |
fabiombed | 5:5e62f661a7ab | 458 | trigger_Op = IDLE; |
fabiombed | 5:5e62f661a7ab | 459 | break; |
fabiombed | 5:5e62f661a7ab | 460 | case READ_DIF: |
fabiombed | 4:17f96ffc073a | 461 | difference_bleCharacteristic.read(); |
fabiombed | 5:5e62f661a7ab | 462 | trigger_Op = IDLE; |
fabiombed | 5:5e62f661a7ab | 463 | break; |
fabiombed | 5:5e62f661a7ab | 464 | case READ_POS: |
fabiombed | 4:17f96ffc073a | 465 | position_bleCharacteristic.read(); |
fabiombed | 5:5e62f661a7ab | 466 | trigger_Op = IDLE; |
fabiombed | 5:5e62f661a7ab | 467 | break; |
fabiombed | 5:5e62f661a7ab | 468 | case READ_SUN: |
fabiombed | 4:17f96ffc073a | 469 | sunpanel_bleCharacteristic.read(); |
fabiombed | 5:5e62f661a7ab | 470 | trigger_Op = IDLE; |
fabiombed | 5:5e62f661a7ab | 471 | break; |
fabiombed | 7:eb2215fe86b2 | 472 | case WRITE_STA: |
fabiombed | 5:5e62f661a7ab | 473 | if (trigger_button == true) |
fabiombed | 8:63b01a643f4d | 474 | { |
fabiombed | 7:eb2215fe86b2 | 475 | memset (value_write, 0, COMMAND_DATA_LEN); |
fabiombed | 7:eb2215fe86b2 | 476 | //value_write = Feature (4) + Type (1) + Data (2) |
fabiombed | 7:eb2215fe86b2 | 477 | value_write[0] = (FeatureStatus & 0xff000000) >> 24; |
fabiombed | 7:eb2215fe86b2 | 478 | value_write[1] = (FeatureStatus & 0x00ff0000) >> 16; |
fabiombed | 7:eb2215fe86b2 | 479 | value_write[2] = (FeatureStatus & 0x0000ff00) >> 8; |
fabiombed | 7:eb2215fe86b2 | 480 | value_write[3] = (FeatureStatus & 0x000000ff); |
fabiombed | 7:eb2215fe86b2 | 481 | value_write[4] = (0x00); |
fabiombed | 8:63b01a643f4d | 482 | value_write[5] = (Status_to_Write & 0xff00) >> 8; |
fabiombed | 8:63b01a643f4d | 483 | value_write[6] = (Status_to_Write & 0x00ff); |
fabiombed | 7:eb2215fe86b2 | 484 | command_bleCharacteristic.write(COMMAND_DATA_LEN, value_write); |
fabiombed | 7:eb2215fe86b2 | 485 | |
fabiombed | 5:5e62f661a7ab | 486 | trigger_Op = IDLE; |
fabiombed | 5:5e62f661a7ab | 487 | trigger_button = false; |
fabiombed | 5:5e62f661a7ab | 488 | } else { |
fabiombed | 5:5e62f661a7ab | 489 | #ifdef Sensors |
fabiombed | 5:5e62f661a7ab | 490 | trigger_Op = WRITE_DIF; |
fabiombed | 5:5e62f661a7ab | 491 | #else |
fabiombed | 7:eb2215fe86b2 | 492 | trigger_Op = READ_STA; |
fabiombed | 5:5e62f661a7ab | 493 | #endif |
fabiombed | 5:5e62f661a7ab | 494 | } |
fabiombed | 5:5e62f661a7ab | 495 | break; |
fabiombed | 5:5e62f661a7ab | 496 | case WRITE_DIF: |
fabiombed | 5:5e62f661a7ab | 497 | #ifdef Sensors |
fabiombed | 5:5e62f661a7ab | 498 | accelerometer->Get_X_Axes(acc_data); |
fabiombed | 5:5e62f661a7ab | 499 | acc_diff = acc_data[0]; |
fabiombed | 5:5e62f661a7ab | 500 | //printf("Send BLE Difference %d lux/mems\n\r", acc_diff); |
fabiombed | 7:eb2215fe86b2 | 501 | |
fabiombed | 7:eb2215fe86b2 | 502 | memset (value_write, 0, COMMAND_DATA_LEN); |
fabiombed | 7:eb2215fe86b2 | 503 | //value_write = Feature (4) + Type (1) + Data (2) |
fabiombed | 7:eb2215fe86b2 | 504 | value_write[0] = (FeatureDifference & 0xff000000) >> 24; |
fabiombed | 7:eb2215fe86b2 | 505 | value_write[1] = (FeatureDifference & 0x00ff0000) >> 16; |
fabiombed | 7:eb2215fe86b2 | 506 | value_write[2] = (FeatureDifference & 0x0000ff00) >> 8; |
fabiombed | 7:eb2215fe86b2 | 507 | value_write[3] = (FeatureDifference & 0x000000ff); |
fabiombed | 7:eb2215fe86b2 | 508 | value_write[4] = (0x00); |
fabiombed | 7:eb2215fe86b2 | 509 | value_write[5] = (acc_diff & 0xff00) >> 8; |
fabiombed | 7:eb2215fe86b2 | 510 | value_write[6] = (acc_diff & 0x00ff); |
fabiombed | 7:eb2215fe86b2 | 511 | command_bleCharacteristic.write(COMMAND_DATA_LEN, value_write); |
fabiombed | 7:eb2215fe86b2 | 512 | |
fabiombed | 5:5e62f661a7ab | 513 | trigger_Op = IDLE; |
fabiombed | 5:5e62f661a7ab | 514 | #else |
fabiombed | 7:eb2215fe86b2 | 515 | trigger_Op = READ_STA; |
fabiombed | 5:5e62f661a7ab | 516 | #endif |
fabiombed | 5:5e62f661a7ab | 517 | break; |
fabiombed | 5:5e62f661a7ab | 518 | default: |
fabiombed | 5:5e62f661a7ab | 519 | break; |
fabiombed | 4:17f96ffc073a | 520 | } |
fabiombed | 4:17f96ffc073a | 521 | |
apalmieri | 0:2e972e7d3f64 | 522 | } |
fabiombed | 4:17f96ffc073a | 523 | ble.waitForEvent(); |
fabiombed | 7:eb2215fe86b2 | 524 | } |
apalmieri | 0:2e972e7d3f64 | 525 | } |
apalmieri | 0:2e972e7d3f64 | 526 | |
fabiombed | 5:5e62f661a7ab | 527 | /* Ticker --------------------------------------------------------------------*/ |
fabiombed | 5:5e62f661a7ab | 528 | /* |
fabiombed | 5:5e62f661a7ab | 529 | void ticker_Callback(void) |
fabiombed | 5:5e62f661a7ab | 530 | { |
fabiombed | 5:5e62f661a7ab | 531 | |
fabiombed | 5:5e62f661a7ab | 532 | } |
fabiombed | 5:5e62f661a7ab | 533 | */ |
fabiombed | 4:17f96ffc073a | 534 | /* Main ----------------------------------------------------------------------*/ |
fabiombed | 4:17f96ffc073a | 535 | |
apalmieri | 0:2e972e7d3f64 | 536 | int main(void) |
apalmieri | 0:2e972e7d3f64 | 537 | { |
fabiombed | 7:eb2215fe86b2 | 538 | printf("\r\n\r\nSunTracker Remote Control by Fabio Brembilla\r\n\r\n"); |
fabiombed | 4:17f96ffc073a | 539 | |
fabiombed | 4:17f96ffc073a | 540 | #ifdef Sensors |
fabiombed | 4:17f96ffc073a | 541 | // Initializing Sensors Component |
fabiombed | 4:17f96ffc073a | 542 | mems=X_NUCLEO_IKS01A1::Instance(&dev_i2c); |
fabiombed | 4:17f96ffc073a | 543 | accelerometer = mems->GetAccelerometer(); |
fabiombed | 4:17f96ffc073a | 544 | printf("Init Sensors OK\r\n"); |
fabiombed | 4:17f96ffc073a | 545 | #endif |
fabiombed | 5:5e62f661a7ab | 546 | |
fabiombed | 5:5e62f661a7ab | 547 | //Ticker ticker; |
fabiombed | 5:5e62f661a7ab | 548 | //ticker.attach(ticker_Callback, 1); // every 1 second |
fabiombed | 4:17f96ffc073a | 549 | |
fabiombed | 4:17f96ffc073a | 550 | mybutton.fall(&User_Button_Pressed); |
fabiombed | 4:17f96ffc073a | 551 | |
fabiombed | 7:eb2215fe86b2 | 552 | BLE::Instance().init(bleInitComplete); |
fabiombed | 7:eb2215fe86b2 | 553 | |
fabiombed | 4:17f96ffc073a | 554 | } |