test
Dependencies: BLE_API mbed nRF51822
Fork of Connect_Test4 by
main.cpp
- Committer:
- peteratsl
- Date:
- 2015-11-09
- Revision:
- 18:8454c63488ee
- Parent:
- 17:dfe796bba97c
- Child:
- 19:da48cacdb4c1
File content as of revision 18:8454c63488ee:
#include "mbed.h" #include "BLEDevice.h" BLEDevice ble; DigitalOut led(LED1); uint16_t customServiceUUID = 0xA000; uint16_t readCharUUID = 0xA001; uint16_t writeCharUUID = 0xA002; uint16_t A16 = 0xA005; uint8_t b1_id = 0x01; uint8_t b2_id = 0x02; uint8_t b3_id = 0x03; uint8_t b4_id = 0x04; uint8_t b5_id = 0x05; uint8_t b6_id = 0x06; uint8_t b7_id = 0x07; PinName flood_p = P0_4; PinName b1_p = P0_28; PinName b2_p = P0_29; PinName b3_p = P0_15; PinName b4_p = P0_10; //CTX PinName b5_p = P0_9; //TXD PinName b6_p = P0_11; //RXD PinName b7_p = P0_8; //RTS /* PinName b4_p = CTX; //CTX PinName b5_p = TXD; //TXD PinName b6_p = D0; //RXD PinName b7_p = D3; //RTS */ const static char DEVICE_NAME[] = "PETE2"; // change this static const uint16_t uuid16_list[] = {0xFF02}; //Custom UUID, FFFF is reserved for development // Set Up custom Characteristics static uint8_t readValue[10] = {0}; ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(readCharUUID, readValue); static uint8_t writeValue[10] = {0}; WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue); // Set up custom service GattCharacteristic *characteristics[] = {&readChar, &writeChar}; GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); //setup led DigitalOut led1(P0_19); DigitalOut b1(b1_p); DigitalOut b2(b2_p); DigitalOut b3(b3_p); DigitalOut b4(b4_p); DigitalOut b5(b5_p); DigitalOut b6(b6_p); DigitalOut b7(b7_p); DigitalIn enable(flood_p); /* * Restart advertising when phone app disconnects */ void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) { ble.startAdvertising(); } /* * handle writes to writeCharacteristic */ void writeCharCallback(const GattCharacteristicWriteCBParams *params) { // check to see what characteristic was written, by handle if(params->charHandle == writeChar.getValueHandle()) { // toggle LED if... if(params->data[0] == b1_id) { b1 = !b1; //led1 = 0; } else if(params->data[0] == b2_id) { b2 = !b2; //led1 = 0; } else if(params->data[0] == b3_id) { b3 = !b3; //led1 = 0; } if(params->data[0] == b4_id) { b4 = !b4; } else if(params->data[0] == b5_id) { b5 = !b5; //led1 = 0; } else if(params->data[0] == b6_id) { b6 = !b6; //led1 = 0; } else if(params->data[0] == b7_id) { b7 = !b7; led1 = 0; wait(3); led1 = 1; } // print the data if more than 1 byte is written else { printf("\n\r Data received: length = %d, data = 0x",params->len); for(int x=0; x < params->len; x++) { printf("%x",params->data[x]); } } // update the readChar with the value of writeChar ble.updateCharacteristicValue(readChar.getValueHandle(),params->data,params->len); } } /* * main loop */ int main(void) { /* initialize stuff */ printf("\n\r********* Starting Main Loop *********\n\r"); led1 = 1; b1 = 1;//b1_p = P0_28; b2 = 1;//b2_p = P0_29; b3 = 1;//b3_p = P0_9; b4 = 1;//b4_p = P0_10; //CTX b5 = 1;//b5_p = P0_9; //TXD b6 = 1;//b6_p = P0_11; //RXD b7 = 1;//b7_p = P0_8; //RTS ble.init(); ble.onDisconnection(disconnectionCallback); ble.onDataWritten(writeCharCallback); /* setup advertising */ ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet ble.setAdvertisingInterval(100); // 100ms. // add our custom service ble.addService(customService); // start advertising ble.startAdvertising(); // infinite loop waiting for BLE interrupt events while (true) { ble.waitForEvent(); //Save power } }