Fixed ble issue
Dependencies: BLE_API mbed nRF51822
Fork of BLE_Central_Light_Demo_23 by
main.cpp@18:ef42e2579463, 2017-11-08 (annotated)
- Committer:
- hmiot
- Date:
- Wed Nov 08 12:07:38 2017 +0000
- Revision:
- 18:ef42e2579463
- Parent:
- 17:0b0a7dccf151
Updated for Zigo Demo
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:ef42e2579463 | 9 | DiscoveredCharacteristic notifyCharacteristic; |
hmiot | 12:cd54180657ee | 10 | static s_serviceInfo lightChar = {NULL,NULL,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); |
hmiot | 8:285ebd0e83fb | 17 | |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 18 | |
hmiot | 6:71e7a446ae6a | 19 | void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { |
hmiot | 6:71e7a446ae6a | 20 | uint8_t con_status =0; |
hmiot | 6:71e7a446ae6a | 21 | //0x51 for Magic light |
hmiot | 6:71e7a446ae6a | 22 | //0x5A |
hariom | 17:0b0a7dccf151 | 23 | |
hmiot | 15:ef3cf37827d2 | 24 | if (params->peerAddr[0] == 0x5A){ // 0x2F for red bear1.5 /* !ALERT! Alter this filter to suit your device. */ |
hmiot | 8:285ebd0e83fb | 25 | |
hmiot | 8:285ebd0e83fb | 26 | pc.printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 27 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 28 | params->rssi, params->isScanResponse, params->type); |
hmiot | 6:71e7a446ae6a | 29 | pc.printf("Data Length : %d\r\n",params->advertisingDataLen); |
hariom | 17:0b0a7dccf151 | 30 | con_status = BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_PUBLIC, NULL, NULL); |
hmiot | 8:285ebd0e83fb | 31 | pc.printf("Connection Status : %d\r\n",con_status); |
hmiot | 8:285ebd0e83fb | 32 | connect_status = 1; |
hmiot | 13:9b2a587ee7ac | 33 | BLE::Instance().gap().stopScan(); |
hmiot | 9:b967a01810e1 | 34 | |
hariom | 16:0cbb6fa27253 | 35 | }else{ |
hmiot | 11:58d8a3129877 | 36 | printf("Not Matched your device\r\n"); |
hmiot | 11:58d8a3129877 | 37 | return; |
hmiot | 11:58d8a3129877 | 38 | } |
hmiot | 8:285ebd0e83fb | 39 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 40 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 41 | void serviceDiscoveryCallback(const DiscoveredService *service) { |
hmiot | 6:71e7a446ae6a | 42 | pc.printf("Service Discovery Callback\r\n"); |
hmiot | 6:71e7a446ae6a | 43 | if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) { |
hmiot | 6:71e7a446ae6a | 44 | pc.printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle()); |
hmiot | 6:71e7a446ae6a | 45 | } else { |
hmiot | 6:71e7a446ae6a | 46 | pc.printf("S UUID-"); |
hmiot | 6:71e7a446ae6a | 47 | const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); |
hmiot | 6:71e7a446ae6a | 48 | for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { |
hmiot | 6:71e7a446ae6a | 49 | printf("%02x", longUUIDBytes[i]); |
hmiot | 6:71e7a446ae6a | 50 | } |
hmiot | 6:71e7a446ae6a | 51 | pc.printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle()); |
hmiot | 6:71e7a446ae6a | 52 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 53 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 54 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 55 | void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) { |
hmiot | 6:71e7a446ae6a | 56 | pc.printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast()); |
hmiot | 13:9b2a587ee7ac | 57 | |
hmiot | 9:b967a01810e1 | 58 | if (characteristicP->getUUID().getShortUUID() == 0xffe9) { /* !ALERT! Alter this filter to suit your device. */ |
hmiot | 8:285ebd0e83fb | 59 | lightCharacteristic = *characteristicP; |
hmiot | 6:71e7a446ae6a | 60 | pc.printf("Matched char UUID\r\n"); |
hmiot | 8:285ebd0e83fb | 61 | printf("Conn Handle = %dand %d \r\n",characteristicP->getConnectionHandle(), lightCharacteristic.getConnectionHandle()); |
hmiot | 8:285ebd0e83fb | 62 | printf("Value Handle = %d and %d\r\n",characteristicP->getValueHandle(),lightCharacteristic.getValueHandle()); |
hmiot | 10:5e5fa5bf77b5 | 63 | AddCharNodes(lightCharacteristic); |
hmiot | 18:ef42e2579463 | 64 | write_charDiscover = true; |
hmiot | 18:ef42e2579463 | 65 | |
hmiot | 18:ef42e2579463 | 66 | } |
hmiot | 18:ef42e2579463 | 67 | else if(characteristicP->getUUID().getShortUUID() == 0xffe4) |
hmiot | 18:ef42e2579463 | 68 | { |
hmiot | 18:ef42e2579463 | 69 | notifyCharacteristic = *characteristicP; |
hmiot | 18:ef42e2579463 | 70 | notify_charDiscover = true; |
hmiot | 18:ef42e2579463 | 71 | pc.printf("Matched char UUID\r\n"); |
hmiot | 18:ef42e2579463 | 72 | printf("Conn Handle = %dand %d \r\n",characteristicP->getConnectionHandle(), notifyCharacteristic.getConnectionHandle()); |
hmiot | 18:ef42e2579463 | 73 | printf("Value Handle = %d and %d\r\n",characteristicP->getValueHandle(),notifyCharacteristic.getValueHandle()); |
hmiot | 18:ef42e2579463 | 74 | |
hmiot | 18:ef42e2579463 | 75 | } |
hmiot | 18:ef42e2579463 | 76 | else { |
hmiot | 8:285ebd0e83fb | 77 | printf("Not Matched char UUID\r\n"); |
hmiot | 18:ef42e2579463 | 78 | } |
hmiot | 18:ef42e2579463 | 79 | |
hmiot | 18:ef42e2579463 | 80 | if(notify_charDiscover && write_charDiscover) |
hmiot | 18:ef42e2579463 | 81 | { |
hmiot | 18:ef42e2579463 | 82 | charDiscover = 1; |
hmiot | 18:ef42e2579463 | 83 | serviceDiscover = true; |
hmiot | 18:ef42e2579463 | 84 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 85 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 86 | |
hmiot | 8:285ebd0e83fb | 87 | void AddCharNodes(DiscoveredCharacteristic light_Characteristic) |
hmiot | 8:285ebd0e83fb | 88 | { |
hmiot | 8:285ebd0e83fb | 89 | printf("Add Char Nodes\r\n"); |
hmiot | 13:9b2a587ee7ac | 90 | lightCharacteristic_t *ptr_temp_char; |
hmiot | 8:285ebd0e83fb | 91 | ptr_temp_char = lightChar.services_Char[0]; |
hmiot | 8:285ebd0e83fb | 92 | |
hmiot | 13:9b2a587ee7ac | 93 | |
hmiot | 8:285ebd0e83fb | 94 | if(NULL == ptr_temp_char) |
hmiot | 8:285ebd0e83fb | 95 | { |
hmiot | 12:cd54180657ee | 96 | ptr_temp_char = new lightCharacteristic_t; |
hmiot | 8:285ebd0e83fb | 97 | lightChar.services_Char[0] = ptr_temp_char; |
hmiot | 8:285ebd0e83fb | 98 | } |
hmiot | 8:285ebd0e83fb | 99 | else |
hmiot | 8:285ebd0e83fb | 100 | { |
hmiot | 8:285ebd0e83fb | 101 | while(NULL != ptr_temp_char->nextChar) |
hmiot | 8:285ebd0e83fb | 102 | { |
hmiot | 8:285ebd0e83fb | 103 | ptr_temp_char = ptr_temp_char->nextChar; |
hmiot | 8:285ebd0e83fb | 104 | } |
hmiot | 8:285ebd0e83fb | 105 | |
hmiot | 8:285ebd0e83fb | 106 | /* add a new node */ |
hmiot | 12:cd54180657ee | 107 | ptr_temp_char->nextChar = new lightCharacteristic_t; |
hmiot | 8:285ebd0e83fb | 108 | ptr_temp_char = ptr_temp_char->nextChar; |
hmiot | 8:285ebd0e83fb | 109 | } |
hmiot | 8:285ebd0e83fb | 110 | |
hmiot | 8:285ebd0e83fb | 111 | /* assign data to the node */ |
hmiot | 8:285ebd0e83fb | 112 | ptr_temp_char->u_characteristicID = 1; |
hmiot | 8:285ebd0e83fb | 113 | printf("Conn Handle = %d\r\n",light_Characteristic.getConnectionHandle()); |
hmiot | 8:285ebd0e83fb | 114 | printf("Value Handle = %d\r\n",light_Characteristic.getValueHandle()); |
hmiot | 8:285ebd0e83fb | 115 | ptr_temp_char->connHandle = light_Characteristic.getConnectionHandle(); |
hmiot | 8:285ebd0e83fb | 116 | ptr_temp_char->valueHandle = light_Characteristic.getValueHandle(); |
hmiot | 8:285ebd0e83fb | 117 | ptr_temp_char->nextChar = NULL; |
hmiot | 8:285ebd0e83fb | 118 | } |
hmiot | 8:285ebd0e83fb | 119 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 120 | void discoveryTerminationCallback(Gap::Handle_t connectionHandle) { |
mbed_tw_hoehoe | 1:2f1203d70643 | 121 | pc.printf("terminated SD for handle %u\r\n", connectionHandle); |
hmiot | 8:285ebd0e83fb | 122 | serviceDiscover = false; |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 123 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 124 | |
hmiot | 6:71e7a446ae6a | 125 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { |
hmiot | 6:71e7a446ae6a | 126 | pc.printf("Connection Callback\n\r"); |
hmiot | 6:71e7a446ae6a | 127 | if (params->role == Gap::CENTRAL) { |
hmiot | 6:71e7a446ae6a | 128 | pc.printf("Service and characterstics discovery callback\r\n"); |
hmiot | 6:71e7a446ae6a | 129 | BLE::Instance().gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); |
hmiot | 18:ef42e2579463 | 130 | BLE::Instance().gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback/*, 0xffe5, 0xffe9*/); |
hmiot | 10:5e5fa5bf77b5 | 131 | // 0xffe5 --> Services UUID |
hmiot | 10:5e5fa5bf77b5 | 132 | // 0xffe9 --> Characteristics UUID |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 133 | } |
hmiot | 6:71e7a446ae6a | 134 | } |
hmiot | 6:71e7a446ae6a | 135 | |
hmiot | 6:71e7a446ae6a | 136 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { |
hmiot | 6:71e7a446ae6a | 137 | pc.printf("disconnected\r\n"); |
hmiot | 8:285ebd0e83fb | 138 | BLE::Instance().gap().startScan(advertisementCallback); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 139 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 140 | |
hmiot | 18:ef42e2579463 | 141 | void hvxCallback(const GattHVXCallbackParams *params) { |
hmiot | 18:ef42e2579463 | 142 | printf("received HVX callback for handle %u; type %s\r\n", params->handle, (params->type == BLE_HVX_NOTIFICATION) ? "notification" : "indication"); |
hmiot | 18:ef42e2579463 | 143 | for (unsigned index = 0; index < params->len; index++) { |
hmiot | 18:ef42e2579463 | 144 | printf(" %02x", params->data[index]); |
hmiot | 18:ef42e2579463 | 145 | } |
hmiot | 18:ef42e2579463 | 146 | strncpy((char *)response_data, (char *)params->data, params->len); |
hmiot | 18:ef42e2579463 | 147 | resp_BuffSize = params->len; |
hmiot | 18:ef42e2579463 | 148 | |
hmiot | 18:ef42e2579463 | 149 | printf("\r\n"); |
hmiot | 18:ef42e2579463 | 150 | } |
hmiot | 6:71e7a446ae6a | 151 | |
hmiot | 6:71e7a446ae6a | 152 | /** |
hmiot | 6:71e7a446ae6a | 153 | * Callback triggered when the ble initialization process has finished |
hmiot | 6:71e7a446ae6a | 154 | */ |
hmiot | 6:71e7a446ae6a | 155 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
hmiot | 6:71e7a446ae6a | 156 | { |
hmiot | 6:71e7a446ae6a | 157 | BLE& ble = params->ble; |
hmiot | 8:285ebd0e83fb | 158 | |
hmiot | 6:71e7a446ae6a | 159 | pc.printf("Ble Init\n\r"); |
hmiot | 9:b967a01810e1 | 160 | /* Ensure that it is the default instance of BLE */ |
hmiot | 6:71e7a446ae6a | 161 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
hmiot | 6:71e7a446ae6a | 162 | return; |
hmiot | 9:b967a01810e1 | 163 | } |
hmiot | 6:71e7a446ae6a | 164 | |
hmiot | 9:b967a01810e1 | 165 | ble.gap().onConnection(connectionCallback); |
hmiot | 9:b967a01810e1 | 166 | ble.gap().onDisconnection(disconnectionCallback); |
hmiot | 9:b967a01810e1 | 167 | ble.gap().setScanParams(1000, 800); |
hmiot | 9:b967a01810e1 | 168 | ble.gap().startScan(advertisementCallback); |
hmiot | 18:ef42e2579463 | 169 | |
hmiot | 18:ef42e2579463 | 170 | ble.gattClient().onHVX(hvxCallback); |
hmiot | 8:285ebd0e83fb | 171 | } |
hmiot | 8:285ebd0e83fb | 172 | |
hmiot | 18:ef42e2579463 | 173 | void light_actions(int service_char[]){ |
hmiot | 8:285ebd0e83fb | 174 | lightCharacteristic_t *ptr_tmp = NULL; |
hmiot | 13:9b2a587ee7ac | 175 | ptr_tmp = lightChar.services_Char[0]; |
hmiot | 13:9b2a587ee7ac | 176 | uint8_t numCount; |
hmiot | 10:5e5fa5bf77b5 | 177 | if(connect_status) |
hmiot | 10:5e5fa5bf77b5 | 178 | { |
hmiot | 9:b967a01810e1 | 179 | |
hmiot | 18:ef42e2579463 | 180 | switch(service_char[1]) |
hmiot | 10:5e5fa5bf77b5 | 181 | { |
hmiot | 10:5e5fa5bf77b5 | 182 | case TURN_OFF: // off |
hmiot | 10:5e5fa5bf77b5 | 183 | write_data[0]=0x56; |
hmiot | 10:5e5fa5bf77b5 | 184 | write_data[1]=0x00; |
hmiot | 10:5e5fa5bf77b5 | 185 | write_data[2]=0x00; |
hmiot | 10:5e5fa5bf77b5 | 186 | write_data[3]=0x00; |
hmiot | 10:5e5fa5bf77b5 | 187 | write_data[4]=0x00; |
hmiot | 10:5e5fa5bf77b5 | 188 | write_data[5]=0xf0; |
hmiot | 10:5e5fa5bf77b5 | 189 | write_data[6]=0xaa; |
hmiot | 10:5e5fa5bf77b5 | 190 | error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,8,write_data); |
hariom | 17:0b0a7dccf151 | 191 | BLE::Instance().processEvents(); |
hmiot | 10:5e5fa5bf77b5 | 192 | break; |
hmiot | 8:285ebd0e83fb | 193 | |
hmiot | 10:5e5fa5bf77b5 | 194 | case TURN_ON: //on |
hmiot | 10:5e5fa5bf77b5 | 195 | pre_write_data[0]=0x56; |
hmiot | 10:5e5fa5bf77b5 | 196 | pre_write_data[1]=0xDE; |
hmiot | 10:5e5fa5bf77b5 | 197 | pre_write_data[2]=0x8E; |
hmiot | 10:5e5fa5bf77b5 | 198 | pre_write_data[3]=0xEE; |
hmiot | 10:5e5fa5bf77b5 | 199 | pre_write_data[4]=0x00; |
hmiot | 10:5e5fa5bf77b5 | 200 | pre_write_data[5]=0xf0; |
hmiot | 10:5e5fa5bf77b5 | 201 | pre_write_data[6]=0xaa; |
hmiot | 15:ef3cf37827d2 | 202 | |
hmiot | 10:5e5fa5bf77b5 | 203 | error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,8,pre_write_data); |
hariom | 17:0b0a7dccf151 | 204 | BLE::Instance().processEvents(); |
hmiot | 10:5e5fa5bf77b5 | 205 | break; |
hmiot | 8:285ebd0e83fb | 206 | |
hmiot | 10:5e5fa5bf77b5 | 207 | case COLOR: // Color |
hmiot | 13:9b2a587ee7ac | 208 | write_data[0]=0x56; |
hmiot | 18:ef42e2579463 | 209 | write_data[1]=lightColor[service_char[2]][0];//0xff |
hmiot | 18:ef42e2579463 | 210 | write_data[2]=lightColor[service_char[2]][1];//0x00; |
hmiot | 18:ef42e2579463 | 211 | write_data[3]=lightColor[service_char[2]][2];//0x00; |
hmiot | 13:9b2a587ee7ac | 212 | write_data[4]=0x00; |
hmiot | 13:9b2a587ee7ac | 213 | write_data[5]=0xf0; |
hmiot | 13:9b2a587ee7ac | 214 | write_data[6]=0xaa; |
hmiot | 13:9b2a587ee7ac | 215 | for(numCount=0; numCount < 7; numCount++){ |
hmiot | 13:9b2a587ee7ac | 216 | pre_write_data[numCount] = write_data[numCount]; |
hmiot | 12:cd54180657ee | 217 | } |
hmiot | 13:9b2a587ee7ac | 218 | first_on=false; |
hmiot | 13:9b2a587ee7ac | 219 | error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,8,write_data); |
hariom | 17:0b0a7dccf151 | 220 | BLE::Instance().processEvents(); |
hmiot | 10:5e5fa5bf77b5 | 221 | break; |
hmiot | 18:ef42e2579463 | 222 | |
hmiot | 18:ef42e2579463 | 223 | case NOTIFY: |
hmiot | 18:ef42e2579463 | 224 | //[0xEF, 0x01, 0x77] Notify Value |
hmiot | 18:ef42e2579463 | 225 | write_data[0]=0xEF; |
hmiot | 18:ef42e2579463 | 226 | write_data[1]=0x01; |
hmiot | 18:ef42e2579463 | 227 | write_data[2]=0x77; |
hmiot | 18:ef42e2579463 | 228 | error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,8,write_data); |
hmiot | 18:ef42e2579463 | 229 | BLE::Instance().processEvents(); |
hmiot | 18:ef42e2579463 | 230 | break; |
hmiot | 18:ef42e2579463 | 231 | |
hmiot | 10:5e5fa5bf77b5 | 232 | default: |
hmiot | 10:5e5fa5bf77b5 | 233 | printf("Invalid Options\r\n"); |
hmiot | 10:5e5fa5bf77b5 | 234 | } |
hmiot | 10:5e5fa5bf77b5 | 235 | printf("Error Status =%d\n\r",error_status); |
hmiot | 13:9b2a587ee7ac | 236 | } |
hmiot | 13:9b2a587ee7ac | 237 | |
hmiot | 8:285ebd0e83fb | 238 | } |
hmiot | 8:285ebd0e83fb | 239 | |
hmiot | 18:ef42e2579463 | 240 | void write_notifyCharacterstics() |
hmiot | 18:ef42e2579463 | 241 | { |
hmiot | 18:ef42e2579463 | 242 | printf("write_notifyCharacterstics \n\r"); |
hmiot | 18:ef42e2579463 | 243 | uint16_t value = BLE_HVX_NOTIFICATION; |
hmiot | 18:ef42e2579463 | 244 | error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_REQ, |
hmiot | 18:ef42e2579463 | 245 | notifyCharacteristic.getConnectionHandle(), |
hmiot | 18:ef42e2579463 | 246 | notifyCharacteristic.getValueHandle(), /* HACK Alert. We're assuming that CCCD descriptor immediately follows the value attribute. */ |
hmiot | 18:ef42e2579463 | 247 | sizeof(uint16_t), /* HACK Alert! size should be made into a BLE_API constant. */ |
hmiot | 18:ef42e2579463 | 248 | reinterpret_cast<const uint8_t *>(&value)); |
hmiot | 18:ef42e2579463 | 249 | printf("Conn Handle = %d \r\n",notifyCharacteristic.getConnectionHandle()); |
hmiot | 18:ef42e2579463 | 250 | printf("Value Handle = %d \r\n",notifyCharacteristic.getValueHandle()); |
hmiot | 18:ef42e2579463 | 251 | |
hmiot | 18:ef42e2579463 | 252 | printf("write_notifyCharacterstics %d \n\r", error_status); |
hmiot | 18:ef42e2579463 | 253 | BLE::Instance().processEvents(); |
hmiot | 18:ef42e2579463 | 254 | } |
hmiot | 12:cd54180657ee | 255 | void datancpy(char pre_write[], char writeData[], int data_size){ |
hmiot | 8:285ebd0e83fb | 256 | int numCount; |
hmiot | 8:285ebd0e83fb | 257 | for(numCount=0; numCount <= data_size; numCount++){ |
hmiot | 8:285ebd0e83fb | 258 | pre_write[numCount] = writeData[numCount]; |
hmiot | 8:285ebd0e83fb | 259 | } |
hmiot | 8:285ebd0e83fb | 260 | |
mbed_tw_hoehoe | 2:4b53d13d9851 | 261 | } |
mbed_tw_hoehoe | 2:4b53d13d9851 | 262 | |
hmiot | 8:285ebd0e83fb | 263 | void bleint() |
hmiot | 8:285ebd0e83fb | 264 | { |
hmiot | 8:285ebd0e83fb | 265 | printf("Init\r\n"); |
hmiot | 9:b967a01810e1 | 266 | BLE &ble = BLE::Instance(); |
hmiot | 8:285ebd0e83fb | 267 | BLE::Instance().init(bleInitComplete); |
hmiot | 8:285ebd0e83fb | 268 | } |
hmiot | 8:285ebd0e83fb | 269 | |
hmiot | 8:285ebd0e83fb | 270 | int main() |
hmiot | 8:285ebd0e83fb | 271 | { |
hmiot | 8:285ebd0e83fb | 272 | // Serial port configuration |
mbed_tw_hoehoe | 1:2f1203d70643 | 273 | pc.baud(9600); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 274 | wait(8.0); |
hmiot | 9:b967a01810e1 | 275 | pc.printf("Start\n\r"); |
hmiot | 13:9b2a587ee7ac | 276 | |
hmiot | 12:cd54180657ee | 277 | spiSlave.reply(191); |
hmiot | 13:9b2a587ee7ac | 278 | statusWR=1; |
hmiot | 9:b967a01810e1 | 279 | bleint(); |
hmiot | 10:5e5fa5bf77b5 | 280 | bufferSize = 0; |
hmiot | 18:ef42e2579463 | 281 | int temp = 0; |
hmiot | 18:ef42e2579463 | 282 | //int index=0; |
hmiot | 18:ef42e2579463 | 283 | |
hmiot | 18:ef42e2579463 | 284 | //int temp = 0x00; |
hmiot | 18:ef42e2579463 | 285 | //int color = 0x00; |
hmiot | 18:ef42e2579463 | 286 | //int i=0; |
hmiot | 10:5e5fa5bf77b5 | 287 | while(1){ |
hmiot | 9:b967a01810e1 | 288 | //printf("loop\r\n"); |
hmiot | 14:f84a9745c10a | 289 | if(!(connect_status && charDiscover)) |
hmiot | 10:5e5fa5bf77b5 | 290 | waitBleEvent(); |
hmiot | 10:5e5fa5bf77b5 | 291 | |
hmiot | 18:ef42e2579463 | 292 | if(charDiscover && connect_status && notify_write ) //&& notify_write |
hmiot | 18:ef42e2579463 | 293 | { |
hmiot | 18:ef42e2579463 | 294 | printf("Write Notify \n\r"); |
hmiot | 18:ef42e2579463 | 295 | write_notifyCharacterstics(); |
hmiot | 18:ef42e2579463 | 296 | notify_write = false; |
hmiot | 18:ef42e2579463 | 297 | |
hmiot | 18:ef42e2579463 | 298 | } |
hmiot | 18:ef42e2579463 | 299 | |
hmiot | 12:cd54180657ee | 300 | if(spiSlave.receive()){ |
hmiot | 10:5e5fa5bf77b5 | 301 | // printf("Slave Read loop1\r\n"); |
hmiot | 18:ef42e2579463 | 302 | |
hmiot | 18:ef42e2579463 | 303 | temp = spiSlave.read(); |
hmiot | 10:5e5fa5bf77b5 | 304 | |
hmiot | 18:ef42e2579463 | 305 | if(temp == 0x86) |
hmiot | 18:ef42e2579463 | 306 | { |
hmiot | 18:ef42e2579463 | 307 | bufferSize = 0; |
hmiot | 18:ef42e2579463 | 308 | startFlag = true; |
hmiot | 18:ef42e2579463 | 309 | endFlag = false; |
hmiot | 18:ef42e2579463 | 310 | spiSlave.reply(0x86); |
hariom | 17:0b0a7dccf151 | 311 | |
hmiot | 18:ef42e2579463 | 312 | }else if(temp == 0x88) |
hmiot | 18:ef42e2579463 | 313 | { |
hmiot | 18:ef42e2579463 | 314 | endFlag= true; |
hmiot | 18:ef42e2579463 | 315 | spiSlave.reply(0x88); |
hmiot | 18:ef42e2579463 | 316 | |
hmiot | 18:ef42e2579463 | 317 | }else if(startFlag == true && endFlag == false) |
hmiot | 18:ef42e2579463 | 318 | { |
hmiot | 18:ef42e2579463 | 319 | spiRX[bufferSize] = temp; |
hmiot | 18:ef42e2579463 | 320 | spiSlave.reply(temp); |
hmiot | 18:ef42e2579463 | 321 | bufferSize++; |
hmiot | 10:5e5fa5bf77b5 | 322 | } |
hmiot | 10:5e5fa5bf77b5 | 323 | |
hmiot | 18:ef42e2579463 | 324 | if(startFlag && endFlag) |
hmiot | 18:ef42e2579463 | 325 | { |
hmiot | 18:ef42e2579463 | 326 | processSPIPacket(spiRX); |
hmiot | 18:ef42e2579463 | 327 | startFlag = false; |
hmiot | 18:ef42e2579463 | 328 | endFlag = false; |
hmiot | 18:ef42e2579463 | 329 | spiSlave.reply(0xAA); |
hmiot | 18:ef42e2579463 | 330 | } |
hmiot | 18:ef42e2579463 | 331 | |
hmiot | 18:ef42e2579463 | 332 | |
hmiot | 12:cd54180657ee | 333 | } |
hmiot | 18:ef42e2579463 | 334 | //wait_ms(1000); |
hmiot | 13:9b2a587ee7ac | 335 | |
hmiot | 13:9b2a587ee7ac | 336 | |
hmiot | 9:b967a01810e1 | 337 | } |
hmiot | 9:b967a01810e1 | 338 | |
hmiot | 9:b967a01810e1 | 339 | } |
hmiot | 9:b967a01810e1 | 340 | |
hmiot | 10:5e5fa5bf77b5 | 341 | void waitBleEvent() |
hmiot | 10:5e5fa5bf77b5 | 342 | { |
hmiot | 10:5e5fa5bf77b5 | 343 | while(serviceDiscover || BLE::Instance().gattClient().isServiceDiscoveryActive()){ |
hmiot | 10:5e5fa5bf77b5 | 344 | BLE::Instance().waitForEvent(); |
hmiot | 10:5e5fa5bf77b5 | 345 | } |
hmiot | 9:b967a01810e1 | 346 | } |
hmiot | 10:5e5fa5bf77b5 | 347 | |
hmiot | 18:ef42e2579463 | 348 | void processSPIPacket(int SPIData[]) |
hmiot | 18:ef42e2579463 | 349 | { |
hmiot | 18:ef42e2579463 | 350 | int start_index = 2; |
hmiot | 18:ef42e2579463 | 351 | int temp_buff[8]; |
hmiot | 18:ef42e2579463 | 352 | for(int k =0; k < SPIData[0]; k++, start_index++) |
hmiot | 18:ef42e2579463 | 353 | { |
hmiot | 18:ef42e2579463 | 354 | temp_buff[k] = SPIData[start_index]; |
hmiot | 18:ef42e2579463 | 355 | |
hmiot | 18:ef42e2579463 | 356 | } |
hmiot | 18:ef42e2579463 | 357 | switch(SPIData[1]) |
hmiot | 18:ef42e2579463 | 358 | { |
hmiot | 18:ef42e2579463 | 359 | case 0xCE: |
hmiot | 18:ef42e2579463 | 360 | { |
hmiot | 18:ef42e2579463 | 361 | light_actions(temp_buff); |
hmiot | 18:ef42e2579463 | 362 | } |
hmiot | 18:ef42e2579463 | 363 | break; |
hmiot | 18:ef42e2579463 | 364 | |
hmiot | 18:ef42e2579463 | 365 | default: |
hmiot | 18:ef42e2579463 | 366 | printf("Unknown data \r\n"); |
hmiot | 18:ef42e2579463 | 367 | break; |
hmiot | 18:ef42e2579463 | 368 | } |
hmiot | 18:ef42e2579463 | 369 | |
hmiot | 18:ef42e2579463 | 370 | } |
hmiot | 18:ef42e2579463 | 371 |