Ble Demo with Raspberry PI updated
Dependencies: BLE_API mbed nRF51822
Fork of BLE_DEMO_SPI by
Revision 16:5bd717644078, committed 2017-10-10
- Comitter:
- hariom
- Date:
- Tue Oct 10 09:42:29 2017 +0000
- Parent:
- 15:ef3cf37827d2
- Child:
- 17:2e0273d799e2
- Commit message:
- ble_demo_hardcode_spi_command
Changed in this revision
Light_Header/hm_config.h | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/Light_Header/hm_config.h Thu Sep 21 07:55:35 2017 +0000 +++ b/Light_Header/hm_config.h Tue Oct 10 09:42:29 2017 +0000 @@ -13,13 +13,13 @@ //class Magic_Light{ static bool connect_status; bool charDiscover; -uint8_t pre_write_data[16]; +//uint8_t pre_write_data[16]; static ble_error_t error_status; bool serviceDiscover = true; bool first_on=true; -int spiRX[8]; +//int spiRX[8]; uint8_t bufferSize; -uint8_t write_data[16]; //[8] = {0x00,};//={0x56,0x00,0xff,0x00,0x00,0xf0,0xaa}; +//uint8_t write_data[16]; //[8] = {0x00,};//={0x56,0x00,0xff,0x00,0x00,0xf0,0xaa}; void AddCharNodes(DiscoveredCharacteristic light_Characteristic); void light_actions(int service_char[]); @@ -42,15 +42,17 @@ MAX_COLOR }light_color; +/* uint8_t lightColor[MAX_COLOR][3] = {{0xFF,0x00,0x00},{0x00,0xFF,0x00}, {0x00,0x00,0xFF},{0xFF,0xFF,0x00}, {0x00,0xFF,0xFF},{0xFF,0x00,0xFF} }; +*/ struct lightCharacteristic_t{ uint8_t u_characteristicID; Gap::Handle_t connHandle; GattAttribute::Handle_t valueHandle; - struct lightCharacteristic_t *nextChar; + //struct lightCharacteristic_t *nextChar; }; typedef struct _serviceinfo
--- a/main.cpp Thu Sep 21 07:55:35 2017 +0000 +++ b/main.cpp Tue Oct 10 09:42:29 2017 +0000 @@ -6,47 +6,65 @@ #include "hm_config.h" DiscoveredCharacteristic lightCharacteristic; -static s_serviceInfo lightChar = {NULL,NULL,NULL,NULL}; +Gap::Handle_t connectionHandle = 0xFFFF; +//static s_serviceInfo lightChar = {NULL,NULL}; Serial pc(USBTX, USBRX); void bleint(); void waitBleEvent(); void ble_actions(int spi_data); uint8_t statusWR; SPISlave spiSlave(P0_9, P0_11, P0_8, P0_10); +lightCharacteristic_t ptr_temp_char; + +//DiscoveredCharacteristic ptr_temp_char; + +bool connection_handle = 0; + +uint8_t Ble_address[6]; void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { uint8_t con_status =0; + int i; //0x51 for Magic light //0x5A - if (params->peerAddr[0] == 0x5A){ // 0x2F for red bear1.5 /* !ALERT! Alter this filter to suit your device. */ + + pc.printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\ndata: %s\r\n", + params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], + params->rssi, params->isScanResponse, params->type, params->advertisingData); + //if (params->peerAddr[0] == 0x5A){ // 0x2F for red bear1.5 /* !ALERT! Alter this filter to suit your device. */ + if (params->peerAddr[0] == 0x5B){ // 0x2F for red bear1.5 /* !ALERT! Alter this filter to suit your device. */ pc.printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], params->rssi, params->isScanResponse, params->type); pc.printf("Data Length : %d\r\n",params->advertisingDataLen); + for (i = 0; i < 6; i++) + Ble_address[i] = params->peerAddr[i]; + con_status = BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_PUBLIC, NULL, NULL); pc.printf("Connection Status : %d\r\n",con_status); connect_status = 1; BLE::Instance().gap().stopScan(); - - }else{ + + } + else{ printf("Not Matched your device\r\n"); return; } } void serviceDiscoveryCallback(const DiscoveredService *service) { - pc.printf("Service Discovery Callback\r\n"); + //pc.printf("Service Discovery Callback\r\n"); if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) { - pc.printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle()); + //pc.printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle()); } else { - pc.printf("S UUID-"); + //pc.printf("S UUID-"); const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { printf("%02x", longUUIDBytes[i]); } - pc.printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle()); + //pc.printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle()); } } @@ -68,15 +86,16 @@ void AddCharNodes(DiscoveredCharacteristic light_Characteristic) { - printf("Add Char Nodes\r\n"); - lightCharacteristic_t *ptr_temp_char; + //printf("Add Char Nodes\r\n"); + // lightCharacteristic_t ptr_temp_char; + /* ptr_temp_char = lightChar.services_Char[0]; if(NULL == ptr_temp_char) { ptr_temp_char = new lightCharacteristic_t; - lightChar.services_Char[0] = ptr_temp_char; + lightChar.services_Char[0] = ptr_temp_char; } else { @@ -85,18 +104,18 @@ ptr_temp_char = ptr_temp_char->nextChar; } - /* add a new node */ - ptr_temp_char->nextChar = new lightCharacteristic_t; + /* add a new node + ptr_temp_char->nextChar = new lightCharacteristic_t; ptr_temp_char = ptr_temp_char->nextChar; - } + }*/ /* assign data to the node */ - ptr_temp_char->u_characteristicID = 1; - printf("Conn Handle = %d\r\n",light_Characteristic.getConnectionHandle()); - printf("Value Handle = %d\r\n",light_Characteristic.getValueHandle()); - ptr_temp_char->connHandle = light_Characteristic.getConnectionHandle(); - ptr_temp_char->valueHandle = light_Characteristic.getValueHandle(); - ptr_temp_char->nextChar = NULL; + ptr_temp_char.u_characteristicID = 1; + pc.printf("Conn Handle = %d\r\n",light_Characteristic.getConnectionHandle()); + pc.printf("Value Handle = %d\r\n",light_Characteristic.getValueHandle()); + ptr_temp_char.connHandle = light_Characteristic.getConnectionHandle(); + ptr_temp_char.valueHandle = light_Characteristic.getValueHandle(); + //ptr_temp_char.nextChar = NULL; } void discoveryTerminationCallback(Gap::Handle_t connectionHandle) { @@ -105,9 +124,10 @@ } void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { - pc.printf("Connection Callback\n\r"); + //pc.printf("Connection Callback\n\r"); if (params->role == Gap::CENTRAL) { - pc.printf("Service and characterstics discovery callback\r\n"); + connectionHandle = params->handle; + //pc.printf("Service and characterstics discovery callback\r\n"); BLE::Instance().gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); BLE::Instance().gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xffe5, 0xffe9); // 0xffe5 --> Services UUID @@ -115,12 +135,26 @@ } } +void writeCallback(const GattWriteCallbackParams *eventDataP) +{ + pc.printf("written callback\r\n"); +} + void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { pc.printf("disconnected\r\n"); BLE::Instance().gap().startScan(advertisementCallback); } +void hvxCallback(const GattHVXCallbackParams *params) { + printf("received HVX callback for handle %u; type %s\r\n", params->handle, (params->type == BLE_HVX_NOTIFICATION) ? "notification" : "indication"); + for (unsigned index = 0; index < params->len; index++) { + printf(" %02x", params->data[index]); + } + printf("\r\n"); +} + + /** * Callback triggered when the ble initialization process has finished */ @@ -138,12 +172,21 @@ ble.gap().onDisconnection(disconnectionCallback); ble.gap().setScanParams(1000, 800); ble.gap().startScan(advertisementCallback); + //ble.gattClient().onHVX(hvxCallback); } -void light_actions(int * service_char){ - lightCharacteristic_t *ptr_tmp = NULL; - ptr_tmp = lightChar.services_Char[0]; +void light_actions(int * service_char) +{ + uint8_t lightColor[MAX_COLOR][3] = {{0xFF,0x00,0x00},{0x00,0xFF,0x00}, + {0x00,0x00,0xFF},{0xFF,0xFF,0x00}, + {0x00,0xFF,0xFF},{0xFF,0x00,0xFF} + }; + uint8_t pre_write_data[16]; + uint8_t write_data[16]; + + //lightCharacteristic_t *ptr_tmp = NULL; + //ptr_tmp = lightChar.services_Char[0]; uint8_t numCount; if(connect_status) { @@ -158,7 +201,11 @@ write_data[4]=0x00; write_data[5]=0xf0; write_data[6]=0xaa; - error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,8,write_data); + //error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,8,write_data); + error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,connectionHandle,ptr_temp_char.valueHandle,7,write_data); + BLE::Instance().processEvents(); + //error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_temp_char.connHandle,ptr_temp_char.valueHandle,7,write_data); + //BLE::Instance().updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), write_data, 7); break; case TURN_ON: //on @@ -170,7 +217,10 @@ pre_write_data[5]=0xf0; pre_write_data[6]=0xaa; - error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,8,pre_write_data); + //error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,8,pre_write_data); + error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,connectionHandle,ptr_temp_char.valueHandle,7,pre_write_data); + BLE::Instance().processEvents(); + //error_status = BLE::Instance().DiscoveredCharacteristic().write(7,pre_write_data); break; case COLOR: // Color @@ -185,7 +235,11 @@ pre_write_data[numCount] = write_data[numCount]; } first_on=false; - error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,8,write_data); + //error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,8,write_data); + error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,connectionHandle,ptr_temp_char.valueHandle,7,write_data); + BLE::Instance().processEvents(); + //error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_temp_char.connHandle,ptr_temp_char.valueHandle,7,write_data); + break; default: @@ -193,7 +247,6 @@ } printf("Error Status =%d\n\r",error_status); } - } void datancpy(char pre_write[], char writeData[], int data_size){ @@ -222,24 +275,55 @@ statusWR=1; bleint(); bufferSize = 0; + int spiRX[5]; + + int temp = 0x00; + int color = 0x00; + int con_status,i; while(1){ - //printf("loop\r\n"); + pc.printf("loop\r\n"); + wait(3.0); if(!(connect_status && charDiscover)) - waitBleEvent(); + waitBleEvent(); - if(spiSlave.receive()){ - // printf("Slave Read loop1\r\n"); - spiRX[bufferSize] = spiSlave.read(); - spiSlave.reply(spiRX[bufferSize]); - bufferSize++; + if (temp == 2 && color == 3) { + temp = 0x00; + color = 0x00; + } + else if (temp == 2 && color < 3) { + color++; + } + else if (temp < 2) + temp++; + + spiRX[0] = 0xB3; + spiRX[1] = 1; + spiRX[2] = temp; + spiRX[3] = color; + pc.printf("spix buffer is: "); + for (i = 0; i < 4; i++) { + pc.printf("%d ", spiRX[i]); + } + pc.printf("\n"); + + //if(spiSlave.receive()){ + //printf("Slave Read loop1\r\n"); + //spiRX[bufferSize] = spiSlave.read(); + //spiSlave.reply(spiRX[bufferSize]); + //bufferSize++; + bufferSize = 5; if(bufferSize >= 5){ bufferSize=0; light_actions(spiRX); + //BLE::Instance().gattClient().reset(); spiSlave.reply(spiRX[2]); //(0xAA); + - if(statusWR >=4) - { wait_ms(500); + if(error_status > 7) + { + error_status = BLE_ERROR_NONE; + //wait_ms(500); BLE::Instance().shutdown(); statusWR =1; connect_status =0; @@ -247,12 +331,11 @@ serviceDiscover = true; bleint(); } - statusWR++; + //statusWR++; + wait_ms(3000); } - } - - + //} }