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@8:63b01a643f4d, 2016-04-22 (annotated)
- Committer:
- fabiombed
- Date:
- Fri Apr 22 14:41:25 2016 +0000
- Revision:
- 8:63b01a643f4d
- Parent:
- 7:eb2215fe86b2
- Child:
- 9:d530044d91b9
Some minor improvements
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 | 7:eb2215fe86b2 | 6 | * @date April, 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 | 7:eb2215fe86b2 | 68 | //const Gap::Address_t BLE_address_BE = {0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA}; // CLIENT address |
fabiombed | 7:eb2215fe86b2 | 69 | //const Gap::Address_t BLE_peer_address_BE = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}; // SERVER address (must be the same of SERVER) |
fabiombed | 7:eb2215fe86b2 | 70 | const Gap::Address_t BLE_address_BE = {0xCC, 0x00, 0x00, 0xE1, 0x80, 0x02}; // CLIENT address |
fabiombed | 7:eb2215fe86b2 | 71 | const Gap::Address_t BLE_peer_address_BE = {0xFD, 0x66, 0x05, 0x13, 0xBE, 0xBA}; // SERVER address (must be the same of SERVER) |
apalmieri | 0:2e972e7d3f64 | 72 | |
fabiombed | 7:eb2215fe86b2 | 73 | //const UUID::ShortUUIDBytes_t CONTROL_COMMAND_CHAR_UUID = 0xA001; |
fabiombed | 7:eb2215fe86b2 | 74 | //const UUID::ShortUUIDBytes_t SENS_STATUS_CHAR_UUID = 0xB001; |
fabiombed | 7:eb2215fe86b2 | 75 | //const UUID::ShortUUIDBytes_t SENS_DIFFERENCE_CHAR_UUID = 0xB002; |
fabiombed | 7:eb2215fe86b2 | 76 | //const UUID::ShortUUIDBytes_t SENS_POSITION_CHAR_UUID = 0xB003; |
fabiombed | 7:eb2215fe86b2 | 77 | //const UUID::ShortUUIDBytes_t SENS_SUNPANEL_CHAR_UUID = 0xB004; |
fabiombed | 7:eb2215fe86b2 | 78 | 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 | 79 | 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 | 80 | 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 | 81 | 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 | 82 | 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 | 83 | |
fabiombed | 7:eb2215fe86b2 | 84 | DiscoveredCharacteristic command_bleCharacteristic; |
fabiombed | 7:eb2215fe86b2 | 85 | DiscoveredCharacteristic status_bleCharacteristic; |
fabiombed | 4:17f96ffc073a | 86 | DiscoveredCharacteristic difference_bleCharacteristic; |
fabiombed | 4:17f96ffc073a | 87 | DiscoveredCharacteristic position_bleCharacteristic; |
fabiombed | 4:17f96ffc073a | 88 | DiscoveredCharacteristic sunpanel_bleCharacteristic; |
fabiombed | 4:17f96ffc073a | 89 | |
fabiombed | 7:eb2215fe86b2 | 90 | #define COMMAND_DATA_LEN 2+4+1+2 // TimeStamp (only for Response) + Feature + Type + Data |
fabiombed | 5:5e62f661a7ab | 91 | // 1 byte 0xFF 8 bit int8 |
fabiombed | 5:5e62f661a7ab | 92 | // 2 byte 0xFFFF 16 bit int16 |
fabiombed | 5:5e62f661a7ab | 93 | |
fabiombed | 7:eb2215fe86b2 | 94 | uint8_t value_write[COMMAND_DATA_LEN]; |
fabiombed | 7:eb2215fe86b2 | 95 | |
fabiombed | 8:63b01a643f4d | 96 | #define FeatureStatus 0x00000800 |
fabiombed | 8:63b01a643f4d | 97 | #define FeatureDifference 0x00001000 |
fabiombed | 8:63b01a643f4d | 98 | #define FeaturePosition 0x00002000 |
fabiombed | 8:63b01a643f4d | 99 | #define FeatureSunPanel 0x00004000 |
fabiombed | 5:5e62f661a7ab | 100 | |
fabiombed | 5:5e62f661a7ab | 101 | /* Variables ------------------------------------------------------------------*/ |
fabiombed | 5:5e62f661a7ab | 102 | |
fabiombed | 5:5e62f661a7ab | 103 | enum { |
fabiombed | 5:5e62f661a7ab | 104 | IDLE = 0, |
fabiombed | 7:eb2215fe86b2 | 105 | READ_STA = 1, |
fabiombed | 5:5e62f661a7ab | 106 | READ_DIF = 2, |
fabiombed | 5:5e62f661a7ab | 107 | READ_POS = 3, |
fabiombed | 5:5e62f661a7ab | 108 | READ_SUN = 4, |
fabiombed | 7:eb2215fe86b2 | 109 | WRITE_STA = 5, |
fabiombed | 5:5e62f661a7ab | 110 | WRITE_DIF = 6 |
fabiombed | 5:5e62f661a7ab | 111 | }; |
fabiombed | 5:5e62f661a7ab | 112 | int trigger_Op = IDLE; |
fabiombed | 5:5e62f661a7ab | 113 | bool trigger_button = false; |
fabiombed | 8:63b01a643f4d | 114 | 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 | 115 | |
fabiombed | 7:eb2215fe86b2 | 116 | 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 | 117 | int16_t diff=0; // Babybear or Accelerometer difference [--> Receive BLE] |
fabiombed | 5:5e62f661a7ab | 118 | int16_t pos=0; // Motor Position [--> Receive BLE] |
fabiombed | 5:5e62f661a7ab | 119 | int16_t measure=0; // ADC Value from SunPanel [--> Receive BLE] |
fabiombed | 4:17f96ffc073a | 120 | |
fabiombed | 4:17f96ffc073a | 121 | /* Initializations ------------------------------------------------------------*/ |
fabiombed | 4:17f96ffc073a | 122 | |
fabiombed | 7:eb2215fe86b2 | 123 | #ifdef Sensors |
apalmieri | 0:2e972e7d3f64 | 124 | |
fabiombed | 4:17f96ffc073a | 125 | // Initializing I2C bus |
fabiombed | 4:17f96ffc073a | 126 | DevI2C dev_i2c(D14, D15); |
fabiombed | 4:17f96ffc073a | 127 | |
fabiombed | 4:17f96ffc073a | 128 | // Initializing Sensors Component IKS01A1 |
fabiombed | 4:17f96ffc073a | 129 | static X_NUCLEO_IKS01A1 *mems; |
fabiombed | 4:17f96ffc073a | 130 | MotionSensor *accelerometer; |
apalmieri | 0:2e972e7d3f64 | 131 | |
fabiombed | 4:17f96ffc073a | 132 | int32_t acc_data[3]; // Accelerometer difference |
fabiombed | 5:5e62f661a7ab | 133 | int16_t acc_diff=0; // Accelerometer difference |
fabiombed | 7:eb2215fe86b2 | 134 | |
fabiombed | 4:17f96ffc073a | 135 | #endif |
fabiombed | 4:17f96ffc073a | 136 | |
fabiombed | 8:63b01a643f4d | 137 | DigitalOut myled(LED1, 0); |
fabiombed | 4:17f96ffc073a | 138 | InterruptIn mybutton(USER_BUTTON); |
fabiombed | 4:17f96ffc073a | 139 | |
fabiombed | 4:17f96ffc073a | 140 | /* User_Button_Pressed -------------------------------------------------------*/ |
fabiombed | 4:17f96ffc073a | 141 | |
fabiombed | 4:17f96ffc073a | 142 | void User_Button_Pressed(void) |
fabiombed | 8:63b01a643f4d | 143 | { |
fabiombed | 8:63b01a643f4d | 144 | |
fabiombed | 5:5e62f661a7ab | 145 | trigger_button = true; |
fabiombed | 7:eb2215fe86b2 | 146 | Status++; |
fabiombed | 8:63b01a643f4d | 147 | Status_to_Write = Status; |
fabiombed | 7:eb2215fe86b2 | 148 | |
fabiombed | 7:eb2215fe86b2 | 149 | #ifdef Sensors |
fabiombed | 8:63b01a643f4d | 150 | if (Status_to_Write>3) { Status=1; Status_to_Write=1; } |
fabiombed | 7:eb2215fe86b2 | 151 | #else |
fabiombed | 8:63b01a643f4d | 152 | if (Status_to_Write>2) { Status=1; Status_to_Write=1; } |
fabiombed | 7:eb2215fe86b2 | 153 | #endif |
fabiombed | 7:eb2215fe86b2 | 154 | |
apalmieri | 0:2e972e7d3f64 | 155 | } |
apalmieri | 0:2e972e7d3f64 | 156 | |
fabiombed | 4:17f96ffc073a | 157 | /* Bluetooth CallBack ---------------------------------------------------------*/ |
fabiombed | 4:17f96ffc073a | 158 | |
fabiombed | 4:17f96ffc073a | 159 | void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) |
fabiombed | 4:17f96ffc073a | 160 | { |
fabiombed | 4:17f96ffc073a | 161 | if (params->peerAddr[0] != BLE_peer_address_BE[0]) // return if miss the server MAC address |
fabiombed | 4:17f96ffc073a | 162 | { |
fabiombed | 6:ce0e5024e3fa | 163 | printf("Missing Expected MAC Address\n\r"); |
fabiombed | 4:17f96ffc073a | 164 | return; // exit from advertisementCallback |
apalmieri | 0:2e972e7d3f64 | 165 | } |
apalmieri | 0:2e972e7d3f64 | 166 | |
fabiombed | 4:17f96ffc073a | 167 | // printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", |
fabiombed | 4:17f96ffc073a | 168 | // params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], |
fabiombed | 4:17f96ffc073a | 169 | // params->rssi, params->isScanResponse, params->type); |
fabiombed | 4:17f96ffc073a | 170 | |
fabiombed | 4:17f96ffc073a | 171 | printf("Found Expected MAC Address: isScanResponse %u, AdvertisementType %u\r\n",params->isScanResponse, params->type); |
apalmieri | 0:2e972e7d3f64 | 172 | |
fabiombed | 4:17f96ffc073a | 173 | if(!params->isScanResponse) |
fabiombed | 4:17f96ffc073a | 174 | { |
apalmieri | 0:2e972e7d3f64 | 175 | BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_PUBLIC, NULL, NULL); |
apalmieri | 0:2e972e7d3f64 | 176 | } |
apalmieri | 0:2e972e7d3f64 | 177 | } |
apalmieri | 0:2e972e7d3f64 | 178 | |
fabiombed | 4:17f96ffc073a | 179 | void serviceDiscoveryCallback(const DiscoveredService *service) |
fabiombed | 4:17f96ffc073a | 180 | { |
fabiombed | 4:17f96ffc073a | 181 | printf("Start Service Discovery\r\n"); |
fabiombed | 7:eb2215fe86b2 | 182 | |
fabiombed | 4:17f96ffc073a | 183 | if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) |
fabiombed | 4:17f96ffc073a | 184 | { |
apalmieri | 0:2e972e7d3f64 | 185 | printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle()); |
fabiombed | 4:17f96ffc073a | 186 | } |
fabiombed | 4:17f96ffc073a | 187 | else |
fabiombed | 4:17f96ffc073a | 188 | { |
apalmieri | 0:2e972e7d3f64 | 189 | printf("S UUID-"); |
apalmieri | 0:2e972e7d3f64 | 190 | const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); |
fabiombed | 4:17f96ffc073a | 191 | for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) |
fabiombed | 4:17f96ffc073a | 192 | { |
apalmieri | 0:2e972e7d3f64 | 193 | printf("%02X", longUUIDBytes[i]); |
apalmieri | 0:2e972e7d3f64 | 194 | } |
apalmieri | 0:2e972e7d3f64 | 195 | printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle()); |
apalmieri | 0:2e972e7d3f64 | 196 | } |
fabiombed | 7:eb2215fe86b2 | 197 | |
apalmieri | 0:2e972e7d3f64 | 198 | } |
fabiombed | 4:17f96ffc073a | 199 | |
fabiombed | 4:17f96ffc073a | 200 | void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) |
fabiombed | 4:17f96ffc073a | 201 | { |
fabiombed | 7:eb2215fe86b2 | 202 | int COMMAND_CHAR_count=0, STATUS_CHAR_count=0, DIFFERENCE_CHAR_count=0, POSITION_CHAR_count=0, SUNPANEL_CHAR_count=0; |
fabiombed | 7:eb2215fe86b2 | 203 | const uint8_t *longUUIDBytes = characteristicP->getUUID().getBaseUUID(); |
fabiombed | 7:eb2215fe86b2 | 204 | |
fabiombed | 7:eb2215fe86b2 | 205 | for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) |
fabiombed | 7:eb2215fe86b2 | 206 | { |
fabiombed | 7:eb2215fe86b2 | 207 | if (longUUIDBytes[i] == CONTROL_COMMAND_CHAR_UUID[i]) COMMAND_CHAR_count++; |
fabiombed | 7:eb2215fe86b2 | 208 | if (longUUIDBytes[i] == SENS_STATUS_CHAR_UUID[i]) STATUS_CHAR_count++; |
fabiombed | 7:eb2215fe86b2 | 209 | if (longUUIDBytes[i] == SENS_DIFFERENCE_CHAR_UUID[i]) DIFFERENCE_CHAR_count++; |
fabiombed | 7:eb2215fe86b2 | 210 | if (longUUIDBytes[i] == SENS_POSITION_CHAR_UUID[i]) POSITION_CHAR_count++; |
fabiombed | 7:eb2215fe86b2 | 211 | if (longUUIDBytes[i] == SENS_SUNPANEL_CHAR_UUID[i]) SUNPANEL_CHAR_count++; |
fabiombed | 4:17f96ffc073a | 212 | } |
fabiombed | 4:17f96ffc073a | 213 | |
fabiombed | 7:eb2215fe86b2 | 214 | //if (characteristicP->getUUID().getShortUUID() == CONTROL_COMMAND_CHAR_UUID) |
fabiombed | 7:eb2215fe86b2 | 215 | if (COMMAND_CHAR_count == UUID::LENGTH_OF_LONG_UUID) |
fabiombed | 7:eb2215fe86b2 | 216 | { |
fabiombed | 7:eb2215fe86b2 | 217 | command_bleCharacteristic = *characteristicP; |
fabiombed | 7:eb2215fe86b2 | 218 | printf("Found Command Characteristic\r\n"); |
fabiombed | 7:eb2215fe86b2 | 219 | } |
fabiombed | 7:eb2215fe86b2 | 220 | |
fabiombed | 7:eb2215fe86b2 | 221 | //if (characteristicP->getUUID().getShortUUID() == SENS_STATUS_CHAR_UUID) |
fabiombed | 7:eb2215fe86b2 | 222 | if (STATUS_CHAR_count == UUID::LENGTH_OF_LONG_UUID) |
fabiombed | 7:eb2215fe86b2 | 223 | { |
fabiombed | 7:eb2215fe86b2 | 224 | status_bleCharacteristic = *characteristicP; |
fabiombed | 7:eb2215fe86b2 | 225 | printf("Found Status Characteristic\r\n"); |
fabiombed | 7:eb2215fe86b2 | 226 | } |
fabiombed | 7:eb2215fe86b2 | 227 | |
fabiombed | 7:eb2215fe86b2 | 228 | //if (characteristicP->getUUID().getShortUUID() == SENS_DIFFERENCE_CHAR_UUID) |
fabiombed | 7:eb2215fe86b2 | 229 | if (DIFFERENCE_CHAR_count == UUID::LENGTH_OF_LONG_UUID) |
fabiombed | 4:17f96ffc073a | 230 | { |
fabiombed | 4:17f96ffc073a | 231 | difference_bleCharacteristic = *characteristicP; |
fabiombed | 7:eb2215fe86b2 | 232 | printf("Found Difference Characteristic\r\n"); |
fabiombed | 4:17f96ffc073a | 233 | } |
fabiombed | 7:eb2215fe86b2 | 234 | |
fabiombed | 7:eb2215fe86b2 | 235 | //if (characteristicP->getUUID().getShortUUID() == SENS_POSITION_CHAR_UUID) |
fabiombed | 7:eb2215fe86b2 | 236 | if (POSITION_CHAR_count == UUID::LENGTH_OF_LONG_UUID) |
fabiombed | 4:17f96ffc073a | 237 | { |
fabiombed | 4:17f96ffc073a | 238 | position_bleCharacteristic = *characteristicP; |
fabiombed | 7:eb2215fe86b2 | 239 | printf("Found Position Characteristic\r\n"); |
fabiombed | 4:17f96ffc073a | 240 | } |
fabiombed | 7:eb2215fe86b2 | 241 | |
fabiombed | 7:eb2215fe86b2 | 242 | //if (characteristicP->getUUID().getShortUUID() == SENS_SUNPANEL_CHAR_UUID) |
fabiombed | 7:eb2215fe86b2 | 243 | if (SUNPANEL_CHAR_count == UUID::LENGTH_OF_LONG_UUID) |
fabiombed | 4:17f96ffc073a | 244 | { |
fabiombed | 4:17f96ffc073a | 245 | sunpanel_bleCharacteristic = *characteristicP; |
fabiombed | 7:eb2215fe86b2 | 246 | printf("Found Sunpanel Characteristic\r\n"); |
apalmieri | 0:2e972e7d3f64 | 247 | } |
apalmieri | 0:2e972e7d3f64 | 248 | } |
apalmieri | 0:2e972e7d3f64 | 249 | |
fabiombed | 4:17f96ffc073a | 250 | void discoveryTerminationCallback(Gap::Handle_t connectionHandle) |
fabiombed | 4:17f96ffc073a | 251 | { |
fabiombed | 4:17f96ffc073a | 252 | //printf("discoveryTerminationCallback for handle %u\r\n", connectionHandle); |
fabiombed | 4:17f96ffc073a | 253 | printf("Stop Service Discovery\r\n\r\n"); |
fabiombed | 5:5e62f661a7ab | 254 | |
fabiombed | 7:eb2215fe86b2 | 255 | trigger_Op = READ_STA; |
apalmieri | 0:2e972e7d3f64 | 256 | } |
apalmieri | 0:2e972e7d3f64 | 257 | |
fabiombed | 4:17f96ffc073a | 258 | void onDataReadCallback(const GattReadCallbackParams *response) |
fabiombed | 4:17f96ffc073a | 259 | { |
fabiombed | 4:17f96ffc073a | 260 | // Read SERVER --> CLIENT |
fabiombed | 4:17f96ffc073a | 261 | |
fabiombed | 7:eb2215fe86b2 | 262 | // From SERVER it receives a sensor in this format: TimeStamp (2) + Data (2) |
fabiombed | 7:eb2215fe86b2 | 263 | uint16_t TimeStamp = response->data[0]<<8 | response->data[1]; |
fabiombed | 7:eb2215fe86b2 | 264 | 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 | 265 | |
fabiombed | 7:eb2215fe86b2 | 266 | //printf("\r\n\ronDataReadCallback (TimeStamp %x)", TimeStamp); |
fabiombed | 7:eb2215fe86b2 | 267 | //printf("\n\ronDataReadCallback (Data Hex %x)", Data); |
fabiombed | 7:eb2215fe86b2 | 268 | //printf("\n\ronDataReadCallback (Data Dec %d)", Data); |
fabiombed | 5:5e62f661a7ab | 269 | |
fabiombed | 7:eb2215fe86b2 | 270 | if (response->handle == status_bleCharacteristic.getValueHandle()) |
fabiombed | 5:5e62f661a7ab | 271 | { |
fabiombed | 7:eb2215fe86b2 | 272 | Status = Data; |
fabiombed | 7:eb2215fe86b2 | 273 | printf("\n\rReceive BLE Display Status %d\n\r", Status); |
fabiombed | 5:5e62f661a7ab | 274 | |
fabiombed | 5:5e62f661a7ab | 275 | trigger_Op = READ_DIF; |
fabiombed | 4:17f96ffc073a | 276 | } |
fabiombed | 4:17f96ffc073a | 277 | |
fabiombed | 4:17f96ffc073a | 278 | if (response->handle == difference_bleCharacteristic.getValueHandle()) |
fabiombed | 5:5e62f661a7ab | 279 | { |
fabiombed | 7:eb2215fe86b2 | 280 | diff = Data; |
fabiombed | 5:5e62f661a7ab | 281 | printf("Receive BLE Difference %d lux/mems\n\r", diff); |
fabiombed | 5:5e62f661a7ab | 282 | |
fabiombed | 5:5e62f661a7ab | 283 | trigger_Op = READ_POS; |
apalmieri | 0:2e972e7d3f64 | 284 | } |
fabiombed | 4:17f96ffc073a | 285 | |
fabiombed | 4:17f96ffc073a | 286 | if (response->handle == position_bleCharacteristic.getValueHandle()) |
fabiombed | 5:5e62f661a7ab | 287 | { |
fabiombed | 7:eb2215fe86b2 | 288 | pos = Data; |
fabiombed | 5:5e62f661a7ab | 289 | printf("Receive BLE Position %d\n\r", pos); |
fabiombed | 5:5e62f661a7ab | 290 | |
fabiombed | 5:5e62f661a7ab | 291 | trigger_Op = READ_SUN; |
fabiombed | 4:17f96ffc073a | 292 | } |
apalmieri | 0:2e972e7d3f64 | 293 | |
fabiombed | 4:17f96ffc073a | 294 | if (response->handle == sunpanel_bleCharacteristic.getValueHandle()) |
fabiombed | 5:5e62f661a7ab | 295 | { |
fabiombed | 7:eb2215fe86b2 | 296 | measure = Data; |
fabiombed | 5:5e62f661a7ab | 297 | printf("Receive BLE Sunpanel %d mV\n\r", measure); |
fabiombed | 4:17f96ffc073a | 298 | |
fabiombed | 7:eb2215fe86b2 | 299 | trigger_Op = WRITE_STA; |
fabiombed | 4:17f96ffc073a | 300 | } |
apalmieri | 0:2e972e7d3f64 | 301 | } |
apalmieri | 0:2e972e7d3f64 | 302 | |
fabiombed | 4:17f96ffc073a | 303 | void myonDataWriteCallback(const GattWriteCallbackParams *response) |
fabiombed | 4:17f96ffc073a | 304 | { |
fabiombed | 4:17f96ffc073a | 305 | // Write CLIENT --> SERVER |
fabiombed | 4:17f96ffc073a | 306 | |
fabiombed | 7:eb2215fe86b2 | 307 | if (response->handle == command_bleCharacteristic.getValueHandle()) |
fabiombed | 7:eb2215fe86b2 | 308 | { |
fabiombed | 7:eb2215fe86b2 | 309 | |
fabiombed | 7:eb2215fe86b2 | 310 | // From CLIENT it writes a command in this format: Feature (4) + Type (1) + Data (2) |
fabiombed | 7:eb2215fe86b2 | 311 | uint32_t Feature = response->data[0]<<24 | response->data[1]<<16 | response->data[2]<<8 | response->data[3]; |
fabiombed | 7:eb2215fe86b2 | 312 | uint8_t Type = response->data[4]; |
fabiombed | 7:eb2215fe86b2 | 313 | int16_t Data = response->data[5]<<8 | response->data[6]; |
fabiombed | 7:eb2215fe86b2 | 314 | |
fabiombed | 7:eb2215fe86b2 | 315 | //printf("\r\nmyonDataWriteCallback (Feature %x)", Feature); |
fabiombed | 7:eb2215fe86b2 | 316 | //printf("\r\nmyonDataWriteCallback (Type %x)", Type); // Not Used |
fabiombed | 7:eb2215fe86b2 | 317 | //printf("\r\nmyonDataWriteCallback (Data Hex %x)", Data); |
fabiombed | 7:eb2215fe86b2 | 318 | //printf("\r\nmyonDataWriteCallback (Data Dec %d)", Data); |
fabiombed | 5:5e62f661a7ab | 319 | |
fabiombed | 7:eb2215fe86b2 | 320 | switch(Feature) |
fabiombed | 7:eb2215fe86b2 | 321 | { |
fabiombed | 7:eb2215fe86b2 | 322 | case FeatureStatus: |
fabiombed | 7:eb2215fe86b2 | 323 | printf("\r\nSend STATUS %d\r\n", Data); |
fabiombed | 7:eb2215fe86b2 | 324 | Status = Data; |
fabiombed | 7:eb2215fe86b2 | 325 | trigger_Op = WRITE_DIF; |
fabiombed | 7:eb2215fe86b2 | 326 | break; |
fabiombed | 7:eb2215fe86b2 | 327 | |
fabiombed | 7:eb2215fe86b2 | 328 | case FeatureDifference: |
fabiombed | 7:eb2215fe86b2 | 329 | printf("\r\nSend DIFF %d\r\n", Data); |
fabiombed | 7:eb2215fe86b2 | 330 | diff = Data; |
fabiombed | 7:eb2215fe86b2 | 331 | trigger_Op = READ_STA; |
fabiombed | 7:eb2215fe86b2 | 332 | break; |
fabiombed | 7:eb2215fe86b2 | 333 | |
fabiombed | 7:eb2215fe86b2 | 334 | case FeaturePosition: |
fabiombed | 7:eb2215fe86b2 | 335 | break; |
fabiombed | 7:eb2215fe86b2 | 336 | |
fabiombed | 7:eb2215fe86b2 | 337 | case FeatureSunPanel: |
fabiombed | 7:eb2215fe86b2 | 338 | break; |
fabiombed | 7:eb2215fe86b2 | 339 | |
fabiombed | 7:eb2215fe86b2 | 340 | default: |
fabiombed | 7:eb2215fe86b2 | 341 | break; |
fabiombed | 7:eb2215fe86b2 | 342 | } |
fabiombed | 5:5e62f661a7ab | 343 | |
fabiombed | 5:5e62f661a7ab | 344 | } |
fabiombed | 7:eb2215fe86b2 | 345 | |
apalmieri | 0:2e972e7d3f64 | 346 | } |
apalmieri | 0:2e972e7d3f64 | 347 | |
apalmieri | 0:2e972e7d3f64 | 348 | /** |
apalmieri | 0:2e972e7d3f64 | 349 | * This function is called when the ble initialization process has failled |
apalmieri | 0:2e972e7d3f64 | 350 | */ |
apalmieri | 0:2e972e7d3f64 | 351 | void onBleInitError(BLE &ble, ble_error_t error) |
apalmieri | 0:2e972e7d3f64 | 352 | { |
apalmieri | 0:2e972e7d3f64 | 353 | /* Initialization error handling should go here */ |
fabiombed | 5:5e62f661a7ab | 354 | |
fabiombed | 5:5e62f661a7ab | 355 | printf("Inizialization Error\n\r"); |
apalmieri | 0:2e972e7d3f64 | 356 | } |
apalmieri | 0:2e972e7d3f64 | 357 | |
fabiombed | 4:17f96ffc073a | 358 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) |
fabiombed | 4:17f96ffc073a | 359 | { |
fabiombed | 7:eb2215fe86b2 | 360 | // Good only for Short UUID |
fabiombed | 7:eb2215fe86b2 | 361 | //uint16_t CONTROL_SERVICE_UUID = 0x00,0x00,0x00,0x00,0x00,0x0F,0x11,0xe1,0x9a,0xb4,0x00,0x02,0xa5,0xd5,0xc5,0x1b; |
fabiombed | 7:eb2215fe86b2 | 362 | //uint16_t COMMAND_CHARACTERISTIC_UUID = 0x00,0x00,0x00,0x02,0x00,0x0F,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b; |
fabiombed | 7:eb2215fe86b2 | 363 | //uint16_t SENSORS_SERVICE_UUID = 0x00,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0x9a,0xb4,0x00,0x02,0xa5,0xd5,0xc5,0x1b; |
fabiombed | 7:eb2215fe86b2 | 364 | //uint16_t STATUS_CHARACTERISTIC_UUID = 0x00,0x00,0x04,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b; |
fabiombed | 7:eb2215fe86b2 | 365 | //uint16_t DIFFERENCE_CHARACTERISTIC_UUID = 0x00,0x00,0x08,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b; |
fabiombed | 7:eb2215fe86b2 | 366 | //uint16_t POSITION_CHARACTERISTIC_UUID = 0x00,0x00,0x10,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b; |
fabiombed | 7:eb2215fe86b2 | 367 | //uint16_t SUNPANEL_CHARACTERISTIC_UUID = 0x00,0x00,0x20,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b; |
fabiombed | 4:17f96ffc073a | 368 | |
fabiombed | 4:17f96ffc073a | 369 | if (params->role == Gap::CENTRAL) |
fabiombed | 4:17f96ffc073a | 370 | { |
fabiombed | 4:17f96ffc073a | 371 | BLE &ble = BLE::Instance(); |
fabiombed | 4:17f96ffc073a | 372 | ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); |
fabiombed | 4:17f96ffc073a | 373 | // Discover all SERVICES and CHARACTERISTICS |
fabiombed | 4:17f96ffc073a | 374 | ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback); |
fabiombed | 4:17f96ffc073a | 375 | |
fabiombed | 5:5e62f661a7ab | 376 | // Works only for one characteristic, if you need to discover all characteristic, use the above command |
fabiombed | 7:eb2215fe86b2 | 377 | //ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, CONTROL_SERVICE_UUID, COMMAND_CHARACTERISTIC_UUID); |
fabiombed | 7:eb2215fe86b2 | 378 | //ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, SENSORS_SERVICE_UUID, STATUS_CHARACTERISTIC_UUID); |
fabiombed | 4:17f96ffc073a | 379 | //ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, SENSORS_SERVICE_UUID, DIFFERENCE_CHARACTERISTIC_UUID); |
fabiombed | 4:17f96ffc073a | 380 | //ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, SENSORS_SERVICE_UUID, POSITION_CHARACTERISTIC_UUID); |
fabiombed | 4:17f96ffc073a | 381 | //ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, SENSORS_SERVICE_UUID, SUNPANEL_CHARACTERISTIC_UUID); |
fabiombed | 4:17f96ffc073a | 382 | } |
fabiombed | 4:17f96ffc073a | 383 | |
fabiombed | 4:17f96ffc073a | 384 | printf("Remote Connected\n\r"); |
fabiombed | 8:63b01a643f4d | 385 | |
fabiombed | 8:63b01a643f4d | 386 | myled = 1; // Power ON Led Green on CLIENT Board to indicate Connection between CLIENT and SERVER |
fabiombed | 4:17f96ffc073a | 387 | } |
fabiombed | 4:17f96ffc073a | 388 | |
fabiombed | 4:17f96ffc073a | 389 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
fabiombed | 4:17f96ffc073a | 390 | { |
fabiombed | 4:17f96ffc073a | 391 | (void)params; |
fabiombed | 5:5e62f661a7ab | 392 | |
fabiombed | 4:17f96ffc073a | 393 | printf("Remote Disconnected\n\r"); |
fabiombed | 8:63b01a643f4d | 394 | |
fabiombed | 8:63b01a643f4d | 395 | myled = 0; // Power OFF Led Green on CLIENT Board to indicate Disconnection between CLIENT and SERVER |
fabiombed | 4:17f96ffc073a | 396 | } |
fabiombed | 4:17f96ffc073a | 397 | |
apalmieri | 0:2e972e7d3f64 | 398 | /** |
apalmieri | 0:2e972e7d3f64 | 399 | * Callback triggered when the ble initialization process has finished |
apalmieri | 0:2e972e7d3f64 | 400 | */ |
apalmieri | 0:2e972e7d3f64 | 401 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
apalmieri | 0:2e972e7d3f64 | 402 | { |
apalmieri | 0:2e972e7d3f64 | 403 | BLE& ble = params->ble; |
apalmieri | 0:2e972e7d3f64 | 404 | ble_error_t error = params->error; |
apalmieri | 0:2e972e7d3f64 | 405 | |
fabiombed | 4:17f96ffc073a | 406 | if (error != BLE_ERROR_NONE) |
fabiombed | 4:17f96ffc073a | 407 | { |
apalmieri | 0:2e972e7d3f64 | 408 | /* In case of error, forward the error handling to onBleInitError */ |
apalmieri | 0:2e972e7d3f64 | 409 | onBleInitError(ble, error); |
apalmieri | 0:2e972e7d3f64 | 410 | return; |
apalmieri | 0:2e972e7d3f64 | 411 | } |
apalmieri | 0:2e972e7d3f64 | 412 | |
apalmieri | 0:2e972e7d3f64 | 413 | /* Ensure that it is the default instance of BLE */ |
fabiombed | 4:17f96ffc073a | 414 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { return; } |
apalmieri | 0:2e972e7d3f64 | 415 | |
apalmieri | 0:2e972e7d3f64 | 416 | // Set BT Address |
apalmieri | 1:e0f4bcce540c | 417 | ble.gap().setAddress(BLEProtocol::AddressType::PUBLIC, BLE_address_BE); |
apalmieri | 0:2e972e7d3f64 | 418 | |
apalmieri | 0:2e972e7d3f64 | 419 | ble.gap().onConnection(connectionCallback); |
apalmieri | 0:2e972e7d3f64 | 420 | ble.onDisconnection(disconnectionCallback); |
apalmieri | 0:2e972e7d3f64 | 421 | |
fabiombed | 4:17f96ffc073a | 422 | ble.gattClient().onDataRead(onDataReadCallback); |
fabiombed | 4:17f96ffc073a | 423 | ble.gattClient().onDataWrite(myonDataWriteCallback); |
apalmieri | 0:2e972e7d3f64 | 424 | |
apalmieri | 0:2e972e7d3f64 | 425 | ble.gap().setScanParams(SCAN_INT, SCAN_WIND); |
apalmieri | 0:2e972e7d3f64 | 426 | ble.gap().startScan(advertisementCallback); |
fabiombed | 7:eb2215fe86b2 | 427 | |
fabiombed | 4:17f96ffc073a | 428 | // Main Loop |
fabiombed | 7:eb2215fe86b2 | 429 | while (true) |
fabiombed | 7:eb2215fe86b2 | 430 | { |
fabiombed | 5:5e62f661a7ab | 431 | |
fabiombed | 5:5e62f661a7ab | 432 | //if (!ble.gattClient().isServiceDiscoveryActive()) |
fabiombed | 5:5e62f661a7ab | 433 | if (trigger_Op!=IDLE) // Wait until discoveryTerminationCallback |
fabiombed | 5:5e62f661a7ab | 434 | { |
fabiombed | 6:ce0e5024e3fa | 435 | |
fabiombed | 5:5e62f661a7ab | 436 | // At present ARM BLE_API don't support Enable/Disable Notify from Client |
fabiombed | 7:eb2215fe86b2 | 437 | //command_bleCharacteristic.notify(); |
fabiombed | 5:5e62f661a7ab | 438 | //difference_bleCharacteristic.notify(); |
fabiombed | 5:5e62f661a7ab | 439 | //position_bleCharacteristic.notify(); |
fabiombed | 5:5e62f661a7ab | 440 | //sunpanel_bleCharacteristic.notify(); |
fabiombed | 5:5e62f661a7ab | 441 | |
fabiombed | 5:5e62f661a7ab | 442 | // Don't run more ble .read or .write in the same loop |
fabiombed | 5:5e62f661a7ab | 443 | // because the callback is an interrupt and it doesn't work correctly if overloaded |
fabiombed | 7:eb2215fe86b2 | 444 | |
fabiombed | 8:63b01a643f4d | 445 | if (Status == 0) |
fabiombed | 8:63b01a643f4d | 446 | { if (trigger_Op == READ_DIF) trigger_Op = WRITE_STA; } // if Display 0, skip READ_DIF,READ_POS,READ_SUN |
fabiombed | 8:63b01a643f4d | 447 | |
fabiombed | 8:63b01a643f4d | 448 | if (Status == 0 || Status == 1 || Status == 2) |
fabiombed | 8:63b01a643f4d | 449 | { if (trigger_Op == WRITE_DIF) trigger_Op = READ_STA; } // if Display 0 or 1 or 2, skip WRITE_DIF |
fabiombed | 5:5e62f661a7ab | 450 | |
fabiombed | 7:eb2215fe86b2 | 451 | if (Status == 3) |
fabiombed | 7:eb2215fe86b2 | 452 | { if (trigger_Op == READ_DIF) trigger_Op = READ_POS; } // if Display 3, skip READ_DIF |
fabiombed | 8:63b01a643f4d | 453 | |
fabiombed | 8:63b01a643f4d | 454 | if (trigger_button == true) { trigger_Op = WRITE_STA; } // jump directly to WRITE_STA |
fabiombed | 8:63b01a643f4d | 455 | |
fabiombed | 5:5e62f661a7ab | 456 | switch(trigger_Op) |
fabiombed | 4:17f96ffc073a | 457 | { |
fabiombed | 7:eb2215fe86b2 | 458 | case READ_STA: |
fabiombed | 7:eb2215fe86b2 | 459 | status_bleCharacteristic.read(); |
fabiombed | 5:5e62f661a7ab | 460 | trigger_Op = IDLE; |
fabiombed | 5:5e62f661a7ab | 461 | break; |
fabiombed | 5:5e62f661a7ab | 462 | case READ_DIF: |
fabiombed | 4:17f96ffc073a | 463 | difference_bleCharacteristic.read(); |
fabiombed | 5:5e62f661a7ab | 464 | trigger_Op = IDLE; |
fabiombed | 5:5e62f661a7ab | 465 | break; |
fabiombed | 5:5e62f661a7ab | 466 | case READ_POS: |
fabiombed | 4:17f96ffc073a | 467 | position_bleCharacteristic.read(); |
fabiombed | 5:5e62f661a7ab | 468 | trigger_Op = IDLE; |
fabiombed | 5:5e62f661a7ab | 469 | break; |
fabiombed | 5:5e62f661a7ab | 470 | case READ_SUN: |
fabiombed | 4:17f96ffc073a | 471 | sunpanel_bleCharacteristic.read(); |
fabiombed | 5:5e62f661a7ab | 472 | trigger_Op = IDLE; |
fabiombed | 5:5e62f661a7ab | 473 | break; |
fabiombed | 7:eb2215fe86b2 | 474 | case WRITE_STA: |
fabiombed | 5:5e62f661a7ab | 475 | if (trigger_button == true) |
fabiombed | 8:63b01a643f4d | 476 | { |
fabiombed | 7:eb2215fe86b2 | 477 | memset (value_write, 0, COMMAND_DATA_LEN); |
fabiombed | 7:eb2215fe86b2 | 478 | //value_write = Feature (4) + Type (1) + Data (2) |
fabiombed | 7:eb2215fe86b2 | 479 | value_write[0] = (FeatureStatus & 0xff000000) >> 24; |
fabiombed | 7:eb2215fe86b2 | 480 | value_write[1] = (FeatureStatus & 0x00ff0000) >> 16; |
fabiombed | 7:eb2215fe86b2 | 481 | value_write[2] = (FeatureStatus & 0x0000ff00) >> 8; |
fabiombed | 7:eb2215fe86b2 | 482 | value_write[3] = (FeatureStatus & 0x000000ff); |
fabiombed | 7:eb2215fe86b2 | 483 | value_write[4] = (0x00); |
fabiombed | 8:63b01a643f4d | 484 | value_write[5] = (Status_to_Write & 0xff00) >> 8; |
fabiombed | 8:63b01a643f4d | 485 | value_write[6] = (Status_to_Write & 0x00ff); |
fabiombed | 7:eb2215fe86b2 | 486 | command_bleCharacteristic.write(COMMAND_DATA_LEN, value_write); |
fabiombed | 7:eb2215fe86b2 | 487 | |
fabiombed | 5:5e62f661a7ab | 488 | trigger_Op = IDLE; |
fabiombed | 5:5e62f661a7ab | 489 | trigger_button = false; |
fabiombed | 5:5e62f661a7ab | 490 | } else { |
fabiombed | 5:5e62f661a7ab | 491 | #ifdef Sensors |
fabiombed | 5:5e62f661a7ab | 492 | trigger_Op = WRITE_DIF; |
fabiombed | 5:5e62f661a7ab | 493 | #else |
fabiombed | 7:eb2215fe86b2 | 494 | trigger_Op = READ_STA; |
fabiombed | 5:5e62f661a7ab | 495 | #endif |
fabiombed | 5:5e62f661a7ab | 496 | } |
fabiombed | 5:5e62f661a7ab | 497 | break; |
fabiombed | 5:5e62f661a7ab | 498 | case WRITE_DIF: |
fabiombed | 5:5e62f661a7ab | 499 | #ifdef Sensors |
fabiombed | 5:5e62f661a7ab | 500 | accelerometer->Get_X_Axes(acc_data); |
fabiombed | 5:5e62f661a7ab | 501 | acc_diff = acc_data[0]; |
fabiombed | 5:5e62f661a7ab | 502 | //printf("Send BLE Difference %d lux/mems\n\r", acc_diff); |
fabiombed | 7:eb2215fe86b2 | 503 | |
fabiombed | 7:eb2215fe86b2 | 504 | memset (value_write, 0, COMMAND_DATA_LEN); |
fabiombed | 7:eb2215fe86b2 | 505 | //value_write = Feature (4) + Type (1) + Data (2) |
fabiombed | 7:eb2215fe86b2 | 506 | value_write[0] = (FeatureDifference & 0xff000000) >> 24; |
fabiombed | 7:eb2215fe86b2 | 507 | value_write[1] = (FeatureDifference & 0x00ff0000) >> 16; |
fabiombed | 7:eb2215fe86b2 | 508 | value_write[2] = (FeatureDifference & 0x0000ff00) >> 8; |
fabiombed | 7:eb2215fe86b2 | 509 | value_write[3] = (FeatureDifference & 0x000000ff); |
fabiombed | 7:eb2215fe86b2 | 510 | value_write[4] = (0x00); |
fabiombed | 7:eb2215fe86b2 | 511 | value_write[5] = (acc_diff & 0xff00) >> 8; |
fabiombed | 7:eb2215fe86b2 | 512 | value_write[6] = (acc_diff & 0x00ff); |
fabiombed | 7:eb2215fe86b2 | 513 | command_bleCharacteristic.write(COMMAND_DATA_LEN, value_write); |
fabiombed | 7:eb2215fe86b2 | 514 | |
fabiombed | 5:5e62f661a7ab | 515 | trigger_Op = IDLE; |
fabiombed | 5:5e62f661a7ab | 516 | #else |
fabiombed | 7:eb2215fe86b2 | 517 | trigger_Op = READ_STA; |
fabiombed | 5:5e62f661a7ab | 518 | #endif |
fabiombed | 5:5e62f661a7ab | 519 | break; |
fabiombed | 5:5e62f661a7ab | 520 | default: |
fabiombed | 5:5e62f661a7ab | 521 | break; |
fabiombed | 4:17f96ffc073a | 522 | } |
fabiombed | 4:17f96ffc073a | 523 | |
apalmieri | 0:2e972e7d3f64 | 524 | } |
fabiombed | 4:17f96ffc073a | 525 | ble.waitForEvent(); |
fabiombed | 7:eb2215fe86b2 | 526 | } |
apalmieri | 0:2e972e7d3f64 | 527 | } |
apalmieri | 0:2e972e7d3f64 | 528 | |
fabiombed | 5:5e62f661a7ab | 529 | /* Ticker --------------------------------------------------------------------*/ |
fabiombed | 5:5e62f661a7ab | 530 | /* |
fabiombed | 5:5e62f661a7ab | 531 | void ticker_Callback(void) |
fabiombed | 5:5e62f661a7ab | 532 | { |
fabiombed | 5:5e62f661a7ab | 533 | |
fabiombed | 5:5e62f661a7ab | 534 | } |
fabiombed | 5:5e62f661a7ab | 535 | */ |
fabiombed | 4:17f96ffc073a | 536 | /* Main ----------------------------------------------------------------------*/ |
fabiombed | 4:17f96ffc073a | 537 | |
apalmieri | 0:2e972e7d3f64 | 538 | int main(void) |
apalmieri | 0:2e972e7d3f64 | 539 | { |
fabiombed | 7:eb2215fe86b2 | 540 | printf("\r\n\r\nSunTracker Remote Control by Fabio Brembilla\r\n\r\n"); |
fabiombed | 4:17f96ffc073a | 541 | |
fabiombed | 4:17f96ffc073a | 542 | #ifdef Sensors |
fabiombed | 4:17f96ffc073a | 543 | // Initializing Sensors Component |
fabiombed | 4:17f96ffc073a | 544 | mems=X_NUCLEO_IKS01A1::Instance(&dev_i2c); |
fabiombed | 4:17f96ffc073a | 545 | accelerometer = mems->GetAccelerometer(); |
fabiombed | 4:17f96ffc073a | 546 | printf("Init Sensors OK\r\n"); |
fabiombed | 4:17f96ffc073a | 547 | #endif |
fabiombed | 5:5e62f661a7ab | 548 | |
fabiombed | 5:5e62f661a7ab | 549 | //Ticker ticker; |
fabiombed | 5:5e62f661a7ab | 550 | //ticker.attach(ticker_Callback, 1); // every 1 second |
fabiombed | 4:17f96ffc073a | 551 | |
fabiombed | 4:17f96ffc073a | 552 | mybutton.fall(&User_Button_Pressed); |
fabiombed | 4:17f96ffc073a | 553 | |
fabiombed | 7:eb2215fe86b2 | 554 | BLE::Instance().init(bleInitComplete); |
fabiombed | 7:eb2215fe86b2 | 555 | |
fabiombed | 4:17f96ffc073a | 556 | } |