BLE_DEMO_SPI_HARDCODE
Dependencies: BLE_API mbed nRF51822
Fork of BLE_Central_Light_Demo_23 by
Revision 10:5e5fa5bf77b5, committed 2017-01-30
- Comitter:
- hmiot
- Date:
- Mon Jan 30 15:20:48 2017 +0000
- Parent:
- 9:b967a01810e1
- Child:
- 11:58d8a3129877
- Commit message:
- SPI Communication working.
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 Wed Jan 25 14:41:05 2017 +0000 +++ b/Light_Header/hm_config.h Mon Jan 30 15:20:48 2017 +0000 @@ -11,13 +11,15 @@ #define MAX_SERVICES 4 //class Magic_Light{ -uint8_t connect_status; -uint8_t charDiscover; -char pre_write_data[8]; -char receiveBuf[4]; +bool connect_status; +bool charDiscover; +uint8_t pre_write_data[8] = {0x00,}; static ble_error_t error_status; - -char write_data[8];//={0x56,0x00,0xff,0x00,0x00,0xf0,0xaa}; +bool serviceDiscover = true; +bool first_on=true; +int spiRX[8]; +uint8_t bufferSize; +uint8_t *write_data; //[8] = {0x00,};//={0x56,0x00,0xff,0x00,0x00,0xf0,0xaa}; void AddCharNodes(DiscoveredCharacteristic light_Characteristic); void light_actions(uint8_t service_char[]);
--- a/main.cpp Wed Jan 25 14:41:05 2017 +0000 +++ b/main.cpp Mon Jan 30 15:20:48 2017 +0000 @@ -12,13 +12,11 @@ void bleint(); void SPI_Slave_read(); +void waitBleEvent(); +void ble_actions(int spi_data); SPISlave spiSlave(P0_9, P0_11, P0_8, P0_10); -bool serviceDiscover = true; -int spiRX[4]; - // uint8_t spiTX[4]; -uint8_t bufferSize; void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { uint8_t con_status =0; @@ -63,9 +61,9 @@ pc.printf("Matched char UUID\r\n"); printf("Conn Handle = %dand %d \r\n",characteristicP->getConnectionHandle(), lightCharacteristic.getConnectionHandle()); printf("Value Handle = %d and %d\r\n",characteristicP->getValueHandle(),lightCharacteristic.getValueHandle()); + AddCharNodes(lightCharacteristic); charDiscover = 1; serviceDiscover = true; - AddCharNodes(lightCharacteristic); }else { printf("Not Matched char UUID\r\n"); } @@ -114,6 +112,8 @@ 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 + // 0xffe9 --> Characteristics UUID } } @@ -138,63 +138,63 @@ ble.gap().onConnection(connectionCallback); ble.gap().onDisconnection(disconnectionCallback); - ble.gap().setScanParams(1000, 800); ble.gap().startScan(advertisementCallback); + } void light_actions(int service_char[]){ - uint8_t *write_data=NULL; - //={0x56,0xff,0x00,0x00,0x00,0xf0,0xaa}; - write_data = new uint8_t[7]; + write_data = new uint8_t[8]; lightCharacteristic_t *ptr_tmp = NULL; ptr_tmp = lightChar.services_Char[service_char[1]-1]; - // printf("Conn Handle = %d\r\n",ptr_tmp->connHandle); - // printf("Value Handle = %d\r\n",ptr_tmp->valueHandle); - if(connect_status) - { + if(connect_status) + { - switch(service_char[2]) - { - case TURN_OFF: // off - write_data[0]=0x56; - write_data[1]=0x00; - write_data[2]=0x00; - write_data[3]=0x00; - 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,7,write_data); - break; + switch(service_char[2]) + { + case TURN_OFF: // off + write_data[0]=0x56; + write_data[1]=0x00; + write_data[2]=0x00; + write_data[3]=0x00; + 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); + break; - case TURN_ON: //on - write_data[0]=0x56; - write_data[1]=0xff; - write_data[2]=0xff; - write_data[3]=0xff; - 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,7,write_data); - break; + case TURN_ON: //on + if(first_on){ + pre_write_data[0]=0x56; + pre_write_data[1]=0xDE; + pre_write_data[2]=0x8E; + pre_write_data[3]=0xEE; + pre_write_data[4]=0x00; + 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); + break; - case COLOR: // Color - write_data[0]=0x56; - write_data[1]=lightColor[service_char[3]][0];//0xff - write_data[2]=lightColor[service_char[3]][1];//0x00; - write_data[3]=lightColor[service_char[3]][2];//0x00; - write_data[4]=0x00; - write_data[5]=0xf0; - write_data[6]=0xaa; - //datancpy(pre_write_data, (char *)write_data,6); - error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,7,write_data); - break; + case COLOR: // Color + write_data[0]=0x56; + write_data[1]=lightColor[service_char[3]][0];//0xff + write_data[2]=lightColor[service_char[3]][1];//0x00; + write_data[3]=lightColor[service_char[3]][2];//0x00; + write_data[4]=0x00; + write_data[5]=0xf0; + write_data[6]=0xaa; + datancpy((char *)pre_write_data, (char *)write_data,6); + first_on=false; + error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,8,write_data); + break; - default: - printf("Invalid Options\r\n"); - } - delete [] write_data; - } + default: + printf("Invalid Options\r\n"); + } + printf("Error Status =%d\n\r",error_status); + delete [] write_data; + } } void datancpy(char *pre_write, char *writeData, int data_size){ @@ -212,42 +212,44 @@ BLE::Instance().init(bleInitComplete); } - int main() { // Serial port configuration pc.baud(9600); wait(8.0); pc.printf("Start\n\r"); + // int options; spiSlave.reply(191); - // Initialize the Ble bleint(); - while(1){ + bufferSize = 0; + while(1){ //printf("loop\r\n"); - if (serviceDiscover || BLE::Instance().gattClient().isServiceDiscoveryActive()){ - BLE::Instance().waitForEvent(); - - }else{ - // printf("Slave Read loop\r\n"); - SPI_Slave_read(); + if(!(connect_status && charDiscover)) + waitBleEvent(); + + if(spiSlave.receive()){ + // printf("Slave Read loop1\r\n"); + spiRX[bufferSize] = spiSlave.read(); + spiSlave.reply(spiRX[bufferSize]); + bufferSize++; + + if(bufferSize >= 5){ + bufferSize=0; + light_actions(spiRX); + spiSlave.reply(0xAA); + } + } - + + wait_us(15); } } -void SPI_Slave_read(){ - - wait_us(2); - if(spiSlave.receive()){ - // printf("Slave Read loop1\r\n"); - spiRX[bufferSize] = spiSlave.read(); - spiSlave.reply(spiRX[bufferSize]); - bufferSize++; - if(bufferSize>=4){ - bufferSize=0; - light_actions(spiRX); - // printf("Receive\n\r"); - } - } +void waitBleEvent() +{ + while(serviceDiscover || BLE::Instance().gattClient().isServiceDiscoveryActive()){ + BLE::Instance().waitForEvent(); + } } +