Working Code
Dependencies: BLE_API mbed nRF51822
Fork of BLE_Central_Light_Demo by
main.cpp@8:285ebd0e83fb, 2017-01-25 (annotated)
- Committer:
- hmiot
- Date:
- Wed Jan 25 08:08:20 2017 +0000
- Revision:
- 8:285ebd0e83fb
- Parent:
- 7:66586d2d7cb5
- Child:
- 9:b967a01810e1
BLE Light
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 | 6:71e7a446ae6a | 8 | DiscoveredCharacteristic lightCharacteristic; |
hmiot | 8:285ebd0e83fb | 9 | s_serviceInfo lightChar = {NULL,NULL,NULL,NULL}; |
hmiot | 6:71e7a446ae6a | 10 | Serial pc(USBTX, USBRX); |
hmiot | 6:71e7a446ae6a | 11 | |
hmiot | 8:285ebd0e83fb | 12 | void bleint(); |
hmiot | 8:285ebd0e83fb | 13 | bool serviceDiscover = true; |
hmiot | 8:285ebd0e83fb | 14 | void readCharacteristics(); |
hmiot | 8:285ebd0e83fb | 15 | void spiRead(); |
hmiot | 8:285ebd0e83fb | 16 | void testaction(uint8_t options); |
hmiot | 8:285ebd0e83fb | 17 | bool isint = true; |
hmiot | 8:285ebd0e83fb | 18 | SPISlave spiSlave(P0_9, P0_11, P0_8, P0_10); |
hmiot | 8:285ebd0e83fb | 19 | |
hmiot | 8:285ebd0e83fb | 20 | int spiRX[4]; |
hmiot | 8:285ebd0e83fb | 21 | // uint8_t spiTX[4]; |
hmiot | 8:285ebd0e83fb | 22 | uint8_t bufferSize; |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 23 | |
hmiot | 6:71e7a446ae6a | 24 | void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { |
hmiot | 6:71e7a446ae6a | 25 | uint8_t con_status =0; |
hmiot | 6:71e7a446ae6a | 26 | // uint8_t i; |
hmiot | 6:71e7a446ae6a | 27 | //0x51 for Magic light |
hmiot | 6:71e7a446ae6a | 28 | //0x5A |
hmiot | 8:285ebd0e83fb | 29 | if ((params->peerAddr[0] == 0x51) || (params->peerAddr[0] == 0x5A)) { // 0x2F for red bear1.5 /* !ALERT! Alter this filter to suit your device. */ |
hmiot | 8:285ebd0e83fb | 30 | |
hmiot | 8:285ebd0e83fb | 31 | pc.printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 32 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 33 | params->rssi, params->isScanResponse, params->type); |
hmiot | 6:71e7a446ae6a | 34 | pc.printf("Data Length : %d\r\n",params->advertisingDataLen); |
hmiot | 8:285ebd0e83fb | 35 | con_status = BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_PUBLIC, NULL, NULL); |
hmiot | 8:285ebd0e83fb | 36 | pc.printf("Connection Status : %d\r\n",con_status); |
hmiot | 8:285ebd0e83fb | 37 | connect_status = 1; |
hmiot | 8:285ebd0e83fb | 38 | BLE::Instance().gap().stopScan(); |
hmiot | 8:285ebd0e83fb | 39 | }else{ |
hmiot | 8:285ebd0e83fb | 40 | printf("Not Matched your device\r\n"); |
hmiot | 8:285ebd0e83fb | 41 | return; |
hmiot | 8:285ebd0e83fb | 42 | } |
hmiot | 8:285ebd0e83fb | 43 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 44 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 45 | void serviceDiscoveryCallback(const DiscoveredService *service) { |
hmiot | 6:71e7a446ae6a | 46 | pc.printf("Service Discovery Callback\r\n"); |
hmiot | 6:71e7a446ae6a | 47 | if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) { |
hmiot | 6:71e7a446ae6a | 48 | pc.printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle()); |
hmiot | 6:71e7a446ae6a | 49 | } else { |
hmiot | 6:71e7a446ae6a | 50 | pc.printf("S UUID-"); |
hmiot | 6:71e7a446ae6a | 51 | const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); |
hmiot | 6:71e7a446ae6a | 52 | for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { |
hmiot | 6:71e7a446ae6a | 53 | printf("%02x", longUUIDBytes[i]); |
hmiot | 6:71e7a446ae6a | 54 | } |
hmiot | 6:71e7a446ae6a | 55 | pc.printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle()); |
hmiot | 6:71e7a446ae6a | 56 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 57 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 58 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 59 | void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) { |
hmiot | 6:71e7a446ae6a | 60 | pc.printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast()); |
hmiot | 8:285ebd0e83fb | 61 | |
hmiot | 8:285ebd0e83fb | 62 | if (characteristicP->getUUID().getShortUUID() == 0xffe9 ) { /* !ALERT! Alter this filter to suit your device. */ |
hmiot | 8:285ebd0e83fb | 63 | lightCharacteristic = *characteristicP; |
hmiot | 6:71e7a446ae6a | 64 | pc.printf("Matched char UUID\r\n"); |
hmiot | 8:285ebd0e83fb | 65 | printf("Conn Handle = %dand %d \r\n",characteristicP->getConnectionHandle(), lightCharacteristic.getConnectionHandle()); |
hmiot | 8:285ebd0e83fb | 66 | printf("Value Handle = %d and %d\r\n",characteristicP->getValueHandle(),lightCharacteristic.getValueHandle()); |
hmiot | 8:285ebd0e83fb | 67 | // lightCharacteristic.writeWoResponse(sizeof(write_data), write_data); |
hmiot | 8:285ebd0e83fb | 68 | charDiscover = 1; |
hmiot | 8:285ebd0e83fb | 69 | serviceDiscover = true; |
hmiot | 8:285ebd0e83fb | 70 | AddCharNodes(lightCharacteristic); |
hmiot | 8:285ebd0e83fb | 71 | //light_actions(1); |
hmiot | 8:285ebd0e83fb | 72 | |
hmiot | 8:285ebd0e83fb | 73 | //BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,characteristicP->getConnectionHandle(),characteristicP->getValueHandle(),sizeof(write_data),write_data); |
hmiot | 8:285ebd0e83fb | 74 | |
hmiot | 8:285ebd0e83fb | 75 | }else { |
hmiot | 8:285ebd0e83fb | 76 | printf("Not Matched char UUID\r\n"); |
mbed_tw_hoehoe | 2:4b53d13d9851 | 77 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 78 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 79 | |
hmiot | 8:285ebd0e83fb | 80 | void AddCharNodes(DiscoveredCharacteristic light_Characteristic) |
hmiot | 8:285ebd0e83fb | 81 | { |
hmiot | 8:285ebd0e83fb | 82 | printf("Add Char Nodes\r\n"); |
hmiot | 8:285ebd0e83fb | 83 | lightCharacteristic_t *ptr_temp_char = NULL; |
hmiot | 8:285ebd0e83fb | 84 | |
hmiot | 8:285ebd0e83fb | 85 | printf("\n\rAdding Node..."); |
hmiot | 8:285ebd0e83fb | 86 | |
hmiot | 8:285ebd0e83fb | 87 | ptr_temp_char = lightChar.services_Char[0]; |
hmiot | 8:285ebd0e83fb | 88 | |
hmiot | 8:285ebd0e83fb | 89 | if(NULL == ptr_temp_char) |
hmiot | 8:285ebd0e83fb | 90 | { |
hmiot | 8:285ebd0e83fb | 91 | ptr_temp_char = (lightCharacteristic_t *)malloc(sizeof(lightCharacteristic_t)); |
hmiot | 8:285ebd0e83fb | 92 | lightChar.services_Char[0] = ptr_temp_char; |
hmiot | 8:285ebd0e83fb | 93 | } |
hmiot | 8:285ebd0e83fb | 94 | else |
hmiot | 8:285ebd0e83fb | 95 | { |
hmiot | 8:285ebd0e83fb | 96 | while(NULL != ptr_temp_char->nextChar) |
hmiot | 8:285ebd0e83fb | 97 | { |
hmiot | 8:285ebd0e83fb | 98 | ptr_temp_char = ptr_temp_char->nextChar; |
hmiot | 8:285ebd0e83fb | 99 | } |
hmiot | 8:285ebd0e83fb | 100 | |
hmiot | 8:285ebd0e83fb | 101 | /* add a new node */ |
hmiot | 8:285ebd0e83fb | 102 | ptr_temp_char->nextChar = (lightCharacteristic_t *)malloc(sizeof(lightCharacteristic_t)); |
hmiot | 8:285ebd0e83fb | 103 | ptr_temp_char = ptr_temp_char->nextChar; |
hmiot | 8:285ebd0e83fb | 104 | } |
hmiot | 8:285ebd0e83fb | 105 | |
hmiot | 8:285ebd0e83fb | 106 | /* assign data to the node */ |
hmiot | 8:285ebd0e83fb | 107 | ptr_temp_char->u_characteristicID = 1; |
hmiot | 8:285ebd0e83fb | 108 | printf("Conn Handle = %d\r\n",light_Characteristic.getConnectionHandle()); |
hmiot | 8:285ebd0e83fb | 109 | printf("Value Handle = %d\r\n",light_Characteristic.getValueHandle()); |
hmiot | 8:285ebd0e83fb | 110 | ptr_temp_char->connHandle = light_Characteristic.getConnectionHandle(); |
hmiot | 8:285ebd0e83fb | 111 | ptr_temp_char->valueHandle = light_Characteristic.getValueHandle(); |
hmiot | 8:285ebd0e83fb | 112 | printf("After Conn Handle = %d\r\n",ptr_temp_char->connHandle); |
hmiot | 8:285ebd0e83fb | 113 | printf("After Value Handle = %d\r\n",ptr_temp_char->valueHandle); |
hmiot | 8:285ebd0e83fb | 114 | ptr_temp_char->nextChar = NULL; |
hmiot | 8:285ebd0e83fb | 115 | } |
hmiot | 8:285ebd0e83fb | 116 | |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 117 | void discoveryTerminationCallback(Gap::Handle_t connectionHandle) { |
mbed_tw_hoehoe | 1:2f1203d70643 | 118 | pc.printf("terminated SD for handle %u\r\n", connectionHandle); |
hmiot | 8:285ebd0e83fb | 119 | serviceDiscover = false; |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 120 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 121 | |
hmiot | 6:71e7a446ae6a | 122 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { |
hmiot | 6:71e7a446ae6a | 123 | pc.printf("Connection Callback\n\r"); |
hmiot | 6:71e7a446ae6a | 124 | if (params->role == Gap::CENTRAL) { |
hmiot | 6:71e7a446ae6a | 125 | pc.printf("Service and characterstics discovery callback\r\n"); |
hmiot | 6:71e7a446ae6a | 126 | BLE::Instance().gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); |
hmiot | 6:71e7a446ae6a | 127 | BLE::Instance().gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xffe5, 0xffe9); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 128 | } |
hmiot | 6:71e7a446ae6a | 129 | } |
hmiot | 6:71e7a446ae6a | 130 | |
hmiot | 6:71e7a446ae6a | 131 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { |
hmiot | 6:71e7a446ae6a | 132 | pc.printf("disconnected\r\n"); |
hmiot | 8:285ebd0e83fb | 133 | BLE::Instance().gap().startScan(advertisementCallback); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 134 | } |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 135 | |
hmiot | 6:71e7a446ae6a | 136 | |
hmiot | 6:71e7a446ae6a | 137 | /** |
hmiot | 6:71e7a446ae6a | 138 | * Callback triggered when the ble initialization process has finished |
hmiot | 6:71e7a446ae6a | 139 | */ |
hmiot | 6:71e7a446ae6a | 140 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
hmiot | 6:71e7a446ae6a | 141 | { |
hmiot | 6:71e7a446ae6a | 142 | BLE& ble = params->ble; |
hmiot | 8:285ebd0e83fb | 143 | |
hmiot | 6:71e7a446ae6a | 144 | pc.printf("Ble Init\n\r"); |
hmiot | 8:285ebd0e83fb | 145 | /* Ensure that it is the default instance of BLE |
hmiot | 6:71e7a446ae6a | 146 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
hmiot | 6:71e7a446ae6a | 147 | return; |
hmiot | 8:285ebd0e83fb | 148 | }*/ |
hmiot | 6:71e7a446ae6a | 149 | |
hmiot | 8:285ebd0e83fb | 150 | // ble.gap().onConnection(connectionCallback); |
hmiot | 8:285ebd0e83fb | 151 | // ble.gap().onDisconnection(disconnectionCallback); |
mbed_tw_hoehoe | 3:d6f80e11a7f4 | 152 | |
hmiot | 8:285ebd0e83fb | 153 | // ble.gap().setScanParams(1000, 800); |
hmiot | 8:285ebd0e83fb | 154 | // ble.gap().startScan(advertisementCallback); |
hmiot | 8:285ebd0e83fb | 155 | } |
hmiot | 8:285ebd0e83fb | 156 | |
hmiot | 8:285ebd0e83fb | 157 | void light_actions(int service_char[]){ |
hmiot | 8:285ebd0e83fb | 158 | uint8_t *write_data=NULL; |
hmiot | 8:285ebd0e83fb | 159 | //={0x56,0xff,0x00,0x00,0x00,0xf0,0xaa}; |
hmiot | 8:285ebd0e83fb | 160 | write_data = new uint8_t[7]; |
hmiot | 8:285ebd0e83fb | 161 | lightCharacteristic_t *ptr_tmp = NULL; |
hmiot | 8:285ebd0e83fb | 162 | // uint8_t op; |
hmiot | 8:285ebd0e83fb | 163 | ptr_tmp = lightChar.services_Char[service_char[0]-1]; |
hmiot | 8:285ebd0e83fb | 164 | printf("Conn Handle = %d\r\n",ptr_tmp->connHandle); |
hmiot | 8:285ebd0e83fb | 165 | printf("Value Handle = %d\r\n",ptr_tmp->valueHandle); |
hmiot | 8:285ebd0e83fb | 166 | if(connect_status) |
hmiot | 8:285ebd0e83fb | 167 | { |
hmiot | 8:285ebd0e83fb | 168 | //scanf("%d", &op); |
hmiot | 8:285ebd0e83fb | 169 | printf("options : %d\n",service_char[1]); |
hmiot | 8:285ebd0e83fb | 170 | switch(service_char[1]) |
hmiot | 8:285ebd0e83fb | 171 | { |
hmiot | 8:285ebd0e83fb | 172 | case TURN_OFF: // off |
hmiot | 8:285ebd0e83fb | 173 | write_data[0]=0x56; |
hmiot | 8:285ebd0e83fb | 174 | write_data[1]=0x00; |
hmiot | 8:285ebd0e83fb | 175 | write_data[2]=0x00; |
hmiot | 8:285ebd0e83fb | 176 | write_data[3]=0x00; |
hmiot | 8:285ebd0e83fb | 177 | write_data[4]=0x00; |
hmiot | 8:285ebd0e83fb | 178 | write_data[5]=0xf0; |
hmiot | 8:285ebd0e83fb | 179 | write_data[6]=0xaa; |
hmiot | 8:285ebd0e83fb | 180 | error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,7,write_data); |
hmiot | 8:285ebd0e83fb | 181 | break; |
hmiot | 8:285ebd0e83fb | 182 | |
hmiot | 8:285ebd0e83fb | 183 | case TURN_ON: //on |
hmiot | 8:285ebd0e83fb | 184 | write_data[0]=0x56; |
hmiot | 8:285ebd0e83fb | 185 | write_data[1]=0xff; |
hmiot | 8:285ebd0e83fb | 186 | write_data[2]=0x00; |
hmiot | 8:285ebd0e83fb | 187 | write_data[3]=0xff; |
hmiot | 8:285ebd0e83fb | 188 | write_data[4]=0x00; |
hmiot | 8:285ebd0e83fb | 189 | write_data[5]=0xf0; |
hmiot | 8:285ebd0e83fb | 190 | write_data[6]=0xaa; |
hmiot | 8:285ebd0e83fb | 191 | error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,7,write_data); |
hmiot | 8:285ebd0e83fb | 192 | break; |
hmiot | 8:285ebd0e83fb | 193 | |
hmiot | 8:285ebd0e83fb | 194 | case COLOR: // Color |
hmiot | 8:285ebd0e83fb | 195 | write_data[0]=0x56; |
hmiot | 8:285ebd0e83fb | 196 | write_data[1]=0xff; //lightColor[service_char[2]][0];//0xff |
hmiot | 8:285ebd0e83fb | 197 | write_data[2]=0x00;//lightColor[service_char[2]][1];//0x00; |
hmiot | 8:285ebd0e83fb | 198 | write_data[3]=0x00;//lightColor[service_char[2]][2];//0x00; |
hmiot | 8:285ebd0e83fb | 199 | write_data[4]=0x00; |
hmiot | 8:285ebd0e83fb | 200 | write_data[5]=0xf0; |
hmiot | 8:285ebd0e83fb | 201 | write_data[6]=0xaa; |
hmiot | 8:285ebd0e83fb | 202 | //datancpy(pre_write_data, (char *)write_data,6); |
hmiot | 8:285ebd0e83fb | 203 | error_status = BLE::Instance().gattClient().write(GattClient::GATT_OP_WRITE_CMD,ptr_tmp->connHandle,ptr_tmp->valueHandle,7,write_data); |
hmiot | 8:285ebd0e83fb | 204 | break; |
hmiot | 8:285ebd0e83fb | 205 | |
hmiot | 8:285ebd0e83fb | 206 | default: |
hmiot | 8:285ebd0e83fb | 207 | printf("Invalid Options\r\n"); |
hmiot | 8:285ebd0e83fb | 208 | |
hmiot | 8:285ebd0e83fb | 209 | } |
hmiot | 8:285ebd0e83fb | 210 | printf("Write Status : %d\r\n",error_status); |
hmiot | 8:285ebd0e83fb | 211 | delete [] write_data; |
hmiot | 8:285ebd0e83fb | 212 | } |
hmiot | 8:285ebd0e83fb | 213 | } |
hmiot | 8:285ebd0e83fb | 214 | |
hmiot | 8:285ebd0e83fb | 215 | void readCharacteristics(){ |
hmiot | 8:285ebd0e83fb | 216 | printf("Read Characteristics \r\n"); |
hmiot | 8:285ebd0e83fb | 217 | lightCharacteristic_t *ptr_tmp = NULL; |
hmiot | 8:285ebd0e83fb | 218 | ptr_tmp = lightChar.services_Char[1]; |
hmiot | 8:285ebd0e83fb | 219 | uint16_t read_data; |
hmiot | 8:285ebd0e83fb | 220 | error_status = BLE::Instance().gattClient().read(ptr_tmp->connHandle,ptr_tmp->valueHandle,read_data); |
hmiot | 8:285ebd0e83fb | 221 | printf("Read Data : %d\r\n",read_data); |
hmiot | 8:285ebd0e83fb | 222 | } |
hmiot | 8:285ebd0e83fb | 223 | |
hmiot | 8:285ebd0e83fb | 224 | void datancpy(char *pre_write, char *writeData, int data_size){ |
hmiot | 8:285ebd0e83fb | 225 | int numCount; |
hmiot | 8:285ebd0e83fb | 226 | printf("length %d\r\n",sizeof(writeData)); |
hmiot | 8:285ebd0e83fb | 227 | for(numCount=0; numCount <= data_size; numCount++){ |
hmiot | 8:285ebd0e83fb | 228 | pre_write[numCount] = writeData[numCount]; |
hmiot | 8:285ebd0e83fb | 229 | // printf("write Value = %02x\r\n",writeData[numCount]); |
hmiot | 8:285ebd0e83fb | 230 | //printf("Copy Value = %2x %d \r\n",pre_write[numCount],pre_write[numCount]); |
hmiot | 8:285ebd0e83fb | 231 | } |
hmiot | 8:285ebd0e83fb | 232 | |
mbed_tw_hoehoe | 2:4b53d13d9851 | 233 | } |
mbed_tw_hoehoe | 2:4b53d13d9851 | 234 | |
hmiot | 8:285ebd0e83fb | 235 | void bleint() |
hmiot | 8:285ebd0e83fb | 236 | { |
hmiot | 8:285ebd0e83fb | 237 | printf("Init\r\n"); |
hmiot | 8:285ebd0e83fb | 238 | if(isint){ |
hmiot | 8:285ebd0e83fb | 239 | BLE::Instance().init(bleInitComplete); |
hmiot | 8:285ebd0e83fb | 240 | isint=false; |
hmiot | 8:285ebd0e83fb | 241 | } |
hmiot | 8:285ebd0e83fb | 242 | |
hmiot | 8:285ebd0e83fb | 243 | } |
hmiot | 8:285ebd0e83fb | 244 | |
hmiot | 8:285ebd0e83fb | 245 | |
hmiot | 8:285ebd0e83fb | 246 | int main() |
hmiot | 8:285ebd0e83fb | 247 | { |
hmiot | 8:285ebd0e83fb | 248 | // Serial port configuration |
mbed_tw_hoehoe | 1:2f1203d70643 | 249 | pc.baud(9600); |
mbed_tw_hoehoe | 0:83b5c6efd8d7 | 250 | wait(8.0); |
hmiot | 8:285ebd0e83fb | 251 | spiSlave.reply(191); |
hmiot | 6:71e7a446ae6a | 252 | pc.printf("Start\n\r"); |
hmiot | 6:71e7a446ae6a | 253 | BLE &ble = BLE::Instance(); |
hmiot | 8:285ebd0e83fb | 254 | //ble.init(bleInitComplete); |
hmiot | 8:285ebd0e83fb | 255 | //bleint(); |
hmiot | 8:285ebd0e83fb | 256 | |
hmiot | 8:285ebd0e83fb | 257 | //ble.waitForEvent(); |
hmiot | 8:285ebd0e83fb | 258 | while(true) { |
hmiot | 8:285ebd0e83fb | 259 | printf("loop\r\n"); |
hmiot | 8:285ebd0e83fb | 260 | printf("Status : %d\r\n",BLE::Instance().gattClient().isServiceDiscoveryActive()); |
hmiot | 8:285ebd0e83fb | 261 | |
hmiot | 8:285ebd0e83fb | 262 | // if(connect_status && charDiscover){ |
hmiot | 8:285ebd0e83fb | 263 | if(spiSlave.receive()) |
hmiot | 8:285ebd0e83fb | 264 | { |
hmiot | 8:285ebd0e83fb | 265 | spiRX[bufferSize] = spiSlave.read(); |
hmiot | 8:285ebd0e83fb | 266 | spiSlave.reply(spiRX[bufferSize]); |
hmiot | 8:285ebd0e83fb | 267 | bufferSize++; |
hmiot | 8:285ebd0e83fb | 268 | if(bufferSize>=4){ |
hmiot | 8:285ebd0e83fb | 269 | bufferSize=0; |
hmiot | 8:285ebd0e83fb | 270 | |
hmiot | 8:285ebd0e83fb | 271 | // light_actions(spiRX); |
hmiot | 8:285ebd0e83fb | 272 | |
hmiot | 8:285ebd0e83fb | 273 | } |
hmiot | 8:285ebd0e83fb | 274 | } |
hmiot | 8:285ebd0e83fb | 275 | bleint(); |
hmiot | 8:285ebd0e83fb | 276 | // } |
hmiot | 8:285ebd0e83fb | 277 | if (serviceDiscover || BLE::Instance().gattClient().isServiceDiscoveryActive()){ |
hmiot | 8:285ebd0e83fb | 278 | ble.waitForEvent(); |
hmiot | 8:285ebd0e83fb | 279 | //serviceDiscover = false; |
hmiot | 8:285ebd0e83fb | 280 | } |
hmiot | 8:285ebd0e83fb | 281 | } |
hmiot | 6:71e7a446ae6a | 282 | } |
hmiot | 8:285ebd0e83fb | 283 | /* |
hmiot | 8:285ebd0e83fb | 284 | void testaction(uint8_t options) |
hmiot | 8:285ebd0e83fb | 285 | { |
hmiot | 8:285ebd0e83fb | 286 | int tx_buf[4]; |
hmiot | 8:285ebd0e83fb | 287 | |
hmiot | 8:285ebd0e83fb | 288 | switch(options) |
hmiot | 8:285ebd0e83fb | 289 | { |
hmiot | 8:285ebd0e83fb | 290 | case 1: //off |
hmiot | 8:285ebd0e83fb | 291 | tx_buf[0] = 0x1; |
hmiot | 8:285ebd0e83fb | 292 | tx_buf[1] = 0x0; |
hmiot | 8:285ebd0e83fb | 293 | tx_buf[2] = 0x0; |
hmiot | 8:285ebd0e83fb | 294 | tx_buf[3] = 0x0; |
hmiot | 8:285ebd0e83fb | 295 | light_actions(tx_buf); |
hmiot | 8:285ebd0e83fb | 296 | break; |
hmiot | 8:285ebd0e83fb | 297 | case 2: //on |
hmiot | 8:285ebd0e83fb | 298 | tx_buf[0] = 0x1; |
hmiot | 8:285ebd0e83fb | 299 | tx_buf[1] = 0x1; |
hmiot | 8:285ebd0e83fb | 300 | tx_buf[2] = 0x0; |
hmiot | 8:285ebd0e83fb | 301 | tx_buf[3] = 0x0; |
hmiot | 8:285ebd0e83fb | 302 | light_actions(tx_buf); |
hmiot | 8:285ebd0e83fb | 303 | break; |
hmiot | 8:285ebd0e83fb | 304 | case 3: //Red |
hmiot | 8:285ebd0e83fb | 305 | tx_buf[0] = 0x1; |
hmiot | 8:285ebd0e83fb | 306 | tx_buf[1] = 0x2; |
hmiot | 8:285ebd0e83fb | 307 | tx_buf[2] = 0x0; |
hmiot | 8:285ebd0e83fb | 308 | tx_buf[3] = 0x0; |
hmiot | 8:285ebd0e83fb | 309 | light_actions(tx_buf); |
hmiot | 8:285ebd0e83fb | 310 | break; |
hmiot | 8:285ebd0e83fb | 311 | case 4: //Green |
hmiot | 8:285ebd0e83fb | 312 | tx_buf[0] = 0x1; |
hmiot | 8:285ebd0e83fb | 313 | tx_buf[1] = 0x2; |
hmiot | 8:285ebd0e83fb | 314 | tx_buf[2] = 0x1; |
hmiot | 8:285ebd0e83fb | 315 | tx_buf[3] = 0x0; |
hmiot | 8:285ebd0e83fb | 316 | light_actions(tx_buf); |
hmiot | 8:285ebd0e83fb | 317 | break; |
hmiot | 8:285ebd0e83fb | 318 | case 5: //Blue |
hmiot | 8:285ebd0e83fb | 319 | tx_buf[0] = 0x1; |
hmiot | 8:285ebd0e83fb | 320 | tx_buf[1] = 0x2; |
hmiot | 8:285ebd0e83fb | 321 | tx_buf[2] = 0x2; |
hmiot | 8:285ebd0e83fb | 322 | tx_buf[3] = 0x0; |
hmiot | 8:285ebd0e83fb | 323 | light_actions(tx_buf); |
hmiot | 8:285ebd0e83fb | 324 | break; */ |
hmiot | 8:285ebd0e83fb | 325 | /* default: |
hmiot | 8:285ebd0e83fb | 326 | printf("Invalid \r\n"); |
hmiot | 8:285ebd0e83fb | 327 | } |
hmiot | 8:285ebd0e83fb | 328 | }*/ |