skaner
Dependencies: BLE_API TextLCD mbed nRF51822
Fork of BLE_LEDBlinker by
Diff: main.cpp
- Revision:
- 12:61812ca1f049
- Parent:
- 11:023d96b0e427
--- a/main.cpp Tue Jan 12 10:49:03 2016 +0000 +++ b/main.cpp Sun Apr 17 17:13:33 2016 +0000 @@ -18,11 +18,16 @@ #include "ble/BLE.h" #include "ble/DiscoveredCharacteristic.h" #include "ble/DiscoveredService.h" +#include "ble/ServiceDiscovery.h" +#include "TextLCD/TextLCD.h" +TextLCD lcd(P0_15, P0_16, P0_17, P0_18, P0_19, P0_20); // rs, e, d4-d7 DigitalOut alivenessLED(LED1, 1); bool triggerLedCharacteristic = false; -DiscoveredCharacteristic ledCharacteristic; +DiscoveredCharacteristic allState; +DiscoveredService Service; + Ticker ticker; @@ -31,19 +36,27 @@ } void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { - if (params->peerAddr[0] != 0x37) { /* !ALERT! Alter this filter to suit your device. */ + if (params->peerAddr[5] != 0xC4) { /* !ALERT! Alter this filter to suit your device. */ return; } 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); + printf("len = %u\r\n", params->advertisingDataLen); + //unsigned index = 0; + //for (; index < params->advertisingDataLen; index++) { + // printf("%02x ", params->advertisingData[index]); + //} + //printf("\r\n"); BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); } void serviceDiscoveryCallback(const DiscoveredService *service) { + //printf("ss dziala"); if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) { printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle()); + printf("ss dziala"); } else { printf("S UUID-"); const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); @@ -51,14 +64,16 @@ printf("%02x", longUUIDBytes[i]); } printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle()); + printf("ss dziala"); } } void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) { printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast()); - if (characteristicP->getUUID().getShortUUID() == 0xa001) { /* !ALERT! Alter this filter to suit your device. */ - ledCharacteristic = *characteristicP; + if (characteristicP->getUUID().getShortUUID() == 0x0014) { /* !ALERT! Alter this filter to suit your device. */ + allState = *characteristicP; triggerLedCharacteristic = true; + //printf("true\n"); } } @@ -68,29 +83,63 @@ void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { if (params->role == Gap::CENTRAL) { - BLE::Instance().gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); - BLE::Instance().gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xa000, 0xa001); + //printf("asdqwd %d\n",params->handle); + + BLE::Instance().gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); + BLE::Instance().gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0x0010, 0x0014); } } void triggerToggledWrite(const GattReadCallbackParams *response) { - if (response->handle == ledCharacteristic.getValueHandle()) { -#if DUMP_READ_DATA + //if (response->handle == allState.getValueHandle()) { +//#if DUMP_READ_DATA printf("triggerToggledWrite: handle %u, offset %u, len %u\r\n", response->handle, response->offset, response->len); + //uint8_t bytes[4]; + + + + // printf("przed forem\n"); for (unsigned index = 0; index < response->len; index++) { - printf("%c[%02x]", response->data[index], response->data[index]); + // printf("[%02x]", response->data[index]); + } + + uint8_t bytes_manual[4]; + bytes_manual[0] = response->data[0]; + bytes_manual[1] = response->data[1]; + bytes_manual[2] = response->data[2]; + bytes_manual[3] = response->data[3]; + float z = *(float *)&bytes_manual; + printf("X = %f\n",z); + uint8_t bytes_manualy[4]; + bytes_manualy[0] = response->data[5]; + bytes_manualy[1] = response->data[6]; + bytes_manualy[2] = response->data[7]; + bytes_manualy[3] = response->data[8]; + float za = *(float *)&bytes_manualy; + printf("Y = %f\n",za); + uint8_t bytes_manualz[4]; + bytes_manualz[0] = response->data[10]; + bytes_manualz[1] = response->data[11]; + bytes_manualz[2] = response->data[12]; + bytes_manualz[3] = response->data[13]; + float zad = *(float *)&bytes_manualz; + printf("Z = %f\n",zad); + + lcd.cls(); + lcd.printf("X:%f;Y:%f;Z:%f\r\n",z,za,zad); + printf("\r\n"); -#endif +//#endif - uint8_t toggledValue = response->data[0] ^ 0x1; - ledCharacteristic.write(1, &toggledValue); - } + //uint8_t toggledValue = response->data[0] ^ 0x1; + //allState.write(1, &toggledValue); + //} } void triggerRead(const GattWriteCallbackParams *response) { - if (response->handle == ledCharacteristic.getValueHandle()) { - ledCharacteristic.read(); + if (response->handle == allState.getValueHandle()) { + allState.read(); } } @@ -136,6 +185,7 @@ } int main(void) { + //uint8_t s; ticker.attach(periodicCallback, 1); BLE &ble = BLE::Instance(); @@ -144,17 +194,12 @@ /* SpinWait for initialization to complete. This is necessary because the * BLE object is used in the main loop below. */ while (ble.hasInitialized() == false) { /* spin loop */ } - + + while (true) { - if (triggerLedCharacteristic && !ble.gattClient().isServiceDiscoveryActive()) { - triggerLedCharacteristic = false; - ledCharacteristic.read(); /* We could have issued this read just as easily from - * characteristicDiscoveryCallback(); but - * invoking it here demonstrates the use - * of isServiceDiscoveryActive() and also - * the fact that it is permitted to - * operate on application-local copies of - * DiscoveredCharacteristic. */ + if (triggerLedCharacteristic) { + //printf("dzialaaasdqaw"); + allState.read(); } ble.waitForEvent(); }