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