Ble Demo with Raspberry PI
Dependencies: BLE_API mbed nRF51822
Fork of BLE_DEMO_SPI_HARDCODE by
main.cpp@18:0eefac249a4b, 2017-10-13 (annotated)
- Committer:
- hmiot
- Date:
- Fri Oct 13 07:25:54 2017 +0000
- Revision:
- 18:0eefac249a4b
- Parent:
- 17:2e0273d799e2
Updated
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hmiot | 7:66586d2d7cb5 | 1 | /************************************************************************ |
hmiot | 7:66586d2d7cb5 | 2 | * * |
hmiot | 7:66586d2d7cb5 | 3 | * * |
hmiot | 7:66586d2d7cb5 | 4 | ************************************************************************/ |
hmiot | 6:71e7a446ae6a | 5 | |
hmiot | 7:66586d2d7cb5 | 6 | #include "hm_config.h" |
hmiot | 7:66586d2d7cb5 | 7 | |
hmiot | 13:9b2a587ee7ac | 8 | DiscoveredCharacteristic lightCharacteristic; |
hmiot | 18:0eefac249a4b | 9 | //Gap::Handle_t connectionHandle = 0xFFFF; |
hariom | 16:5bd717644078 | 10 | //static s_serviceInfo lightChar = {NULL,NULL}; |
hmiot | 6:71e7a446ae6a | 11 | Serial pc(USBTX, USBRX); |
hmiot | 9:b967a01810e1 | 12 | void bleint(); |
hmiot | 10:5e5fa5bf77b5 | 13 | void waitBleEvent(); |
hmiot | 10:5e5fa5bf77b5 | 14 | void ble_actions(int spi_data); |
hmiot | 13:9b2a587ee7ac | 15 | uint8_t statusWR; |
hmiot | 8:285ebd0e83fb | 16 | SPISlave spiSlave(P0_9, P0_11, P0_8, P0_10); |
hariom | 16:5bd717644078 | 17 | lightCharacteristic_t ptr_temp_char; |
hariom | 16:5bd717644078 | 18 | |
hmiot | 18:0eefac249a4b | 19 | |
hmiot | 18:0eefac249a4b | 20 | void read_Characteristics(); |
hmiot | 18:0eefac249a4b | 21 | |
hariom | 16:5bd717644078 | 22 | //DiscoveredCharacteristic ptr_temp_char; |
hariom | 16:5bd717644078 | 23 | |
hariom | 16:5bd717644078 | 24 | bool connection_handle = 0; |
hariom | 16:5bd717644078 | 25 | |
hariom | 16:5bd717644078 | 26 | uint8_t Ble_address[6]; |
hmiot | 8:285ebd0e83fb | 27 | |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 28 | |
hmiot | 6:71e7a446ae6a | 29 | void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { |
hmiot | 6:71e7a446ae6a | 30 | uint8_t con_status =0; |
hariom | 16:5bd717644078 | 31 | int i; |
hmiot | 6:71e7a446ae6a | 32 | //0x51 for Magic light |
hmiot | 6:71e7a446ae6a | 33 | //0x5A |
hariom | 16:5bd717644078 | 34 | |
hmiot | 18:0eefac249a4b | 35 | /*pc.printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\ndata: %s\r\n", |
hariom | 16:5bd717644078 | 36 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], |
hmiot | 18:0eefac249a4b | 37 | params->rssi, params->isScanResponse, params->type, params->advertisingData);*/ |
hariom | 16:5bd717644078 | 38 | //if (params->peerAddr[0] == 0x5A){ // 0x2F for red bear1.5 /* !ALERT! Alter this filter to suit your device. */ |
varshaberya123 | 17:2e0273d799e2 | 39 | if (params->peerAddr[0] == 0xE5){ // 0x2F for red bear1.5 /* !ALERT! Alter this filter to suit your device. */ |
hmiot | 8:285ebd0e83fb | 40 | |
hmiot | 8:285ebd0e83fb | 41 | pc.printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 42 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 43 | params->rssi, params->isScanResponse, params->type); |
hmiot | 6:71e7a446ae6a | 44 | pc.printf("Data Length : %d\r\n",params->advertisingDataLen); |
hariom | 16:5bd717644078 | 45 | for (i = 0; i < 6; i++) |
hariom | 16:5bd717644078 | 46 | Ble_address[i] = params->peerAddr[i]; |
hariom | 16:5bd717644078 | 47 | |
hmiot | 8:285ebd0e83fb | 48 | con_status = BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_PUBLIC, NULL, NULL); |
hmiot | 8:285ebd0e83fb | 49 | pc.printf("Connection Status : %d\r\n",con_status); |
hmiot | 8:285ebd0e83fb | 50 | connect_status = 1; |
hmiot | 13:9b2a587ee7ac | 51 | BLE::Instance().gap().stopScan(); |
hariom | 16:5bd717644078 | 52 | |
hariom | 16:5bd717644078 | 53 | } |
hariom | 16:5bd717644078 | 54 | else{ |
hmiot | 11:58d8a3129877 | 55 | printf("Not Matched your device\r\n"); |
hmiot | 11:58d8a3129877 | 56 | return; |
hmiot | 11:58d8a3129877 | 57 | } |
hmiot | 8:285ebd0e83fb | 58 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 59 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 60 | void serviceDiscoveryCallback(const DiscoveredService *service) { |
hmiot | 18:0eefac249a4b | 61 | pc.printf("Service Discovery Callback\r\n"); |
hmiot | 6:71e7a446ae6a | 62 | if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) { |
hmiot | 18:0eefac249a4b | 63 | pc.printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle()); |
hmiot | 6:71e7a446ae6a | 64 | } else { |
hariom | 16:5bd717644078 | 65 | //pc.printf("S UUID-"); |
hmiot | 6:71e7a446ae6a | 66 | const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); |
hmiot | 6:71e7a446ae6a | 67 | for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { |
hmiot | 6:71e7a446ae6a | 68 | printf("%02x", longUUIDBytes[i]); |
hmiot | 6:71e7a446ae6a | 69 | } |
hmiot | 18:0eefac249a4b | 70 | pc.printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle()); |
hmiot | 6:71e7a446ae6a | 71 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 72 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 73 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 74 | void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) { |
hmiot | 6:71e7a446ae6a | 75 | pc.printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast()); |
hmiot | 13:9b2a587ee7ac | 76 | |
hmiot | 18:0eefac249a4b | 77 | if (characteristicP->getUUID().getShortUUID() == 0xEC0E) { /* !ALERT! Alter this filter to suit your device. */ |
hmiot | 8:285ebd0e83fb | 78 | lightCharacteristic = *characteristicP; |
hmiot | 6:71e7a446ae6a | 79 | pc.printf("Matched char UUID\r\n"); |
hmiot | 8:285ebd0e83fb | 80 | printf("Conn Handle = %dand %d \r\n",characteristicP->getConnectionHandle(), lightCharacteristic.getConnectionHandle()); |
hmiot | 8:285ebd0e83fb | 81 | printf("Value Handle = %d and %d\r\n",characteristicP->getValueHandle(),lightCharacteristic.getValueHandle()); |
hmiot | 10:5e5fa5bf77b5 | 82 | AddCharNodes(lightCharacteristic); |
hmiot | 9:b967a01810e1 | 83 | charDiscover = 1; |
hmiot | 18:0eefac249a4b | 84 | serviceDiscover = false; |
hmiot | 9:b967a01810e1 | 85 | }else { |
hmiot | 8:285ebd0e83fb | 86 | printf("Not Matched char UUID\r\n"); |
mbed_tw_hoehoe | 2:4b53d13d9851 | 87 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 88 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 89 | |
hmiot | 8:285ebd0e83fb | 90 | void AddCharNodes(DiscoveredCharacteristic light_Characteristic) |
hmiot | 8:285ebd0e83fb | 91 | { |
hariom | 16:5bd717644078 | 92 | //printf("Add Char Nodes\r\n"); |
hmiot | 18:0eefac249a4b | 93 | //lightCharacteristic_t ptr_temp_char; |
hariom | 16:5bd717644078 | 94 | /* |
hmiot | 8:285ebd0e83fb | 95 | ptr_temp_char = lightChar.services_Char[0]; |
hmiot | 8:285ebd0e83fb | 96 | |
hmiot | 13:9b2a587ee7ac | 97 | |
hmiot | 8:285ebd0e83fb | 98 | if(NULL == ptr_temp_char) |
hmiot | 8:285ebd0e83fb | 99 | { |
hmiot | 12:cd54180657ee | 100 | ptr_temp_char = new lightCharacteristic_t; |
hariom | 16:5bd717644078 | 101 | lightChar.services_Char[0] = ptr_temp_char; |
hmiot | 8:285ebd0e83fb | 102 | } |
hmiot | 8:285ebd0e83fb | 103 | else |
hmiot | 8:285ebd0e83fb | 104 | { |
hmiot | 8:285ebd0e83fb | 105 | while(NULL != ptr_temp_char->nextChar) |
hmiot | 8:285ebd0e83fb | 106 | { |
hmiot | 8:285ebd0e83fb | 107 | ptr_temp_char = ptr_temp_char->nextChar; |
hmiot | 8:285ebd0e83fb | 108 | } |
hmiot | 8:285ebd0e83fb | 109 | |
hmiot | 18:0eefac249a4b | 110 | // add a new node |
hariom | 16:5bd717644078 | 111 | ptr_temp_char->nextChar = new lightCharacteristic_t; |
hmiot | 8:285ebd0e83fb | 112 | ptr_temp_char = ptr_temp_char->nextChar; |
hariom | 16:5bd717644078 | 113 | }*/ |
hmiot | 8:285ebd0e83fb | 114 | |
hmiot | 8:285ebd0e83fb | 115 | /* assign data to the node */ |
hariom | 16:5bd717644078 | 116 | ptr_temp_char.u_characteristicID = 1; |
hariom | 16:5bd717644078 | 117 | pc.printf("Conn Handle = %d\r\n",light_Characteristic.getConnectionHandle()); |
hariom | 16:5bd717644078 | 118 | pc.printf("Value Handle = %d\r\n",light_Characteristic.getValueHandle()); |
hariom | 16:5bd717644078 | 119 | ptr_temp_char.connHandle = light_Characteristic.getConnectionHandle(); |
hariom | 16:5bd717644078 | 120 | ptr_temp_char.valueHandle = light_Characteristic.getValueHandle(); |
hariom | 16:5bd717644078 | 121 | //ptr_temp_char.nextChar = NULL; |
hmiot | 8:285ebd0e83fb | 122 | } |
hmiot | 8:285ebd0e83fb | 123 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 124 | void discoveryTerminationCallback(Gap::Handle_t connectionHandle) { |
mbed_tw_hoehoe | 1:2f1203d70643 | 125 | pc.printf("terminated SD for handle %u\r\n", connectionHandle); |
hmiot | 8:285ebd0e83fb | 126 | serviceDiscover = false; |
hmiot | 18:0eefac249a4b | 127 | pc.printf("Service Discover handle %d\r\n", serviceDiscover); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 128 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 129 | |
hmiot | 6:71e7a446ae6a | 130 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { |
hmiot | 18:0eefac249a4b | 131 | pc.printf("Connection Callback\n\r"); |
hmiot | 6:71e7a446ae6a | 132 | if (params->role == Gap::CENTRAL) { |
hmiot | 18:0eefac249a4b | 133 | //connectionHandle = params->handle; |
hmiot | 18:0eefac249a4b | 134 | pc.printf("Handle %u\r\n", params->handle); |
hmiot | 18:0eefac249a4b | 135 | pc.printf("Service and characterstics discovery callback\r\n"); |
hmiot | 6:71e7a446ae6a | 136 | BLE::Instance().gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); |
hmiot | 18:0eefac249a4b | 137 | error_status = BLE::Instance().gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xEC00, 0xEC0E); |
hmiot | 18:0eefac249a4b | 138 | printf("Service Discovery Error Status =%d\n\r",error_status); |
hmiot | 10:5e5fa5bf77b5 | 139 | // 0xffe5 --> Services UUID |
hmiot | 10:5e5fa5bf77b5 | 140 | // 0xffe9 --> Characteristics UUID |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 141 | } |
hmiot | 6:71e7a446ae6a | 142 | } |
hmiot | 6:71e7a446ae6a | 143 | |
hariom | 16:5bd717644078 | 144 | void writeCallback(const GattWriteCallbackParams *eventDataP) |
hariom | 16:5bd717644078 | 145 | { |
hariom | 16:5bd717644078 | 146 | pc.printf("written callback\r\n"); |
hariom | 16:5bd717644078 | 147 | } |
hariom | 16:5bd717644078 | 148 | |
hmiot | 6:71e7a446ae6a | 149 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { |
hmiot | 6:71e7a446ae6a | 150 | pc.printf("disconnected\r\n"); |
hmiot | 18:0eefac249a4b | 151 | //ble.gap().startScan(advertisementCallback); |
hmiot | 18:0eefac249a4b | 152 | connect_status = 0; |
hmiot | 18:0eefac249a4b | 153 | charDiscover = 0; |
hmiot | 18:0eefac249a4b | 154 | bleint(); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 155 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 156 | |
hmiot | 6:71e7a446ae6a | 157 | |
hariom | 16:5bd717644078 | 158 | void hvxCallback(const GattHVXCallbackParams *params) { |
hariom | 16:5bd717644078 | 159 | printf("received HVX callback for handle %u; type %s\r\n", params->handle, (params->type == BLE_HVX_NOTIFICATION) ? "notification" : "indication"); |
hariom | 16:5bd717644078 | 160 | for (unsigned index = 0; index < params->len; index++) { |
hariom | 16:5bd717644078 | 161 | printf(" %02x", params->data[index]); |
hariom | 16:5bd717644078 | 162 | } |
hariom | 16:5bd717644078 | 163 | printf("\r\n"); |
hariom | 16:5bd717644078 | 164 | } |
varshaberya123 | 17:2e0273d799e2 | 165 | void triggerRead(const GattReadCallbackParams *response) |
varshaberya123 | 17:2e0273d799e2 | 166 | { |
varshaberya123 | 17:2e0273d799e2 | 167 | printf("reading data : \r\n"); |
varshaberya123 | 17:2e0273d799e2 | 168 | // if (response->handle == GattCharacteristic.getValueHandle()) |
varshaberya123 | 17:2e0273d799e2 | 169 | // if (response->handle == characteristicP->getValueHandle()) |
varshaberya123 | 17:2e0273d799e2 | 170 | { |
varshaberya123 | 17:2e0273d799e2 | 171 | printf("triggerToggledWrite: handle %u, offset %u, len %u\r\n", response->handle, response->offset, response->len); |
varshaberya123 | 17:2e0273d799e2 | 172 | for (unsigned index = 0; index < response->len; index++) |
varshaberya123 | 17:2e0273d799e2 | 173 | { |
varshaberya123 | 17:2e0273d799e2 | 174 | printf("%c[%02x]", response->data[index], response->data[index]); |
varshaberya123 | 17:2e0273d799e2 | 175 | } |
varshaberya123 | 17:2e0273d799e2 | 176 | printf("\r\n"); |
varshaberya123 | 17:2e0273d799e2 | 177 | } |
varshaberya123 | 17:2e0273d799e2 | 178 | } |
hariom | 16:5bd717644078 | 179 | |
hmiot | 6:71e7a446ae6a | 180 | /** |
hmiot | 6:71e7a446ae6a | 181 | * Callback triggered when the ble initialization process has finished |
hmiot | 6:71e7a446ae6a | 182 | */ |
hmiot | 6:71e7a446ae6a | 183 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
hmiot | 6:71e7a446ae6a | 184 | { |
hmiot | 18:0eefac249a4b | 185 | BLE& ble = params->ble; |
hmiot | 8:285ebd0e83fb | 186 | |
hmiot | 6:71e7a446ae6a | 187 | pc.printf("Ble Init\n\r"); |
hmiot | 9:b967a01810e1 | 188 | /* Ensure that it is the default instance of BLE */ |
hmiot | 6:71e7a446ae6a | 189 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
hmiot | 6:71e7a446ae6a | 190 | return; |
hmiot | 9:b967a01810e1 | 191 | } |
hmiot | 6:71e7a446ae6a | 192 | |
hmiot | 9:b967a01810e1 | 193 | ble.gap().onConnection(connectionCallback); |
hmiot | 9:b967a01810e1 | 194 | ble.gap().onDisconnection(disconnectionCallback); |
hmiot | 9:b967a01810e1 | 195 | ble.gap().setScanParams(1000, 800); |
hmiot | 9:b967a01810e1 | 196 | ble.gap().startScan(advertisementCallback); |
hariom | 16:5bd717644078 | 197 | //ble.gattClient().onHVX(hvxCallback); |
hmiot | 18:0eefac249a4b | 198 | //ble.gattClient().onDataRead(triggerRead); |
hmiot | 10:5e5fa5bf77b5 | 199 | |
hmiot | 8:285ebd0e83fb | 200 | } |
hmiot | 8:285ebd0e83fb | 201 | |
hmiot | 18:0eefac249a4b | 202 | void write_Characteristics() |
hmiot | 18:0eefac249a4b | 203 | { uint8_t write_data = 0x45; |
hmiot | 18:0eefac249a4b | 204 | |
hmiot | 18:0eefac249a4b | 205 | error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_REQ, ptr_temp_char.connHandle,ptr_temp_char.valueHandle, sizeof(write_data), &write_data); |
hmiot | 18:0eefac249a4b | 206 | |
hmiot | 18:0eefac249a4b | 207 | printf("Error Status =%d\n\r",error_status); |
hmiot | 18:0eefac249a4b | 208 | //int i; |
hmiot | 18:0eefac249a4b | 209 | printf("write VALUE : "); |
hmiot | 18:0eefac249a4b | 210 | //for(i=0; i<16;i++) |
hmiot | 18:0eefac249a4b | 211 | //{ |
hmiot | 18:0eefac249a4b | 212 | printf("%d \r\n", write_data); |
hmiot | 18:0eefac249a4b | 213 | //} |
hmiot | 18:0eefac249a4b | 214 | BLE::Instance().processEvents(); |
hmiot | 18:0eefac249a4b | 215 | } |
hmiot | 18:0eefac249a4b | 216 | |
hmiot | 18:0eefac249a4b | 217 | void read_Characteristics() |
hmiot | 18:0eefac249a4b | 218 | { uint16_t write_data; |
hmiot | 18:0eefac249a4b | 219 | |
hmiot | 18:0eefac249a4b | 220 | error_status = BLE::Instance().gattClient().read(ptr_temp_char.connHandle,ptr_temp_char.valueHandle, write_data); |
hmiot | 18:0eefac249a4b | 221 | |
hmiot | 18:0eefac249a4b | 222 | printf("Error Status =%d\n\r",error_status); |
hmiot | 18:0eefac249a4b | 223 | //int i; |
hmiot | 18:0eefac249a4b | 224 | printf("READ VALUE : "); |
hmiot | 18:0eefac249a4b | 225 | //for(i=0; i<16;i++) |
hmiot | 18:0eefac249a4b | 226 | //{ |
hmiot | 18:0eefac249a4b | 227 | printf("%x \r\n", write_data); |
hmiot | 18:0eefac249a4b | 228 | //} |
hmiot | 18:0eefac249a4b | 229 | BLE::Instance().processEvents(); |
hmiot | 18:0eefac249a4b | 230 | } |
hariom | 16:5bd717644078 | 231 | void light_actions(int * service_char) |
hariom | 16:5bd717644078 | 232 | { |
hmiot | 18:0eefac249a4b | 233 | |
hmiot | 18:0eefac249a4b | 234 | /* |
hariom | 16:5bd717644078 | 235 | uint8_t lightColor[MAX_COLOR][3] = {{0xFF,0x00,0x00},{0x00,0xFF,0x00}, |
hariom | 16:5bd717644078 | 236 | {0x00,0x00,0xFF},{0xFF,0xFF,0x00}, |
hariom | 16:5bd717644078 | 237 | {0x00,0xFF,0xFF},{0xFF,0x00,0xFF} |
hariom | 16:5bd717644078 | 238 | }; |
hariom | 16:5bd717644078 | 239 | uint8_t pre_write_data[16]; |
hariom | 16:5bd717644078 | 240 | uint8_t write_data[16]; |
hariom | 16:5bd717644078 | 241 | |
hariom | 16:5bd717644078 | 242 | //lightCharacteristic_t *ptr_tmp = NULL; |
hariom | 16:5bd717644078 | 243 | //ptr_tmp = lightChar.services_Char[0]; |
hmiot | 13:9b2a587ee7ac | 244 | uint8_t numCount; |
hmiot | 10:5e5fa5bf77b5 | 245 | if(connect_status) |
hmiot | 10:5e5fa5bf77b5 | 246 | { |
hmiot | 9:b967a01810e1 | 247 | |
hmiot | 10:5e5fa5bf77b5 | 248 | switch(service_char[2]) |
hmiot | 10:5e5fa5bf77b5 | 249 | { |
hmiot | 10:5e5fa5bf77b5 | 250 | case TURN_OFF: // off |
hmiot | 10:5e5fa5bf77b5 | 251 | write_data[0]=0x56; |
hmiot | 10:5e5fa5bf77b5 | 252 | write_data[1]=0x00; |
hmiot | 10:5e5fa5bf77b5 | 253 | write_data[2]=0x00; |
hmiot | 10:5e5fa5bf77b5 | 254 | write_data[3]=0x00; |
hmiot | 10:5e5fa5bf77b5 | 255 | write_data[4]=0x00; |
hmiot | 10:5e5fa5bf77b5 | 256 | write_data[5]=0xf0; |
hmiot | 10:5e5fa5bf77b5 | 257 | write_data[6]=0xaa; |
hariom | 16:5bd717644078 | 258 | //error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,8,write_data); |
hariom | 16:5bd717644078 | 259 | error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,connectionHandle,ptr_temp_char.valueHandle,7,write_data); |
hariom | 16:5bd717644078 | 260 | BLE::Instance().processEvents(); |
hariom | 16:5bd717644078 | 261 | //error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_temp_char.connHandle,ptr_temp_char.valueHandle,7,write_data); |
hariom | 16:5bd717644078 | 262 | //BLE::Instance().updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), write_data, 7); |
hmiot | 10:5e5fa5bf77b5 | 263 | break; |
hmiot | 8:285ebd0e83fb | 264 | |
hmiot | 10:5e5fa5bf77b5 | 265 | case TURN_ON: //on |
hmiot | 10:5e5fa5bf77b5 | 266 | pre_write_data[0]=0x56; |
hmiot | 10:5e5fa5bf77b5 | 267 | pre_write_data[1]=0xDE; |
hmiot | 10:5e5fa5bf77b5 | 268 | pre_write_data[2]=0x8E; |
hmiot | 10:5e5fa5bf77b5 | 269 | pre_write_data[3]=0xEE; |
hmiot | 10:5e5fa5bf77b5 | 270 | pre_write_data[4]=0x00; |
hmiot | 10:5e5fa5bf77b5 | 271 | pre_write_data[5]=0xf0; |
hmiot | 10:5e5fa5bf77b5 | 272 | pre_write_data[6]=0xaa; |
hmiot | 15:ef3cf37827d2 | 273 | |
hariom | 16:5bd717644078 | 274 | //error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,8,pre_write_data); |
hariom | 16:5bd717644078 | 275 | error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,connectionHandle,ptr_temp_char.valueHandle,7,pre_write_data); |
hariom | 16:5bd717644078 | 276 | BLE::Instance().processEvents(); |
hariom | 16:5bd717644078 | 277 | //error_status = BLE::Instance().DiscoveredCharacteristic().write(7,pre_write_data); |
hmiot | 10:5e5fa5bf77b5 | 278 | break; |
hmiot | 8:285ebd0e83fb | 279 | |
hmiot | 10:5e5fa5bf77b5 | 280 | case COLOR: // Color |
hmiot | 13:9b2a587ee7ac | 281 | write_data[0]=0x56; |
hmiot | 13:9b2a587ee7ac | 282 | write_data[1]=lightColor[service_char[3]][0];//0xff |
hmiot | 13:9b2a587ee7ac | 283 | write_data[2]=lightColor[service_char[3]][1];//0x00; |
hmiot | 13:9b2a587ee7ac | 284 | write_data[3]=lightColor[service_char[3]][2];//0x00; |
hmiot | 13:9b2a587ee7ac | 285 | write_data[4]=0x00; |
hmiot | 13:9b2a587ee7ac | 286 | write_data[5]=0xf0; |
hmiot | 13:9b2a587ee7ac | 287 | write_data[6]=0xaa; |
hmiot | 13:9b2a587ee7ac | 288 | for(numCount=0; numCount < 7; numCount++){ |
hmiot | 13:9b2a587ee7ac | 289 | pre_write_data[numCount] = write_data[numCount]; |
hmiot | 12:cd54180657ee | 290 | } |
hmiot | 13:9b2a587ee7ac | 291 | first_on=false; |
hariom | 16:5bd717644078 | 292 | //error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,8,write_data); |
hariom | 16:5bd717644078 | 293 | error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,connectionHandle,ptr_temp_char.valueHandle,7,write_data); |
hariom | 16:5bd717644078 | 294 | BLE::Instance().processEvents(); |
hariom | 16:5bd717644078 | 295 | //error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_temp_char.connHandle,ptr_temp_char.valueHandle,7,write_data); |
hariom | 16:5bd717644078 | 296 | |
hmiot | 10:5e5fa5bf77b5 | 297 | break; |
hmiot | 8:285ebd0e83fb | 298 | |
hmiot | 10:5e5fa5bf77b5 | 299 | default: |
hmiot | 10:5e5fa5bf77b5 | 300 | printf("Invalid Options\r\n"); |
hmiot | 10:5e5fa5bf77b5 | 301 | } |
hmiot | 10:5e5fa5bf77b5 | 302 | printf("Error Status =%d\n\r",error_status); |
hmiot | 13:9b2a587ee7ac | 303 | } |
hmiot | 18:0eefac249a4b | 304 | */ |
hmiot | 8:285ebd0e83fb | 305 | } |
hmiot | 8:285ebd0e83fb | 306 | |
hmiot | 12:cd54180657ee | 307 | void datancpy(char pre_write[], char writeData[], int data_size){ |
hmiot | 8:285ebd0e83fb | 308 | int numCount; |
hmiot | 8:285ebd0e83fb | 309 | for(numCount=0; numCount <= data_size; numCount++){ |
hmiot | 8:285ebd0e83fb | 310 | pre_write[numCount] = writeData[numCount]; |
hmiot | 8:285ebd0e83fb | 311 | } |
hmiot | 8:285ebd0e83fb | 312 | |
mbed_tw_hoehoe | 2:4b53d13d9851 | 313 | } |
mbed_tw_hoehoe | 2:4b53d13d9851 | 314 | |
hmiot | 8:285ebd0e83fb | 315 | void bleint() |
hmiot | 8:285ebd0e83fb | 316 | { |
hmiot | 8:285ebd0e83fb | 317 | printf("Init\r\n"); |
hmiot | 9:b967a01810e1 | 318 | BLE &ble = BLE::Instance(); |
hmiot | 8:285ebd0e83fb | 319 | BLE::Instance().init(bleInitComplete); |
hmiot | 8:285ebd0e83fb | 320 | } |
hmiot | 8:285ebd0e83fb | 321 | |
hmiot | 8:285ebd0e83fb | 322 | int main() |
hmiot | 8:285ebd0e83fb | 323 | { |
hmiot | 8:285ebd0e83fb | 324 | // Serial port configuration |
mbed_tw_hoehoe | 1:2f1203d70643 | 325 | pc.baud(9600); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 326 | wait(8.0); |
hmiot | 9:b967a01810e1 | 327 | pc.printf("Start\n\r"); |
hmiot | 13:9b2a587ee7ac | 328 | |
hmiot | 18:0eefac249a4b | 329 | //spiSlave.reply(191); |
hmiot | 13:9b2a587ee7ac | 330 | statusWR=1; |
hmiot | 9:b967a01810e1 | 331 | bleint(); |
hmiot | 10:5e5fa5bf77b5 | 332 | bufferSize = 0; |
hmiot | 18:0eefac249a4b | 333 | /*int spiRX[5]; |
hariom | 16:5bd717644078 | 334 | |
hariom | 16:5bd717644078 | 335 | int temp = 0x00; |
hariom | 16:5bd717644078 | 336 | int color = 0x00; |
hmiot | 18:0eefac249a4b | 337 | int con_status,i;*/ |
hmiot | 10:5e5fa5bf77b5 | 338 | while(1){ |
hariom | 16:5bd717644078 | 339 | pc.printf("loop\r\n"); |
hmiot | 18:0eefac249a4b | 340 | //wait(1.0); |
hmiot | 18:0eefac249a4b | 341 | printf( "Connect Status %d and Char Discover %d \r\n", connect_status, charDiscover); |
hmiot | 14:f84a9745c10a | 342 | if(!(connect_status && charDiscover)) |
hmiot | 18:0eefac249a4b | 343 | { |
hmiot | 18:0eefac249a4b | 344 | printf( " Wait Event Connect Status %d and Char Discover %d \r\n", connect_status, charDiscover); |
hmiot | 18:0eefac249a4b | 345 | waitBleEvent(); |
hmiot | 18:0eefac249a4b | 346 | } |
hmiot | 18:0eefac249a4b | 347 | printf("Readchar\r\n"); |
hmiot | 18:0eefac249a4b | 348 | if(connect_status && charDiscover) |
hmiot | 18:0eefac249a4b | 349 | { |
hmiot | 18:0eefac249a4b | 350 | pc.printf("Read\r\n"); |
hmiot | 10:5e5fa5bf77b5 | 351 | |
hmiot | 18:0eefac249a4b | 352 | write_Characteristics(); |
hmiot | 18:0eefac249a4b | 353 | wait(2.0); |
hmiot | 18:0eefac249a4b | 354 | read_Characteristics(); |
hmiot | 18:0eefac249a4b | 355 | } |
hmiot | 18:0eefac249a4b | 356 | |
hmiot | 18:0eefac249a4b | 357 | /* |
hariom | 16:5bd717644078 | 358 | if (temp == 2 && color == 3) { |
hariom | 16:5bd717644078 | 359 | temp = 0x00; |
hariom | 16:5bd717644078 | 360 | color = 0x00; |
hariom | 16:5bd717644078 | 361 | } |
hariom | 16:5bd717644078 | 362 | else if (temp == 2 && color < 3) { |
hariom | 16:5bd717644078 | 363 | color++; |
hariom | 16:5bd717644078 | 364 | } |
hariom | 16:5bd717644078 | 365 | else if (temp < 2) |
hariom | 16:5bd717644078 | 366 | temp++; |
hariom | 16:5bd717644078 | 367 | |
hariom | 16:5bd717644078 | 368 | spiRX[0] = 0xB3; |
hariom | 16:5bd717644078 | 369 | spiRX[1] = 1; |
hariom | 16:5bd717644078 | 370 | spiRX[2] = temp; |
hariom | 16:5bd717644078 | 371 | spiRX[3] = color; |
hariom | 16:5bd717644078 | 372 | pc.printf("spix buffer is: "); |
hariom | 16:5bd717644078 | 373 | for (i = 0; i < 4; i++) { |
hariom | 16:5bd717644078 | 374 | pc.printf("%d ", spiRX[i]); |
hariom | 16:5bd717644078 | 375 | } |
hariom | 16:5bd717644078 | 376 | pc.printf("\n"); |
hariom | 16:5bd717644078 | 377 | |
hariom | 16:5bd717644078 | 378 | //if(spiSlave.receive()){ |
hariom | 16:5bd717644078 | 379 | //printf("Slave Read loop1\r\n"); |
hariom | 16:5bd717644078 | 380 | //spiRX[bufferSize] = spiSlave.read(); |
hariom | 16:5bd717644078 | 381 | //spiSlave.reply(spiRX[bufferSize]); |
hariom | 16:5bd717644078 | 382 | //bufferSize++; |
hariom | 16:5bd717644078 | 383 | bufferSize = 5; |
hmiot | 10:5e5fa5bf77b5 | 384 | |
hmiot | 10:5e5fa5bf77b5 | 385 | if(bufferSize >= 5){ |
hmiot | 10:5e5fa5bf77b5 | 386 | bufferSize=0; |
hmiot | 10:5e5fa5bf77b5 | 387 | light_actions(spiRX); |
hariom | 16:5bd717644078 | 388 | //BLE::Instance().gattClient().reset(); |
hmiot | 15:ef3cf37827d2 | 389 | spiSlave.reply(spiRX[2]); //(0xAA); |
hariom | 16:5bd717644078 | 390 | |
hmiot | 13:9b2a587ee7ac | 391 | |
hariom | 16:5bd717644078 | 392 | if(error_status > 7) |
hariom | 16:5bd717644078 | 393 | { |
hariom | 16:5bd717644078 | 394 | error_status = BLE_ERROR_NONE; |
hariom | 16:5bd717644078 | 395 | //wait_ms(500); |
hmiot | 13:9b2a587ee7ac | 396 | BLE::Instance().shutdown(); |
hmiot | 13:9b2a587ee7ac | 397 | statusWR =1; |
hmiot | 13:9b2a587ee7ac | 398 | connect_status =0; |
hmiot | 13:9b2a587ee7ac | 399 | charDiscover =0; |
hmiot | 13:9b2a587ee7ac | 400 | serviceDiscover = true; |
hmiot | 13:9b2a587ee7ac | 401 | bleint(); |
hmiot | 13:9b2a587ee7ac | 402 | } |
hariom | 16:5bd717644078 | 403 | //statusWR++; |
hariom | 16:5bd717644078 | 404 | wait_ms(3000); |
hmiot | 10:5e5fa5bf77b5 | 405 | } |
hmiot | 10:5e5fa5bf77b5 | 406 | |
hmiot | 18:0eefac249a4b | 407 | //} */ |
hmiot | 13:9b2a587ee7ac | 408 | |
hmiot | 9:b967a01810e1 | 409 | } |
hmiot | 9:b967a01810e1 | 410 | |
hmiot | 9:b967a01810e1 | 411 | } |
hmiot | 9:b967a01810e1 | 412 | |
hmiot | 10:5e5fa5bf77b5 | 413 | void waitBleEvent() |
hmiot | 10:5e5fa5bf77b5 | 414 | { |
hmiot | 18:0eefac249a4b | 415 | //|| BLE::Instance().gattClient().isServiceDiscoveryActive() |
hmiot | 18:0eefac249a4b | 416 | while(serviceDiscover ){ |
hmiot | 18:0eefac249a4b | 417 | printf("Service Discover =%d\r\n",serviceDiscover); |
hmiot | 18:0eefac249a4b | 418 | printf(" Service Status = %d \r\n",BLE::Instance().gattClient().isServiceDiscoveryActive()); |
hmiot | 10:5e5fa5bf77b5 | 419 | BLE::Instance().waitForEvent(); |
hmiot | 18:0eefac249a4b | 420 | printf( "waitBleEvent loop\r\n"); |
hmiot | 18:0eefac249a4b | 421 | } |
hmiot | 18:0eefac249a4b | 422 | printf( "waitBleEvent End\r\n"); |
hmiot | 9:b967a01810e1 | 423 | } |
hmiot | 10:5e5fa5bf77b5 | 424 |